Skip to content

Commit

Permalink
Merge e67418f into 6d722a2
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Nov 5, 2022
2 parents 6d722a2 + e67418f commit 29a71d6
Show file tree
Hide file tree
Showing 23 changed files with 676 additions and 229 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
coverage/
tmp/
assets/
assets/
dist/
11 changes: 9 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"extends": "hexo",
"root": true
"root": true,
"extends": "hexo/ts.js",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
},
"rules": {
"@typescript-eslint/no-var-requires": 0
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tmp/
.idea/
.nyc_output/
.vscode/
dist/
12 changes: 5 additions & 7 deletions lib/console/help.js → lib/console/help.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

const { underline, bold } = require('picocolors');
const { readFile } = require('hexo-fs');
const { join } = require('path');
const Promise = require('bluebird');
import { underline, bold } from 'picocolors';
import { readFile } from 'hexo-fs';
import { join } from 'path';
import Promise from 'bluebird';

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

Expand Down Expand Up @@ -115,4 +113,4 @@ function printCompletion(type) {
});
}

module.exports = helpConsole;
export = helpConsole;
12 changes: 7 additions & 5 deletions lib/console/index.js → lib/console/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
import helpConsole from './help';
import initConsole from './init';
import versionConsole from './version';

module.exports = function(ctx) {
export = function(ctx) {
const { console } = ctx.extend;

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

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

console.register('version', 'Display version information.', {}, require('./version'));
console.register('version', 'Display version information.', {}, versionConsole);
};
22 changes: 10 additions & 12 deletions lib/console/init.js → lib/console/init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

const Promise = require('bluebird');
const { join, resolve } = require('path');
const { magenta } = require('picocolors');
const { existsSync, readdirSync, rmdir, unlink, copyDir, readdir, stat } = require('hexo-fs');
const tildify = require('tildify');
const { spawn } = require('hexo-util');
const commandExistsSync = require('command-exists').sync;
import BlueBirdPromise from 'bluebird';
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 { sync as commandExistsSync } from 'command-exists';

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

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

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

await Promise.all([
await BlueBirdPromise.all([
removeGitDir(target),
removeGitModules(target)
]);
Expand Down Expand Up @@ -111,4 +109,4 @@ async function removeGitModules(target) {
}
}

module.exports = initConsole;
export = initConsole;
14 changes: 6 additions & 8 deletions lib/console/version.js → lib/console/version.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

const os = require('os');
import os from 'os';
const pkg = require('../../package.json');
const Promise = require('bluebird');
const { spawn } = require('hexo-util');
import BlueBirdPromise from 'bluebird';
import { spawn } from 'hexo-util';

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

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

await Promise.resolve();
await BlueBirdPromise.resolve();
}

module.exports = versionConsole;
export = versionConsole;
30 changes: 19 additions & 11 deletions lib/context.js → lib/context.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
'use strict';

const logger = require('hexo-log');
const { underline } = require('picocolors');
const { EventEmitter } = require('events');
const Promise = require('bluebird');
const ConsoleExtend = require('./extend/console');
import logger from 'hexo-log';
import { underline } from 'picocolors';
import { EventEmitter } from 'events';
import Promise from 'bluebird';
import ConsoleExtend from './extend/console';

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

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

class Context extends EventEmitter {
base_dir: string;
log: logger;
extend: {
console: ConsoleExtend;
};

constructor(base = process.cwd(), args = {}) {
super();
this.base_dir = base;
Expand All @@ -24,9 +30,11 @@ class Context extends EventEmitter {
// Do nothing
}

call(name, args, 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') {
callback = args;
callback = args as Callback;
args = {};
}

Expand All @@ -41,7 +49,7 @@ class Context extends EventEmitter {
}).asCallback(callback);
}

exit(err) {
exit(err?: Error) {
if (err) {
this.log.fatal(
{err},
Expand All @@ -58,4 +66,4 @@ class Context extends EventEmitter {
}
}

module.exports = Context;
export = Context;
37 changes: 28 additions & 9 deletions lib/extend/console.js → lib/extend/console.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
'use strict';
import Promise from 'bluebird';
import abbrev from 'abbrev';

const Promise = require('bluebird');
const abbrev = require('abbrev');
interface Callback {
(args?: object): any;
options?: object;
desc?: string;
}

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

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

class Console {
store: Store;
alias: Alias;

constructor() {
this.store = {};
this.alias = {};
}

get(name) {
get(name: string) {
name = name.toLowerCase();
return this.store[this.alias[name]];
}
Expand All @@ -18,13 +33,17 @@ class Console {
return this.store;
}

register(name, desc, options, fn) {
register(name: string, desc: string, options: object, fn: Callback): void;
register(name: string, options: object, 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) {
if (!name) throw new TypeError('name is required');

if (!fn) {
if (options) {
if (typeof options === 'function') {
fn = options;
fn = options as Callback;

if (typeof desc === 'object') { // name, options, fn
options = desc;
Expand All @@ -38,7 +57,7 @@ class Console {
} else {
// name, fn
if (typeof desc === 'function') {
fn = desc;
fn = desc as Callback;
options = {};
desc = '';
} else {
Expand All @@ -56,10 +75,10 @@ class Console {
this.store[name.toLowerCase()] = fn;
const c = fn;
c.options = options;
c.desc = desc;
c.desc = desc as string;

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

module.exports = Console;
export = Console;
16 changes: 9 additions & 7 deletions lib/find_pkg.js → lib/find_pkg.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
'use strict';
import { resolve, join, dirname } from 'path';
import { readFile } from 'hexo-fs';

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

function findPkg(cwd, args = {}) {
function findPkg(cwd: string, args: findPkgArgs = {}) {
if (args.cwd) {
cwd = resolve(cwd, args.cwd);
}

return checkPkg(cwd);
}

function checkPkg(path) {
function checkPkg(path: string) {
const pkgPath = join(path, 'package.json');

return readFile(pkgPath).then(content => {
const json = JSON.parse(content);
const json = JSON.parse(content as string);
if (typeof json.hexo === 'object') return path;
}).catch(err => {
if (err && err.code === 'ENOENT') {
Expand All @@ -29,4 +31,4 @@ function checkPkg(path) {
});
}

module.exports = findPkg;
export = findPkg;
4 changes: 1 addition & 3 deletions lib/goodbye.js → lib/goodbye.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const byeWords = [
'Good bye',
'See you again',
Expand All @@ -9,4 +7,4 @@ const byeWords = [
'Catch you later'
];

module.exports = () => byeWords[(Math.random() * byeWords.length) | 0];
export = () => byeWords[(Math.random() * byeWords.length) | 0];
36 changes: 19 additions & 17 deletions lib/hexo.js → lib/hexo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

const { magenta } = require('picocolors');
const tildify = require('tildify');
const Promise = require('bluebird');
const Context = require('./context');
const findPkg = require('./find_pkg');
const goodbye = require('./goodbye');
const minimist = require('minimist');
const resolve = require('resolve');
const { camelCaseKeys } = require('hexo-util');
import { magenta } from 'picocolors';
import tildify from 'tildify';
import Promise from 'bluebird';
import Context from './context';
import findPkg from './find_pkg';
import goodbye from './goodbye';
import minimist from 'minimist';
import resolve from 'resolve';
import { camelCaseKeys } from 'hexo-util';
import registerConsole from './console';
import helpConsole from './console/help';
import initConsole from './console/init';
import versionConsole from './console/version';

class HexoNotFoundError extends Error {}

Expand Down Expand Up @@ -44,7 +46,7 @@ function entry(cwd = process.cwd(), args) {
if (mod) hexo = mod;
log = hexo.log;

require('./console')(hexo);
registerConsole(hexo);

return hexo.init();
}).then(() => {
Expand All @@ -65,12 +67,12 @@ function entry(cwd = process.cwd(), args) {
}

entry.console = {
init: require('./console/init'),
help: require('./console/help'),
version: require('./console/version')
init: initConsole,
help: helpConsole,
version: versionConsole
};

entry.version = require('../package.json').version;
entry.version = require('../package.json').version as string;

function loadModule(path, args) {
return Promise.try(() => {
Expand All @@ -93,4 +95,4 @@ function watchSignal(hexo) {
});
}

module.exports = entry;
export = entry;

0 comments on commit 29a71d6

Please sign in to comment.