This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 877
/
Copy pathdoc-shredder.js
342 lines (307 loc) · 13.1 KB
/
doc-shredder.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Canonical path provides a consistent path (i.e. always forward slashes) across different OSes
var path = require('canonical-path');
var del = require('del');
var Dgeni = require('dgeni');
var _ = require('lodash');
var globby = require('globby');
var ignoreDirs = ['**/node_modules/**', '**/dist/**', '**/dart/**/build/**', '**/.pub/**'];
var _getLogLevel = function (options) { return options.logLevel || 'info'; }
var shred = function(shredOptions) {
try {
var pkg;
if (shredOptions.jadeDir) {
pkg = createShredJadePackage(shredOptions);
} else {
pkg = createShredExamplePackage(shredOptions);
}
var dgeni = new Dgeni([pkg]);
return dgeni.generate();
} catch(err) {
console.log(err);
console.log(err.stack);
throw err;
}
}
var shredSingleExampleDir = function(shredOptions, fileDir) {
shredOptions = resolveShredOptions(shredOptions);
var relativePath = path.relative(shredOptions.examplesDir, fileDir);
var examplesDir = path.join(shredOptions.examplesDir, relativePath);
var fragmentsDir = path.join(shredOptions.fragmentsDir, relativePath);
var options = {
includeSubdirs: true,
examplesDir: examplesDir,
fragmentsDir: fragmentsDir,
logLevel: _getLogLevel(shredOptions)
}
var cleanPath = path.join(fragmentsDir, '*.*')
return del([ cleanPath, '!**/*.ovr.*']).then(function(paths) {
// console.log('Deleted files/folders:\n', paths.join('\n'));
return shred(options);
});
}
var shredSingleDir = function(shredOptions, filePath) {
shredOptions = resolveShredOptions(shredOptions);
var fileDir = path.dirname(filePath);
var relativePath = path.relative(shredOptions.examplesDir, fileDir);
var examplesDir = path.join(shredOptions.examplesDir, relativePath);
var fragmentsDir = path.join(shredOptions.fragmentsDir, relativePath);
var options = {
includeSubdirs: false,
examplesDir: examplesDir,
fragmentsDir: fragmentsDir,
logLevel: _getLogLevel(shredOptions)
}
var cleanPath = path.join(fragmentsDir, '*.*')
return del([ cleanPath, '!**/*.ovr.*']).then(function(paths) {
// console.log('Deleted files/folders:\n', paths.join('\n'));
return shred(options);
});
}
var shredSingleJadeDir = function(shredOptions, filePath) {
shredOptions = resolveShredOptions(shredOptions);
var fileDir = path.dirname(filePath);
var relativePath = path.relative(shredOptions.jadeDir, fileDir);
var jadeDir = path.join(shredOptions.jadeDir, relativePath);
var options = {
includeSubdirs: false,
jadeDir: jadeDir,
logLevel: _getLogLevel(shredOptions)
}
// var cleanPath = path.join(jadeDir, '_.*.jade')
//return delPromise([ cleanPath]).then(function(paths) {
// console.log('Deleted files/folders:\n', paths.join('\n'));
// return shred(options);
//});
return shred(options);
}
var buildShredMap = function(shredMapOptions) {
try {
var pkg = createShredMapPackage(shredMapOptions);
var dgeni = new Dgeni([ pkg]);
return dgeni.generate();
} catch(err) {
console.log(err);
console.log(err.stack);
throw err;
}
}
module.exports = {
shred: shred,
shredSingleExampleDir: shredSingleExampleDir,
shredSingleDir: shredSingleDir,
shredSingleJadeDir: shredSingleJadeDir,
buildShredMap: buildShredMap
};
function createShredExamplePackage(shredOptions) {
var pkg = new Dgeni.Package('doc-shredder', [
// require('dgeni-packages/base') - doesn't work
]);
var options = resolveShredOptions(shredOptions);
initializePackage(pkg)
.factory(require('./fileReaders/regionFileReader'))
.processor(require('./processors/renderAsMarkdownProcessor'))
.config(function(readFilesProcessor, regionFileReader) {
readFilesProcessor.fileReaders = [regionFileReader];
})
// default configs - may be overridden
.config(function(log, readFilesProcessor) {
log.level = _getLogLevel(shredOptions);
// Specify the base path used when resolving relative paths to source and output files
readFilesProcessor.basePath = "/";
// Specify collections of source files that should contain the documentation to extract
var extns = ['*.ts', '*.html', '*.js', '*.css', '*.json', '*.dart', '*.yaml', '*.es6' ];
var includeFiles = extns.map(function(extn) {
if (options.includeSubdirs) {
return path.join(options.examplesDir, '**', extn);
} else {
return path.join(options.examplesDir, extn);
}
});
// HACK ( next two lines) because the glob function that dgeni uses internally isn't good at removing 'node_modules' early
// this just uses globby to 'preglob' the include files ( and exclude the node_modules).
var includeFiles = globby.sync( includeFiles, { ignore: ignoreDirs } );
log.info(`Shredding ${includeFiles.length} files inside ${shredOptions.examplesDir}`);
readFilesProcessor.sourceFiles = [ {
// Process all candidate files in `src` and its subfolders ...
include: includeFiles ,
exclude: [ '**/node_modules/**', '**/dist/**', '**/dart/build/**'],
// When calculating the relative path to these files use this as the base path.
// So `src/foo/bar.js` will have relative path of `foo/bar.js`
basePath: options.examplesDir
} ];
})
.config(function(writeFilesProcessor) {
// Specify where the writeFilesProcessor will write our generated doc files
writeFilesProcessor.outputFolder = path.resolve(options.fragmentsDir);
});
return pkg;
}
function createShredJadePackage(shredOptions) {
var pkg = new Dgeni.Package('jade-doc-shredder', [
// require('dgeni-packages/base') - doesn't work
]);
var options = shredOptions;
options.jadeDir = path.resolve(options.jadeDir);
options.includeSubdirs = options.includeSubdirs == null ? true : options.includeSubdirs;
initializePackage(pkg)
.factory(require('./fileReaders/regionFileReader'))
.processor(require('./processors/renderAsJadeProcessor'))
.config(function(log, readFilesProcessor, regionFileReader) {
log.level = _getLogLevel(shredOptions);
readFilesProcessor.fileReaders = [regionFileReader];
})
// default configs - may be overridden
.config(function(readFilesProcessor) {
// Specify the base path used when resolving relative paths to source and output files
readFilesProcessor.basePath = "/";
// Specify collections of source files that should contain the documentation to extract
var extns = ['*.jade' ];
var includeFiles = extns.map(function(extn) {
if (options.includeSubdirs) {
return path.join(options.jadeDir, '**', extn);
} else {
return path.join(options.jadeDir, extn);
}
});
// HACK ( next two lines) because the glob function that dgeni uses internally isn't good at removing 'node_modules' early
// this just uses globby to 'preglob' the include files ( and exclude the node_modules).
var includeFiles = globby.sync( includeFiles, { ignore: ignoreDirs } );
readFilesProcessor.sourceFiles = [ {
// Process all candidate files in `src` and its subfolders ...
include: includeFiles ,
exclude: [ '**/node_modules/**', '**/dart/build/**', '**/_code-examples.jade'],
// When calculating the relative path to these files use this as the base path.
// So `src/foo/bar.js` will have relative path of `foo/bar.js`
basePath: options.jadeDir
} ];
})
.config(function(writeFilesProcessor) {
// Specify where the writeFilesProcessor will write our generated doc files
writeFilesProcessor.outputFolder = '.';
});
return pkg;
}
var createShredMapPackage = function(mapOptions) {
var pkg = new Dgeni.Package('doc-shred-mapper', [
require('dgeni-packages/base'),
require('dgeni-packages/nunjucks')
]);
var options = resolveMapOptions(mapOptions);
initializePackage(pkg)
.factory(require('./fileReaders/extractPathsReader'))
.processor(require('./processors/shredMapProcessor'))
.config(function(shredMapProcessor) {
shredMapProcessor.options = options;
})
.config(function(log, readFilesProcessor, extractPathsReader ) {
log.level = _getLogLevel(mapOptions);
readFilesProcessor.fileReaders = [ extractPathsReader];
})
// default configs - may be overridden
.config(function(readFilesProcessor) {
// Specify the base path used when resolving relative paths to source and output files
readFilesProcessor.basePath = '/';
// Specify collections of source files that should contain the documentation to extract
var extns = ['*.jade' ];
var includeFiles = extns.map(function(extn) {
if (options.includeSubdirs) {
return path.join(options.jadeDir, '**', extn);
} else {
return path.join(options.jadeDir, extn);
}
});
// HACK ( next two lines) because the glob function that dgeni uses internally isn't good at removing 'node_modules' early
// this just uses globby to 'preglob' the include files ( and exclude the node_modules).
var includeFiles = globby.sync( includeFiles, { ignore: ignoreDirs } );
readFilesProcessor.sourceFiles = [ {
// Process all candidate files in `src` and its subfolders ...
include: includeFiles,
exclude: ['**/node_modules/**', '**/dart/build/**'],
// When calculating the relative path to these files use this as the base path.
// So `src/foo/bar.js` will have relative path of `foo/bar.js`
basePath: options.jadeDir
} ];
})
.config(function(writeFilesProcessor, renderDocsProcessor, unescapeCommentsProcessor) {
if (!mapOptions.writeFilesEnabled) {
// dgeni hack to allow a geni task to simply return the results of the shredMapProcessor directly
renderDocsProcessor.$enabled = false;
writeFilesProcessor.$enabled = false;
unescapeCommentsProcessor.$enabled = false;
} else {
// Specify where the writeFilesProcessor will write our generated doc files
writeFilesProcessor.outputFolder = path.resolve(options.outputDir);
}
})
.config(function(templateFinder) {
// look for templates in this folder
templateFinder.templateFolders = [ path.resolve(__dirname) ];
// Specify how to match docs to templates.
// In this case we just use the same static template for all docs
templateFinder.templatePatterns = [ '${ doc.docType }.template' ];
})
.config(function(computePathsProcessor, computeIdsProcessor) {
computePathsProcessor.$enabled = false;
computeIdsProcessor.$enabled = false;
// Unused for now.
//computePathsProcessor.pathTemplates.push({
// docTypes: ['foo'],
// pathTemplate: '',
// getOutputPath: function () {
// },
//});
//
//computeIdsProcessor.idTemplates.push({
// docTypes: ['foo'],
// getAliases: function (doc) {
// return [doc.id];
// }
//});
});
return pkg;
}
function resolveShredOptions(shredOptions) {
var DOCS_FOLDER = '../../public/docs';
var so = _.defaults({}, shredOptions, {
// read files from any subdir under here
examplesDir: path.join(DOCS_FOLDER, "_examples"),
// shredded files get copied here with same subdir structure.
fragmentsDir: path.join(DOCS_FOLDER, "_fragments"),
// whether to include subdirectories when shredding.
includeSubdirs: true
});
so.examplesDir = path.resolve(so.examplesDir);
so.fragmentsDir = path.resolve(so.fragmentsDir);
return so;
}
function resolveMapOptions(mapOptions) {
var so = _.defaults({}, mapOptions, {
includeSubdirs: true
});
so.jadeDir = path.resolve(so.jadeDir);
so.devguideExamplesDir = path.resolve(so.devguideExamplesDir);
so.apiExamplesDir = path.resolve(so.apiExamplesDir);
so.fragmentsDir = path.resolve(so.fragmentsDir);
return so;
}
function initializePackage(pkg) {
return pkg
.processor(require('dgeni-packages/base/processors/read-files'))
.processor(require('dgeni-packages/base/processors/write-files'))
.factory(require('dgeni-packages/base/services/writeFile'))
// Ugh... Boilerplate that dgeni needs to sequence operations
.processor({ name: 'reading-files' })
.processor({ name: 'files-read', $runAfter: ['reading-files'] })
.processor({ name: 'processing-docs', $runAfter: ['files-read'] })
.processor({ name: 'docs-processed', $runAfter: ['processing-docs'] })
.processor({ name: 'adding-extra-docs', $runAfter: ['docs-processed'] })
.processor({ name: 'extra-docs-added', $runAfter: ['adding-extra-docs'] })
.processor({ name: 'computing-ids', $runAfter: ['extra-docs-added'] })
.processor({ name: 'ids-computed', $runAfter: ['computing-ids'] })
.processor({ name: 'computing-paths', $runAfter: ['ids-computed'] })
.processor({ name: 'paths-computed', $runAfter: ['computing-paths'] })
.processor({ name: 'rendering-docs', $runAfter: ['paths-computed'] })
.processor({ name: 'docs-rendered', $runAfter: ['rendering-docs'] })
.processor({ name: 'writing-files', $runAfter: ['docs-rendered'] })
.processor({ name: 'files-written', $runAfter: ['writing-files'] })
}