Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 86f3e41

Browse files
chore(docs/writer): fix Windows path incompatibilities
NodeJS on Windows uses back slashes for path separators. This difference can be mitigated by use of the nodeJS path library. In particular the `sep` property and the `dirname()`, `normalize()` and `join()` methods of this library. All path based arguments on exported functions need to be normalized and `join` and `sep` must be used instead of string manipulation to work with paths.
1 parent c9c3f71 commit 86f3e41

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

docs/src/writer.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,33 @@
22
* All writing related code here. This is so that we can separate the async code from sync code
33
* for testability
44
*/
5+
var pathUtils = require('path');
56
var qfs = require('q-fs');
67
var Q = require('qq');
7-
var OUTPUT_DIR = 'build/docs/';
8+
var OUTPUT_DIR = pathUtils.join('build','docs');
9+
var TEMPLATES_DIR = pathUtils.join('docs','src','templates');
810
var fs = require('fs');
911

1012
exports.output = output;
1113
function output(file, content) {
12-
var fullPath = OUTPUT_DIR + file;
13-
var dir = parent(fullPath);
14+
var fullPath = pathUtils.join(OUTPUT_DIR,file);
15+
var dir = pathUtils.dirname(fullPath);
1416
return Q.when(exports.makeDir(dir), function(error) {
1517
qfs.write(fullPath, exports.toString(content));
1618
});
17-
};
19+
}
1820

1921
//recursively create directory
2022
exports.makeDir = function(p) {
21-
var parts = p.split(/\//);
23+
p = pathUtils.normalize(p);
24+
var parts = p.split(pathUtils.sep);
2225
var path = ".";
2326

2427
// Recursively rebuild directory structure
2528
return qfs.exists(p).
2629
then(function createPart(exists) {
2730
if(!exists && parts.length) {
28-
path += "/" + parts.shift();
31+
path = pathUtils.join(path, parts.shift());
2932
return qfs.exists(path).then(function(exists) {
3033
if (!exists) {
3134
return qfs.makeDirectory(path).then(createPart, createPart);
@@ -38,7 +41,8 @@ exports.makeDir = function(p) {
3841
};
3942

4043
exports.copyTemplate = function(filename) {
41-
return exports.copy('docs/src/templates/' + filename, filename);
44+
// Don't need to normalize here as `exports.copy` will do it for us
45+
return exports.copy(pathUtils.join(TEMPLATES_DIR,filename), filename);
4246
};
4347

4448
/* Copy files from one place to another.
@@ -47,13 +51,13 @@ exports.copyTemplate = function(filename) {
4751
* @param transform{function=} transfromation function to be applied before return
4852
*/
4953
exports.copy = function(from, to, transform) {
50-
var args = Array.prototype.slice.call(arguments, 3);
54+
from = pathUtils.normalize(from);
55+
to = pathUtils.normalize(to);
5156

5257
// We have to use binary reading, Since some characters are unicode.
5358
return qfs.read(from, 'b').then(function(content) {
5459
if (transform) {
55-
args.unshift(content.toString());
56-
content = transform.apply(null, args);
60+
content = transform.call(null, content.toString(), from, to, transform);
5761
}
5862
return output(to, content);
5963
});
@@ -62,6 +66,7 @@ exports.copy = function(from, to, transform) {
6266

6367
exports.symlink = symlink;
6468
function symlink(from, to, type) {
69+
// qfs will normalize the path arguments for us here
6570
return qfs.exists(to).then(function(exists) {
6671
if (!exists) {
6772
return qfs.symbolicLink(to, from, type);
@@ -72,9 +77,10 @@ function symlink(from, to, type) {
7277

7378
exports.symlinkTemplate = symlinkTemplate;
7479
function symlinkTemplate(filename, type) {
75-
var dest = OUTPUT_DIR + filename,
76-
dirDepth = dest.split('/').length,
77-
src = Array(dirDepth).join('../') + 'docs/src/templates/' + filename;
80+
// pathUtils.join will normalize the filename for us
81+
var dest = pathUtils.join(OUTPUT_DIR, filename),
82+
dirDepth = dest.split(pathUtils.sep).length,
83+
src = pathUtils.join(Array(dirDepth).join('..' + pathUtils.sep), TEMPLATES_DIR, filename);
7884
return symlink(src, dest, type);
7985
}
8086

@@ -84,24 +90,29 @@ function symlinkTemplate(filename, type) {
8490
* @param replacements{obj} key and value pairs in which key will be replaced with value in content
8591
*/
8692
exports.replace = function(content, replacements) {
87-
for(key in replacements) {
93+
for(var key in replacements) {
8894
content = content.replace(key, replacements[key]);
8995
}
9096
return content;
91-
}
97+
};
9298

9399
exports.copyDir = function copyDir(from, to) {
100+
from = pathUtils.normalize(from);
101+
to = pathUtils.normalize(to);
94102
return qfs.listTree(from).then(function(files) {
95103
files.forEach(function(file) {
96104
var path = to ? file.replace(from, to) : from;
105+
// Not sure why this next line is here...
97106
path = path.replace('/docs/build', '');
98107
exports.copy(file, path);
99108
});
100109
});
101110
};
102111

103112
exports.merge = function(srcs, to) {
104-
return merge(srcs.map(function(src) { return 'docs/src/templates/' + src; }), to);
113+
// pathUtils.join will normalize each of the srcs inside the mapping
114+
to = pathUtils.normalize(to);
115+
return merge(srcs.map(function(src) { return pathUtils.join(TEMPLATES_DIR, src); }), to);
105116
};
106117

107118
function merge(srcs, to) {
@@ -124,13 +135,6 @@ function merge(srcs, to) {
124135

125136
//----------------------- Synchronous Methods ----------------------------------
126137

127-
function parent(file) {
128-
var parts = file.split('/');
129-
parts.pop();
130-
return parts.join('/');
131-
}
132-
133-
134138
exports.toString = function toString(obj) {
135139
switch (typeof obj) {
136140
case 'string':
@@ -151,5 +155,5 @@ exports.toString = function toString(obj) {
151155
};
152156

153157

154-
function noop() {};
158+
function noop() {}
155159

0 commit comments

Comments
 (0)