Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate typescript #5092

Merged
merged 45 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8bf2b69
refactor: esm
stevenjoezhang Nov 1, 2022
0913097
refactor: migrate typescript
stevenjoezhang Nov 1, 2022
66c1a39
update files
stevenjoezhang Nov 1, 2022
a4dad2c
remove `use strict`
stevenjoezhang Nov 2, 2022
73d5ef9
fix indentation
stevenjoezhang Nov 2, 2022
7782709
refactor: migrate typescript
stevenjoezhang Nov 2, 2022
5d6ab84
refactor: migrate typescript
stevenjoezhang Nov 2, 2022
ca8c642
update
stevenjoezhang Nov 20, 2022
3ad21b9
export from
stevenjoezhang Nov 24, 2022
b2b73de
extend/filter
stevenjoezhang Nov 24, 2022
1a1f29e
post
stevenjoezhang Nov 24, 2022
ce93be0
Schema and SchemaType
stevenjoezhang Nov 24, 2022
d50a3ab
NunjucksError
stevenjoezhang Nov 24, 2022
1251c28
declare module
stevenjoezhang Nov 24, 2022
a1abae6
update tests
stevenjoezhang Nov 24, 2022
a77aebb
fix export
stevenjoezhang Nov 24, 2022
71c22ca
update
stevenjoezhang Nov 24, 2022
8660df6
update
stevenjoezhang Nov 24, 2022
067de95
number_format
stevenjoezhang Nov 25, 2022
4e1f175
update highlight
stevenjoezhang Dec 16, 2022
6f3fdc9
update
stevenjoezhang Jan 1, 2023
d5eade2
add secondary argument when create `Permalink` instance
yoshinorin Feb 15, 2023
99a34a7
specify `eslint` target directory
yoshinorin Feb 15, 2023
030da69
ignore two rules for testing
yoshinorin Feb 15, 2023
d222a0a
use `@ts-expect-error` for avoiding compile error
yoshinorin Feb 16, 2023
ea02b4d
use `any` for avoiding compile error
yoshinorin Feb 16, 2023
4de2cec
chore: add `dist/` to `.gitignore`
yoshinorin Feb 19, 2023
e8de597
test(eslint): update `.eslintrc`
yoshinorin Feb 19, 2023
4a0520a
refactor: `var` to `const`
yoshinorin Feb 19, 2023
30c4136
refactor: delete unnecessary `'use strict';`
yoshinorin Feb 19, 2023
fff9d9a
test: use `dist/modules/types/moment/SchemaTypeMoment` instead of `wa…
yoshinorin Feb 23, 2023
d1fa0f8
fix: export of processors
yoshinorin Feb 23, 2023
6d1be46
test: fix bind
yoshinorin Feb 28, 2023
752665a
test: fix processer
yoshinorin Feb 28, 2023
254f772
fix fs import
stevenjoezhang Mar 7, 2023
4d365b1
fix hexo-i18n and hexo-log
stevenjoezhang Mar 7, 2023
755212a
fix param order
stevenjoezhang Mar 7, 2023
4c717d2
build hexo in benchmark
stevenjoezhang Mar 7, 2023
fc1ecf4
fix eslint
stevenjoezhang Mar 7, 2023
fd690a5
fix export & drop travis
stevenjoezhang Mar 7, 2023
87216db
Update .husky/pre-commit
stevenjoezhang Mar 7, 2023
36f3f8b
add spaces
stevenjoezhang Mar 7, 2023
1d54103
refactor: ts debounce fn
renbaoshuo Mar 8, 2023
a7d0eb6
fix: lint errors
renbaoshuo Mar 8, 2023
2749b23
fix: lint errors
stevenjoezhang Mar 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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