Skip to content

Commit 3adc90c

Browse files
authored
fix: json parse with tsconfig (#8)
1 parent 1c30a16 commit 3adc90c

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const {
44
debounce,
55
getIp,
66
deleteFolderRecursive,
7-
readJSONFile,
7+
readJSONCFile,
88
} = require('./util');
99
const path = require('path');
10+
const fs = require('fs');
1011

1112
function run() {
1213
const [runCmd, tscArgs, runArgs, isCleanDirectory] = parseArgs(process.argv);
@@ -16,19 +17,30 @@ function run() {
1617
const projectIndex = tscArgs.findIndex(arg => arg === '--project');
1718
if (projectIndex !== -1) {
1819
const projectPath = tscArgs[projectIndex + 1];
19-
const tsconfig = readJSONFile(path.resolve(cwd, projectPath));
20-
outDir = tsconfig.compilerOptions.outDir;
20+
const tsconfig = readJSONCFile(path.resolve(cwd, projectPath));
21+
if (tsconfig.compilerOptions) {
22+
outDir = tsconfig.compilerOptions.outDir;
23+
}
2124
} else {
2225
const outDirIndex = tscArgs.findIndex(arg => arg === '--outDir');
2326
if (outDirIndex !== -1) {
2427
outDir = tscArgs[outDirIndex + 1];
2528
} else {
26-
const tsconfig = readJSONFile(path.resolve(cwd, 'tsconfig.json'));
27-
outDir = tsconfig.compilerOptions.outDir;
29+
const tsconfig = readJSONCFile(path.resolve(cwd, 'tsconfig.json'));
30+
if (tsconfig.compilerOptions) {
31+
outDir = tsconfig.compilerOptions.outDir;
32+
}
2833
}
2934
}
3035

31-
runArgs.push('--baseDir', path.resolve(cwd, outDir));
36+
if (!outDir) {
37+
outDir = 'dist';
38+
}
39+
40+
const baseDir = path.resolve(cwd, outDir);
41+
if (fs.existsSync(baseDir)) {
42+
runArgs.push('--baseDir', path.resolve(cwd, outDir));
43+
}
3244

3345
if (isCleanDirectory) {
3446
/**

lib/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,13 @@ exports.readJSONFile = function (filePath) {
131131
return {};
132132
}
133133
};
134+
135+
exports.readJSONCFile = function (filePath) {
136+
// eslint-disable-next-line node/no-unpublished-require
137+
const ts = require('typescript');
138+
const configFile = ts.readConfigFile(filePath, ts.sys.readFile);
139+
if (configFile.error) {
140+
console.error(configFile.error.messageText);
141+
}
142+
return configFile.config;
143+
};

test/fixtures/base/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es2018",
3+
"target": "es2018", // target es2018
44
"module": "commonjs",
55
"moduleResolution": "node",
66
"experimentalDecorators": true,

0 commit comments

Comments
 (0)