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: refactor types #5344

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 4 additions & 4 deletions lib/box/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class File {
this.type = type;
}

read(options?: ReadFileOptions): Promise<string | Buffer> {
return readFile(this.source, options);
read(options?: ReadFileOptions): Promise<string> {
return readFile(this.source, options) as Promise<string>;
}

readSync(options?: ReadFileOptions): string | Buffer {
return readFileSync(this.source, options);
readSync(options?: ReadFileOptions): string {
return readFileSync(this.source, options) as string;
}

stat(): Promise<fs.Stats> {
Expand Down
3 changes: 2 additions & 1 deletion lib/box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { magenta } from 'picocolors';
import { EventEmitter } from 'events';
import { isMatch, makeRe } from 'micromatch';
import type Hexo from '../hexo';
import type { NodeJSLikeCallback } from '../types';

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

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

class Box extends EventEmitter {
Expand Down
5 changes: 3 additions & 2 deletions lib/extend/console.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Promise from 'bluebird';
import abbrev from 'abbrev';
import type { NodeJSLikeCallback } from '../types';

type Option = Partial<{
usage: string;
Expand All @@ -19,7 +20,7 @@ interface Args {
_: string[];
[key: string]: string | boolean | string[];
}
type AnyFn = (args: Args) => any;
type AnyFn = (args: Args, callback?: NodeJSLikeCallback<any>) => any;
interface StoreFunction extends AnyFn {
desc?: string;
options?: Option;
Expand All @@ -29,7 +30,7 @@ interface Store {
[key: string]: StoreFunction
}
interface Alias {
[key: string]: string
[abbreviation: string]: string
}

class Console {
Expand Down
3 changes: 2 additions & 1 deletion lib/extend/deployer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

interface StoreFunction {
(deployArg: {
type: string;
[key: string]: any
}) : any;
}, callback?: NodeJSLikeCallback<any>) : any;
}
interface Store {
[key: string]: StoreFunction
Expand Down
5 changes: 3 additions & 2 deletions lib/extend/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface FilterOptions {
args?: any[];
}


interface StoreFunction {
(data?: any, ...args: any[]): any;
priority?: number;
Expand Down Expand Up @@ -75,7 +76,7 @@ class Filter {
if (index !== -1) list.splice(index, 1);
}

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

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

execSync(type: string, data: any[], 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
3 changes: 2 additions & 1 deletion lib/extend/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

interface BaseObj {
path: string;
Expand All @@ -9,7 +10,7 @@ type ReturnType = BaseObj | BaseObj[];
type GeneratorReturnType = ReturnType | Promise<ReturnType>;

interface GeneratorFunction {
(locals: object): GeneratorReturnType;
(locals: object, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
}

type StoreFunctionReturn = Promise<ReturnType>;
Expand Down
4 changes: 3 additions & 1 deletion lib/extend/migrator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

interface StoreFunction {
(args: any): any
(args: any, callback?: NodeJSLikeCallback<any>): any
}

interface Store {
Expand Down
9 changes: 5 additions & 4 deletions lib/extend/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { extname } from 'path';
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

const getExtname = (str: string) => {
if (typeof str !== 'string') return '';
Expand All @@ -13,15 +14,15 @@ export interface StoreFunctionData {
text?: string;
engine?: string;
toString?: any;
onRenderEnd?: any;
onRenderEnd?: (...args: any[]) => any;
}

export interface StoreSyncFunction {
[x: string]: any;
(
data: StoreFunctionData,
options: object,
// callback: NodeJSLikeCallback<string>
// callback?: NodeJSLikeCallback<string>
): any;
output?: string;
compile?: (local: object) => any;
Expand All @@ -30,6 +31,7 @@ export interface StoreFunction {
(
data: StoreFunctionData,
options: object,
callback?: NodeJSLikeCallback<any>
): Promise<any>;
(
data: StoreFunctionData,
Expand Down Expand Up @@ -57,7 +59,7 @@ class Renderer {
this.storeSync = {};
}

list(sync: boolean): Store | SyncStore {
list(sync?: boolean): Store | SyncStore {
D-Sketon marked this conversation as resolved.
Show resolved Hide resolved
return sync ? this.storeSync : this.store;
}

Expand Down Expand Up @@ -97,7 +99,6 @@ class Renderer {
this.storeSync[name].output = output;

this.store[name] = Promise.method(fn);
// eslint-disable-next-line no-extra-parens
this.store[name].disableNunjucks = (fn as StoreFunction).disableNunjucks;
} else {
if (fn.length > 2) fn = Promise.promisify(fn);
Expand Down
11 changes: 8 additions & 3 deletions lib/extend/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import { cyan, magenta, red, bold } from 'picocolors';
import { Environment } from 'nunjucks';
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

const rSwigRawFullBlock = /{% *raw *%}/;
const rCodeTag = /<code[^<>]*>[\s\S]+?<\/code>/g;
const escapeSwigTag = (str: string) => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;');

interface TagFunction {
(args: any[], content: string): string;
(args: any[], content: string, callback?: NodeJSLikeCallback<any>): string | PromiseLike<string>;
}
interface AsyncTagFunction {
(args: any[], content: string): Promise<string>;
Expand Down Expand Up @@ -57,7 +59,7 @@
return node;
}

run(context, args, body, callback) {

Check warning on line 62 in lib/extend/tag.ts

View workflow job for this annotation

GitHub Actions / linter

'body' is defined but never used

Check warning on line 62 in lib/extend/tag.ts

View workflow job for this annotation

GitHub Actions / linter

'callback' is defined but never used
return this._run(context, args, '');
}

Expand All @@ -78,14 +80,14 @@
return new nodes.CallExtension(this, 'run', node, [body]);
}

_parseBody(parser, nodes, lexer) {

Check warning on line 83 in lib/extend/tag.ts

View workflow job for this annotation

GitHub Actions / linter

'nodes' is defined but never used

Check warning on line 83 in lib/extend/tag.ts

View workflow job for this annotation

GitHub Actions / linter

'lexer' is defined but never used
const body = parser.parseUntilBlocks(`end${this.tags[0]}`);

parser.advanceAfterBlockEnd();
return body;
}

run(context, args, body, callback) {

Check warning on line 90 in lib/extend/tag.ts

View workflow job for this annotation

GitHub Actions / linter

'callback' is defined but never used
return this._run(context, args, trimBody(body));
}
}
Expand Down Expand Up @@ -251,14 +253,17 @@
if (env.hasExtension(name)) env.removeExtension(name);
}

render(str: string, options: { source?: string } = {}, callback?: NodeJSLikeCallback<any>): Promise<any> {
render(str: string): Promise<any>;
render(str: string, callback: NodeJSLikeCallback<any>): Promise<any>;
render(str: string, options: { source?: string, [key: string]: any }, callback?: NodeJSLikeCallback<any>): Promise<any>;
render(str: string, options: { source?: string, [key: string]: any } | NodeJSLikeCallback<any> = {}, callback?: NodeJSLikeCallback<any>): Promise<any> {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
}

// Get path of post from source
const { source = '' } = options;
const { source = '' } = options as { source?: string };

return Promise.fromCallback(cb => {
this.env.renderString(
Expand Down
4 changes: 3 additions & 1 deletion lib/hexo/default_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export = {
wrap: true,
exclude_languages: [],
language_attr: false,
hljs: false
hljs: false,
line_threshold: 0,
first_line_number: 'always1'
},
prismjs: {
preprocess: true,
Expand Down
Loading
Loading