From d0c9fd1141998827e296019dd8ac4193c977b699 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Mon, 4 Sep 2023 11:29:11 +0800 Subject: [PATCH] add NodeJSLikeCallback --- lib/box/index.ts | 4 ++-- lib/extend/renderer.ts | 4 ++-- lib/extend/tag.ts | 2 +- lib/hexo/index.ts | 8 ++++---- lib/hexo/post.ts | 10 +++++----- lib/hexo/scaffold.ts | 6 +++--- lib/types.ts | 1 + 7 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 lib/types.ts diff --git a/lib/box/index.ts b/lib/box/index.ts index 11ffdf01be..996594f8e8 100644 --- a/lib/box/index.ts +++ b/lib/box/index.ts @@ -121,7 +121,7 @@ class Box extends EventEmitter { })); } - process(callback?: (...args: any[]) => void) { + process(callback?: NodeJSLikeCallback) { const { base, Cache, context: ctx } = this; return stat(base).then(stats => { @@ -183,7 +183,7 @@ class Box extends EventEmitter { }).thenReturn(path); } - watch(callback?: (...args: any[]) => void) { + watch(callback?: NodeJSLikeCallback) { if (this.isWatching()) { return BlueBirdPromise.reject(new Error('Watcher has already started.')).asCallback(callback); } diff --git a/lib/extend/renderer.ts b/lib/extend/renderer.ts index 701c5d0031..3dd0b42e84 100644 --- a/lib/extend/renderer.ts +++ b/lib/extend/renderer.ts @@ -21,7 +21,7 @@ export interface StoreSyncFunction { ( data: StoreFunctionData, options: object, - // callback: (err: Error, value: string) => any + // callback: NodeJSLikeCallback ): any; output?: string; compile?: (local: object) => any; @@ -34,7 +34,7 @@ export interface StoreFunction { ( data: StoreFunctionData, options: object, - callback: (err: Error, value: string) => any + callback: NodeJSLikeCallback ): void; output?: string; compile?: (local: object) => any; diff --git a/lib/extend/tag.ts b/lib/extend/tag.ts index b90fb444af..7e8426840f 100644 --- a/lib/extend/tag.ts +++ b/lib/extend/tag.ts @@ -251,7 +251,7 @@ class Tag { if (env.hasExtension(name)) env.removeExtension(name); } - render(str: string, options: { source?: string } = {}, callback?: (...args: any[]) => void) { + render(str: string, options: { source?: string } = {}, callback?: NodeJSLikeCallback) { if (!callback && typeof options === 'function') { callback = options; options = {}; diff --git a/lib/hexo/index.ts b/lib/hexo/index.ts index 69cb45caf4..392407fb5d 100644 --- a/lib/hexo/index.ts +++ b/lib/hexo/index.ts @@ -336,7 +336,7 @@ class Hexo extends EventEmitter { }); } - call(name: string, args: any, callback?: (...args: any[]) => void) { + call(name: string, args: any, callback?: NodeJSLikeCallback) { if (!callback && typeof args === 'function') { callback = args; args = {}; @@ -371,7 +371,7 @@ class Hexo extends EventEmitter { } } - loadPlugin(path: string, callback?: (...args: any[]) => void) { + loadPlugin(path: string, callback?: NodeJSLikeCallback) { return readFile(path).then(script => { // Based on: https://github.com/joyent/node/blob/v0.10.33/src/node.js#L516 const module = new Module(path); @@ -401,7 +401,7 @@ class Hexo extends EventEmitter { return args.draft || args.drafts || this.config.render_drafts; } - load(callback: (...args: any[]) => void) { + load(callback: NodeJSLikeCallback) { return loadDatabase(this).then(() => { this.log.info('Start processing'); @@ -415,7 +415,7 @@ class Hexo extends EventEmitter { }).asCallback(callback); } - watch(callback?: (...args: any[]) => void) { + watch(callback?: NodeJSLikeCallback) { let useCache = false; const { cache } = Object.assign({ cache: false diff --git a/lib/hexo/post.ts b/lib/hexo/post.ts index 87fa93392a..099770fe9e 100644 --- a/lib/hexo/post.ts +++ b/lib/hexo/post.ts @@ -247,9 +247,9 @@ class Post { this.context = context; } - create(data: PostData, callback?: (...args: any[]) => void); - create(data: PostData, replace: boolean, callback?: (...args: any[]) => void); - create(data: PostData, replace: boolean | ((...args: any[]) => void), callback?: (...args: any[]) => void) { + create(data: PostData, callback?: NodeJSLikeCallback); + create(data: PostData, replace: boolean, callback?: NodeJSLikeCallback); + create(data: PostData, replace: boolean | (NodeJSLikeCallback), callback?: NodeJSLikeCallback) { if (!callback && typeof replace === 'function') { callback = replace; replace = false; @@ -335,7 +335,7 @@ class Post { }); } - publish(data: PostData, replace: boolean, callback?: (...args: any[]) => void) { + publish(data: PostData, replace: boolean, callback?: NodeJSLikeCallback) { if (!callback && typeof replace === 'function') { callback = replace; replace = false; @@ -388,7 +388,7 @@ class Post { }).thenReturn(result).asCallback(callback); } - render(source: string, data: Data = {}, callback?: (...args: any[]) => void) { + render(source: string, data: Data = {}, callback?: NodeJSLikeCallback) { const ctx = this.context; const { config } = ctx; const { tag } = ctx.extend; diff --git a/lib/hexo/scaffold.ts b/lib/hexo/scaffold.ts index 786c4d237c..37b6e65e86 100644 --- a/lib/hexo/scaffold.ts +++ b/lib/hexo/scaffold.ts @@ -40,7 +40,7 @@ class Scaffold { return this._listDir().then(list => list.find(item => item.name === name)); } - get(name: string, callback?: (...args: any[]) => void) { + get(name: string, callback?: NodeJSLikeCallback) { return this._getScaffold(name).then(item => { if (item) { return readFile(item.path); @@ -50,7 +50,7 @@ class Scaffold { }).asCallback(callback); } - set(name: string, content: any, callback: (...args: any[]) => void) { + set(name: string, content: any, callback: NodeJSLikeCallback) { const { scaffoldDir } = this; return this._getScaffold(name).then(item => { @@ -61,7 +61,7 @@ class Scaffold { }).asCallback(callback); } - remove(name: string, callback: (...args: any[]) => void) { + remove(name: string, callback: NodeJSLikeCallback) { return this._getScaffold(name).then(item => { if (!item) return; diff --git a/lib/types.ts b/lib/types.ts new file mode 100644 index 0000000000..caa69deadb --- /dev/null +++ b/lib/types.ts @@ -0,0 +1 @@ +type NodeJSLikeCallback = (err: E, result?: R) => void \ No newline at end of file