Skip to content

Commit

Permalink
refactor: refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Nov 9, 2023
1 parent be9a1f2 commit e5a2679
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 71 deletions.
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
4 changes: 2 additions & 2 deletions lib/hexo/load_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { deepMerge } from 'hexo-util';
import validateConfig from './validate_config';
import type Hexo from './index';

export = async (ctx: Hexo) => {
export = async (ctx: Hexo): Promise<void> => {
if (!ctx.env.init) return;

const baseDir = ctx.base_dir;
Expand Down Expand Up @@ -66,7 +66,7 @@ export = async (ctx: Hexo) => {

};

async function findConfigPath(path: string) {
async function findConfigPath(path: string): Promise<string> {
const { dir, name } = parse(path);

const files = await readdir(dir);
Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/load_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { exists, unlink } from 'hexo-fs';
import Promise from 'bluebird';
import type Hexo from './index';

export = (ctx: Hexo) => {
export = (ctx: Hexo): Promise<void> => {
if (ctx._dbLoaded) return Promise.resolve();

const db = ctx.database;
Expand Down
4 changes: 2 additions & 2 deletions lib/hexo/load_plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import Promise from 'bluebird';
import { magenta } from 'picocolors';
import type Hexo from './index';

export = (ctx: Hexo) => {
export = (ctx: Hexo): Promise<void[][]> => {
if (!ctx.env.init || ctx.env.safe) return;

return loadModules(ctx).then(() => loadScripts(ctx));
};

function loadModuleList(ctx: Hexo, basedir: string) {
function loadModuleList(ctx: Hexo, basedir: string): Promise<any> {
const packagePath = join(basedir, 'package.json');

// Make sure package.json exists
Expand Down
5 changes: 3 additions & 2 deletions lib/hexo/load_theme_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { exists, readdir } from 'hexo-fs';
import { magenta } from 'picocolors';
import { deepMerge } from 'hexo-util';
import type Hexo from './index';
import type Promise from 'bluebird';

export = (ctx: Hexo) => {
export = (ctx: Hexo): Promise<void> => {
if (!ctx.env.init) return;
if (!ctx.config.theme) return;

Expand All @@ -31,7 +32,7 @@ export = (ctx: Hexo) => {
});
};

function findConfigPath(path: string) {
function findConfigPath(path: string): Promise<string> {
const { dir, name } = parse(path);

return readdir(dir).then(files => {
Expand Down
6 changes: 3 additions & 3 deletions lib/hexo/locals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Locals {
});
}

set(name: string, value: any) {
set(name: string, value: any): this {
if (typeof name !== 'string') throw new TypeError('name must be a string!');
if (value == null) throw new TypeError('value is required!');

Expand All @@ -32,7 +32,7 @@ class Locals {
return this;
}

remove(name: string) {
remove(name: string): this {
if (typeof name !== 'string') throw new TypeError('name must be a string!');

this.getters[name] = null;
Expand All @@ -41,7 +41,7 @@ class Locals {
return this;
}

invalidate() {
invalidate(): this {
this.cache.flush();

return this;
Expand Down
4 changes: 2 additions & 2 deletions lib/hexo/multi_config_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yml from 'js-yaml';
import { deepMerge } from 'hexo-util';
import type Hexo from './index';

export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: string) {
export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: string): string {
const { log } = ctx;
const defaultPath = join(base, '_config.yml');

Expand Down Expand Up @@ -43,7 +43,7 @@ export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: stri
}

// files read synchronously to ensure proper overwrite order
const file = readFileSync(configPath);
const file = readFileSync(configPath) as string;
const ext = extname(paths[i]).toLowerCase();

if (ext === '.yml') {
Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/register_models.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as models from '../models';
import type Hexo from './index';

export = (ctx: Hexo) => {
export = (ctx: Hexo): void => {
const db = ctx.database;

const keys = Object.keys(models);
Expand Down
25 changes: 13 additions & 12 deletions lib/hexo/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import Promise from 'bluebird';
import { readFile, readFileSync } from 'hexo-fs';
import type Hexo from './index';
import type { Renderer } from '../extend';
import type { StoreFunctionData } from '../extend/renderer';
import type { StoreFunction, StoreFunctionData, StoreSyncFunction } from '../extend/renderer';
import { NodeJSLikeCallback } from '../types';

const getExtname = (str: string) => {
const getExtname = (str: string): string => {
if (typeof str !== 'string') return '';

const ext = extname(str);
return ext.startsWith('.') ? ext.slice(1) : ext;
};

const toString = (result, options) => {
const toString = (result: any, options: StoreFunctionData) => {
if (!Object.prototype.hasOwnProperty.call(options, 'toString') || typeof result === 'string') return result;

if (typeof options.toString === 'function') {
Expand All @@ -35,27 +36,27 @@ class Render {
this.renderer = ctx.extend.renderer;
}

isRenderable(path: string) {
isRenderable(path: string): boolean {
return this.renderer.isRenderable(path);
}

isRenderableSync(path: string) {
isRenderableSync(path: string): boolean {
return this.renderer.isRenderableSync(path);
}

getOutput(path: string) {
getOutput(path: string): string {
return this.renderer.getOutput(path);
}

getRenderer(ext: string, sync?: boolean) {
getRenderer(ext: string, sync?: boolean): StoreSyncFunction | StoreFunction {
return this.renderer.get(ext, sync);
}

getRendererSync(ext: string) {
getRendererSync(ext: string): StoreSyncFunction | StoreFunction {
return this.getRenderer(ext, true);
}

render(data: StoreFunctionData, options?: { highlight?: boolean; }, callback?: undefined) {
render(data: StoreFunctionData, options?: { highlight?: boolean; }, callback?: NodeJSLikeCallback<any>): Promise<any> {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
Expand All @@ -64,7 +65,7 @@ class Render {
const ctx = this.context;
let ext = '';

let promise;
let promise: Promise<string | Buffer>;

if (!data) return Promise.reject(new TypeError('No input file or string!'));

Expand All @@ -76,7 +77,7 @@ class Render {
promise = readFile(data.path);
}

return promise.then(text => {
return promise.then((text: string) => {
data.text = text;
ext = data.engine || getExtname(data.path);
if (!ext || !this.isRenderable(ext)) return text;
Expand All @@ -99,7 +100,7 @@ class Render {
}).asCallback(callback);
}

renderSync(data: StoreFunctionData, options = {}) {
renderSync(data: StoreFunctionData, options = {}): any {
if (!data) throw new TypeError('No input file or string!');

const ctx = this.context;
Expand Down
20 changes: 10 additions & 10 deletions lib/hexo/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare module 'stream' {
class RouteStream extends Readable {
public _data: any;
public _ended: boolean;
public modified: any;
public modified: boolean;

constructor(data: Data) {
super({ objectMode: true });
Expand All @@ -28,7 +28,7 @@ class RouteStream extends Readable {
}

// Assume we only accept Buffer, plain object, or string
_toBuffer(data) {
_toBuffer(data: Buffer | object | string): Buffer | null {
if (data instanceof Buffer) {
return data;
}
Expand All @@ -41,7 +41,7 @@ class RouteStream extends Readable {
return null;
}

_read() {
_read(): boolean {
const data = this._data;

if (typeof data !== 'function') {
Expand Down Expand Up @@ -84,7 +84,7 @@ class RouteStream extends Readable {
}
}

const _format = (path: string) => {
const _format = (path: string): string => {
path = path || '';
if (typeof path !== 'string') throw new TypeError('path must be a string!');

Expand Down Expand Up @@ -113,16 +113,16 @@ class Router extends EventEmitter {
this.routes = {};
}

list() {
list(): string[] {
const { routes } = this;
return Object.keys(routes).filter(key => routes[key]);
}

format(path: string) {
format(path: string): string {
return _format(path);
}

get(path: string) {
get(path: string): RouteStream {
if (typeof path !== 'string') throw new TypeError('path must be a string!');

const data = this.routes[this.format(path)];
Expand All @@ -131,14 +131,14 @@ class Router extends EventEmitter {
return new RouteStream(data);
}

isModified(path: string) {
isModified(path: string): boolean {
if (typeof path !== 'string') throw new TypeError('path must be a string!');

const data = this.routes[this.format(path)];
return data ? data.modified : false;
}

set(path: string, data: any) {
set(path: string, data: any): this {
if (typeof path !== 'string') throw new TypeError('path must be a string!');
if (data == null) throw new TypeError('data is required!');

Expand Down Expand Up @@ -173,7 +173,7 @@ class Router extends EventEmitter {
return this;
}

remove(path: string) {
remove(path: string): this {
if (typeof path !== 'string') throw new TypeError('path must be a string!');
path = this.format(path);

Expand Down
21 changes: 15 additions & 6 deletions lib/hexo/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { extname, join } from 'path';
import { exists, listDir, readFile, unlink, writeFile } from 'hexo-fs';
import type Hexo from './index';
import type { NodeJSLikeCallback } from '../types';
import type Promise from 'bluebird';

class Scaffold {
public context: Hexo;
public scaffoldDir: string;
public defaults: any;
public defaults: {
normal: string
};

constructor(context: Hexo) {
this.context = context;
Expand All @@ -23,7 +26,10 @@ class Scaffold {
};
}

_listDir() {
_listDir(): Promise<{
name: string;
path: string;
}[]> {
const { scaffoldDir } = this;

return exists(scaffoldDir).then(exist => {
Expand All @@ -38,11 +44,14 @@ class Scaffold {
}));
}

_getScaffold(name: string) {
_getScaffold(name: string): Promise<{
name: string;
path: string;
}> {
return this._listDir().then(list => list.find(item => item.name === name));
}

get(name: string, callback?: NodeJSLikeCallback<any>) {
get(name: string, callback?: NodeJSLikeCallback<any>): Promise<string> {
return this._getScaffold(name).then(item => {
if (item) {
return readFile(item.path);
Expand All @@ -52,7 +61,7 @@ class Scaffold {
}).asCallback(callback);
}

set(name: string, content: any, callback: NodeJSLikeCallback<void>) {
set(name: string, content: any, callback: NodeJSLikeCallback<void>): Promise<void> {
const { scaffoldDir } = this;

return this._getScaffold(name).then(item => {
Expand All @@ -63,7 +72,7 @@ class Scaffold {
}).asCallback(callback);
}

remove(name: string, callback: NodeJSLikeCallback<void>) {
remove(name: string, callback: NodeJSLikeCallback<void>): Promise<void> {
return this._getScaffold(name).then(item => {
if (!item) return;

Expand Down
5 changes: 3 additions & 2 deletions lib/hexo/update_package.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { join } from 'path';
import { writeFile, exists, readFile } from 'hexo-fs';
import type Hexo from './index';
import type Promise from 'bluebird';

export = (ctx: Hexo) => {
export = (ctx: Hexo): Promise<void> => {
const pkgPath = join(ctx.base_dir, 'package.json');

return readPkg(pkgPath).then(pkg => {
Expand All @@ -19,7 +20,7 @@ export = (ctx: Hexo) => {
});
};

function readPkg(path: string) {
function readPkg(path: string): Promise<any> {
return exists(path).then(exist => {
if (!exist) return;

Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/validate_config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type Hexo from './index';

export = (ctx: Hexo) => {
export = (ctx: Hexo): void => {
const { config, log } = ctx;

log.info('Validating config');
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/renderer/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { StoreFunctionData } from '../../extend/renderer';

function jsonRenderer(data: StoreFunctionData) {
function jsonRenderer(data: StoreFunctionData): any {
return JSON.parse(data.text);
}

Expand Down
Loading

0 comments on commit e5a2679

Please sign in to comment.