Skip to content

Commit

Permalink
refactor: migrate typescript (#185)
Browse files Browse the repository at this point in the history
* refactor: migrate typescript

* refactor: migrate typescript

* fix

* ts-expect-error
  • Loading branch information
D-Sketon committed Apr 4, 2024
1 parent 9b266fe commit ecb53c7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/fs.ts
Expand Up @@ -51,7 +51,7 @@ function checkParent(path: string) {

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 Down
4 changes: 3 additions & 1 deletion package.json
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
@@ -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
}
}
75 changes: 48 additions & 27 deletions test/index.js → 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',
Expand All @@ -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', () => {
Expand All @@ -50,6 +48,7 @@ describe('fs', () => {

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

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

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

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

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

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

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

it('appendFileSync() - path is required', () => {
try {
// @ts-expect-error
fs.appendFileSync();
should.fail();
} catch (err) {
Expand All @@ -215,14 +221,15 @@ 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'))
]);
});

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

it('copyFile() - dest is required', async () => {
try {
// @ts-expect-error
await fs.copyFile('123');
should.fail();
} catch (err) {
Expand All @@ -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) {
Expand All @@ -275,6 +284,7 @@ describe('fs', () => {

it('copyDir() - dest is required', async () => {
try {
// @ts-expect-error
await fs.copyDir('123');
should.fail();
} catch (err) {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -350,6 +360,7 @@ describe('fs', () => {

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

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

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

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

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

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

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

it('rmdirSync() - path is required', () => {
try {
// @ts-expect-error
fs.rmdirSync();
should.fail();
} catch (err) {
Expand All @@ -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<string>((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')
]);
Expand All @@ -895,6 +913,7 @@ describe('fs', () => {

it('watch() - path is required', async () => {
try {
// @ts-expect-error
await fs.watch();
should.fail();
} catch (err) {
Expand All @@ -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'));

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

it('ensurePath() - path is required', async () => {
try {
// @ts-expect-error
await fs.ensurePath();
should.fail();
} catch (err) {
Expand All @@ -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'));

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

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

0 comments on commit ecb53c7

Please sign in to comment.