Skip to content

Commit

Permalink
refactor: migrate ts and refactor types (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Apr 6, 2024
1 parent cba603b commit 16dcfb3
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 9,664 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ tmp/
.idea/
.nyc_output/
.vscode/
dist/
dist/
package-lock.json
coverage/
22 changes: 16 additions & 6 deletions lib/console/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import { underline, bold } from 'picocolors';
import { readFile } from 'hexo-fs';
import { join } from 'path';
import Promise from 'bluebird';
import type Context from '../context';
import type { Callback, Store, Command } from '../types';

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

function helpConsole(args) {
interface HelpArgs {
_: string[];
v?: boolean;
version?: boolean;
consoleList?: boolean;
completion?: string;
}

function helpConsole(this: Context, args: HelpArgs) {
if (args.v || args.version) {
return this.call('version');
} else if (args.consoleList) {
Expand All @@ -24,7 +34,7 @@ function helpConsole(args) {
return printAllHelp(this.extend.console.list());
}

function printHelpForCommand(command, data) {
function printHelpForCommand(command: string, data: Callback) {
const { options } = data;

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

function printAllHelp(list) {
function printAllHelp(list: Store) {
const keys = Object.keys(list);
const commands = [];
const { length } = keys;
Expand Down Expand Up @@ -73,7 +83,7 @@ function printAllHelp(list) {
return Promise.resolve();
}

function printList(title, list) {
function printList(title: string, list: Command[]) {
list.sort((a, b) => {
const nameA = a.name;
const nameB = b.name;
Expand Down Expand Up @@ -101,13 +111,13 @@ function printList(title, list) {
return Promise.resolve();
}

function printConsoleList(list) {
function printConsoleList(list: Store) {
console.log(Object.keys(list).join('\n'));

return Promise.resolve();
}

function printCompletion(type) {
function printCompletion(type: string) {
return readFile(join(COMPLETION_DIR, type)).then(content => {
console.log(content);
});
Expand Down
3 changes: 2 additions & 1 deletion lib/console/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type Context from '../context';
import helpConsole from './help';
import initConsole from './init';
import versionConsole from './version';

export = function(ctx) {
export = function(ctx: Context) {
const { console } = ctx.extend;

console.register('help', 'Get help on a command.', {}, helpConsole);
Expand Down
17 changes: 12 additions & 5 deletions lib/console/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import { join, resolve } from 'path';
import { magenta } from 'picocolors';
import { existsSync, readdirSync, rmdir, unlink, copyDir, readdir, stat } from 'hexo-fs';
import tildify from 'tildify';
import { spawn } from 'hexo-util';
import spawn from 'hexo-util/dist/spawn'; // for rewire
import { sync as commandExistsSync } from 'command-exists';
import type Context from '../context';

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

async function initConsole(args) {
interface InitArgs {
_: string[];
install?: boolean;
clone?: boolean;
}

async function initConsole(this: Context, args: InitArgs) {
args = Object.assign({ install: true, clone: true }, args);

const baseDir = this.base_dir;
Expand Down Expand Up @@ -83,11 +90,11 @@ async function initConsole(args) {
}
}

async function copyAsset(target) {
async function copyAsset(target: string) {
await copyDir(ASSET_DIR, target, { ignoreHidden: false });
}

function removeGitDir(target) {
function removeGitDir(target: string) {
const gitDir = join(target, '.git');

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

async function removeGitModules(target) {
async function removeGitModules(target: string) {
try {
await unlink(join(target, '.gitmodules'));
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion lib/console/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import os from 'os';
const pkg = require('../../package.json');
import BlueBirdPromise from 'bluebird';
import { spawn } from 'hexo-util';
import type Context from '../context';

async function versionConsole() {
async function versionConsole(this: Context) {
const { versions, platform } = process;
const keys = Object.keys(versions);

Expand Down
5 changes: 3 additions & 2 deletions lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Promise from 'bluebird';
import ConsoleExtend from './extend/console';

// a stub Hexo object
// see `hexojs/hexo/lib/hexo/index.js`
// see `hexojs/hexo/lib/hexo/index.ts`

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

Check warning on line 9 in lib/context.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type

Check warning on line 9 in lib/context.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type

Expand All @@ -14,6 +14,7 @@ class Context {
extend: {
console: ConsoleExtend;
};
version?: string | null;

constructor(base = process.cwd(), args = {}) {
this.base_dir = base;
Expand All @@ -28,7 +29,7 @@ class Context {
// Do nothing
}

call(name: string, args: object, callback: Callback);
call(name: string, args: object, callback?: Callback);
call(name: string, callback?: Callback);
call(name: string, args?: object | Callback, callback?: Callback) {
if (!callback && typeof args === 'function') {
Expand Down
23 changes: 5 additions & 18 deletions lib/extend/console.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import Promise from 'bluebird';
import abbrev from 'abbrev';

interface Callback {
(args?: object): any;
options?: object;
desc?: string;
}

interface Store {
[key: string]: Callback;
}

interface Alias {
[key: string]: string;
}
import type { Options, Callback, Store, Alias } from '../types';

class Console {
store: Store;
Expand All @@ -33,11 +20,11 @@ class Console {
return this.store;
}

register(name: string, desc: string, options: object, fn: Callback): void;
register(name: string, options: object, fn: Callback): void;
register(name: string, desc: string, options: Options, fn: Callback): void;
register(name: string, options: Options, fn: Callback): void;
register(name: string, desc: string, fn: Callback): void;
register(name: string, fn: Callback): void;
register(name: string, desc: string | object | Callback, options?: object | Callback, fn?: Callback) {
register(name: string, desc: string | Options | Callback, options?: Options | Callback, fn?: Callback) {
if (!name) throw new TypeError('name is required');

if (!fn) {
Expand Down Expand Up @@ -74,7 +61,7 @@ class Console {

this.store[name.toLowerCase()] = fn;
const c = fn;
c.options = options;
c.options = options as Options;
c.desc = desc as string;

this.alias = abbrev(Object.keys(this.store));
Expand Down
2 changes: 1 addition & 1 deletion lib/hexo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function loadModule(path, args) {
});
}

function watchSignal(hexo) {
function watchSignal(hexo: Context) {
process.on('SIGINT', () => {
hexo.log.info(goodbye());
hexo.unwatch();
Expand Down
29 changes: 29 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export interface Command {
name: string;
desc: string;
description?: string;
}

export interface Options {
desc?: string;
description?: string;
usage?: string;
arguments?: Command[];
options?: Command[];
commands?: Command[];
}

export interface Callback {
(args?: object): any;

Check warning on line 17 in lib/types.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
options?: Options;
desc?: string;
description?: string;
}

export interface Store {
[key: string]: Callback;
}

export interface Alias {
[key: string]: string;
}
Loading

0 comments on commit 16dcfb3

Please sign in to comment.