Skip to content

Commit d5e6aeb

Browse files
committed
continued windows path bug fixes
1 parent 552144e commit d5e6aeb

File tree

7 files changed

+42
-13
lines changed

7 files changed

+42
-13
lines changed

lib/importPaths.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
var path_1 = require('path');
3-
var isWindows = window.navigator.appVersion.indexOf('Win') > -1;
3+
var system_1 = require('./system');
44
var importPathRegex = /require\(["'](BASE.+)["']\)([a-zA-Z0-9\-\_]+)?|^import.+?\s?["'](BASE.+)["'];?$/m;
55
var relativePathRegex = /^BASE/;
66
function fixImportPaths(_a) {
@@ -14,8 +14,8 @@ function fixImportPaths(_a) {
1414
var importPath = isMatch[1] || isMatch[2] || isMatch[3];
1515
if (importPath.match(relativePathRegex)) {
1616
var newPath = path_1.join(dir, importPath.replace('BASE', ''));
17-
if (isWindows) {
18-
newPath = newPath.split('\\').join('\\\\');
17+
if (system_1.isWindows) {
18+
newPath = system_1.fixPathForWindows(newPath);
1919
}
2020
var newLine = line.replace(importPath, newPath);
2121
if (!entries.has(newLine)) {

lib/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
"use strict";
22
var importPaths_1 = require('./importPaths');
3+
var system_1 = require('./system');
34
var jsCodeRoad = function (_a) {
4-
var dir = _a.dir, content = _a.content, isWindows = _a.isWindows;
5-
return ("\n(function(){'use strict';\nrequire('babel-register')({plugins:[['transform-es2015-modules-commonjs',{loose:true,sourceRoot:'" + dir + "'}]]});\n" + require('process-console-log').logger + "\nlet _fileExists = require('node-file-exists').default;\nlet _path = require('path');\nglobal.exists = (p) => _fileExists(_path.join('" + dir + "',p));\nrequire = require('rewire-coderoad');\n\n// unit tests\n\n" + importPaths_1.default({ dir: dir, content: content }) + "\n\n}());");
5+
var dir = _a.dir, content = _a.content;
6+
if (system_1.isWindows) {
7+
dir = system_1.fixPathForWindows(dir);
8+
}
9+
return "\n(function(){'use strict';\nrequire('babel-register')({plugins:[['transform-es2015-modules-commonjs',{loose:true,sourceRoot:'" + dir + "'}]]});\n" + require('process-console-log').logger + "\nconst _fileExists = require('node-file-exists').default;\nconst _path = require('path');\nfunction exists(p) { return _fileExists(_path.join('" + dir + "',p)); }\nrequire = require('rewire-coderoad');\n\n// unit tests\n\n" + importPaths_1.default({ dir: dir, content: content }) + "\n\n}());";
610
};
711
Object.defineProperty(exports, "__esModule", { value: true });
812
exports.default = jsCodeRoad;

lib/system.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
exports.isWindows = window.navigator.appVersion.indexOf('Win') > -1;
3+
exports.fixPathForWindows = function (p) {
4+
p.split('\\\\').join('\\');
5+
p.split('\\').join('\\\\');
6+
p.split('/').join('\\\\');
7+
return p;
8+
};

src/importPaths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { join } from 'path';
2+
import { isWindows, fixPathForWindows } from './system';
23

34
/*
45
import paths won't match the context of the test runner
56
fixImportPaths will replace paths with absolute paths
67
*/
7-
const isWindows = window.navigator.appVersion.indexOf('Win') > -1;
88

99
// import or require statement
1010
const importPathRegex =
@@ -31,7 +31,7 @@ export default function fixImportPaths({dir, content}): string {
3131

3232
// fix buggy Windows paths
3333
if (isWindows) {
34-
newPath = newPath.split('\\').join('\\\\');
34+
newPath = fixPathForWindows(newPath);
3535
}
3636

3737
const newLine = line.replace(importPath, newPath);

src/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import fixImportPaths from './importPaths';
2+
import { isWindows, fixPathForWindows } from './system';
23

3-
const jsCodeRoad = ({dir, content, isWindows}) => (`
4+
const jsCodeRoad = ({dir, content}) => {
5+
6+
// fix Windows paths
7+
if (isWindows) {
8+
dir = fixPathForWindows(dir);
9+
}
10+
11+
return `
412
(function(){'use strict';
513
require('babel-register')({plugins:[['transform-es2015-modules-commonjs',{loose:true,sourceRoot:'${dir}'}]]});
614
${require('process-console-log').logger}
7-
let _fileExists = require('node-file-exists').default;
8-
let _path = require('path');
9-
global.exists = (p) => _fileExists(_path.join('${dir}',p));
15+
const _fileExists = require('node-file-exists').default;
16+
const _path = require('path');
17+
function exists(p) { return _fileExists(_path.join('${dir}',p)); }
1018
require = require('rewire-coderoad');
1119
1220
// unit tests
1321
1422
${fixImportPaths({dir, content})}
1523
16-
}());`
17-
);
24+
}());`;
25+
};
1826
export default jsCodeRoad;

src/system.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const isWindows = window.navigator.appVersion.indexOf('Win') > -1;
2+
3+
export const fixPathForWindows = (p: string) => {
4+
p.split('\\\\').join('\\');
5+
p.split('\\').join('\\\\');
6+
p.split('/').join('\\\\');
7+
return p;
8+
};

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"files": [
1919
"src/importPaths.ts",
2020
"src/index.ts",
21+
"src/system.ts",
2122
"src/typings/chai/chai.d.ts",
2223
"src/typings/cr/cr.d.ts",
2324
"src/typings/cr/globals.d.ts",

0 commit comments

Comments
 (0)