Skip to content

Commit

Permalink
fix: json parse with tsconfig (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jan 18, 2024
1 parent 1c30a16 commit 3adc90c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
24 changes: 18 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const {
debounce,
getIp,
deleteFolderRecursive,
readJSONFile,
readJSONCFile,
} = require('./util');
const path = require('path');
const fs = require('fs');

function run() {
const [runCmd, tscArgs, runArgs, isCleanDirectory] = parseArgs(process.argv);
Expand All @@ -16,19 +17,30 @@ function run() {
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;
const tsconfig = readJSONCFile(path.resolve(cwd, projectPath));
if (tsconfig.compilerOptions) {
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;
const tsconfig = readJSONCFile(path.resolve(cwd, 'tsconfig.json'));
if (tsconfig.compilerOptions) {
outDir = tsconfig.compilerOptions.outDir;
}
}
}

runArgs.push('--baseDir', path.resolve(cwd, outDir));
if (!outDir) {
outDir = 'dist';
}

const baseDir = path.resolve(cwd, outDir);
if (fs.existsSync(baseDir)) {
runArgs.push('--baseDir', path.resolve(cwd, outDir));
}

if (isCleanDirectory) {
/**
Expand Down
10 changes: 10 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ exports.readJSONFile = function (filePath) {
return {};
}
};

exports.readJSONCFile = function (filePath) {
// eslint-disable-next-line node/no-unpublished-require
const ts = require('typescript');
const configFile = ts.readConfigFile(filePath, ts.sys.readFile);
if (configFile.error) {
console.error(configFile.error.messageText);
}
return configFile.config;
};
2 changes: 1 addition & 1 deletion test/fixtures/base/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2018",
"target": "es2018", // target es2018
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
Expand Down

0 comments on commit 3adc90c

Please sign in to comment.