Skip to content

Commit

Permalink
add NodeJSLikeCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Sep 4, 2023
1 parent 214800f commit d0c9fd1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Box extends EventEmitter {
}));
}

process(callback?: (...args: any[]) => void) {
process(callback?: NodeJSLikeCallback<any>) {
const { base, Cache, context: ctx } = this;

return stat(base).then(stats => {
Expand Down Expand Up @@ -183,7 +183,7 @@ class Box extends EventEmitter {
}).thenReturn(path);
}

watch(callback?: (...args: any[]) => void) {
watch(callback?: NodeJSLikeCallback<never>) {
if (this.isWatching()) {
return BlueBirdPromise.reject(new Error('Watcher has already started.')).asCallback(callback);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/extend/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface StoreSyncFunction {
(
data: StoreFunctionData,
options: object,
// callback: (err: Error, value: string) => any
// callback: NodeJSLikeCallback<string>
): any;
output?: string;
compile?: (local: object) => any;
Expand All @@ -34,7 +34,7 @@ export interface StoreFunction {
(
data: StoreFunctionData,
options: object,
callback: (err: Error, value: string) => any
callback: NodeJSLikeCallback<string>
): void;
output?: string;
compile?: (local: object) => any;
Expand Down
2 changes: 1 addition & 1 deletion lib/extend/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
Expand Down
8 changes: 4 additions & 4 deletions lib/hexo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class Hexo extends EventEmitter {
});
}

call(name: string, args: any, callback?: (...args: any[]) => void) {
call(name: string, args: any, callback?: NodeJSLikeCallback<any>) {
if (!callback && typeof args === 'function') {
callback = args;
args = {};
Expand Down Expand Up @@ -371,7 +371,7 @@ class Hexo extends EventEmitter {
}
}

loadPlugin(path: string, callback?: (...args: any[]) => void) {
loadPlugin(path: string, callback?: NodeJSLikeCallback<any>) {
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);
Expand Down Expand Up @@ -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<any>) {
return loadDatabase(this).then(() => {
this.log.info('Start processing');

Expand All @@ -415,7 +415,7 @@ class Hexo extends EventEmitter {
}).asCallback(callback);
}

watch(callback?: (...args: any[]) => void) {
watch(callback?: NodeJSLikeCallback<any>) {
let useCache = false;
const { cache } = Object.assign({
cache: false
Expand Down
10 changes: 5 additions & 5 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>);
create(data: PostData, replace: boolean, callback?: NodeJSLikeCallback<any>);
create(data: PostData, replace: boolean | (NodeJSLikeCallback<any>), callback?: NodeJSLikeCallback<any>) {
if (!callback && typeof replace === 'function') {
callback = replace;
replace = false;
Expand Down Expand Up @@ -335,7 +335,7 @@ class Post {
});
}

publish(data: PostData, replace: boolean, callback?: (...args: any[]) => void) {
publish(data: PostData, replace: boolean, callback?: NodeJSLikeCallback<Result>) {
if (!callback && typeof replace === 'function') {
callback = replace;
replace = false;
Expand Down Expand Up @@ -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<never>) {
const ctx = this.context;
const { config } = ctx;
const { tag } = ctx.extend;
Expand Down
6 changes: 3 additions & 3 deletions lib/hexo/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) {
return this._getScaffold(name).then(item => {
if (item) {
return readFile(item.path);
Expand All @@ -50,7 +50,7 @@ class Scaffold {
}).asCallback(callback);
}

set(name: string, content: any, callback: (...args: any[]) => void) {
set(name: string, content: any, callback: NodeJSLikeCallback<void>) {
const { scaffoldDir } = this;

return this._getScaffold(name).then(item => {
Expand All @@ -61,7 +61,7 @@ class Scaffold {
}).asCallback(callback);
}

remove(name: string, callback: (...args: any[]) => void) {
remove(name: string, callback: NodeJSLikeCallback<void>) {
return this._getScaffold(name).then(item => {
if (!item) return;

Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type NodeJSLikeCallback<R, E = any> = (err: E, result?: R) => void

0 comments on commit d0c9fd1

Please sign in to comment.