Skip to content

Commit 091ce3d

Browse files
committed
refactor(tsconfig): move converting json from guess to index
1 parent 6274acf commit 091ce3d

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

guess.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var path = require('path');
44
var ts = require('typescript');
55

66
var pattern = 'test/**/*.ts';
7-
var packageData = require(path.join(process.cwd(), 'package.json'));
7+
var cwd = process.cwd();
8+
var packageData = require(path.join(cwd, 'package.json'));
89

910
if (packageData &&
1011
typeof packageData.directories === 'object' &&
@@ -13,21 +14,22 @@ if (packageData &&
1314
pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + '**/*.ts';
1415
}
1516

16-
var compilerOptions = findAndParseTsConfig(process.cwd());
17+
var tsconfigPath = ts.findConfigFile(cwd);
18+
var tsconfigBasepath = null;
19+
var compilerOptions = null;
20+
if (tsconfigPath) {
21+
compilerOptions = parseTsConfig(tsconfigPath);
22+
tsconfigBasepath = path.dirname(tsconfigPath);
23+
}
1724

1825
require('./index')({
19-
cwd: process.cwd(),
26+
cwd: cwd,
2027
pattern: pattern,
21-
compilerOptions: compilerOptions
28+
compilerOptions: compilerOptions,
29+
basepath: tsconfigBasepath
2230
});
2331

24-
function findAndParseTsConfig(cwd) {
25-
var tsconfigPath = ts.findConfigFile(process.cwd());
26-
if (!tsconfigPath) {
27-
return null;
28-
}
29-
30-
var compilerOptions = null;
32+
function parseTsConfig(tsconfigPath) {
3133
var parsed = ts.parseConfigFileTextToJson(tsconfigPath, fs.readFileSync(tsconfigPath, 'utf8'));
3234
if (parsed.error) {
3335
throw new Error(parsed.error.messageText);
@@ -37,10 +39,5 @@ function findAndParseTsConfig(cwd) {
3739
return null;
3840
}
3941

40-
var converted = ts.convertCompilerOptionsFromJson(parsed.config.compilerOptions, process.cwd());
41-
if (converted.errors && converted.errors.length > 0) {
42-
var msg = converted.errors.map(function(e) {return e.messageText}).join(', ');
43-
throw new Error(msg);
44-
}
45-
return converted.options;
42+
return parsed.config.compilerOptions;
4643
}

index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ var fs = require('fs');
44

55
var espowerSource = require('espower-source');
66
var minimatch = require('minimatch');
7+
var ts = require('typescript');
78
var TypeScriptSimple = require('typescript-simple').TypeScriptSimple;
89

910
function espowerTypeScript(options) {
1011
var separator = (options.pattern.lastIndexOf('/', 0) === 0) ? '' : '/';
1112
var pattern = options.cwd + separator + options.pattern;
12-
var tss = new TypeScriptSimple(options.compilerOptions, false);
13+
var compilerOptions = convertCompilerOptions(options.compilerOptions, options.basepath);
14+
var tss = new TypeScriptSimple(compilerOptions, false);
1315

1416
require.extensions['.ts'] = function(localModule, filepath) {
1517
var result = tss.compile(fs.readFileSync(filepath, 'utf-8'));
@@ -20,4 +22,18 @@ function espowerTypeScript(options) {
2022
};
2123
}
2224

25+
function convertCompilerOptions(compilerOptions, basepath) {
26+
if (!compilerOptions) {
27+
return null;
28+
}
29+
30+
var basepath = basepath || process.cwd();
31+
var converted = ts.convertCompilerOptionsFromJson(compilerOptions, basepath);
32+
if (converted.errors && converted.errors.length > 0) {
33+
var msg = converted.errors.map(function(e) {return e.messageText}).join(', ');
34+
throw new Error(msg);
35+
}
36+
return converted.options;
37+
}
38+
2339
module.exports = espowerTypeScript;

0 commit comments

Comments
 (0)