Skip to content

Commit c5a694f

Browse files
authored
Merge pull request #7 from emartech/typescript
feat(typescript): transform codebase to Typescript
2 parents 04d3383 + b37aee4 commit c5a694f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+526
-337
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# Created by https://www.gitignore.io/api/osx,node,linux,intellij+all,visualstudiocode
33
package-lock.json
4+
dist
45
### Intellij+all ###
56
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
67
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
.idea
44
examples
5+
src

examples/express.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const express = require('express');
4-
const logFactory = require('../index');
4+
const logFactory = require('../dist');
55
const clsAdapter = require('@emartech/cls-adapter');
66
const logger = logFactory('example');
77
const port = 3000;

examples/index.js renamed to examples/index-js.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
process.env.DEBUG = 'redis';
4-
const logger = require('../index');
4+
const logger = require('../dist');
55

66
const mongoLogger = logger('mongo');
77
const redisLogger = logger('redis');

examples/index-ts.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
process.env.DEBUG = 'redis';
2+
import logger from '../dist';
3+
4+
const mongoLogger = logger('mongo');
5+
const redisLogger = logger('redis');
6+
7+
// simple info logging with enabled namespace
8+
redisLogger.info('connected', { domain: 'yahoo' });
9+
10+
// not enabled
11+
mongoLogger.info('connected', { domain: 'google' });
12+
13+
// error objects
14+
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
15+
16+
// displays as is
17+
console.log(JSON.stringify({ example: 'output' }));

examples/koa.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const Koa = require('koa');
4-
const logFactory = require('../index');
4+
const logFactory = require('../dist');
5+
56
const clsAdapter = require('@emartech/cls-adapter');
67
const logger = logFactory('example');
78
const port = 3000;

index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
"name": "@emartech/json-logger",
33
"version": "0.0.0",
44
"description": "Tiny and fast json logger with namespace support",
5-
"main": "index.js",
5+
"main": "dist/index.js",
66
"scripts": {
7-
"test": "mocha ./src/ --recursive",
8-
"test:watch": "mocha ./src/ --recursive --watch",
7+
"test": "mocha --require ts-node/register ./src/ --recursive",
8+
"test:watch": "mocha --require ts-node/register ./src/ --recursive --watch",
9+
"build": "rm -rf dist && tsc --project ./tsconfig.json",
910
"semantic-release": "CI=true semantic-release",
11+
"example-js": "DEBUG=* node examples/index-js.js",
12+
"example-ts": "DEBUG=* ts-node examples/index-ts.ts",
1013
"koa-example": "DEBUG=* node examples/koa.js",
1114
"express-example": "DEBUG=* node examples/express.js"
1215
},
@@ -29,13 +32,16 @@
2932
},
3033
"devDependencies": {
3134
"@emartech/cls-adapter": "1.3.0",
35+
"@types/node": "18.7.2",
3236
"chai": "4.3.6",
3337
"express": "4.18.1",
3438
"koa": "2.13.4",
3539
"mocha": "10.0.0",
3640
"semantic-release": "15.5.0",
3741
"sinon": "14.0.0",
38-
"sinon-chai": "3.7.0"
42+
"sinon-chai": "3.7.0",
43+
"ts-node": "10.9.1",
44+
"typescript": "4.7.4"
3945
},
4046
"repository": {
4147
"type": "git",

src/config.js renamed to src/config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
'use strict';
1+
import chalk from 'chalk';
22

3-
const chalk = require('chalk');
3+
interface Color {
4+
number: number;
5+
name: string;
6+
}
47

5-
const levels = {
8+
const levels: Record<string, Color> = {
69
trace: {
710
number: 10,
811
name: 'TRACE'
@@ -31,9 +34,9 @@ const levels = {
3134

3235
const availableLevels = Object.keys(levels);
3336

34-
const coloredNames = {};
37+
const coloredNames: Record<string, string> = {};
3538
availableLevels.forEach((levelName) => {
3639
coloredNames[levels[levelName].number] = levels[levelName].name;
3740
});
3841

39-
module.exports = { levels, availableLevels, coloredNames };
42+
export const config = { levels, availableLevels, coloredNames };

src/enabled/enabled.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const isNamespaceEnabled = require('./enabled');
3+
const { isNamespaceEnabled } = require('./enabled');
44

55
describe('isNamespaceAvailable', function() {
66
it('should enable when variables only contain one', function() {

0 commit comments

Comments
 (0)