Skip to content

Commit

Permalink
refactor: migrate typescript (#119)
Browse files Browse the repository at this point in the history
* refactor: migrate typescript

* test: fix test cases

* chore: impelement type of wrraped console

* chore: eslint for typescript

* chore(deps): add deps for typescript-eslint

* chore: commit forgotten package-lock.json

* test: fix test case for GHActions

* chore: add `pretest`

* chore: add eslint ignore comment

* Update lib/log.ts

Co-authored-by: Baoshuo Ren <i@baoshuo.ren>

* fix: main entry

* add `default export` to createLogger`

Co-authored-by: Baoshuo Ren <i@baoshuo.ren>
  • Loading branch information
yoshinorin and renbaoshuo committed Oct 28, 2022
1 parent 8f9dff3 commit a0ce3ee
Show file tree
Hide file tree
Showing 10 changed files with 681 additions and 254 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
coverage/
tmp/
tmp/
dist/
12 changes: 10 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"extends": "hexo",
"root": true
"root": true,
"extends": "hexo/ts.js",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
},
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/ban-ts-comment": 0
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/
tmp/
*.log
.idea/
.nyc_output/
.nyc_output/
dist
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ $ npm install hexo-log --save
## Usage

``` js
// v3.x.x
const log = require('hexo-log')({
debug: false,
silent: false
})
});

// v4.x.x
const log = require('hexo-log').default({
debug: false,
silent: false
});

log.info('Hello world');
```
Expand Down
37 changes: 28 additions & 9 deletions lib/log.js → lib/log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const { Console } = require('console');
const picocolors = require('picocolors');
import { Console } from 'console';
import picocolors from 'picocolors';

const TRACE = 10;
const DEBUG = 20;
Expand Down Expand Up @@ -34,10 +32,31 @@ const console = new Console({
colorMode: false
});

interface Options {
debug?: boolean,
silent?: boolean
}

// @ts-ignore
type writeLogF = (...args: any[]) => void;

class Logger {
constructor(options = {}) {
const silent = options.silent || false;
this._debug = options.debug || false;

_silent: boolean;
_debug: boolean;
level: number;
d: writeLogF;
i: writeLogF;
w: writeLogF;
e: writeLogF;
log: writeLogF;

constructor({
debug = false,
silent = false
}: Options = {}) {
this._silent = silent || false;
this._debug = debug || false;

this.level = INFO;

Expand Down Expand Up @@ -129,7 +148,7 @@ class Logger {
}
}

function createLogger(options) {
export default function createLogger(options: Options) {
const logger = new Logger(options);

logger.d = logger.debug;
Expand All @@ -141,4 +160,4 @@ function createLogger(options) {
return logger;
}

module.exports = createLogger;
export const logger = (option: Options) => createLogger(option);
Loading

0 comments on commit a0ce3ee

Please sign in to comment.