This repository was archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopyFiles.js
More file actions
230 lines (185 loc) · 6.17 KB
/
Copy pathcopyFiles.js
File metadata and controls
230 lines (185 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* @fileoverview Generate the documentation for the included packages.
*/
var path = require('path'),
fs = require('fs'),
spawn = require('child_process').spawn;
os = require('os');
//var generator = require('doc-generator'),
var walk = require(__dirname + "/lib/walk.js"),
Promise = require('promise'),
copyFile = require(__dirname + '/lib/copyFile.js'),
Generator = require('./Generator.js');
walk = Promise.denodeify(walk);
copyFile = Promise.denodeify(copyFile);
module.exports = function(packages, outputDir, format, htmlTemplate){
var docsLocation = path.resolve(outputDir);
var tempFolder = path.resolve(__dirname, getRandomString());
while(fs.existsSync(tempFolder))
tempFolder = path.resolve(__dirname, getRandomString());
// Build a map of document locations to which package they came from
var docFilesToPackages = {};
// Build a map of new document location from old document location.
// The value will be an array encase we have multiple files with the same name
// from different packages
var docNewPathFrom = {};
var ps = [];
packages.forEach(function(packageName){
var packageDocLocation =
path.dirname(require.resolve(packageName)) + "/doc";
var p = walk(packageDocLocation)
.then(function(paths){
paths.forEach(function(p){
docFilesToPackages[p] = packageName;
var newPath = path.resolve(tempFolder,
p.slice(packageDocLocation.length + 1));
docNewPathFrom[newPath] = docNewPathFrom[newPath] || [];
docNewPathFrom[newPath].push(p);
});
return Promise.resolve();
})
.catch(function(err){
console.error(err);
})
ps.push(p);
});
return Promise.all(ps)
// Now lets copy all our documentation into one folder
.then(function(){
fs.mkdir(tempFolder);
var ps = [];
for (var newPath in docNewPathFrom){
var paths = docNewPathFrom[newPath];
// If newPath lands on any of the reseved locations we have to add the
// package name to it to stop clashes
var isReserved = (newPath == path.resolve(tempFolder, "index.md")
|| newPath == path.resolve(tempFolder, "all.md"))
if (paths.length > 1 || isReserved){
paths.forEach(function(p){
var dir = path.dirname(newPath),
ext = path.extname(p),
basename = path.basename(p, ext),
packageName = docFilesToPackages[p];
var localNewPath = path.resolve(
dir,
makePathSafe(basename + "#" + packageName + ext));
ps.push(copyFile(p, localNewPath));
});
} else {
ps.push(copyFile(paths[0], newPath));
}
}
return Promise.all(ps);
})
.then(function(){
return walk(tempFolder);
})
.then(function(paths){
// Create an all.md file
var allMdContent = "";
paths.forEach(function(p){
var include = p.slice(tempFolder.length + 1);
allMdContent += "@include " + include + os.EOL;
});
// Assumes an all.md file doesn't exist
var w = fs.createWriteStream(tempFolder + "/all.md");
w.write(allMdContent);
w.end();
// Generate an index file, needs some logic to not overrite
// an existing file TODO
var indexContent = "";
paths.forEach(function(p){
var filename = p.slice(tempFolder.length + 1),
ext = path.extname(filename),
filename = path.basename(filename, ext);
indexContent += " - [" + filename + "](" + encodeURIComponent(filename)
+ ".html" + ")" + os.EOL;
});
// Assumes an all.md file doesn't exist
var w = fs.createWriteStream(tempFolder + "/index.md");
w.write(indexContent);
w.end();
// Do this after so they don't include each other
paths.push(tempFolder + "/all.md");
paths.push(tempFolder + "/index.md");
return Promise.resolve(paths);
})
.then(function(paths){
// Generate the html
var ps = [];
paths.forEach(function(p){
var newPath = p.slice(tempFolder.length + 1);
newPath = docsLocation + "/" + newPath;
var dir = path.dirname(newPath),
ext = path.extname(newPath),
basename = path.basename(newPath, ext);
if (!fs.existsSync(dir))
fs.mkdir(dir);
switch (format){
case 'html':
var promise = new Promise(function(resolve, reject){
var htmlPath = dir + "/" + basename + ".html";
var stream = fs.createWriteStream(htmlPath, {'encoding':'utf8'});
var generator = new Generator(stream, 'html', htmlTemplate);
generator.generate(p, function(err){
stream.end();
if (err) return reject(err);
resolve();
});
});
break;
case 'json':
var promise = new Promise(function(resolve, reject){
var jsonPath = dir + "/" + basename + ".json";
var stream = fs.createWriteStream(jsonPath, {'encoding':'utf8'});
var generator = new Generator(stream, 'json');
generator.generate(p, function(err){
stream.end();
if (err) return reject(err);
resolve();
});
});
break;
default:
throw 'Format not supported';
}
ps.push(promise);
})
return Promise.all(ps).then(function(){
// Delete all the files
paths.forEach(function(p){
fs.unlinkSync(p);
});
return Promise.resolve();
})
.catch(function(err){
// Delete all the files
paths.forEach(function(p){
fs.unlinkSync(p);
});
})
})
.then(function(){
fs.rmdirSync(tempFolder);
})
.catch(function(err){
fs.rmdirSync(tempFolder);
console.error(err);
})
};
function getRandomString(){
var chars = "abcdefghijklmnopqrstuvwxyz";
var result = "";
var limit = 8;
while (limit > 0){
result += chars[Math.floor(Math.random() * ((chars.length - 1) - 0))];
limit--;
}
return result;
};
// Removes or replaces characters that arn't allowed in a directory
function makePathSafe(dir){
// \/:*?"<>|
// index#@alastair/pbx-api.html
return dir.replace(/[\/\\:*?"<>]/gi, '_');
};