Skip to content

Commit

Permalink
refactor: migrate typescript (#5092)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Mar 15, 2023
1 parent 7edbf25 commit 148ed7c
Show file tree
Hide file tree
Showing 280 changed files with 1,469 additions and 1,237 deletions.
17 changes: 13 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"extends": "hexo",
"root": true
}
{
"root": true,
"extends": "hexo/ts.js",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
},
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-var-requires": 0,
"node/no-missing-require": 0
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ package-lock.json
coverage/
.tmp*
.vscode
dist/
15 changes: 11 additions & 4 deletions lib/box/file.js → lib/box/file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
'use strict';

const { readFile, readFileSync, stat, statSync } = require('hexo-fs');
import { readFile, readFileSync, stat, statSync } from 'hexo-fs';

class File {
public source: any;
public path: any;
public params: any;
public type: any;
static TYPE_CREATE: 'create';
static TYPE_UPDATE: 'update';
static TYPE_SKIP: 'skip';
static TYPE_DELETE: 'delete';

constructor({ source, path, params, type }) {
this.source = source;
this.path = path;
Expand Down Expand Up @@ -32,4 +39,4 @@ File.TYPE_UPDATE = 'update';
File.TYPE_SKIP = 'skip';
File.TYPE_DELETE = 'delete';

module.exports = File;
export = File;
60 changes: 40 additions & 20 deletions lib/box/index.js → lib/box/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
'use strict';

const { join, sep } = require('path');
const Promise = require('bluebird');
const File = require('./file');
const { Pattern, createSha1Hash } = require('hexo-util');
const { createReadStream, readdir, stat, watch } = require('hexo-fs');
const { magenta } = require('picocolors');
const { EventEmitter } = require('events');
const { isMatch, makeRe } = require('micromatch');
import { join, sep } from 'path';
import BlueBirdPromise from 'bluebird';
import File from './file';
import { Pattern, createSha1Hash } from 'hexo-util';
import { createReadStream, readdir, stat, watch } from 'hexo-fs';
import { magenta } from 'picocolors';
import { EventEmitter } from 'events';
import { isMatch, makeRe } from 'micromatch';

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

interface Processor {
pattern: Pattern;
process: (file: File) => void;
}

class Box extends EventEmitter {
constructor(ctx, base, options) {
public options: any;
public context: any;
public base: any;
public processors: Processor[];
public _processingFiles: any;
public watcher: any;
public Cache: any;
// TODO: replace runtime class _File
public File: any;
public ignore: any;
public source: any;
public emit: any;
public ctx: any;

constructor(ctx, base, options?: object) {
super();

this.options = Object.assign({
Expand Down Expand Up @@ -40,10 +57,13 @@ class Box extends EventEmitter {
this.ignore = targets;
this.options.ignored = targets.map(s => toRegExp(ctx, s)).filter(x => x);
}

_createFileClass() {
const ctx = this.context;

class _File extends File {
public box: Box;

render(options) {
return ctx.render.render({
path: this.source
Expand Down Expand Up @@ -100,7 +120,7 @@ class Box extends EventEmitter {
}));
}

process(callback) {
process(callback?) {
const { base, Cache, context: ctx } = this;

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

_processFile(type, path) {
if (this._processingFiles[path]) {
return Promise.resolve();
return BlueBirdPromise.resolve();
}

this._processingFiles[path] = true;
Expand All @@ -133,7 +153,7 @@ class Box extends EventEmitter {
path
});

return Promise.reduce(this.processors, (count, processor) => {
return BlueBirdPromise.reduce(this.processors, (count, processor) => {
const params = processor.pattern.match(path);
if (!params) return count;

Expand All @@ -144,7 +164,7 @@ class Box extends EventEmitter {
type
});

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

watch(callback) {
if (this.isWatching()) {
return Promise.reject(new Error('Watcher has already started.')).asCallback(callback);
return BlueBirdPromise.reject(new Error('Watcher has already started.')).asCallback(callback);
}

const { base } = this;
Expand Down Expand Up @@ -218,7 +238,7 @@ function getHash(path) {
const src = createReadStream(path);
const hasher = createSha1Hash();

const finishedPromise = new Promise((resolve, reject) => {
const finishedPromise = new BlueBirdPromise((resolve, reject) => {
src.once('error', reject);
src.once('end', resolve);
});
Expand Down Expand Up @@ -247,9 +267,9 @@ function isIgnoreMatch(path, ignore) {
}

function readDirWalker(ctx, base, results, ignore, prefix) {
if (isIgnoreMatch(base, ignore)) return Promise.resolve();
if (isIgnoreMatch(base, ignore)) return BlueBirdPromise.resolve();

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

module.exports = Box;
export = Box;
11 changes: 6 additions & 5 deletions lib/extend/console.js → lib/extend/console.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const Promise = require('bluebird');
const abbrev = require('abbrev');
import Promise from 'bluebird';
import abbrev from 'abbrev';

/**
* Console plugin option
Expand All @@ -12,6 +10,9 @@ const abbrev = require('abbrev');
*/

class Console {
public store: any;
public alias: any;

constructor() {
this.store = {};
this.alias = {};
Expand Down Expand Up @@ -82,4 +83,4 @@ class Console {
}
}

module.exports = Console;
export = Console;
12 changes: 6 additions & 6 deletions lib/extend/deployer.js → lib/extend/deployer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const Promise = require('bluebird');
import Promise from 'bluebird';

class Deployer {
public store: any;

constructor() {
this.store = {};
}
Expand All @@ -11,11 +11,11 @@ class Deployer {
return this.store;
}

get(name) {
get(name: string) {
return this.store[name];
}

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

Expand All @@ -29,4 +29,4 @@ class Deployer {
}
}

module.exports = Deployer;
export = Deployer;
43 changes: 30 additions & 13 deletions lib/extend/filter.js → lib/extend/filter.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,64 @@
'use strict';

const Promise = require('bluebird');
import Promise from 'bluebird';

const typeAlias = {
pre: 'before_post_render',
post: 'after_post_render',
'after_render:html': '_after_html_render'
};

interface FilterOptions {
context?: any;
args?: any;
}

interface StoreFunction {
(...args: any[]): any;
priority?: number;
}

interface Store {
[key: string]: StoreFunction[]
}

class Filter {
public store: Store;

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

list(type) {
list(): Store;
list(type: string): StoreFunction[];
list(type?: string) {
if (!type) return this.store;
return this.store[type] || [];
}

register(type, fn, priority) {
register(fn: StoreFunction, priority: number);
register(type?: string | StoreFunction, fn?: StoreFunction | number, priority?: number) {
if (!priority) {
if (typeof type === 'function') {
priority = fn;
priority = fn as number;
fn = type;
type = 'after_post_render';
}
}

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

type = typeAlias[type] || type;
type = typeAlias[type as string] || type;
priority = priority == null ? 10 : priority;

const store = this.store[type] || [];
this.store[type] = store;
const store = this.store[type as string] || [];
this.store[type as string] = store;

fn.priority = priority;
store.push(fn);

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

unregister(type, fn) {
unregister(type: string, fn: StoreFunction) {
if (!type) throw new TypeError('type is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');

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

exec(type, data, options = {}) {
exec(type: string, data, options: FilterOptions = {}) {
const filters = this.list(type);
if (filters.length === 0) return Promise.resolve(data);

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

execSync(type, data, options = {}) {
execSync(type: string, data, options: FilterOptions = {}) {
const filters = this.list(type);
const filtersLen = filters.length;
if (filtersLen === 0) return data;
Expand All @@ -89,4 +106,4 @@ class Filter {
}
}

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

const Promise = require('bluebird');
import Promise from 'bluebird';

class Generator {
public id: any;
public store: any;

constructor() {
this.id = 0;
this.store = {};
Expand All @@ -12,7 +13,7 @@ class Generator {
return this.store;
}

get(name) {
get(name: string) {
return this.store[name];
}

Expand All @@ -31,4 +32,4 @@ class Generator {
}
}

module.exports = Generator;
export = Generator;
10 changes: 5 additions & 5 deletions lib/extend/helper.js → lib/extend/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

class Helper {
public store: any;

constructor() {
this.store = {};
}
Expand All @@ -17,7 +17,7 @@ class Helper {
* @param {String} name - The name of the helper plugin
* @returns {Function}
*/
get(name) {
get(name: string) {
return this.store[name];
}

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

this.store[name] = fn;
}
}

module.exports = Helper;
export = Helper;
Loading

0 comments on commit 148ed7c

Please sign in to comment.