2
2
* All writing related code here. This is so that we can separate the async code from sync code
3
3
* for testability
4
4
*/
5
+ var pathUtils = require ( 'path' ) ;
5
6
var qfs = require ( 'q-fs' ) ;
6
7
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' ) ;
8
10
var fs = require ( 'fs' ) ;
9
11
10
12
exports . output = output ;
11
13
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 ) ;
14
16
return Q . when ( exports . makeDir ( dir ) , function ( error ) {
15
17
qfs . write ( fullPath , exports . toString ( content ) ) ;
16
18
} ) ;
17
- } ;
19
+ }
18
20
19
21
//recursively create directory
20
22
exports . makeDir = function ( p ) {
21
- var parts = p . split ( / \/ / ) ;
23
+ p = pathUtils . normalize ( p ) ;
24
+ var parts = p . split ( pathUtils . sep ) ;
22
25
var path = "." ;
23
26
24
27
// Recursively rebuild directory structure
25
28
return qfs . exists ( p ) .
26
29
then ( function createPart ( exists ) {
27
30
if ( ! exists && parts . length ) {
28
- path += "/" + parts . shift ( ) ;
31
+ path = pathUtils . join ( path , parts . shift ( ) ) ;
29
32
return qfs . exists ( path ) . then ( function ( exists ) {
30
33
if ( ! exists ) {
31
34
return qfs . makeDirectory ( path ) . then ( createPart , createPart ) ;
@@ -38,7 +41,8 @@ exports.makeDir = function(p) {
38
41
} ;
39
42
40
43
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 ) ;
42
46
} ;
43
47
44
48
/* Copy files from one place to another.
@@ -47,13 +51,13 @@ exports.copyTemplate = function(filename) {
47
51
* @param transform{function=} transfromation function to be applied before return
48
52
*/
49
53
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 ) ;
51
56
52
57
// We have to use binary reading, Since some characters are unicode.
53
58
return qfs . read ( from , 'b' ) . then ( function ( content ) {
54
59
if ( transform ) {
55
- args . unshift ( content . toString ( ) ) ;
56
- content = transform . apply ( null , args ) ;
60
+ content = transform . call ( null , content . toString ( ) , from , to , transform ) ;
57
61
}
58
62
return output ( to , content ) ;
59
63
} ) ;
@@ -62,6 +66,7 @@ exports.copy = function(from, to, transform) {
62
66
63
67
exports . symlink = symlink ;
64
68
function symlink ( from , to , type ) {
69
+ // qfs will normalize the path arguments for us here
65
70
return qfs . exists ( to ) . then ( function ( exists ) {
66
71
if ( ! exists ) {
67
72
return qfs . symbolicLink ( to , from , type ) ;
@@ -72,9 +77,10 @@ function symlink(from, to, type) {
72
77
73
78
exports . symlinkTemplate = symlinkTemplate ;
74
79
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 ) ;
78
84
return symlink ( src , dest , type ) ;
79
85
}
80
86
@@ -84,24 +90,29 @@ function symlinkTemplate(filename, type) {
84
90
* @param replacements{obj} key and value pairs in which key will be replaced with value in content
85
91
*/
86
92
exports . replace = function ( content , replacements ) {
87
- for ( key in replacements ) {
93
+ for ( var key in replacements ) {
88
94
content = content . replace ( key , replacements [ key ] ) ;
89
95
}
90
96
return content ;
91
- }
97
+ } ;
92
98
93
99
exports . copyDir = function copyDir ( from , to ) {
100
+ from = pathUtils . normalize ( from ) ;
101
+ to = pathUtils . normalize ( to ) ;
94
102
return qfs . listTree ( from ) . then ( function ( files ) {
95
103
files . forEach ( function ( file ) {
96
104
var path = to ? file . replace ( from , to ) : from ;
105
+ // Not sure why this next line is here...
97
106
path = path . replace ( '/docs/build' , '' ) ;
98
107
exports . copy ( file , path ) ;
99
108
} ) ;
100
109
} ) ;
101
110
} ;
102
111
103
112
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 ) ;
105
116
} ;
106
117
107
118
function merge ( srcs , to ) {
@@ -124,13 +135,6 @@ function merge(srcs, to) {
124
135
125
136
//----------------------- Synchronous Methods ----------------------------------
126
137
127
- function parent ( file ) {
128
- var parts = file . split ( '/' ) ;
129
- parts . pop ( ) ;
130
- return parts . join ( '/' ) ;
131
- }
132
-
133
-
134
138
exports . toString = function toString ( obj ) {
135
139
switch ( typeof obj ) {
136
140
case 'string' :
@@ -151,5 +155,5 @@ exports.toString = function toString(obj) {
151
155
} ;
152
156
153
157
154
- function noop ( ) { } ;
158
+ function noop ( ) { }
155
159
0 commit comments