8000 refactor: refactor types (#5344) · hexojs/hexo@86350d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86350d9

Browse files
D-SketonSukkaW
andauthored
refactor: refactor types (#5344)
Co-authored-by: Sukka <isukkaw@gmail.com>
1 parent 6a91fb6 commit 86350d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+463
-214
lines changed

lib/box/file.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class File {
1919
this.type = type;
2020
}
2121

22-
read(options?: ReadFileOptions): Promise<string | Buffer> {
23-
return readFile(this.source, options);
22+
read(options?: ReadFileOptions): Promise<string> {
23+
return readFile(this.source, options) as Promise<string>;
2424
}
2525

26-
readSync(options?: ReadFileOptions): string | Buffer {
27-
return readFileSync(this.source, options);
26+
readSync(options?: ReadFileOptions): string {
27+
return readFileSync(this.source, options) as string;
2828
}
2929

3030
stat(): Promise<fs.Stats> {

lib/box/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import { magenta } from 'picocolors';
77
import { EventEmitter } from 'events';
88
import { isMatch, makeRe } from 'micromatch';
99
import type Hexo from '../hexo';
10+
import type { NodeJSLikeCallback } from '../types';
1011

1112
const defaultPattern = new Pattern(() => ({}));
1213

1314
interface Processor {
1415
pattern: Pattern;
15-
process: (file: File) => void;
16+
process: (file?: File) => any;
1617
}
1718

1819
class Box extends EventEmitter {

lib/extend/console.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Promise from 'bluebird';
22
import abbrev from 'abbrev';
3+
import type { NodeJSLikeCallback } from '../types';
34

45
type Option = Partial<{
56
usage: string;
@@ -19,7 +20,7 @@ interface Args {
1920
_: string[];
2021
[key: string]: string | boolean | string[];
2122
}
22-
type AnyFn = (args: Args) => any;
23+
type AnyFn = (args: Args, callback?: NodeJSLikeCallback<any>) => any;
2324
interface StoreFunction extends AnyFn {
2425
desc?: string;
2526
options?: Option;
@@ -29,7 +30,7 @@ interface Store {
2930
[key: string]: StoreFunction
3031
}
3132
interface Alias {
32-
[key: string]: string
33+
[abbreviation: string]: string
3334
}
3435

3536
class Console {

lib/extend/deployer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Promise from 'bluebird';
2+
import type { NodeJSLikeCallback } from '../types';
23

34
interface StoreFunction {
45
(deployArg: {
56
type: string;
67
[key: string]: any
7-
}) : any;
8+
}, callback?: NodeJSLikeCallback<any>) : any;
89
}
910
interface Store {
1011
[key: string]: StoreFunction

lib/extend/filter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface FilterOptions {
1111
args?: any[];
1212
}
1313

14+
1415
interface StoreFunction {
1516
(data?: any, ...args: any[]): any;
1617
priority?: number;
@@ -75,7 +76,7 @@ class Filter {
7576
if (index !== -1) list.splice(index, 1);
7677
}
7778

78-
exec(type: string, data: any[], options: FilterOptions = {}): Promise<any> {
79+
exec(type: string, data: any, options: FilterOptions = {}): Promise<any> {
7980
const filters = this.list(type);
8081
if (filters.length === 0) return Promise.resolve(data);
8182

@@ -90,7 +91,7 @@ class Filter {
9091
})).then(() => args[0]);
9192
}
9293

93-
execSync(type: string, data: any[], options: FilterOptions = {}) {
94+
execSync(type: string, data: any, options: FilterOptions = {}) {
9495
const filters = this.list(type);
9596
const filtersLen = filters.length;
9697
if (filtersLen === 0) return data;

lib/extend/generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Promise from 'bluebird';
2+
import type { NodeJSLikeCallback } from '../types';
23

34
interface BaseObj {
45
path: string;
@@ -9,7 +10,7 @@ type ReturnType = BaseObj | BaseObj[];
910
type GeneratorReturnType = ReturnType | Promise<ReturnType>;
1011

1112
interface GeneratorFunction {
12-
(locals: object): GeneratorReturnType;
13+
(locals: object, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
1314
}
1415

1516
type StoreFunctionReturn = Promise<ReturnType>;

lib/extend/migrator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Promise from 'bluebird';
2+
import type { NodeJSLikeCallback } from '../types';
3+
24
interface StoreFunction {
3-
(args: any): any
5+
(args: any, callback?: NodeJSLikeCallback<any>): any
46
}
57

68
interface Store {

lib/extend/renderer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { extname } from 'path';
22
import Promise from 'bluebird';
3+
import type { NodeJSLikeCallback } from '../types';
34

45
const getExtname = (str: string) => {
56
if (typeof str !== 'string') return '';
@@ -13,15 +14,15 @@ export interface StoreFunctionData {
1314
text?: string;
1415
engine?: string;
1516
toString?: any;
16-
onRenderEnd?: any;
17+
onRenderEnd?: (...args: any[]) => any;
1718
}
1819

1920
export interface StoreSyncFunction {
2021
[x: string]: any;
2122
(
2223
data: StoreFunctionData,
2324
options: object,
24-
// callback: NodeJSLikeCallback<string>
25+
// callback?: NodeJSLikeCallback<string>
2526
): any;
2627
output?: string;
2728
compile?: (local: object) => any;
@@ -30,6 +31,7 @@ export interface StoreFunction {
3031
(
3132
data: StoreFunctionData,
3233
options: object,
34+
callback?: NodeJSLikeCallback<any>
3335
): Promise<any>;
3436
(
3537
data: StoreFunctionData,
@@ -57,7 +59,7 @@ class Renderer {
5759
this.storeSync = {};
5860
}
5961

60-
list(sync: boolean): Store | SyncStore {
62+
list(sync = false): Store | SyncStore {
6163
return sync ? this.storeSync : this.store;
6264
}
6365

@@ -97,7 +99,6 @@ class Renderer {
9799
this.storeSync[name].output = output;
98100

99101
this.store[name] = Promise.method(fn);
100-
// eslint-disable-next-line no-extra-parens
101102
this.store[name].disableNunjucks = (fn as StoreFunction).disableNunjucks;
102103
} else {
103104
if (fn.length > 2) fn = Promise.promisify(fn);

lib/extend/tag.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { stripIndent } from 'hexo-util';
22
import { cyan, magenta, red, bold } from 'picocolors';
33
import { Environment } from 'nunjucks';
44
import Promise from 'bluebird';
5+
import type { NodeJSLikeCallback } from '../types';
6+
57
const rSwigRawFullBlock = /{% *raw *%}/;
68
const rCodeTag = /<code[^<>]*>[\s\S]+?<\/code>/g;
79
const escapeSwigTag = (str: string) => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;');
810

911
interface TagFunction {
10-
(args: any[], content: string): string;
12+
(args: any[], content: string, callback?: NodeJSLikeCallback<any>): string | PromiseLike<string>;
1113
}
1214
interface AsyncTagFunction {
1315
(args: any[], content: string): Promise<string>;
@@ -251,14 +253,17 @@ class Tag {
251253
if (env.hasExtension(name)) env.removeExtension(name);
252254
}
253255

254-
render(str: string, options: { source?: string } = {}, callback?: NodeJSLikeCallback<any>): Promise<any> {
256+
render(str: string): Promise<any>;
257+
render(str: string, callback: NodeJSLikeCallback<any>): Promise<any>;
258+
render(str: string, options: { source?: string, [key: string]: any }, callback?: NodeJSLikeCallback<any>): Promise<any>;
259+
render(str: string, options: { source?: string, [key: string]: any } | NodeJSLikeCallback<any> = {}, callback?: NodeJSLikeCallback<any>): Promise<any> {
255260
if (!callback && typeof options === 'function') {
256261
callback = options;
257262
options = {};
258263
}
259264

260265
// Get path of post from source
261-
const { source = '' } = options;
266+
const { source = '' } = options as { source?: string };
262267

263268
return Promise.fromCallback(cb => {
264269
this.env.renderString(

lib/hexo/default_config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export = {
4646
wrap: true,
4747
exclude_languages: [],
4848
language_attr: false,
49-
hljs: false
49+
hljs: false,
50+
line_threshold: 0,
51+
first_line_number: 'always1'
5052
},
5153
prismjs: {
5254
preprocess: true,

0 commit comments

Comments
 (0)
0