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 #185

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

export function writeFile(
path: string,
data: any,
data?: any,

Check warning on line 54 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
options?: WriteFileOptions
) {
if (!path) throw new TypeError('path is required!');
Expand All @@ -63,7 +63,7 @@
}


export function writeFileSync(path: string, data: any, options?: WriteFileOptions) {

Check warning on line 66 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
if (!path) throw new TypeError('path is required!');

fs.mkdirSync(dirname(path), { recursive: true });
Expand All @@ -72,7 +72,7 @@

export function appendFile(
path: string,
data: any,

Check warning on line 75 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
options?: WriteFileOptions) {
if (!path) throw new TypeError('path is required!');

Expand All @@ -80,7 +80,7 @@
.then(() => fsPromises.appendFile(path, data, options));
}

export function appendFileSync(path: string, data: any, options?: WriteFileOptions) {

Check warning on line 83 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
if (!path) throw new TypeError('path is required!');

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

function ignoreExcludeFiles(arr: any[], parent: string) {

Check warning on line 113 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
if (!arr || !arr.length) return trueFn;

const set = new Set(arr);
Expand Down Expand Up @@ -141,7 +141,7 @@
}

async function _copyDirWalker(
src: string, dest: string, results: any[], parent: string,

Check warning on line 144 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
options: ReadDirOptions) {
return BlueBirdPromise.map(_readAndFilterDir(src, options), item => {
const childSrc = join(src, item.name);
Expand Down Expand Up @@ -169,7 +169,7 @@
}

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

Check warning on line 172 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const promises = [];

for (const item of await _readAndFilterDir(path, options)) {
Expand Down Expand Up @@ -197,7 +197,7 @@
}

function _listDirSyncWalker(
path: string, results: any[], parent: string, options: ReadDirOptions) {

Check warning on line 200 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
for (const item of _readAndFilterDirSync(path, options)) {
const currentPath = join(parent, item.name);

Expand Down Expand Up @@ -269,7 +269,7 @@

async function _emptyDir(
path: string, parent?: string,
options?: ReadDirOptions & { exclude?: any[] }) {

Check warning on line 272 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const entries = (await _readAndFilterDir(path, options)).filter(
ignoreExcludeFiles(options.exclude, parent));
const results = [];
Expand All @@ -294,7 +294,7 @@
}

export function emptyDir(
path: string, options: ReadDirOptions & { exclude?: any[] } = {}) {

Check warning on line 297 in lib/fs.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
if (!path) throw new TypeError('path is required!');

return BlueBirdPromise.resolve(_emptyDir(path, '', options));
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "tsc -b",
"clean": "tsc -b --clean",
"eslint": "eslint .",
"test": "mocha test/index.js --require ts-node/register",
"test": "mocha test/index.ts --require ts-node/register",
"test-cov": "c8 --reporter=lcovonly npm run test"
},
"files": [
Expand Down Expand Up @@ -36,7 +36,9 @@
},
"devDependencies": {
"@types/bluebird": "^3.5.36",
"@types/chai": "^4.3.12",
"@types/graceful-fs": "^4.1.5",
"@types/mocha": "^10.0.6",
"@types/node": "^18.7.16 <18.19.9",
"c8": "^9.1.0",
"chai": "^4.3.6",
Expand Down
7 changes: 5 additions & 2 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "hexo/test",
"rules": { "@typescript-eslint/no-var-requires": 0 }
"extends": "hexo/ts-test",
"rules": {
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/ban-ts-comment": 0
}
}
58 changes: 40 additions & 18 deletions test/index.js → test/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict';

const { should } = require('chai');
should();

const { join, dirname } = require('path');
const Promise = require('bluebird');
const fs = require('../lib/fs.ts');

function createDummyFolder(path) {
import chai from 'chai';
import { join, dirname } from 'path';
// @ts-ignore
import Promise from 'bluebird';
D-Sketon marked this conversation as resolved.
Show resolved Hide resolved
import * as fs from '../lib/fs';
import type { FSWatcher } from 'chokidar';
const should = chai.should();

function createDummyFolder(path: string) {
const filesMap = {
// Normal files in a hidden folder
[join('.hidden', 'a.txt')]: 'a',
Expand All @@ -28,7 +27,7 @@ function createDummyFolder(path) {
return Promise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key]));
}

function createAnotherDummyFolder(path) {
function createAnotherDummyFolder(path: string) {
const filesMap = {
[join('folder', '.txt')]: 'txt',
[join('folder', '.js')]: 'js'
Expand All @@ -50,6 +49,7 @@ describe('fs', () => {

it('exists() - path is required', async () => {
try {
// @ts-ignore
await fs.exists();
should.fail();
} catch (err) {
Expand All @@ -64,6 +64,7 @@ describe('fs', () => {

it('existsSync() - path is required', () => {
try {
// @ts-ignore
fs.existsSync();
should.fail();
} catch (err) {
Expand All @@ -88,6 +89,7 @@ describe('fs', () => {

it('mkdirs() - path is required', async () => {
try {
// @ts-ignore
await fs.mkdirs();
should.fail();
} catch (err) {
Expand All @@ -108,6 +110,7 @@ describe('fs', () => {

it('mkdirsSync() - path is required', () => {
try {
// @ts-ignore
fs.mkdirsSync();
should.fail();
} catch (err) {
Expand All @@ -129,6 +132,7 @@ describe('fs', () => {

it('writeFile() - path is required', async () => {
try {
// @ts-ignore
await fs.writeFile();
D-Sketon marked this conversation as resolved.
Show resolved Hide resolved
should.fail();
} catch (err) {
Expand All @@ -150,6 +154,7 @@ describe('fs', () => {

it('writeFileSync() - path is required', () => {
try {
// @ts-ignore
fs.writeFileSync();
should.fail();
} catch (err) {
Expand All @@ -174,6 +179,7 @@ describe('fs', () => {

it('appendFile() - path is required', async () => {
try {
// @ts-ignore
await fs.appendFile();
should.fail();
} catch (err) {
Expand All @@ -197,6 +203,7 @@ describe('fs', () => {

it('appendFileSync() - path is required', () => {
try {
// @ts-ignore
fs.appendFileSync();
should.fail();
} catch (err) {
Expand All @@ -223,6 +230,7 @@ describe('fs', () => {

it('copyFile() - src is required', async () => {
try {
// @ts-ignore
await fs.copyFile();
should.fail();
} catch (err) {
Expand All @@ -232,6 +240,7 @@ describe('fs', () => {

it('copyFile() - dest is required', async () => {
try {
// @ts-ignore
await fs.copyFile('123');
should.fail();
} catch (err) {
Expand All @@ -254,9 +263,9 @@ describe('fs', () => {
const files = await fs.copyDir(src, dest);
files.should.eql(filenames);

const result = [];
const result: string[] = [];
for (const file of files) {
const output = await fs.readFile(join(dest, file));
const output = await fs.readFile(join(dest, file)) as string;
result.push(output);
}
result.should.eql(['e', 'f', 'h', 'i']);
Expand All @@ -266,6 +275,7 @@ describe('fs', () => {

it('copyDir() - src is required', async () => {
try {
// @ts-ignore
await fs.copyDir();
should.fail();
} catch (err) {
Expand All @@ -275,6 +285,7 @@ describe('fs', () => {

it('copyDir() - dest is required', async () => {
try {
// @ts-ignore
await fs.copyDir('123');
should.fail();
} catch (err) {
Expand Down Expand Up @@ -302,9 +313,9 @@ describe('fs', () => {
const files = await fs.copyDir(src, dest, { ignoreHidden: false });
files.should.have.members(filenames);

const result = [];
const result: string[] = [];
for (const file of files) {
const output = await fs.readFile(join(dest, file));
const output = await fs.readFile(join(dest, file)) as string;
result.push(output);
}
result.should.have.members(['a', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j']);
Expand All @@ -322,9 +333,9 @@ describe('fs', () => {
const files = await fs.copyDir(src, dest, { ignorePattern: /\.js/ });
files.should.eql(filenames);

const result = [];
const result: string[] = [];
for (const file of files) {
const output = await fs.readFile(join(dest, file));
const output = await fs.readFile(join(dest, file)) as string;
result.push(output);
}
result.should.eql(['e', 'h']);
Expand All @@ -350,6 +361,7 @@ describe('fs', () => {

it('listDir() - path is required', async () => {
try {
// @ts-ignore
await fs.listDir();
should.fail();
} catch (err) {
Expand Down Expand Up @@ -408,6 +420,7 @@ describe('fs', () => {

it('listDirSync() - path is required', () => {
try {
// @ts-ignore
fs.listDirSync();
should.fail();
} catch (err) {
Expand Down Expand Up @@ -460,6 +473,7 @@ describe('fs', () => {

it('readFile() - path is required', async () => {
try {
// @ts-ignore
await fs.readFile();
should.fail();
} catch (err) {
Expand Down Expand Up @@ -514,6 +528,7 @@ describe('fs', () => {

it('readFileSync() - path is required', () => {
try {
// @ts-ignore
fs.readFileSync();
should.fail();
} catch (err) {
Expand Down Expand Up @@ -622,6 +637,7 @@ describe('fs', () => {

it('emptyDir() - path is required', async () => {
try {
// @ts-ignore
await fs.emptyDir();
should.fail();
} catch (err) {
Expand Down Expand Up @@ -747,6 +763,7 @@ describe('fs', () => {

it('emptyDirSync() - path is required', () => {
try {
// @ts-ignore
fs.emptyDirSync();
should.fail();
} catch (err) {
Expand Down Expand Up @@ -848,6 +865,7 @@ describe('fs', () => {

it('rmdir() - path is required', async () => {
try {
// @ts-ignore
await fs.rmdir();
should.fail();
} catch (err) {
Expand All @@ -866,6 +884,7 @@ describe('fs', () => {

it('rmdirSync() - path is required', () => {
try {
// @ts-ignore
fs.rmdirSync();
should.fail();
} catch (err) {
Expand All @@ -876,7 +895,7 @@ describe('fs', () => {
it('watch()', async () => {
const target = join(tmpDir, 'test.txt');

const testerWrap = _watcher => new Promise((resolve, reject) => {
const testerWrap = (_watcher: FSWatcher) => new Promise<string>((resolve, reject) => {
_watcher.on('add', resolve).on('error', reject);
});

Expand All @@ -895,6 +914,7 @@ describe('fs', () => {

it('watch() - path is required', async () => {
try {
// @ts-ignore
await fs.watch();
should.fail();
} catch (err) {
Expand All @@ -921,6 +941,7 @@ describe('fs', () => {

it('ensurePath() - path is required', async () => {
try {
// @ts-ignore
await fs.ensurePath();
should.fail();
} catch (err) {
Expand Down Expand Up @@ -948,6 +969,7 @@ describe('fs', () => {

it('ensurePathSync() - path is required', () => {
try {
// @ts-ignore
fs.ensurePathSync();
should.fail();
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"declaration": true,
"esModuleInterop": true,
"types": [
"node"
"node",
"mocha"
]
},
"include": [
Expand Down