forked from ember-learn/ember-cli-addon-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
361 lines (297 loc) · 11.2 KB
/
index.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
'use strict';
const fs = require('fs');
const path = require('path');
const UnwatchedDir = require('broccoli-source').UnwatchedDir;
const MergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
const EmberApp = require('ember-cli/lib/broccoli/ember-app'); // eslint-disable-line node/no-unpublished-require
const Plugin = require('broccoli-plugin');
const walkSync = require('walk-sync');
const buildTailwind = require('ember-cli-tailwind/lib/build-tailwind');
const LATEST_VERSION_NAME = '-latest';
module.exports = {
name: 'ember-cli-addon-docs',
LATEST_VERSION_NAME,
options: {
nodeAssets: {
'highlight.js': {
public: {
include: [ 'styles/monokai.css' ]
},
vendor: {
include: [ 'styles/monokai.css' ]
}
}
},
svgJar: {
sourceDirs: [
'public',
'node_modules/ember-cli-addon-docs/public',
'tests/dummy/public'
],
optimizer : {
plugins: [
{
removeAttrs: {
attrs: [ 'fill' ]
}
}
]
}
}
},
config(env, baseConfig) {
let repo = this.parent.pkg.repository;
let info = require('hosted-git-info').fromUrl(repo.url || repo);
let userConfig = this._readUserConfig();
let config = {
'ember-cli-addon-docs': {
projectName: this.parent.pkg.name,
projectDescription: this.parent.pkg.description,
projectTag: this.parent.pkg.version,
projectHref: info && info.browse(),
primaryBranch: userConfig.getPrimaryBranch(),
latestVersionName: LATEST_VERSION_NAME,
deployVersion: 'ADDON_DOCS_DEPLOY_VERSION',
searchTokenSeparator: "\\s+"
},
'ember-component-css': {
namespacing: false
},
'ember-cli-tailwind': {
shouldIncludeStyleguide: false,
shouldBuildTailwind: false
}
};
let updatedConfig = Object.assign({}, baseConfig, config);
// Augment config with addons we depend on
updatedConfig = this.addons.reduce((config, addon) => {
if (addon.config) {
config = Object.assign({}, addon.config(env, config), config);
}
return config;
}, updatedConfig);
return updatedConfig;
},
included(includer) {
if (includer.parent) {
throw new Error(`ember-cli-addon-docs should be in your package.json's devDependencies`);
} else if (includer.name === this.project.name()) {
throw new Error(`ember-cli-addon-docs only currently works with addons, not applications`);
}
this._super.included.apply(this, arguments);
const hasPlugins = this.project.addons.some(function(addon) {
const isPlugin = addon.pkg.keywords.indexOf('ember-cli-addon-docs-plugin') !== -1;
const isPluginPack = addon.pkg.keywords.indexOf('ember-cli-addon-docs-plugin-pack') !== -1;
return isPlugin || isPluginPack;
});
if (!hasPlugins) {
this.ui.writeWarnLine('ember-cli-addon-docs needs plugins to generate API documentation. You can install the default with `ember install ember-cli-addon-docs-yuidoc`');
}
this.addonOptions = Object.assign({}, includer.options['ember-cli-addon-docs']);
this.addonOptions.projects = Object.assign({}, this.addonOptions.projects);
includer.options.includeFileExtensionInSnippetNames = includer.options.includeFileExtensionInSnippetNames || false;
includer.options.snippetSearchPaths = includer.options.snippetSearchPaths || ['tests/dummy/app'];
includer.options.snippetRegexes = Object.assign({}, {
begin: /{{#(?:docs-snippet|demo.example)\sname=(?:"|')(\S+)(?:"|')/,
end: /{{\/(?:docs-snippet|demo.example)}}/,
}, includer.options.snippetRegexes);
let importer = findImporter(this);
importer.import('vendor/lunr/lunr.js', {
using: [{ transformation: 'amd', as: 'lunr' }]
});
// importer.import('vendor/highlightjs-styles/default.css');
// importer.import('vendor/styles/highlightjs-styles/default.css');
// importer.import('vendor/highlight.js/styles/monokai.css');
// importer.import('vendor/highlightjs-styles/github.css');
},
createDeployPlugin() {
const AddonDocsDeployPlugin = require('./lib/deploy/plugin');
return new AddonDocsDeployPlugin(this._readUserConfig());
},
setupPreprocessorRegistry(type, registry) {
if (type === 'parent') {
let TemplateCompiler = require('./lib/preprocessors/markdown-template-compiler');
let ContentExtractor = require('./lib/preprocessors/hbs-content-extractor');
registry.add('template', new TemplateCompiler());
registry.add('template', new ContentExtractor(this.getBroccoliBridge()));
}
},
contentFor(type) {
if (type === 'body') {
return fs.readFileSync(`${__dirname}/vendor/ember-cli-addon-docs/github-spa.html`, 'utf-8');
}
if (type === 'head-footer') {
return `<link href="https://fonts.googleapis.com/css?family=Crimson+Text:400,600" rel="stylesheet">`;
}
},
treeForApp(app) {
let trees = [ app ];
let addonPath = this.project.findAddonByName(this.name).root;
let addonTree = new Funnel(path.join(addonPath, 'addon'), {
include: ['**/*.js']
});
let autoExportedAddonTree = new AutoExportAddonToApp([ addonTree ]);
trees.push(autoExportedAddonTree);
return new MergeTrees(trees);
},
treeForAddon(tree) {
let dummyAppFiles = new FindDummyAppFiles([ 'tests/dummy/app' ]);
let addonFiles = new FindAddonFiles([ 'addon' ].filter(dir => fs.existsSync(dir)));
return this._super(new MergeTrees([ tree, dummyAppFiles, addonFiles ]));
},
treeForAddonStyles(tree) {
let trees = tree ? [ tree ] : [];
trees.push(buildTailwind(this));
return new MergeTrees(trees);
},
treeForVendor(vendor) {
return new MergeTrees([
vendor,
this._highlightJSTree(),
this._lunrTree()
].filter(Boolean));
},
getBroccoliBridge() {
if (!this._broccoliBridge) {
const Bridge = require('broccoli-bridge');
this._broccoliBridge = new Bridge();
}
return this._broccoliBridge;
},
postprocessTree(type, tree) {
let parentAddon = this.parent.findAddonByName(this.parent.name());
if (!parentAddon || type !== 'all') { return tree; }
let PluginRegistry = require('./lib/models/plugin-registry');
let DocsCompiler = require('./lib/broccoli/docs-compiler');
let SearchIndexer = require('./lib/broccoli/search-indexer');
let project = this.project;
let docsTrees = [];
this.addonOptions.projects.main = this.addonOptions.projects.main || generateDefaultProject(parentAddon);
for (let projectName in this.addonOptions.projects) {
let addonSourceTree = this.addonOptions.projects[projectName];
let pluginRegistry = new PluginRegistry(project);
let docsGenerators = pluginRegistry.createDocsGenerators(addonSourceTree, {
destDir: 'docs',
project,
parentAddon
});
docsTrees.push(
new DocsCompiler(docsGenerators, {
name: projectName === 'main' ? parentAddon.name : projectName,
project
})
);
}
let docsTree = new MergeTrees(docsTrees);
let templateContentsTree = this.getBroccoliBridge().placeholderFor('template-contents');
let searchIndexTree = new SearchIndexer(new MergeTrees([docsTree, templateContentsTree]), {
outputFile: 'ember-cli-addon-docs/search-index.json',
config: this.project.config(EmberApp.env())
});
return new MergeTrees([ tree, docsTree, searchIndexTree ]);
},
_lunrTree() {
return new Funnel(path.dirname(require.resolve('lunr/package.json')), { destDir: 'lunr' });
},
_highlightJSTree() {
return new Funnel(path.dirname(require.resolve('highlightjs/package.json')), {
srcDir: 'styles',
destDir: 'highlightjs-styles'
});
},
_readUserConfig() {
if (!this._userConfig) {
const readConfig = require('./lib/utils/read-config');
this._userConfig = readConfig(this.project);
}
return this._userConfig;
}
};
function findImporter(addon) {
if (typeof addon.import === 'function') {
// If addon.import() is present (CLI 2.7+) use that
return addon;
} else {
// Otherwise, reuse the _findHost implementation that would power addon.import()
let current = addon;
let app;
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
return app;
}
}
function generateDefaultProject(parentAddon) {
let includeFunnels = [
// We need to be very careful to avoid triggering a watch on the addon root here
// because of https://github.com/nodejs/node/issues/15683
new Funnel(new UnwatchedDir(parentAddon.root), {
include: ['package.json', 'README.md']
})
];
let addonTreePath = path.join(parentAddon.root, parentAddon.treePaths['addon']);
let testSupportPath = path.join(parentAddon.root, parentAddon.treePaths['addon-test-support']);
if (fs.existsSync(addonTreePath)) {
includeFunnels.push(new Funnel(addonTreePath, {
destDir: parentAddon.name
}));
}
if (fs.existsSync(testSupportPath)) {
includeFunnels.push(new Funnel(testSupportPath, {
destDir: `${parentAddon.name}/test-support`
}));
}
return new MergeTrees(includeFunnels);
}
class FindDummyAppFiles extends Plugin {
build() {
let addonPath = this.inputPaths[0];
let paths = walkSync(addonPath, { directories: false })
let pathsString = JSON.stringify(paths);
fs.writeFileSync(path.join(this.outputPath, 'app-files.js'), `export default ${pathsString};`);
}
}
class FindAddonFiles extends Plugin {
build() {
let addonPath = this.inputPaths[0];
let paths = addonPath ? walkSync(addonPath, { directories: false }) : [];
let pathsString = JSON.stringify(paths);
fs.writeFileSync(path.join(this.outputPath, 'addon-files.js'), `export default ${pathsString};`);
}
}
class AutoExportAddonToApp extends Plugin {
build() {
let addonPath = this.inputPaths[0];
// Components
walkSync(path.join(addonPath, 'components'), { directories: false })
.forEach(addonFile => {
let module = addonFile.replace('/component.js', '');
let file = path.join(this.outputPath, 'components', `${module}.js`);
ensureDirectoryExistence(file);
fs.writeFileSync(file, `export { default } from 'ember-cli-addon-docs/components/${module}/component';`);
});
// Non-pods modules (slightly different logic)
[ 'adapters', 'controllers', 'helpers', 'models', 'routes', 'serializers', 'services', 'transitions' ].forEach(moduleType => {
let addonFullPath = path.join(addonPath, moduleType);
if (!fs.existsSync(addonFullPath)) {
return;
}
let addonFiles = walkSync(addonFullPath, { directories: false });
addonFiles.forEach(addonFile => {
let module = addonFile.replace('.js', '');
let file = path.join(this.outputPath, moduleType, `${module}.js`);
ensureDirectoryExistence(file);
fs.writeFileSync(file, `export { default } from 'ember-cli-addon-docs/${moduleType}/${module}';`);
});
});
}
}
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}