forked from ember-learn/ember-cli-addon-docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
206 lines (172 loc) · 6.14 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
'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');
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);
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');
}
},
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 DocsGenerator = require('./lib/broccoli/docs-generator');
let SearchIndexer = require('./lib/broccoli/search-indexer');
let addonSources = path.resolve(parentAddon.root, parentAddon.treePaths.addon);
let docsTree = new DocsGenerator([addonSources], {
project: this.project,
destDir: 'docs'
});
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())
});
let notFoundSnippet = new Funnel(`${__dirname}/vendor/ember-cli-addon-docs`, {
include: ['404.html']
});
return new MergeTrees([ defaultTree, notFoundSnippet, 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};`);
}
}