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

fix: overload type #186

Merged
merged 1 commit into from
Apr 13, 2024
Merged
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
45 changes: 25 additions & 20 deletions lib/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { dirname, join, extname, basename } from 'path';
import { escapeRegExp } from 'hexo-util';

import fs from 'graceful-fs';
import type { Stream } from 'stream';

const fsPromises = fs.promises;

Expand Down Expand Up @@ -51,7 +52,7 @@ function checkParent(path: string) {

export function writeFile(
path: string,
data?: any,
data?: string | NodeJS.ArrayBufferView | Iterable<string | NodeJS.ArrayBufferView> | AsyncIterable<string | NodeJS.ArrayBufferView> | Stream,
options?: WriteFileOptions
) {
if (!path) throw new TypeError('path is required!');
Expand All @@ -63,7 +64,7 @@ export function writeFile(
}


export function writeFileSync(path: string, data: any, options?: WriteFileOptions) {
export function writeFileSync(path: string, data: string | NodeJS.ArrayBufferView, options?: WriteFileOptions) {
if (!path) throw new TypeError('path is required!');

fs.mkdirSync(dirname(path), { recursive: true });
Expand All @@ -72,15 +73,15 @@ export function writeFileSync(path: string, data: any, options?: WriteFileOption

export function appendFile(
path: string,
data: any,
data: string | Uint8Array,
options?: WriteFileOptions) {
if (!path) throw new TypeError('path is required!');

return checkParent(path)
.then(() => fsPromises.appendFile(path, data, options));
}

export function appendFileSync(path: string, data: any, options?: WriteFileOptions) {
export function appendFileSync(path: string, data: string | Uint8Array, options?: WriteFileOptions) {
if (!path) throw new TypeError('path is required!');

fs.mkdirSync(dirname(path), { recursive: true });
Expand Down Expand Up @@ -110,7 +111,7 @@ function ignoreFilesRegex(regex?: RegExp) {
return ({ name }) => !regex.test(name);
}

function ignoreExcludeFiles(arr: any[], parent: string) {
function ignoreExcludeFiles(arr: string[], parent: string) {
if (!arr || !arr.length) return trueFn;

const set = new Set(arr);
Expand Down Expand Up @@ -141,7 +142,7 @@ function _readAndFilterDirSync(path: string, options?: ReadDirOptions) {
}

async function _copyDirWalker(
src: string, dest: string, results: any[], parent: string,
src: string, dest: string, results: string[], parent: string,
options: ReadDirOptions) {
return BlueBirdPromise.map(_readAndFilterDir(src, options), item => {
const childSrc = join(src, item.name);
Expand All @@ -161,15 +162,15 @@ export function copyDir(
if (!src) throw new TypeError('src is required!');
if (!dest) throw new TypeError('dest is required!');

const results = [];
const results: string[] = [];

return checkParent(dest)
.then(() => _copyDirWalker(src, dest, results, '', options))
.return(results);
}

async function _listDirWalker(
path: string, results: any[], parent?: string, options?: ReadDirOptions) {
path: string, results: string[], parent?: string, options?: ReadDirOptions) {
const promises = [];

for (const item of await _readAndFilterDir(path, options)) {
Expand All @@ -190,14 +191,14 @@ export function listDir(
path: string, options: ReadDirOptions = {}) {
if (!path) throw new TypeError('path is required!');

const results = [];
const results: string[] = [];

return BlueBirdPromise.resolve(_listDirWalker(path, results, '', options))
.return(results);
}

function _listDirSyncWalker(
path: string, results: any[], parent: string, options: ReadDirOptions) {
path: string, results: string[], parent: string, options: ReadDirOptions) {
for (const item of _readAndFilterDirSync(path, options)) {
const currentPath = join(parent, item.name);

Expand All @@ -211,7 +212,7 @@ function _listDirSyncWalker(

export function listDirSync(path: string, options: ReadDirOptions = {}) {
if (!path) throw new TypeError('path is required!');
const results = [];
const results: string[] = [];

_listDirSyncWalker(path, results, '', options);

Expand All @@ -226,7 +227,7 @@ export function escapeBOM(str: string) {
return str.charCodeAt(0) === 0xFEFF ? str.substring(1) : str;
}

export function escapeFileContent(content) {
export function escapeFileContent(content: string) {
return escapeBOM(escapeEOL(content));
}

Expand All @@ -239,19 +240,23 @@ async function _readFile(path: string, options: ReadFileOptions | null = {}) {
const content = await fsPromises.readFile(path, options);

if (options.escape == null || options.escape) {
return escapeFileContent(content);
return escapeFileContent(content as string);
}

return content;
}

export function readFile(path: string): BlueBirdPromise<string>;
export function readFile(path: string, options?: ReadFileOptions | null): BlueBirdPromise<string | Buffer>;
export function readFile(
path: string, options?: ReadFileOptions | null) {
if (!path) throw new TypeError('path is required!');

return BlueBirdPromise.resolve(_readFile(path, options));
}

export function readFileSync(path: string): string;
export function readFileSync(path: string, options?: ReadFileOptions): string | Buffer;
export function readFileSync(path: string, options: ReadFileOptions = {}) {
if (!path) throw new TypeError('path is required!');

Expand All @@ -261,18 +266,18 @@ export function readFileSync(path: string, options: ReadFileOptions = {}) {
const content = fs.readFileSync(path, options);

if (options.escape == null || options.escape) {
return escapeFileContent(content);
return escapeFileContent(content as string);
}

return content;
}

async function _emptyDir(
path: string, parent?: string,
options?: ReadDirOptions & { exclude?: any[] }) {
options?: ReadDirOptions & { exclude?: string[] }) {
const entries = (await _readAndFilterDir(path, options)).filter(
ignoreExcludeFiles(options.exclude, parent));
const results = [];
const results: string[] = [];

await BlueBirdPromise.map(entries, (item: Dirent) => {
const fullPath = join(path, item.name);
Expand All @@ -294,19 +299,19 @@ async function _emptyDir(
}

export function emptyDir(
path: string, options: ReadDirOptions & { exclude?: any[] } = {}) {
path: string, options: ReadDirOptions & { exclude?: string[] } = {}) {
if (!path) throw new TypeError('path is required!');

return BlueBirdPromise.resolve(_emptyDir(path, '', options));
}

function _emptyDirSync(
path: string, options: ReadDirOptions & { exclude?: any[] },
path: string, options: ReadDirOptions & { exclude?: string[] },
parent?: string) {
const entries = _readAndFilterDirSync(path, options)
.filter(ignoreExcludeFiles(options.exclude, parent));

const results = [];
const results: string[] = [];

for (const item of entries) {
const childPath = join(path, item.name);
Expand All @@ -330,7 +335,7 @@ function _emptyDirSync(
}

export function emptyDirSync(
path: string, options: ReadDirOptions & { exclude?: any[] } = {}) {
path: string, options: ReadDirOptions & { exclude?: string[] } = {}) {
if (!path) throw new TypeError('path is required!');

return _emptyDirSync(path, options, '');
Expand Down