Skip to content

Commit 16dcfb3

Browse files
authored
refactor: migrate ts and refactor types (#501)
1 parent cba603b commit 16dcfb3

20 files changed

+163
-9664
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ tmp/
55
.idea/
66
.nyc_output/
77
.vscode/
8-
dist/
8+
dist/
9+
package-lock.json
10+
coverage/

lib/console/help.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ import { underline, bold } from 'picocolors';
22
import { readFile } from 'hexo-fs';
33
import { join } from 'path';
44
import Promise from 'bluebird';
5+
import type Context from '../context';
6+
import type { Callback, Store, Command } from '../types';
57

68
const COMPLETION_DIR = join(__dirname, '../../completion');
79

8-
function helpConsole(args) {
10+
interface HelpArgs {
11+
_: string[];
12+
v?: boolean;
13+
version?: boolean;
14+
consoleList?: boolean;
15+
completion?: string;
16+
}
17+
18+
function helpConsole(this: Context, args: HelpArgs) {
919
if (args.v || args.version) {
1020
return this.call('version');
1121
} else if (args.consoleList) {
@@ -24,7 +34,7 @@ function helpConsole(args) {
2434
return printAllHelp(this.extend.console.list());
2535
}
2636

27-
function printHelpForCommand(command, data) {
37+
function printHelpForCommand(command: string, data: Callback) {
2838
const { options } = data;
2939

3040
const desc = options.description || options.desc || data.description || data.desc;
@@ -40,7 +50,7 @@ function printHelpForCommand(command, data) {
4050
return Promise.resolve();
4151
}
4252

43-
function printAllHelp(list) {
53+
function printAllHelp(list: Store) {
4454
const keys = Object.keys(list);
4555
const commands = [];
4656
const { length } = keys;
@@ -73,7 +83,7 @@ function printAllHelp(list) {
7383
return Promise.resolve();
7484
}
7585

76-
function printList(title, list) {
86+
function printList(title: string, list: Command[]) {
7787
list.sort((a, b) => {
7888
const nameA = a.name;
7989
const nameB = b.name;
@@ -101,13 +111,13 @@ function printList(title, list) {
101111
return Promise.resolve();
102112
}
103113

104-
function printConsoleList(list) {
114+
function printConsoleList(list: Store) {
105115
console.log(Object.keys(list).join('\n'));
106116

107117
return Promise.resolve();
108118
}
109119

110-
function printCompletion(type) {
120+
function printCompletion(type: string) {
111121
return readFile(join(COMPLETION_DIR, type)).then(content => {
112122
console.log(content);
113123
});

lib/console/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type Context from '../context';
12
import helpConsole from './help';
23
import initConsole from './init';
34
import versionConsole from './version';
45

5-
export = function(ctx) {
6+
export = function(ctx: Context) {
67
const { console } = ctx.extend;
78

89
console.register('help', 'Get help on a command.', {}, helpConsole);

lib/console/init.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import { join, resolve } from 'path';
33
import { magenta } from 'picocolors';
44
import { existsSync, readdirSync, rmdir, unlink, copyDir, readdir, stat } from 'hexo-fs';
55
import tildify from 'tildify';
6-
import { spawn } from 'hexo-util';
6+
import spawn from 'hexo-util/dist/spawn'; // for rewire
77
import { sync as commandExistsSync } from 'command-exists';
8+
import type Context from '../context';
89

910
const ASSET_DIR = join(__dirname, '../../assets');
1011
const GIT_REPO_URL = 'https://github.com/hexojs/hexo-starter.git';
1112

12-
async function initConsole(args) {
13+
interface InitArgs {
14+
_: string[];
15+
install?: boolean;
16+
clone?: boolean;
17+
}
18+
19+
async function initConsole(this: Context, args: InitArgs) {
1320
args = Object.assign({ install: true, clone: true }, args);
1421

1522
const baseDir = this.base_dir;
@@ -83,11 +90,11 @@ async function initConsole(args) {
8390
}
8491
}
8592

86-
async function copyAsset(target) {
93+
async function copyAsset(target: string) {
8794
await copyDir(ASSET_DIR, target, { ignoreHidden: false });
8895
}
8996

90-
function removeGitDir(target) {
97+
function removeGitDir(target: string) {
9198
const gitDir = join(target, '.git');
9299

93100
return stat(gitDir).catch(err => {
@@ -100,7 +107,7 @@ function removeGitDir(target) {
100107
}).then(() => readdir(target)).map(path => join(target, path)).filter(path => stat(path).then(stats => stats.isDirectory())).each(removeGitDir);
101108
}
102109

103-
async function removeGitModules(target) {
110+
async function removeGitModules(target: string) {
104111
try {
105112
await unlink(join(target, '.gitmodules'));
106113
} catch (err) {

lib/console/version.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import os from 'os';
22
const pkg = require('../../package.json');
33
import BlueBirdPromise from 'bluebird';
44
import { spawn } from 'hexo-util';
5+
import type Context from '../context';
56

6-
async function versionConsole() {
7+
async function versionConsole(this: Context) {
78
const { versions, platform } = process;
89
const keys = Object.keys(versions);
910

lib/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Promise from 'bluebird';
44
import ConsoleExtend from './extend/console';
55

66
// a stub Hexo object
7-
// see `hexojs/hexo/lib/hexo/index.js`
7+
// see `hexojs/hexo/lib/hexo/index.ts`
88

99
type Callback = (err?: any, value?: any) => void;
1010

@@ -14,6 +14,7 @@ class Context {
1414
extend: {
1515
console: ConsoleExtend;
1616
};
17+
version?: string | null;
1718

1819
constructor(base = process.cwd(), args = {}) {
1920
this.base_dir = base;
@@ -28,7 +29,7 @@ class Context {
2829
// Do nothing
2930
}
3031

31-
call(name: string, args: object, callback: Callback);
32+
call(name: string, args: object, callback?: Callback);
3233
call(name: string, callback?: Callback);
3334
call(name: string, args?: object | Callback, callback?: Callback) {
3435
if (!callback && typeof args === 'function') {

lib/extend/console.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import Promise from 'bluebird';
22
import abbrev from 'abbrev';
3-
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-
}
3+
import type { Options, Callback, Store, Alias } from '../types';
174

185
class Console {
196
store: Store;
@@ -33,11 +20,11 @@ class Console {
3320
return this.store;
3421
}
3522

36-
register(name: string, desc: string, options: object, fn: Callback): void;
37-
register(name: string, options: object, fn: Callback): void;
23+
register(name: string, desc: string, options: Options, fn: Callback): void;
24+
register(name: string, options: Options, fn: Callback): void;
3825
register(name: string, desc: string, fn: Callback): void;
3926
register(name: string, fn: Callback): void;
40-
register(name: string, desc: string | object | Callback, options?: object | Callback, fn?: Callback) {
27+
register(name: string, desc: string | Options | Callback, options?: Options | Callback, fn?: Callback) {
4128
if (!name) throw new TypeError('name is required');
4229

4330
if (!fn) {
@@ -74,7 +61,7 @@ class Console {
7461

7562
this.store[name.toLowerCase()] = fn;
7663
const c = fn;
77-
c.options = options;
64+
c.options = options as Options;
7865
c.desc = desc as string;
7966

8067
this.alias = abbrev(Object.keys(this.store));

lib/hexo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function loadModule(path, args) {
8383
});
8484
}
8585

86-
function watchSignal(hexo) {
86+
function watchSignal(hexo: Context) {
8787
process.on('SIGINT', () => {
8888
hexo.log.info(goodbye());
8989
hexo.unwatch();

lib/types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export interface Command {
2+
name: string;
3+
desc: string;
4+
description?: string;
5+
}
6+
7+
export interface Options {
8+
desc?: string;
9+
description?: string;
10+
usage?: string;
11+
arguments?: Command[];
12+
options?: Command[];
13+
commands?: Command[];
14+
}
15+
16+
export interface Callback {
17+
(args?: object): any;
18+
options?: Options;
19+
desc?: string;
20+
description?: string;
21+
}
22+
23+
export interface Store {
24+
[key: string]: Callback;
25+
}
26+
27+
export interface Alias {
28+
[key: string]: string;
29+
}

0 commit comments

Comments
 (0)