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 92% rename from test/index.js rename to test/index.ts index 517f375..46cd4d1 100644 --- a/test/index.js +++ b/test/index.ts @@ -1,13 +1,11 @@ -'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'; +import BlueBirdPromise from 'bluebird'; +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', @@ -25,15 +23,15 @@ function createDummyFolder(path) { // 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) { +function createAnotherDummyFolder(path: string) { const filesMap = { [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', () => { @@ -50,6 +48,7 @@ describe('fs', () => { it('exists() - path is required', async () => { try { + // @ts-expect-error await fs.exists(); should.fail(); } catch (err) { @@ -64,6 +63,7 @@ describe('fs', () => { it('existsSync() - path is required', () => { try { + // @ts-expect-error fs.existsSync(); should.fail(); } catch (err) { @@ -88,6 +88,7 @@ describe('fs', () => { it('mkdirs() - path is required', async () => { try { + // @ts-expect-error await fs.mkdirs(); should.fail(); } catch (err) { @@ -108,6 +109,7 @@ describe('fs', () => { it('mkdirsSync() - path is required', () => { try { + // @ts-expect-error fs.mkdirsSync(); should.fail(); } catch (err) { @@ -129,6 +131,7 @@ describe('fs', () => { it('writeFile() - path is required', async () => { try { + // @ts-expect-error await fs.writeFile(); should.fail(); } catch (err) { @@ -150,6 +153,7 @@ describe('fs', () => { it('writeFileSync() - path is required', () => { try { + // @ts-expect-error fs.writeFileSync(); should.fail(); } catch (err) { @@ -174,6 +178,7 @@ describe('fs', () => { it('appendFile() - path is required', async () => { try { + // @ts-expect-error await fs.appendFile(); should.fail(); } catch (err) { @@ -197,6 +202,7 @@ describe('fs', () => { it('appendFileSync() - path is required', () => { try { + // @ts-expect-error fs.appendFileSync(); should.fail(); } catch (err) { @@ -215,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')) ]); @@ -223,6 +229,7 @@ describe('fs', () => { it('copyFile() - src is required', async () => { try { + // @ts-expect-error await fs.copyFile(); should.fail(); } catch (err) { @@ -232,6 +239,7 @@ describe('fs', () => { it('copyFile() - dest is required', async () => { try { + // @ts-expect-error await fs.copyFile('123'); should.fail(); } catch (err) { @@ -254,18 +262,19 @@ 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']); - await Promise.all([fs.rmdir(src), fs.rmdir(dest)]); + await BlueBirdPromise.all([fs.rmdir(src), fs.rmdir(dest)]); }); it('copyDir() - src is required', async () => { try { + // @ts-expect-error await fs.copyDir(); should.fail(); } catch (err) { @@ -275,6 +284,7 @@ describe('fs', () => { it('copyDir() - dest is required', async () => { try { + // @ts-expect-error await fs.copyDir('123'); should.fail(); } catch (err) { @@ -302,14 +312,14 @@ 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']); - await Promise.all([fs.rmdir(src), fs.rmdir(dest)]); + await BlueBirdPromise.all([fs.rmdir(src), fs.rmdir(dest)]); }); it('copyDir() - ignorePattern', async () => { @@ -322,14 +332,14 @@ 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']); - await Promise.all([fs.rmdir(src), fs.rmdir(dest)]); + await BlueBirdPromise.all([fs.rmdir(src), fs.rmdir(dest)]); }); it('listDir()', async () => { @@ -350,6 +360,7 @@ describe('fs', () => { it('listDir() - path is required', async () => { try { + // @ts-expect-error await fs.listDir(); should.fail(); } catch (err) { @@ -408,6 +419,7 @@ describe('fs', () => { it('listDirSync() - path is required', () => { try { + // @ts-expect-error fs.listDirSync(); should.fail(); } catch (err) { @@ -460,6 +472,7 @@ describe('fs', () => { it('readFile() - path is required', async () => { try { + // @ts-expect-error await fs.readFile(); should.fail(); } catch (err) { @@ -514,6 +527,7 @@ describe('fs', () => { it('readFileSync() - path is required', () => { try { + // @ts-expect-error fs.readFileSync(); should.fail(); } catch (err) { @@ -622,6 +636,7 @@ describe('fs', () => { it('emptyDir() - path is required', async () => { try { + // @ts-expect-error await fs.emptyDir(); should.fail(); } catch (err) { @@ -747,6 +762,7 @@ describe('fs', () => { it('emptyDirSync() - path is required', () => { try { + // @ts-expect-error fs.emptyDirSync(); should.fail(); } catch (err) { @@ -848,6 +864,7 @@ describe('fs', () => { it('rmdir() - path is required', async () => { try { + // @ts-expect-error await fs.rmdir(); should.fail(); } catch (err) { @@ -866,6 +883,7 @@ describe('fs', () => { it('rmdirSync() - path is required', () => { try { + // @ts-expect-error fs.rmdirSync(); should.fail(); } catch (err) { @@ -876,12 +894,12 @@ describe('fs', () => { it('watch()', async () => { const target = join(tmpDir, 'test.txt'); - const testerWrap = _watcher => 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') ]); @@ -895,6 +913,7 @@ describe('fs', () => { it('watch() - path is required', async () => { try { + // @ts-expect-error await fs.watch(); should.fail(); } catch (err) { @@ -906,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')); @@ -921,6 +940,7 @@ describe('fs', () => { it('ensurePath() - path is required', async () => { try { + // @ts-expect-error await fs.ensurePath(); should.fail(); } catch (err) { @@ -932,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')); @@ -948,6 +968,7 @@ describe('fs', () => { it('ensurePathSync() - path is required', () => { try { + // @ts-expect-error 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": [