Skip to content

Commit 45831ca

Browse files
committed
add import path replacing function
1 parent 7175adb commit 45831ca

9 files changed

+77
-7
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.10.0] - 2016-08-06
6+
- handle JS imports relative to project directory
7+
58
## [0.9.3] - 2016-08-01
69
- add "exists" global file path checker
710
- upgrade to [email protected]

lib/import-paths.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var importPathRegex = /require\(["'](.+)["']\);?$|^import.+from\s?["'](.+)["'];?$/;
4+
var relativePathRegex = /^\./;
5+
function fixImportPaths(str) {
6+
return str.split('\n').map(function (line) {
7+
var isMatch = line.match(importPathRegex);
8+
if (!isMatch) {
9+
return line;
10+
}
11+
var importPath = isMatch[1] || isMatch[2];
12+
if (importPath.match(relativePathRegex)) {
13+
return line.replace(importPath, path_1.join(process.env.DIR, importPath));
14+
}
15+
return line;
16+
}).join('\n');
17+
}
18+
Object.defineProperty(exports, "__esModule", { value: true });
19+
exports.default = fixImportPaths;

lib/runner-process.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function runnerProcess(config) {
4444
'--bail',
4545
'--harmony',
4646
'--no-colors',
47+
'--timeout=3000',
4748
("--reporter=" + path_1.join(__dirname, 'reporter.js')),
4849
constants_1.testPath
4950
], options);

lib/write-test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ var process_console_log_1 = require('process-console-log');
44
var exists_1 = require('./exists');
55
var constants_1 = require('./constants');
66
var ts = require('typescript');
7+
var import_paths_1 = require('./import-paths');
78
function writeTest(config, testString) {
89
return new Promise(function (resolve, reject) {
9-
var output = process_console_log_1.logger + exists_1.default(config.dir) + ts.transpile(testString, {
10+
var testStringWithFixedImportPaths = import_paths_1.default(testString);
11+
var output = ''
12+
.concat(process_console_log_1.logger)
13+
.concat(exists_1.default(config.dir))
14+
.concat(ts.transpile(testStringWithFixedImportPaths, {
1015
module: ts.ModuleKind.CommonJS
11-
});
16+
}));
1217
fs_1.writeFile(constants_1.testPath, output, function (err) {
1318
if (err) {
1419
reject(err);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"homepage": "https://github.com/coderoad/mocha-coderoad#readme",
2828
"dependencies": {
29-
"mocha": "^3.0.0",
29+
"mocha": "^3.0.1",
3030
"node-file-exists": "^1.1.0",
3131
"process-console-log": "^0.2.2",
3232
"typescript": "^1.8.10"

src/import-paths.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { join } from 'path';
2+
3+
/*
4+
import paths won't match the context of the test runner
5+
fixImportPaths will replace paths with absolute paths
6+
*/
7+
8+
// import or require statement
9+
const importPathRegex = /require\(["'](.+)["']\);?$|^import.+from\s?["'](.+)["'];?$/;
10+
const relativePathRegex = /^\./;
11+
12+
export default function fixImportPaths(str: string): string {
13+
return str.split('\n').map(line => {
14+
const isMatch = line.match(importPathRegex);
15+
if (!isMatch) {
16+
return line;
17+
}
18+
// multiple cases due to import or require regex
19+
const importPath = isMatch[1] || isMatch[2];
20+
// import path: may be relative or absolute
21+
// // relative path
22+
if (importPath.match(relativePathRegex)) {
23+
// process.env.DIR
24+
return line.replace(importPath, join(process.env.DIR, importPath));
25+
}
26+
// no match, return line
27+
return line;
28+
}).join('\n');
29+
}

src/runner-process.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default function runnerProcess(config: CR.Config) {
5252
'--bail',
5353
'--harmony',
5454
'--no-colors',
55+
'--timeout=3000',
5556
`--reporter=${join(__dirname, 'reporter.js')}`,
5657
testPath
5758

src/write-test.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ import {logger} from 'process-console-log';
33
import exists from './exists';
44
import {testPath} from './constants';
55
import * as ts from 'typescript';
6+
import importPaths from './import-paths';
67

78
export default function writeTest(config, testString: string) {
89
return new Promise((resolve, reject) => {
9-
// append logger, compile using ts compiler
10-
const output = logger + exists(config.dir) + ts.transpile(testString, {
11-
module: ts.ModuleKind.CommonJS
12-
});
10+
// fix import paths relative to project dir instead of test runner
11+
const testStringWithFixedImportPaths = importPaths(testString);
12+
13+
const output = ''
14+
// append logger
15+
.concat(logger)
16+
// exists polyfill for file/folder exists checks
17+
.concat(exists(config.dir))
18+
// compile using ts
19+
.concat(
20+
ts.transpile(testStringWithFixedImportPaths, {
21+
module: ts.ModuleKind.CommonJS
22+
})
23+
);
1324
// write test file
1425
writeFile(testPath, output, (err) => {
1526
if (err) { reject(err); }

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"files": [
1818
"src/constants.ts",
1919
"src/exists.ts",
20+
"src/import-paths.ts",
2021
"src/index.ts",
2122
"src/reporter.ts",
2223
"src/runner-process.ts",

0 commit comments

Comments
 (0)