Skip to content

Commit 8c5d506

Browse files
refactor: migrate typescript (#418)
Co-authored-by: yoshinorin <[email protected]>
1 parent 6d722a2 commit 8c5d506

23 files changed

+676
-229
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
coverage/
33
tmp/
4-
assets/
4+
assets/
5+
dist/

.eslintrc.json

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

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ tmp/
55
.idea/
66
.nyc_output/
77
.vscode/
8+
dist/

lib/console/help.js renamed to lib/console/help.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const { underline, bold } = require('picocolors');
4-
const { readFile } = require('hexo-fs');
5-
const { join } = require('path');
6-
const Promise = require('bluebird');
1+
import { underline, bold } from 'picocolors';
2+
import { readFile } from 'hexo-fs';
3+
import { join } from 'path';
4+
import Promise from 'bluebird';
75

86
const COMPLETION_DIR = join(__dirname, '../../completion');
97

@@ -115,4 +113,4 @@ function printCompletion(type) {
115113
});
116114
}
117115

118-
module.exports = helpConsole;
116+
export = helpConsole;
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
'use strict';
1+
import helpConsole from './help';
2+
import initConsole from './init';
3+
import versionConsole from './version';
24

3-
module.exports = function(ctx) {
5+
export = function(ctx) {
46
const { console } = ctx.extend;
57

6-
console.register('help', 'Get help on a command.', {}, require('./help'));
8+
console.register('help', 'Get help on a command.', {}, helpConsole);
79

810
console.register('init', 'Create a new Hexo folder.', {
911
desc: 'Create a new Hexo folder at the specified path or the current directory.',
@@ -15,7 +17,7 @@ module.exports = function(ctx) {
1517
{name: '--no-clone', desc: 'Copy files instead of cloning from GitHub'},
1618
{name: '--no-install', desc: 'Skip npm install'}
1719
]
18-
}, require('./init'));
20+
}, initConsole);
1921

20-
console.register('version', 'Display version information.', {}, require('./version'));
22+
console.register('version', 'Display version information.', {}, versionConsole);
2123
};

lib/console/init.js renamed to lib/console/init.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
2-
3-
const Promise = require('bluebird');
4-
const { join, resolve } = require('path');
5-
const { magenta } = require('picocolors');
6-
const { existsSync, readdirSync, rmdir, unlink, copyDir, readdir, stat } = require('hexo-fs');
7-
const tildify = require('tildify');
8-
const { spawn } = require('hexo-util');
9-
const commandExistsSync = require('command-exists').sync;
1+
import BlueBirdPromise from 'bluebird';
2+
import { join, resolve } from 'path';
3+
import { magenta } from 'picocolors';
4+
import { existsSync, readdirSync, rmdir, unlink, copyDir, readdir, stat } from 'hexo-fs';
5+
import tildify from 'tildify';
6+
import { spawn } from 'hexo-util';
7+
import { sync as commandExistsSync } from 'command-exists';
108

119
const ASSET_DIR = join(__dirname, '../../assets');
1210
const GIT_REPO_URL = 'https://github.com/hexojs/hexo-starter.git';
@@ -20,7 +18,7 @@ async function initConsole(args) {
2018

2119
if (existsSync(target) && readdirSync(target).length !== 0) {
2220
log.fatal(`${magenta(tildify(target))} not empty, please run \`hexo init\` on an empty folder and then copy your files into it`);
23-
await Promise.reject(new Error('target not empty'));
21+
await BlueBirdPromise.reject(new Error('target not empty'));
2422
}
2523

2624
log.info('Cloning hexo-starter', GIT_REPO_URL);
@@ -38,7 +36,7 @@ async function initConsole(args) {
3836
await copyAsset(target);
3937
}
4038

41-
await Promise.all([
39+
await BlueBirdPromise.all([
4240
removeGitDir(target),
4341
removeGitModules(target)
4442
]);
@@ -111,4 +109,4 @@ async function removeGitModules(target) {
111109
}
112110
}
113111

114-
module.exports = initConsole;
112+
export = initConsole;

lib/console/version.js renamed to lib/console/version.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict';
2-
3-
const os = require('os');
1+
import os from 'os';
42
const pkg = require('../../package.json');
5-
const Promise = require('bluebird');
6-
const { spawn } = require('hexo-util');
3+
import BlueBirdPromise from 'bluebird';
4+
import { spawn } from 'hexo-util';
75

8-
async function versionConsole(args) {
6+
async function versionConsole() {
97
const { versions, platform } = process;
108
const keys = Object.keys(versions);
119

@@ -33,7 +31,7 @@ async function versionConsole(args) {
3331
console.log('%s: %s', key, versions[key]);
3432
}
3533

36-
await Promise.resolve();
34+
await BlueBirdPromise.resolve();
3735
}
3836

39-
module.exports = versionConsole;
37+
export = versionConsole;

lib/context.js renamed to lib/context.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
'use strict';
2-
3-
const logger = require('hexo-log');
4-
const { underline } = require('picocolors');
5-
const { EventEmitter } = require('events');
6-
const Promise = require('bluebird');
7-
const ConsoleExtend = require('./extend/console');
1+
import logger from 'hexo-log';
2+
import { underline } from 'picocolors';
3+
import { EventEmitter } from 'events';
4+
import Promise from 'bluebird';
5+
import ConsoleExtend from './extend/console';
86

97
// a stub Hexo object
108
// see `hexojs/hexo/lib/hexo/index.js`
119

10+
type Callback = (err?: any, value?: any) => void;
11+
1212
class Context extends EventEmitter {
13+
base_dir: string;
14+
log: logger;
15+
extend: {
16+
console: ConsoleExtend;
17+
};
18+
1319
constructor(base = process.cwd(), args = {}) {
1420
super();
1521
this.base_dir = base;
@@ -24,9 +30,11 @@ class Context extends EventEmitter {
2430
// Do nothing
2531
}
2632

27-
call(name, args, callback) {
33+
call(name: string, args: object, callback: Callback);
34+
call(name: string, callback?: Callback);
35+
call(name: string, args?: object | Callback, callback?: Callback) {
2836
if (!callback && typeof args === 'function') {
29-
callback = args;
37+
callback = args as Callback;
3038
args = {};
3139
}
3240

@@ -41,7 +49,7 @@ class Context extends EventEmitter {
4149
}).asCallback(callback);
4250
}
4351

44-
exit(err) {
52+
exit(err?: Error) {
4553
if (err) {
4654
this.log.fatal(
4755
{err},
@@ -58,4 +66,4 @@ class Context extends EventEmitter {
5866
}
5967
}
6068

61-
module.exports = Context;
69+
export = Context;

lib/extend/console.js renamed to lib/extend/console.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
'use strict';
1+
import Promise from 'bluebird';
2+
import abbrev from 'abbrev';
23

3-
const Promise = require('bluebird');
4-
const abbrev = require('abbrev');
4+
interface Callback {
5+
(args?: object): any;
6+
options?: object;
7+
desc?: string;
8+
}
9+
10+
interface Store {
11+
[key: string]: Callback;
12+
}
13+
14+
interface Alias {
15+
[key: string]: string;
16+
}
517

618
class Console {
19+
store: Store;
20+
alias: Alias;
21+
722
constructor() {
823
this.store = {};
924
this.alias = {};
1025
}
1126

12-
get(name) {
27+
get(name: string) {
1328
name = name.toLowerCase();
1429
return this.store[this.alias[name]];
1530
}
@@ -18,13 +33,17 @@ class Console {
1833
return this.store;
1934
}
2035

21-
register(name, desc, options, fn) {
36+
register(name: string, desc: string, options: object, fn: Callback): void;
37+
register(name: string, options: object, fn: Callback): void;
38+
register(name: string, desc: string, fn: Callback): void;
39+
register(name: string, fn: Callback): void;
40+
register(name: string, desc: string | object | Callback, options?: object | Callback, fn?: Callback) {
2241
if (!name) throw new TypeError('name is required');
2342

2443
if (!fn) {
2544
if (options) {
2645
if (typeof options === 'function') {
27-
fn = options;
46+
fn = options as Callback;
2847

2948
if (typeof desc === 'object') { // name, options, fn
3049
options = desc;
@@ -38,7 +57,7 @@ class Console {
3857
} else {
3958
// name, fn
4059
if (typeof desc === 'function') {
41-
fn = desc;
60+
fn = desc as Callback;
4261
options = {};
4362
desc = '';
4463
} else {
@@ -56,10 +75,10 @@ class Console {
5675
this.store[name.toLowerCase()] = fn;
5776
const c = fn;
5877
c.options = options;
59-
c.desc = desc;
78+
c.desc = desc as string;
6079

6180
this.alias = abbrev(Object.keys(this.store));
6281
}
6382
}
6483

65-
module.exports = Console;
84+
export = Console;
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
'use strict';
1+
import { resolve, join, dirname } from 'path';
2+
import { readFile } from 'hexo-fs';
23

3-
const { resolve, join, dirname } = require('path');
4-
const { readFile } = require('hexo-fs');
4+
interface findPkgArgs {
5+
cwd?: string;
6+
}
57

6-
function findPkg(cwd, args = {}) {
8+
function findPkg(cwd: string, args: findPkgArgs = {}) {
79
if (args.cwd) {
810
cwd = resolve(cwd, args.cwd);
911
}
1012

1113
return checkPkg(cwd);
1214
}
1315

14-
function checkPkg(path) {
16+
function checkPkg(path: string) {
1517
const pkgPath = join(path, 'package.json');
1618

1719
return readFile(pkgPath).then(content => {
18-
const json = JSON.parse(content);
20+
const json = JSON.parse(content as string);
1921
if (typeof json.hexo === 'object') return path;
2022
}).catch(err => {
2123
if (err && err.code === 'ENOENT') {
@@ -29,4 +31,4 @@ function checkPkg(path) {
2931
});
3032
}
3133

32-
module.exports = findPkg;
34+
export = findPkg;

0 commit comments

Comments
 (0)