forked from ember-learn/ember-cli-addon-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
313 lines (257 loc) · 9.61 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
'use strict';
const fs = require('fs');
const path = require('path');
const resolve = require('resolve');
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 DEFAULT_PROJECTS = {
main: {
tree: null,
include: null,
includeTrees: ['addon']
}
}
module.exports = {
name: 'ember-cli-addon-docs',
isDevelopingAddon() {
return true;
},
options: {
ace: {
modes: ['handlebars']
},
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'
]
}
},
config(env, baseConfig) {
let config = {
'ember-component-css': {
namespacing: false
},
'ember-cli-addon-docs': {
packageJson: this.parent.pkg
}
};
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({}, DEFAULT_PROJECTS, 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|demo.live-example)\sname=(?:"|')(\S+)(?:"|')/,
end: /{{\/(?:docs-snippet|demo.example|demo.live-example)}}/,
}, includer.options.snippetRegexes);
let importer = findImporter(this);
importer.import(`${this._hasEmberSource() ? 'vendor' : 'bower_components'}/ember/ember-template-compiler.js`);
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');
const readConfig = require('./lib/utils/read-config');
let userConfig = readConfig(this.project);
return new AddonDocsDeployPlugin(userConfig);
},
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', this.contentExtractor = new ContentExtractor());
}
},
contentFor(type) {
if (type === 'body') {
return fs.readFileSync(`${__dirname}/vendor/ember-cli-addon-docs/github-spa.html`, 'utf-8');
}
},
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' ]);
return this._super(new MergeTrees([ tree, dummyAppFiles ]));
},
treeForVendor(vendor) {
return new MergeTrees([
vendor,
this._highlightJSTree(),
this._lunrTree(),
this._templateCompilerTree()
].filter(Boolean));
},
treeForPublic() {
let parentAddon = this.parent.findAddonByName(this.parent.name());
let defaultTree = this._super.treeForPublic.apply(this, arguments);
if (!parentAddon) { return defaultTree; }
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 = [];
for (let projectName in this.addonOptions.projects) {
let docProject = this.addonOptions.projects[projectName];
let addonSourceTree;
if (docProject.tree) {
addonSourceTree = docProject.tree;
} else {
let include = docProject.include || [];
let includeTrees = docProject.includeTrees || [];
let includeTreePaths = includeTrees.map(t => `${parentAddon.treePaths[t]}/**/*`);
addonSourceTree = new Funnel(parentAddon.root, {
include: include.concat(includeTreePaths).concat('package.json', 'README.md')
});
}
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.contentExtractor.getTemplateContentsTree();
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([ defaultTree, 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'
});
},
_templateCompilerTree() {
if (this._hasEmberSource()) {
return new Funnel(path.dirname(resolve.sync('ember-source/package.json'), { basedir: this.project.root }), {
srcDir: 'dist',
destDir: 'ember'
});
}
},
_hasEmberSource() {
return 'ember-source' in this.project.pkg.devDependencies;
}
};
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;
}
}
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 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);
}