Skip to content

Commit 148ed7c

Browse files
stevenjoezhangyoshinorinrenbaoshuo
authored
refactor: migrate typescript (#5092)
* refactor: esm * refactor: migrate typescript * update files * remove `use strict` * fix indentation * refactor: migrate typescript * refactor: migrate typescript * update * export from * extend/filter * post * Schema and SchemaType * NunjucksError * declare module * update tests * fix export * update * update * number_format * update highlight * update * add secondary argument when create `Permalink` instance * specify `eslint` target directory * ignore two rules for testing * use `@ts-expect-error` for avoiding compile error * use `any` for avoiding compile error * chore: add `dist/` to `.gitignore` * test(eslint): update `.eslintrc` * refactor: `var` to `const` * refactor: delete unnecessary `'use strict';` * test: use `dist/modules/types/moment/SchemaTypeMoment` instead of `warehouse.SchemaTypeMoment` * fix: export of processors * test: fix bind * test: fix processer * fix fs import * fix hexo-i18n and hexo-log * fix param order * build hexo in benchmark * fix eslint * fix export & drop travis * Update .husky/pre-commit * add spaces * refactor: ts debounce fn * fix: lint errors * fix: lint errors --------- Co-authored-by: yoshinorin <yoshinorin.net@outlook.com> Co-authored-by: Baoshuo <i@baoshuo.ren>
1 parent 7edbf25 commit 148ed7c

File tree

280 files changed

+1469
-1237
lines changed

Some content is hidden

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

280 files changed

+1469
-1237
lines changed

.eslintrc.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
{
2-
"extends": "hexo",
3-
"root": true
4-
}
1+
{
2+
"root": true,
3+
"extends": "hexo/ts.js",
4+
"parserOptions": {
5+
"sourceType": "module",
6+
"ecmaVersion": 2020
7+
},
8+
"rules": {
9+
"@typescript-eslint/no-explicit-any": 0,
10+
"@typescript-eslint/no-var-requires": 0,
11+
"node/no-missing-require": 0
12+
}
13+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ package-lock.json
99
coverage/
1010
.tmp*
1111
.vscode
12+
dist/

lib/box/file.js renamed to lib/box/file.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
'use strict';
2-
3-
const { readFile, readFileSync, stat, statSync } = require('hexo-fs');
1+
import { readFile, readFileSync, stat, statSync } from 'hexo-fs';
42

53
class File {
4+
public source: any;
5+
public path: any;
6+
public params: any;
7+
public type: any;
8+
static TYPE_CREATE: 'create';
9+
static TYPE_UPDATE: 'update';
10+
static TYPE_SKIP: 'skip';
11+
static TYPE_DELETE: 'delete';
12+
613
constructor({ source, path, params, type }) {
714
this.source = source;
815
this.path = path;
@@ -32,4 +39,4 @@ File.TYPE_UPDATE = 'update';
3239
File.TYPE_SKIP = 'skip';
3340
File.TYPE_DELETE = 'delete';
3441

35-
module.exports = File;
42+
export = File;

lib/box/index.js renamed to lib/box/index.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
'use strict';
2-
3-
const { join, sep } = require('path');
4-
const Promise = require('bluebird');
5-
const File = require('./file');
6-
const { Pattern, createSha1Hash } = require('hexo-util');
7-
const { createReadStream, readdir, stat, watch } = require('hexo-fs');
8-
const { magenta } = require('picocolors');
9-
const { EventEmitter } = require('events');
10-
const { isMatch, makeRe } = require('micromatch');
1+
import { join, sep } from 'path';
2+
import BlueBirdPromise from 'bluebird';
3+
import File from './file';
4+
import { Pattern, createSha1Hash } from 'hexo-util';
5+
import { createReadStream, readdir, stat, watch } from 'hexo-fs';
6+
import { magenta } from 'picocolors';
7+
import { EventEmitter } from 'events';
8+
import { isMatch, makeRe } from 'micromatch';
119

1210
const defaultPattern = new Pattern(() => ({}));
1311

12+
interface Processor {
13+
pattern: Pattern;
14+
process: (file: File) => void;
15+
}
16+
1417
class Box extends EventEmitter {
15-
constructor(ctx, base, options) {
18+
public options: any;
19+
public context: any;
20+
public base: any;
21+
public processors: Processor[];
22+
public _processingFiles: any;
23+
public watcher: any;
24+
public Cache: any;
25+
// TODO: replace runtime class _File
26+
public File: any;
27+
public ignore: any;
28+
public source: any;
29+
public emit: any;
30+
public ctx: any;
31+
32+
constructor(ctx, base, options?: object) {
1633
super();
1734

1835
this.options = Object.assign({
@@ -40,10 +57,13 @@ class Box extends EventEmitter {
4057
this.ignore = targets;
4158
this.options.ignored = targets.map(s => toRegExp(ctx, s)).filter(x => x);
4259
}
60+
4361
_createFileClass() {
4462
const ctx = this.context;
4563

4664
class _File extends File {
65+
public box: Box;
66+
4767
render(options) {
4868
return ctx.render.render({
4969
path: this.source
@@ -100,7 +120,7 @@ class Box extends EventEmitter {
100120
}));
101121
}
102122

103-
process(callback) {
123+
process(callback?) {
104124
const { base, Cache, context: ctx } = this;
105125

106126
return stat(base).then(stats => {
@@ -121,7 +141,7 @@ class Box extends EventEmitter {
121141

122142
_processFile(type, path) {
123143
if (this._processingFiles[path]) {
124-
return Promise.resolve();
144+
return BlueBirdPromise.resolve();
125145
}
126146

127147
this._processingFiles[path] = true;
@@ -133,7 +153,7 @@ class Box extends EventEmitter {
133153
path
134154
});
135155

136-
return Promise.reduce(this.processors, (count, processor) => {
156+
return BlueBirdPromise.reduce(this.processors, (count, processor) => {
137157
const params = processor.pattern.match(path);
138158
if (!params) return count;
139159

@@ -144,7 +164,7 @@ class Box extends EventEmitter {
144164
type
145165
});
146166

147-
return Reflect.apply(Promise.method(processor.process), ctx, [file])
167+
return Reflect.apply(BlueBirdPromise.method(processor.process), ctx, [file])
148168
.thenReturn(count + 1);
149169
}, 0).then(count => {
150170
if (count) {
@@ -164,7 +184,7 @@ class Box extends EventEmitter {
164184

165185
watch(callback) {
166186
if (this.isWatching()) {
167-
return Promise.reject(new Error('Watcher has already started.')).asCallback(callback);
187+
return BlueBirdPromise.reject(new Error('Watcher has already started.')).asCallback(callback);
168188
}
169189

170190
const { base } = this;
@@ -218,7 +238,7 @@ function getHash(path) {
218238
const src = createReadStream(path);
219239
const hasher = createSha1Hash();
220240

221-
const finishedPromise = new Promise((resolve, reject) => {
241+
const finishedPromise = new BlueBirdPromise((resolve, reject) => {
222242
src.once('error', reject);
223243
src.once('end', resolve);
224244
});
@@ -247,9 +267,9 @@ function isIgnoreMatch(path, ignore) {
247267
}
248268

249269
function readDirWalker(ctx, base, results, ignore, prefix) {
250-
if (isIgnoreMatch(base, ignore)) return Promise.resolve();
270+
if (isIgnoreMatch(base, ignore)) return BlueBirdPromise.resolve();
251271

252-
return Promise.map(readdir(base).catch(err => {
272+
return BlueBirdPromise.map(readdir(base).catch(err => {
253273
ctx.log.error({ err }, 'Failed to read directory: %s', base);
254274
if (err && err.code === 'ENOENT') return [];
255275
throw err;
@@ -272,4 +292,4 @@ function readDirWalker(ctx, base, results, ignore, prefix) {
272292
});
273293
}
274294

275-
module.exports = Box;
295+
export = Box;
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const Promise = require('bluebird');
4-
const abbrev = require('abbrev');
1+
import Promise from 'bluebird';
2+
import abbrev from 'abbrev';
53

64
/**
75
* Console plugin option
@@ -12,6 +10,9 @@ const abbrev = require('abbrev');
1210
*/
1311

1412
class Console {
13+
public store: any;
14+
public alias: any;
15+
1516
constructor() {
1617
this.store = {};
1718
this.alias = {};
@@ -82,4 +83,4 @@ class Console {
8283
}
8384
}
8485

85-
module.exports = Console;
86+
export = Console;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
'use strict';
2-
3-
const Promise = require('bluebird');
1+
import Promise from 'bluebird';
42

53
class Deployer {
4+
public store: any;
5+
66
constructor() {
77
this.store = {};
88
}
@@ -11,11 +11,11 @@ class Deployer {
1111
return this.store;
1212
}
1313

14-
get(name) {
14+
get(name: string) {
1515
return this.store[name];
1616
}
1717

18-
register(name, fn) {
18+
register(name: string, fn) {
1919
if (!name) throw new TypeError('name is required');
2020
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
2121

@@ -29,4 +29,4 @@ class Deployer {
2929
}
3030
}
3131

32-
module.exports = Deployer;
32+
export = Deployer;
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,64 @@
1-
'use strict';
2-
3-
const Promise = require('bluebird');
1+
import Promise from 'bluebird';
42

53
const typeAlias = {
64
pre: 'before_post_render',
75
post: 'after_post_render',
86
'after_render:html': '_after_html_render'
97
};
108

9+
interface FilterOptions {
10+
context?: any;
11+
args?: any;
12+
}
13+
14+
interface StoreFunction {
15+
(...args: any[]): any;
16+
priority?: number;
17+
}
18+
19+
interface Store {
20+
[key: string]: StoreFunction[]
21+
}
22+
1123
class Filter {
24+
public store: Store;
25+
1226
constructor() {
1327
this.store = {};
1428
}
1529

16-
list(type) {
30+
list(): Store;
31+
list(type: string): StoreFunction[];
32+
list(type?: string) {
1733
if (!type) return this.store;
1834
return this.store[type] || [];
1935
}
2036

21-
register(type, fn, priority) {
37+
register(fn: StoreFunction, priority: number);
38+
register(type?: string | StoreFunction, fn?: StoreFunction | number, priority?: number) {
2239
if (!priority) {
2340
if (typeof type === 'function') {
24-
priority = fn;
41+
priority = fn as number;
2542
fn = type;
2643
type = 'after_post_render';
2744
}
2845
}
2946

3047
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
3148

32-
type = typeAlias[type] || type;
49+
type = typeAlias[type as string] || type;
3350
priority = priority == null ? 10 : priority;
3451

35-
const store = this.store[type] || [];
36-
this.store[type] = store;
52+
const store = this.store[type as string] || [];
53+
this.store[type as string] = store;
3754

3855
fn.priority = priority;
3956
store.push(fn);
4057

4158
store.sort((a, b) => a.priority - b.priority);
4259
}
4360

44-
unregister(type, fn) {
61+
unregister(type: string, fn: StoreFunction) {
4562
if (!type) throw new TypeError('type is required');
4663
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
4764

@@ -55,7 +72,7 @@ class Filter {
5572
if (index !== -1) list.splice(index, 1);
5673
}
5774

58-
exec(type, data, options = {}) {
75+
exec(type: string, data, options: FilterOptions = {}) {
5976
const filters = this.list(type);
6077
if (filters.length === 0) return Promise.resolve(data);
6178

@@ -70,7 +87,7 @@ class Filter {
7087
})).then(() => args[0]);
7188
}
7289

73-
execSync(type, data, options = {}) {
90+
execSync(type: string, data, options: FilterOptions = {}) {
7491
const filters = this.list(type);
7592
const filtersLen = filters.length;
7693
if (filtersLen === 0) return data;
@@ -89,4 +106,4 @@ class Filter {
89106
}
90107
}
91108

92-
module.exports = Filter;
109+
export = Filter;
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
'use strict';
2-
3-
const Promise = require('bluebird');
1+
import Promise from 'bluebird';
42

53
class Generator {
4+
public id: any;
5+
public store: any;
6+
67
constructor() {
78
this.id = 0;
89
this.store = {};
@@ -12,7 +13,7 @@ class Generator {
1213
return this.store;
1314
}
1415

15-
get(name) {
16+
get(name: string) {
1617
return this.store[name];
1718
}
1819

@@ -31,4 +32,4 @@ class Generator {
3132
}
3233
}
3334

34-
module.exports = Generator;
35+
export = Generator;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'use strict';
2-
31
class Helper {
2+
public store: any;
3+
44
constructor() {
55
this.store = {};
66
}
@@ -17,7 +17,7 @@ class Helper {
1717
* @param {String} name - The name of the helper plugin
1818
* @returns {Function}
1919
*/
20-
get(name) {
20+
get(name: string) {
2121
return this.store[name];
2222
}
2323

@@ -26,12 +26,12 @@ class Helper {
2626
* @param {String} name - The name of the helper plugin
2727
* @param {Function} fn - The helper plugin function
2828
*/
29-
register(name, fn) {
29+
register(name: string, fn) {
3030
if (!name) throw new TypeError('name is required');
3131
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
3232

3333
this.store[name] = fn;
3434
}
3535
}
3636

37-
module.exports = Helper;
37+
export = Helper;

0 commit comments

Comments
 (0)