From 0438cc82c3fc05d821c3ef4abcd9dbbbe6c4cb6d Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 14 Sep 2017 13:49:07 +0900 Subject: [PATCH] Added types hexo-log --- types/hexo-log/hexo-log-tests.ts | 90 +++++++++++++++++ types/hexo-log/index.d.ts | 162 +++++++++++++++++++++++++++++++ types/hexo-log/tsconfig.json | 22 +++++ types/hexo-log/tslint.json | 1 + 4 files changed, 275 insertions(+) create mode 100644 types/hexo-log/hexo-log-tests.ts create mode 100644 types/hexo-log/index.d.ts create mode 100644 types/hexo-log/tsconfig.json create mode 100644 types/hexo-log/tslint.json diff --git a/types/hexo-log/hexo-log-tests.ts b/types/hexo-log/hexo-log-tests.ts new file mode 100644 index 0000000000..9efa129cd8 --- /dev/null +++ b/types/hexo-log/hexo-log-tests.ts @@ -0,0 +1,90 @@ +import mocha = require('mocha'); +import chai = require('chai'); +const should = chai.should(); // eslint-disable-line +import rewire = require('rewire'); +import sinon = require('sinon'); +import logger = require('hexo-log'); + +const _c = logger(); +type HexoLogger = typeof _c; + +describe('hexo-log', () => { + const loggerModule = rewire<(options?: { name?: string; silent?: boolean; debug?: boolean; }) => HexoLogger>('./'); + + it('add alias for levels', () => { + const log = logger(); + + log.d.should.eql(log.debug); + log.i.should.eql(log.info); + log.w.should.eql(log.warn); + log.e.should.eql(log.error); + log.log.should.eql(log.info); + }); + + it('default name is hexo', () => { + const log = logger(); + + log.fields.name.should.eql('hexo'); + }); + + it('options.name', () => { + const log = logger({ name: 'foo' }); + + log.fields.name.should.eql('foo'); + }); + + it('level should be trace if options.debug is true', () => { + const log: any = logger({ debug: true }); + + log.streams[0].level.should.eql(10); + }); + + it('should add file stream if options.debug is true', () => { + const log: any = logger({ debug: true }); + + log.streams[1].path.should.eql('debug.log'); + }); + + it('should remove console stream if options.silent is true', () => { + const log: any = logger({ silent: true }); + + log.streams.length.should.eql(0); + }); + + it('should display time if options.debug is true', () => { + const spy = sinon.spy(); + const now = new Date(); + + loggerModule.__with__({ + process: { + stdout: { + write: spy + } + } + })(() => { + sinon.useFakeTimers(now.valueOf()); + const log = loggerModule({ debug: true }); + log.info('test'); + sinon.restore(undefined); + }); + + spy.args[0][0].should.contain(now.toISOString().substring(11, 23)); + }); + + it('should print error to process.stderr stream', () => { + const spy = sinon.spy(); + + loggerModule.__with__({ + process: { + stderr: { + write: spy + } + } + })(() => { + const log = loggerModule(); + log.error('test'); + }); + + spy.calledOnce.should.be.true; + }); +}); diff --git a/types/hexo-log/index.d.ts b/types/hexo-log/index.d.ts new file mode 100644 index 0000000000..bbd18bbb39 --- /dev/null +++ b/types/hexo-log/index.d.ts @@ -0,0 +1,162 @@ +// Type definitions for hexo-log 0.2 +// Project: http://hexo.io/ +// Definitions by: segayuu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import bunyan = require('hexo-bunyan'); +import streams = require('stream'); + +declare class HexoLogger extends bunyan { + /* tslint:disable:unified-signatures */ + + /** + * Returns a boolean: is the `debug` level enabled? + * + * This is equivalent to `log.isDebugEnabled()` or `log.isEnabledFor(DEBUG)` in log4j. + */ + d(): boolean; + + /** + * Special case to log an `Error` instance to the record. + * This adds an `err` field with exception details + * (including the stack) and sets `msg` to the exception + * message or you can specify the `msg`. + */ + d(error: Error, ...params: any[]): void; + + /** + * The first field can optionally be a "fields" object, which + * is merged into the log record. + * + * To pass in an Error *and* other fields, use the `err` + * field name for the Error instance. + */ + d(obj: object, ...params: any[]): void; + + /** + * Uses `util.format` for msg formatting. + */ + d(format: any, ...params: any[]): void; + + /** + * Returns a boolean: is the `info` level enabled? + * + * This is equivalent to `log.isInfoEnabled()` or `log.isEnabledFor(INFO)` in log4j. + */ + i(): boolean; + + /** + * Special case to log an `Error` instance to the record. + * This adds an `err` field with exception details + * (including the stack) and sets `msg` to the exception + * message or you can specify the `msg`. + */ + i(error: Error, ...params: any[]): void; + + /** + * The first field can optionally be a "fields" object, which + * is merged into the log record. + * + * To pass in an Error *and* other fields, use the `err` + * field name for the Error instance. + */ + i(obj: object, ...params: any[]): void; + + /** + * Uses `util.format` for msg formatting. + */ + i(format: any, ...params: any[]): void; + + /** + * Returns a boolean: is the `warn` level enabled? + * + * This is equivalent to `log.isWarnEnabled()` or `log.isEnabledFor(WARN)` in log4j. + */ + w(): boolean; + + /** + * Special case to log an `Error` instance to the record. + * This adds an `err` field with exception details + * (including the stack) and sets `msg` to the exception + * message or you can specify the `msg`. + */ + w(error: Error, ...params: any[]): void; + + /** + * The first field can optionally be a "fields" object, which + * is merged into the log record. + * + * To pass in an Error *and* other fields, use the `err` + * field name for the Error instance. + */ + w(obj: object, ...params: any[]): void; + + /** + * Uses `util.format` for msg formatting. + */ + w(format: any, ...params: any[]): void; + + /** + * Returns a boolean: is the `error` level enabled? + * + * This is equivalent to `log.isErrorEnabled()` or `log.isEnabledFor(ERROR)` in log4j. + */ + e(): boolean; + + /** + * Special case to log an `Error` instance to the record. + * This adds an `err` field with exception details + * (including the stack) and sets `msg` to the exception + * message or you can specify the `msg`. + */ + e(error: Error, ...params: any[]): void; + + /** + * The first field can optionally be a "fields" object, which + * is merged into the log record. + * + * To pass in an Error *and* other fields, use the `err` + * field name for the Error instance. + */ + e(obj: object, ...params: any[]): void; + + /** + * Uses `util.format` for msg formatting. + */ + e(format: any, ...params: any[]): void; + + /** + * Returns a boolean: is the `info` level enabled? + * + * This is equivalent to `log.isInfoEnabled()` or `log.isEnabledFor(INFO)` in log4j. + */ + log(): boolean; + + /** + * Special case to log an `Error` instance to the record. + * This adds an `err` field with exception details + * (including the stack) and sets `msg` to the exception + * message or you can specify the `msg`. + */ + log(error: Error, ...params: any[]): void; + + /** + * The first field can optionally be a "fields" object, which + * is merged into the log record. + * + * To pass in an Error *and* other fields, use the `err` + * field name for the Error instance. + */ + log(obj: object, ...params: any[]): void; + + /** + * Uses `util.format` for msg formatting. + */ + log(format: any, ...params: any[]): void; + + /* tslint:enable:unified-signatures */ +} + +declare function createLogger(options?: { name?: string; silent?: boolean; debug?: boolean; }): HexoLogger; + +export = createLogger; diff --git a/types/hexo-log/tsconfig.json b/types/hexo-log/tsconfig.json new file mode 100644 index 0000000000..88d5965c59 --- /dev/null +++ b/types/hexo-log/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "hexo-log-tests.ts" + ] +} diff --git a/types/hexo-log/tslint.json b/types/hexo-log/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/hexo-log/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }