From 3cb0d542b281245f94beb04311942970ff4bc6a2 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sun, 3 Mar 2024 20:31:55 +0800 Subject: [PATCH 1/4] refactor: migrate typescript --- lib/fs.ts | 2 +- package.json | 4 ++- test/.eslintrc.json | 7 +++-- test/{index.js => index.ts} | 52 ++++++++++++++++++++++++++----------- tsconfig.json | 3 ++- 5 files changed, 48 insertions(+), 20 deletions(-) rename test/{index.js => index.ts} (95%) diff --git a/lib/fs.ts b/lib/fs.ts index 83da6d1..9b64196 100644 --- a/lib/fs.ts +++ b/lib/fs.ts @@ -51,7 +51,7 @@ function checkParent(path: string) { export function writeFile( path: string, - data: any, + data?: any, options?: WriteFileOptions ) { if (!path) throw new TypeError('path is required!'); diff --git a/package.json b/package.json index e126d2a..4839325 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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", diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 5625b43..44ee061 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -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 + } } diff --git a/test/index.js b/test/index.ts similarity index 95% rename from test/index.js rename to test/index.ts index 517f375..87c6b27 100644 --- a/test/index.js +++ b/test/index.ts @@ -1,11 +1,10 @@ -'use strict'; - -const { should } = require('chai'); -should(); - -const { join, dirname } = require('path'); -const Promise = require('bluebird'); -const fs = require('../lib/fs.ts'); +import chai from 'chai'; +import { join, dirname } from 'path'; +// @ts-ignore +import Promise from 'bluebird'; +import * as fs from '../lib/fs'; +import { FSWatcher } from 'chokidar'; +const should = chai.should(); function createDummyFolder(path) { const filesMap = { @@ -50,6 +49,7 @@ describe('fs', () => { it('exists() - path is required', async () => { try { + // @ts-ignore await fs.exists(); should.fail(); } catch (err) { @@ -64,6 +64,7 @@ describe('fs', () => { it('existsSync() - path is required', () => { try { + // @ts-ignore fs.existsSync(); should.fail(); } catch (err) { @@ -88,6 +89,7 @@ describe('fs', () => { it('mkdirs() - path is required', async () => { try { + // @ts-ignore await fs.mkdirs(); should.fail(); } catch (err) { @@ -108,6 +110,7 @@ describe('fs', () => { it('mkdirsSync() - path is required', () => { try { + // @ts-ignore fs.mkdirsSync(); should.fail(); } catch (err) { @@ -129,6 +132,7 @@ describe('fs', () => { it('writeFile() - path is required', async () => { try { + // @ts-ignore await fs.writeFile(); should.fail(); } catch (err) { @@ -150,6 +154,7 @@ describe('fs', () => { it('writeFileSync() - path is required', () => { try { + // @ts-ignore fs.writeFileSync(); should.fail(); } catch (err) { @@ -174,6 +179,7 @@ describe('fs', () => { it('appendFile() - path is required', async () => { try { + // @ts-ignore await fs.appendFile(); should.fail(); } catch (err) { @@ -197,6 +203,7 @@ describe('fs', () => { it('appendFileSync() - path is required', () => { try { + // @ts-ignore fs.appendFileSync(); should.fail(); } catch (err) { @@ -223,6 +230,7 @@ describe('fs', () => { it('copyFile() - src is required', async () => { try { + // @ts-ignore await fs.copyFile(); should.fail(); } catch (err) { @@ -232,6 +240,7 @@ describe('fs', () => { it('copyFile() - dest is required', async () => { try { + // @ts-ignore await fs.copyFile('123'); should.fail(); } catch (err) { @@ -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']); @@ -266,6 +275,7 @@ describe('fs', () => { it('copyDir() - src is required', async () => { try { + // @ts-ignore await fs.copyDir(); should.fail(); } catch (err) { @@ -275,6 +285,7 @@ describe('fs', () => { it('copyDir() - dest is required', async () => { try { + // @ts-ignore await fs.copyDir('123'); should.fail(); } catch (err) { @@ -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']); @@ -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']); @@ -350,6 +361,7 @@ describe('fs', () => { it('listDir() - path is required', async () => { try { + // @ts-ignore await fs.listDir(); should.fail(); } catch (err) { @@ -408,6 +420,7 @@ describe('fs', () => { it('listDirSync() - path is required', () => { try { + // @ts-ignore fs.listDirSync(); should.fail(); } catch (err) { @@ -460,6 +473,7 @@ describe('fs', () => { it('readFile() - path is required', async () => { try { + // @ts-ignore await fs.readFile(); should.fail(); } catch (err) { @@ -514,6 +528,7 @@ describe('fs', () => { it('readFileSync() - path is required', () => { try { + // @ts-ignore fs.readFileSync(); should.fail(); } catch (err) { @@ -622,6 +637,7 @@ describe('fs', () => { it('emptyDir() - path is required', async () => { try { + // @ts-ignore await fs.emptyDir(); should.fail(); } catch (err) { @@ -747,6 +763,7 @@ describe('fs', () => { it('emptyDirSync() - path is required', () => { try { + // @ts-ignore fs.emptyDirSync(); should.fail(); } catch (err) { @@ -848,6 +865,7 @@ describe('fs', () => { it('rmdir() - path is required', async () => { try { + // @ts-ignore await fs.rmdir(); should.fail(); } catch (err) { @@ -866,6 +884,7 @@ describe('fs', () => { it('rmdirSync() - path is required', () => { try { + // @ts-ignore fs.rmdirSync(); should.fail(); } catch (err) { @@ -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((resolve, reject) => { _watcher.on('add', resolve).on('error', reject); }); @@ -895,6 +914,7 @@ describe('fs', () => { it('watch() - path is required', async () => { try { + // @ts-ignore await fs.watch(); should.fail(); } catch (err) { @@ -921,6 +941,7 @@ describe('fs', () => { it('ensurePath() - path is required', async () => { try { + // @ts-ignore await fs.ensurePath(); should.fail(); } catch (err) { @@ -948,6 +969,7 @@ describe('fs', () => { it('ensurePathSync() - path is required', () => { try { + // @ts-ignore fs.ensurePathSync(); should.fail(); } catch (err) { diff --git a/tsconfig.json b/tsconfig.json index bfb25bd..75d3733 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,8 @@ "declaration": true, "esModuleInterop": true, "types": [ - "node" + "node", + "mocha" ] }, "include": [ From a48cb6ed85f7471ae43372a527c348307222dc26 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sun, 3 Mar 2024 20:34:44 +0800 Subject: [PATCH 2/4] refactor: migrate typescript --- test/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/index.ts b/test/index.ts index 87c6b27..00ffdcd 100644 --- a/test/index.ts +++ b/test/index.ts @@ -3,10 +3,10 @@ import { join, dirname } from 'path'; // @ts-ignore import Promise from 'bluebird'; import * as fs from '../lib/fs'; -import { FSWatcher } from 'chokidar'; +import type { FSWatcher } from 'chokidar'; const should = chai.should(); -function createDummyFolder(path) { +function createDummyFolder(path: string) { const filesMap = { // Normal files in a hidden folder [join('.hidden', 'a.txt')]: 'a', @@ -27,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' From 157a8148976c80a88b825697862515d17ea29d18 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Wed, 6 Mar 2024 09:36:25 +0800 Subject: [PATCH 3/4] fix --- test/index.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/index.ts b/test/index.ts index 00ffdcd..cde6071 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,7 +1,6 @@ import chai from 'chai'; import { join, dirname } from 'path'; -// @ts-ignore -import Promise from 'bluebird'; +import BlueBirdPromise from 'bluebird'; import * as fs from '../lib/fs'; import type { FSWatcher } from 'chokidar'; const should = chai.should(); @@ -24,7 +23,7 @@ function createDummyFolder(path: string) { // A hidden files in a normal folder [join('folder', '.j')]: 'j' }; - return Promise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key])); + return BlueBirdPromise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key])); } function createAnotherDummyFolder(path: string) { @@ -32,7 +31,7 @@ function createAnotherDummyFolder(path: string) { [join('folder', '.txt')]: 'txt', [join('folder', '.js')]: 'js' }; - return Promise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key])); + return BlueBirdPromise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key])); } describe('fs', () => { @@ -222,7 +221,7 @@ describe('fs', () => { const result = await fs.readFile(dest); result.should.eql(body); - await Promise.all([ + await BlueBirdPromise.all([ fs.unlink(src), fs.rmdir(join(tmpDir, 'a')) ]); @@ -270,7 +269,7 @@ describe('fs', () => { } result.should.eql(['e', 'f', 'h', 'i']); - await Promise.all([fs.rmdir(src), fs.rmdir(dest)]); + await BlueBirdPromise.all([fs.rmdir(src), fs.rmdir(dest)]); }); it('copyDir() - src is required', async () => { @@ -320,7 +319,7 @@ describe('fs', () => { } result.should.have.members(['a', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j']); - await Promise.all([fs.rmdir(src), fs.rmdir(dest)]); + await BlueBirdPromise.all([fs.rmdir(src), fs.rmdir(dest)]); }); it('copyDir() - ignorePattern', async () => { @@ -340,7 +339,7 @@ describe('fs', () => { } result.should.eql(['e', 'h']); - await Promise.all([fs.rmdir(src), fs.rmdir(dest)]); + await BlueBirdPromise.all([fs.rmdir(src), fs.rmdir(dest)]); }); it('listDir()', async () => { @@ -895,12 +894,12 @@ describe('fs', () => { it('watch()', async () => { const target = join(tmpDir, 'test.txt'); - const testerWrap = (_watcher: FSWatcher) => new Promise((resolve, reject) => { + const testerWrap = (_watcher: FSWatcher) => new BlueBirdPromise((resolve, reject) => { _watcher.on('add', resolve).on('error', reject); }); const watcher = await fs.watch(tmpDir); - const result = await Promise.all([ + const result = await BlueBirdPromise.all([ testerWrap(watcher), fs.writeFile(target, 'test') ]); @@ -926,7 +925,7 @@ describe('fs', () => { const target = join(tmpDir, 'test'); const filenames = ['foo.txt', 'foo-1.txt', 'foo-2.md', 'bar.txt']; - await Promise.map(filenames, path => fs.writeFile(join(target, path))); + await BlueBirdPromise.map(filenames, path => fs.writeFile(join(target, path))); const result = await fs.ensurePath(join(target, 'foo.txt')); result.should.eql(join(target, 'foo-2.txt')); @@ -953,7 +952,7 @@ describe('fs', () => { const target = join(tmpDir, 'test'); const filenames = ['foo.txt', 'foo-1.txt', 'foo-2.md', 'bar.txt']; - await Promise.map(filenames, path => fs.writeFile(join(target, path))); + await BlueBirdPromise.map(filenames, path => fs.writeFile(join(target, path))); const path = fs.ensurePathSync(join(target, 'foo.txt')); path.should.eql(join(target, 'foo-2.txt')); From 8ddb66f920966df516f434f252637e8b1a1e4a8c Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Thu, 4 Apr 2024 16:53:30 +0800 Subject: [PATCH 4/4] ts-expect-error --- test/index.ts | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/test/index.ts b/test/index.ts index cde6071..46cd4d1 100644 --- a/test/index.ts +++ b/test/index.ts @@ -48,7 +48,7 @@ describe('fs', () => { it('exists() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.exists(); should.fail(); } catch (err) { @@ -63,7 +63,7 @@ describe('fs', () => { it('existsSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.existsSync(); should.fail(); } catch (err) { @@ -88,7 +88,7 @@ describe('fs', () => { it('mkdirs() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.mkdirs(); should.fail(); } catch (err) { @@ -109,7 +109,7 @@ describe('fs', () => { it('mkdirsSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.mkdirsSync(); should.fail(); } catch (err) { @@ -131,7 +131,7 @@ describe('fs', () => { it('writeFile() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.writeFile(); should.fail(); } catch (err) { @@ -153,7 +153,7 @@ describe('fs', () => { it('writeFileSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.writeFileSync(); should.fail(); } catch (err) { @@ -178,7 +178,7 @@ describe('fs', () => { it('appendFile() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.appendFile(); should.fail(); } catch (err) { @@ -202,7 +202,7 @@ describe('fs', () => { it('appendFileSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.appendFileSync(); should.fail(); } catch (err) { @@ -229,7 +229,7 @@ describe('fs', () => { it('copyFile() - src is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.copyFile(); should.fail(); } catch (err) { @@ -239,7 +239,7 @@ describe('fs', () => { it('copyFile() - dest is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.copyFile('123'); should.fail(); } catch (err) { @@ -274,7 +274,7 @@ describe('fs', () => { it('copyDir() - src is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.copyDir(); should.fail(); } catch (err) { @@ -284,7 +284,7 @@ describe('fs', () => { it('copyDir() - dest is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.copyDir('123'); should.fail(); } catch (err) { @@ -360,7 +360,7 @@ describe('fs', () => { it('listDir() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.listDir(); should.fail(); } catch (err) { @@ -419,7 +419,7 @@ describe('fs', () => { it('listDirSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.listDirSync(); should.fail(); } catch (err) { @@ -472,7 +472,7 @@ describe('fs', () => { it('readFile() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.readFile(); should.fail(); } catch (err) { @@ -527,7 +527,7 @@ describe('fs', () => { it('readFileSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.readFileSync(); should.fail(); } catch (err) { @@ -636,7 +636,7 @@ describe('fs', () => { it('emptyDir() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.emptyDir(); should.fail(); } catch (err) { @@ -762,7 +762,7 @@ describe('fs', () => { it('emptyDirSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.emptyDirSync(); should.fail(); } catch (err) { @@ -864,7 +864,7 @@ describe('fs', () => { it('rmdir() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.rmdir(); should.fail(); } catch (err) { @@ -883,7 +883,7 @@ describe('fs', () => { it('rmdirSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.rmdirSync(); should.fail(); } catch (err) { @@ -913,7 +913,7 @@ describe('fs', () => { it('watch() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.watch(); should.fail(); } catch (err) { @@ -940,7 +940,7 @@ describe('fs', () => { it('ensurePath() - path is required', async () => { try { - // @ts-ignore + // @ts-expect-error await fs.ensurePath(); should.fail(); } catch (err) { @@ -968,7 +968,7 @@ describe('fs', () => { it('ensurePathSync() - path is required', () => { try { - // @ts-ignore + // @ts-expect-error fs.ensurePathSync(); should.fail(); } catch (err) {