Skip to content

Commit

Permalink
feat: support --cleanOutDir args (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jan 14, 2024
1 parent 22526a5 commit 580e233
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
37 changes: 35 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
const { forkTsc, forkRun } = require('./process');
const { parseArgs, debounce, getIp } = require('./util');
const {
parseArgs,
debounce,
getIp,
deleteFolderRecursive,
readJSONFile,
} = require('./util');
const path = require('path');

function run() {
const [runCmd, tscArgs, runArgs] = parseArgs(process.argv);
const [runCmd, tscArgs, runArgs, isCleanDirectory] = parseArgs(process.argv);
const cwd = process.cwd();

if (isCleanDirectory) {
/**
* 删除编译目录,有几种情况
* 1、tscArgs 中有 --outDir 参数,直接删除
* 2、tscArgs 中没有 --outDir 参数,从 tsconfig.json 中读取 outDir 参数
* 3、如果 tscArgs 中包含 --project 参数,那么就以 --project 参数指定的 tsconfig.json 为准
*/
let outDir;
const projectIndex = tscArgs.findIndex(arg => arg === '--project');
if (projectIndex !== -1) {
const projectPath = tscArgs[projectIndex + 1];
const tsconfig = readJSONFile(path.resolve(cwd, projectPath));
outDir = tsconfig.compilerOptions.outDir;
} else {
const outDirIndex = tscArgs.findIndex(arg => arg === '--outDir');
if (outDirIndex !== -1) {
outDir = tscArgs[outDirIndex + 1];
} else {
const tsconfig = readJSONFile(path.resolve(cwd, 'tsconfig.json'));
outDir = tsconfig.compilerOptions.outDir;
}
}

deleteFolderRecursive(path.resolve(cwd, outDir));
}

let runChild;
const restart = debounce(() => {
runChild && runChild.restart();
Expand Down
49 changes: 44 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
const { networkInterfaces } = require('os');
const fs = require('fs');
const path = require('path');

exports.parseArgs = argv => {
// 去除头两位参数
const pargs = argv.slice(2);
let pargs = argv.slice(2);

// 是否清理目录
let isCleanDirectory = false;
if (pargs.includes('--cleanOutDir')) {
isCleanDirectory = true;
// 移除 --cleanOutDir 命令
pargs.splice(
pargs.findIndex(arg => arg === '--cleanOutDir'),
1
);
}

// 找到 --run 命令以及后面的所有参数
const runIndex = pargs.findIndex(arg => arg === '--run');

if (runIndex === -1) {
return [undefined, pargs, []];
return [undefined, pargs, [], isCleanDirectory];
}

// 分出 --run,后一个路径,以及剩余的其他参数
Expand All @@ -19,10 +32,9 @@ exports.parseArgs = argv => {

// 从 pargs 中移除 --run 命令后面所有参数
if (runIndex !== -1) {
pargs.splice(runIndex);
pargs = pargs.slice(0, runIndex);
}

return [runCmdPath, pargs, runChildArgs];
return [runCmdPath, pargs, runChildArgs, isCleanDirectory];
};

exports.debounce = function (func, wait, immediate = false) {
Expand Down Expand Up @@ -92,3 +104,30 @@ exports.getIp = function () {
}
}
};

function deleteFolderRecursive(directory) {
if (fs.existsSync(directory)) {
fs.readdirSync(directory).forEach((file, index) => {
const curPath = path.join(directory, file);
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(directory);
}
}

exports.deleteFolderRecursive = deleteFolderRecursive;

exports.readJSONFile = function (filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
return JSON.parse(fileContent);
} catch (err) {
return {};
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mwtsc",
"version": "1.3.2",
"version": "1.4.0-beta.3",
"description": "tsc wrapper for midway development",
"main": "lib/index.js",
"bin": {
Expand Down
45 changes: 38 additions & 7 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
const { parseArgs } = require('../lib/util');
const { parseArgs, deleteFolderRecursive } = require('../lib/util');
const fs = require('fs');
const path = require('path')

describe('test/util.test.js', () => {
it('should parse', () => {
expect(parseArgs(['node', 'tsc'])).toEqual([undefined, [], []]);
expect(parseArgs(['node', 'tsc', '--watch'])).toEqual([undefined, ['--watch'], []]);
expect(parseArgs(['node', 'tsc', '--watch', '--run', 'index.js'])).toEqual(['index.js', ['--watch'], []]);
expect(parseArgs(['node', 'tsc', '--watch', '--run', 'index.js', '--', '--port', '3000'])).toEqual(['index.js', ['--watch'], ['--', '--port', '3000']]);
expect(parseArgs(['node', 'tsc', '--watch', '--project', 'tsconfig.prod.json'])).toEqual([undefined, ['--watch', '--project', 'tsconfig.prod.json'], []]);
expect(parseArgs(['node', 'tsc', '--watch', '--run'])).toEqual([undefined, ['--watch'], []]);
expect(parseArgs(['node', 'tsc'])).toEqual([undefined, [], [], false]);
expect(parseArgs(['node', 'tsc', '--watch'])).toEqual([undefined, ['--watch'], [], false]);
expect(parseArgs(['node', 'tsc', '--watch', '--run', 'index.js'])).toEqual(['index.js', ['--watch'], [], false]);
expect(parseArgs(['node', 'tsc', '--watch', '--run', 'index.js', '--', '--port', '3000'])).toEqual(['index.js', ['--watch'], ['--', '--port', '3000'], false]);
expect(parseArgs(['node', 'tsc', '--watch', '--project', 'tsconfig.prod.json'])).toEqual([undefined, ['--watch', '--project', 'tsconfig.prod.json'], [], false]);
expect(parseArgs(['node', 'tsc', '--watch', '--run'])).toEqual([undefined, ['--watch'], [], false]);
expect(parseArgs(['node', 'tsc', '--cleanOutDir'])).toEqual([undefined, [], [], true]);
expect(parseArgs(['node', 'tsc', '--watch', '--cleanOutDir', '--build', '--run'])).toEqual([undefined, ['--watch', '--build'], [], true]);
});

describe('deleteFolderRecursive', () => {
const testDirPath = path.join(__dirname, 'testDir');

// 在每个测试用例之前,创建一个测试目录和一些测试文件
beforeEach(() => {
fs.mkdirSync(testDirPath, { recursive: true });
fs.writeFileSync(path.join(testDirPath, 'file1.txt'), 'Hello, world!');
fs.mkdirSync(path.join(testDirPath, 'subDir'));
fs.writeFileSync(path.join(testDirPath, 'subDir', 'file2.txt'), 'Hello, world!');
});

// 在每个测试用例之后,确保测试目录已被删除
afterEach(() => {
if (fs.existsSync(testDirPath)) {
deleteFolderRecursive(testDirPath);
}
});

it('should delete the directory and all its contents', () => {
deleteFolderRecursive(testDirPath);

// 检查目录是否已被删除
expect(fs.existsSync(testDirPath)).toBe(false);
});
});
});

0 comments on commit 580e233

Please sign in to comment.