Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate typescript #119

Merged
merged 12 commits into from
Oct 28, 2022
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's suitable here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll check it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@renbaoshuo
Done. Ready :)

Loading