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 types #5178

Merged
merged 6 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion lib/box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class Box extends EventEmitter {
}).thenReturn(path);
}

watch(callback) {
watch(callback?) {
if (this.isWatching()) {
return BlueBirdPromise.reject(new Error('Watcher has already started.')).asCallback(callback);
}
Expand Down
59 changes: 43 additions & 16 deletions lib/extend/console.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import Promise from 'bluebird';
import abbrev from 'abbrev';

/**
* Console plugin option
* @typedef {Object} Option
* @property {String} usage - The usage of a console command
* @property {{name: String, desc: String}[]} arguments - The description of each argument of a console command
* @property {{name: String, desc: String}[]} options - The description of each option of a console command
*/
type Option = Partial<{
usage: string;
desc: string;
init: boolean;
arguments: {
name: string;
desc: string;
}[];
options: {
name: string;
desc: string;
}[];
}>

interface Args {
_: string[];
[key: string]: string | boolean | string[];
}
type AnyFn = (args: Args) => any;
interface StoreFunction extends AnyFn {
desc?: string;
options?: Option;
}

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

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

constructor() {
this.store = {};
Expand All @@ -21,9 +44,9 @@ class Console {
/**
* Get a console plugin function by name
* @param {String} name - The name of the console plugin
* @returns {Function} - The console plugin function
* @returns {StoreFunction} - The console plugin function
*/
get(name) {
get(name: string): StoreFunction {
name = name.toLowerCase();
return this.store[this.alias[name]];
}
Expand All @@ -37,9 +60,13 @@ class Console {
* @param {String} name - The name of console plugin to be registered
* @param {String} desc - More detailed information about a console command
* @param {Option} options - The description of each option of a console command
* @param {Function} fn - The console plugin to be registered
* @param {AnyFn} fn - The console plugin to be registered
*/
register(name, desc, options, fn) {
register(name: string, fn: AnyFn): void
register(name: string, desc: string, fn: AnyFn): void
register(name: string, options: Option, fn: AnyFn): void
register(name: string, desc: string, options: Option, fn: AnyFn): void
register(name: string, desc: string | Option | AnyFn, options?: Option | AnyFn, fn?: AnyFn) {
if (!name) throw new TypeError('name is required');

if (!fn) {
Expand Down Expand Up @@ -74,10 +101,10 @@ class Console {
fn = Promise.method(fn);
}

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

this.alias = abbrev(Object.keys(this.store));
}
Expand Down
14 changes: 12 additions & 2 deletions lib/extend/deployer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import Promise from 'bluebird';

interface StoreFunction {
(deployArg: {
type: string;
[key: string]: any
}) : any;
}
interface Store {
[key: string]: StoreFunction
}

class Deployer {
public store: any;
public store: Store;

constructor() {
this.store = {};
Expand All @@ -15,7 +25,7 @@ class Deployer {
return this.store[name];
}

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

Expand Down
15 changes: 9 additions & 6 deletions lib/extend/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const typeAlias = {

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

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

Expand All @@ -34,8 +34,11 @@ class Filter {
return this.store[type] || [];
}

register(fn: StoreFunction, priority: number);
register(type?: string | StoreFunction, fn?: StoreFunction | number, priority?: number) {
register(fn: StoreFunction): void
register(fn: StoreFunction, priority: number): void
register(type: string, fn: StoreFunction): void
register(type: string, fn: StoreFunction, priority: number): void
register(type: string | StoreFunction, fn?: StoreFunction | number, priority?: number) {
if (!priority) {
if (typeof type === 'function') {
priority = fn as number;
Expand Down Expand Up @@ -72,7 +75,7 @@ class Filter {
if (index !== -1) list.splice(index, 1);
}

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

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

execSync(type: string, data, options: FilterOptions = {}) {
execSync(type: string, data: any[], options: FilterOptions = {}) {
const filters = this.list(type);
const filtersLen = filters.length;
if (filtersLen === 0) return data;
Expand Down
34 changes: 29 additions & 5 deletions lib/extend/generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import Promise from 'bluebird';

interface BaseObj {
path: string;
data: any;
layout?: string;
}
type ReturnType = BaseObj | BaseObj[];
type GeneratorReturnType = ReturnType | Promise<ReturnType>;

interface GeneratorFunction {
(locals: object): GeneratorReturnType;
}

type StoreFunctionReturn = Promise<ReturnType>;

interface StoreFunction {
(locals: object): StoreFunctionReturn;
}

interface Store {
[key: string]: StoreFunction
}

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

constructor() {
this.id = 0;
Expand All @@ -17,9 +39,11 @@ class Generator {
return this.store[name];
}

register(name, fn) {
register(fn: GeneratorFunction): void
register(name: string, fn: GeneratorFunction): void
register(name: string | GeneratorFunction, fn?: GeneratorFunction) {
if (!fn) {
if (typeof name === 'function') {
if (typeof name === 'function') { // fn
fn = name;
name = `generator-${this.id++}`;
} else {
Expand All @@ -28,7 +52,7 @@ class Generator {
}

if (fn.length > 1) fn = Promise.promisify(fn);
this.store[name] = Promise.method(fn);
this.store[name as string] = Promise.method(fn);
}
}

Expand Down
23 changes: 16 additions & 7 deletions lib/extend/helper.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
interface StoreFunction {
(...args: any[]): string;
}

interface Store {
[key: string]: StoreFunction
}


class Helper {
public store: any;
public store: Store;

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

/**
* @returns {Object} - The plugin store
* @returns {Store} - The plugin store
*/
list() {
list(): Store {
return this.store;
}

/**
* Get helper plugin function by name
* @param {String} name - The name of the helper plugin
* @returns {Function}
* @returns {StoreFunction}
*/
get(name: string) {
get(name: string): StoreFunction {
return this.store[name];
}

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

Expand Down
18 changes: 13 additions & 5 deletions lib/extend/injector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Cache } from 'hexo-util';

type Entry = 'head_begin' | 'head_end' | 'body_begin' | 'body_end';

type Store = {
[key in Entry]: {
[key: string]: Set<unknown>;
};
};

class Injector {
public store: any;
public store: Store;
public cache: any;
public page: any;

Expand All @@ -20,21 +28,21 @@ class Injector {
return this.store;
}

get(entry, to = 'default') {
get(entry: Entry, to = 'default') {
return Array.from(this.store[entry][to] || []);
}

getText(entry, to = 'default') {
getText(entry: Entry, to = 'default') {
const arr = this.get(entry, to);
if (!arr || !arr.length) return '';
return arr.join('');
}

getSize(entry) {
getSize(entry: Entry) {
return this.cache.apply(`${entry}-size`, Object.keys(this.store[entry]).length);
}

register(entry, value, to = 'default') {
register(entry: Entry, value: string | (() => string), to = 'default') {
if (!entry) throw new TypeError('entry is required');
if (typeof value === 'function') value = value();

Expand Down
11 changes: 9 additions & 2 deletions lib/extend/migrator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import Promise from 'bluebird';
interface StoreFunction {
(args: any): any
}

interface Store {
[key: string]: StoreFunction
}

class Migrator {
public store: any;
public store: Store;

constructor() {
this.store = {};
Expand All @@ -15,7 +22,7 @@ class Migrator {
return this.store[name];
}

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

Expand Down
19 changes: 16 additions & 3 deletions lib/extend/processor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import Promise from 'bluebird';
import { Pattern } from 'hexo-util';
import type File from '../box/file';

interface StoreFunction {
(file: File): any
}

type Store = {
pattern: Pattern;
process: StoreFunction
}[];

type patternType = Exclude<ConstructorParameters<typeof Pattern>[0], ((str: string) => string)>;
class Processor {
public store: any;
public store: Store;

constructor() {
this.store = [];
Expand All @@ -12,7 +23,9 @@ class Processor {
return this.store;
}

register(pattern, fn) {
register(fn: StoreFunction): void;
register(pattern: patternType, fn: StoreFunction): void;
register(pattern: patternType | StoreFunction, fn?: StoreFunction) {
if (!fn) {
if (typeof pattern === 'function') {
fn = pattern;
Expand All @@ -29,7 +42,7 @@ class Processor {
}

this.store.push({
pattern: new Pattern(pattern),
pattern: new Pattern(pattern as patternType),
process: fn
});
}
Expand Down
Loading