Skip to content

Commit c016bc4

Browse files
committed
fixes for windows
1 parent d5e6aeb commit c016bc4

File tree

9 files changed

+48
-54
lines changed

9 files changed

+48
-54
lines changed

lib/importPaths.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ var importPathRegex = /require\(["'](BASE.+)["']\)([a-zA-Z0-9\-\_]+)?|^import.+?
55
var relativePathRegex = /^BASE/;
66
function fixImportPaths(_a) {
77
var dir = _a.dir, content = _a.content;
8-
var entries = new Set([]);
98
return content.split('\n').map(function (line) {
109
var isMatch = line.match(importPathRegex);
1110
if (!isMatch) {
1211
return line;
1312
}
1413
var importPath = isMatch[1] || isMatch[2] || isMatch[3];
1514
if (importPath.match(relativePathRegex)) {
16-
var newPath = path_1.join(dir, importPath.replace('BASE', ''));
15+
var newPath = void 0;
1716
if (system_1.isWindows) {
18-
newPath = system_1.fixPathForWindows(newPath);
17+
newPath = path_1.join(dir, importPath.replace('BASE', ''))
18+
.split('\\').join('\\\\');
1919
}
20-
var newLine = line.replace(importPath, newPath);
21-
if (!entries.has(newLine)) {
22-
entries.add(newLine);
23-
return newLine;
20+
else {
21+
newPath = path_1.join(dir, importPath.replace('BASE', ''));
2422
}
25-
return '';
23+
return line.replace(importPath, newPath);
2624
}
2725
return line;
2826
}).join('\n');

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ var system_1 = require('./system');
44
var jsCodeRoad = function (_a) {
55
var dir = _a.dir, content = _a.content;
66
if (system_1.isWindows) {
7-
dir = system_1.fixPathForWindows(dir);
7+
dir = dir.split('\\').join('\\\\');
88
}
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}());";
9+
return "(function(){'use strict';\nrequire('babel-register')({plugins:[\n ['transform-es2015-modules-commonjs',{loose:true,sourceRoot:'" + dir + "'}]\n]});\n" + require('process-console-log').logger + "\nconst _f_e = require('node-file-exists').default;\nconst _join = require('path').join;\nfunction exists(p) { return _f_e(_join('" + dir + "',p)); }\nrequire = require('rewire-coderoad');\n\n" + importPaths_1.default({ dir: dir, content: content }) + "\n\n}());";
1010
};
1111
Object.defineProperty(exports, "__esModule", { value: true });
1212
exports.default = jsCodeRoad;

lib/system.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
"use strict";
2+
var window = window || ({ navigator: { appVersion: '' } });
23
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-
};

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929
"watch:test": "ava test/*.js --watch"
3030
},
3131
"dependencies": {
32-
"babel-plugin-transform-es2015-modules-commonjs": "6.11.5",
33-
"babel-preset-es2015": "^6.13.2",
34-
"babel-register": "^6.11.6",
35-
"node-file-exists": "1.1.0",
36-
"process-console-log": "0.2.3",
37-
"rewire-coderoad": "^3.1.1"
32+
"babel-plugin-transform-es2015-modules-commonjs": "^6.14.0",
33+
"babel-register": "^6.14.0",
34+
"node-file-exists": "^1.1.0",
35+
"process-console-log": "^0.2.5",
36+
"rewire-coderoad": "^3.1.3"
3837
},
3938
"devDependencies": {
4039
"ava": "^0.16.0",

src/importPaths.ts

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

44
/*
55
import paths won't match the context of the test runner
@@ -12,9 +12,6 @@ const importPathRegex =
1212
const relativePathRegex = /^BASE/;
1313

1414
export default function fixImportPaths({dir, content}): string {
15-
// collect import lines
16-
let entries = new Set([]);
17-
1815
return content.split('\n').map(line => {
1916
// line has an import or require ?
2017
const isMatch = line.match(importPathRegex);
@@ -27,20 +24,19 @@ export default function fixImportPaths({dir, content}): string {
2724

2825
// is a relative path
2926
if (importPath.match(relativePathRegex)) {
30-
let newPath = join(dir, importPath.replace('BASE', ''));
27+
let newPath;
3128

32-
// fix buggy Windows paths
3329
if (isWindows) {
34-
newPath = fixPathForWindows(newPath);
30+
// fix buggy Windows paths
31+
// note: necessary to split and join before newPath is set to
32+
// a variable or backslashes are interpreted as escaped characters
33+
newPath = join(dir, importPath.replace('BASE', ''))
34+
.split('\\').join('\\\\');
35+
} else {
36+
newPath = join(dir, importPath.replace('BASE', ''));
3537
}
3638

37-
const newLine = line.replace(importPath, newPath);
38-
// add to map of entry files
39-
if (!entries.has(newLine)) {
40-
entries.add(newLine);
41-
return newLine;
42-
}
43-
return '';
39+
return line.replace(importPath, newPath);
4440
}
4541
// no match, return line
4642
return line;

src/index.ts

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

44
const jsCodeRoad = ({dir, content}) => {
55

66
// fix Windows paths
77
if (isWindows) {
8-
dir = fixPathForWindows(dir);
8+
dir = dir.split('\\').join('\\\\');
99
}
1010

11-
return `
12-
(function(){'use strict';
13-
require('babel-register')({plugins:[['transform-es2015-modules-commonjs',{loose:true,sourceRoot:'${dir}'}]]});
11+
return `(function(){'use strict';
12+
require('babel-register')({plugins:[
13+
['transform-es2015-modules-commonjs',{loose:true,sourceRoot:'${dir}'}]
14+
]});
1415
${require('process-console-log').logger}
15-
const _fileExists = require('node-file-exists').default;
16-
const _path = require('path');
17-
function exists(p) { return _fileExists(_path.join('${dir}',p)); }
16+
const _f_e = require('node-file-exists').default;
17+
const _join = require('path').join;
18+
function exists(p) { return _f_e(_join('${dir}',p)); }
1819
require = require('rewire-coderoad');
1920
20-
// unit tests
21-
2221
${fixImportPaths({dir, content})}
2322
2423
}());`;

src/system.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
export const isWindows = window.navigator.appVersion.indexOf('Win') > -1;
1+
let window = window || ({ navigator: { appVersion: '' } });
22

3-
export const fixPathForWindows = (p: string) => {
4-
p.split('\\\\').join('\\');
5-
p.split('\\').join('\\\\');
6-
p.split('/').join('\\\\');
7-
return p;
8-
};
3+
export const isWindows = window.navigator.appVersion.indexOf('Win') > -1;

test/importPaths.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const fixImportPaths = require('../lib/importPaths').default;
44
const macDir = '/usr/name/desktop/folder';
55
const winDir = `C:\\usr\\name\\desktop\\folder`;
66

7+
// window.navigator.appVersion = 'Mac';
8+
79
test('does nothing if no imports', t => {
810
const content = 'var a = 42;';
911
const result = fixImportPaths({
@@ -59,7 +61,7 @@ test('handles named imports beginning with BASE', t => {
5961

6062
// Windows
6163

62-
test('handles test paths on Windows', t => {
64+
test.skip('handles test paths on Windows', t => {
6365
const content = `var example = require('BASE/example');`;
6466
const result = fixImportPaths({
6567
dir: winDir,

test/system.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const test = require('ava');
2+
const fixPathForWindows = require('../lib/system').fixPathForWindows;
3+
4+
const winPath = 'C:\path\to\file';
5+
6+
test('adds double slashes to a regular path', t => {
7+
const result = fixPathForWindows(winPath);
8+
const expected = 'C:\\path\\to\\file';
9+
t.is(result, expected);
10+
});

0 commit comments

Comments
 (0)