@@ -53,17 +53,35 @@ module.exports = {
53
53
} ,
54
54
55
55
config ( env , baseConfig ) {
56
- let repo = this . parent . pkg . repository ;
56
+ let pkg = this . parent . pkg ;
57
+ if ( this . _documentingAddonAt ( ) ) {
58
+ pkg = require ( path . join ( this . _documentingAddonAt ( ) , 'package.json' ) ) ;
59
+ }
60
+
61
+ let repo = pkg . repository ;
57
62
let info = require ( 'hosted-git-info' ) . fromUrl ( repo . url || repo ) ;
58
63
let userConfig = this . _readUserConfig ( ) ;
59
64
65
+ let docsAppPathInRepo = path . relative (
66
+ this . _getRepoRoot ( ) ,
67
+ path . join (
68
+ path . resolve ( path . dirname ( this . project . configPath ( ) ) , '..' ) ,
69
+ 'app'
70
+ )
71
+ ) ;
72
+
73
+ let addonPathInRepo = this . _documentingAddonAt ( )
74
+ ? path . relative ( this . _getRepoRoot ( ) , path . join ( this . _documentingAddonAt ( ) , 'addon' ) )
75
+ : path . relative ( this . _getRepoRoot ( ) , path . join ( this . project . root , 'addon' ) ) ;
76
+
60
77
let config = {
61
78
'ember-cli-addon-docs' : {
62
- projectName : this . parent . pkg . name ,
63
- projectDescription : this . parent . pkg . description ,
64
- projectTag : this . parent . pkg . version ,
79
+ projectName : pkg . name ,
80
+ projectDescription : pkg . description ,
81
+ projectTag : pkg . version ,
65
82
projectHref : info && info . browse ( ) ,
66
- projectPathInRepo : path . relative ( this . _getRepoRoot ( ) , this . project . root ) ,
83
+ docsAppPathInRepo,
84
+ addonPathInRepo,
67
85
primaryBranch : userConfig . getPrimaryBranch ( ) ,
68
86
latestVersionName : LATEST_VERSION_NAME ,
69
87
deployVersion : 'ADDON_DOCS_DEPLOY_VERSION' ,
@@ -92,11 +110,24 @@ module.exports = {
92
110
if ( includer . parent ) {
93
111
throw new Error ( `ember-cli-addon-docs should be in your package.json's devDependencies` ) ;
94
112
} else if ( includer . name === this . project . name ( ) ) {
95
- throw new Error ( `ember-cli-addon-docs only currently works with addons, not applications` ) ;
113
+ if ( this . _documentingAddonAt ( ) ) {
114
+ // we're being used in a standalone documentation app that documents an
115
+ // addon but is not that addon's dummy app.
116
+ } else {
117
+ throw new Error ( `to use ember-cli-addon-docs in an application (as opposed to an addon) you must set documentingAddonAt` ) ;
118
+ }
96
119
}
97
120
98
121
includer . options . includeFileExtensionInSnippetNames = includer . options . includeFileExtensionInSnippetNames || false ;
99
- includer . options . snippetSearchPaths = includer . options . snippetSearchPaths || [ 'tests/dummy/app' ] ;
122
+ if ( ! includer . options . snippetSearchPaths ) {
123
+ if ( this . _documentingAddonAt ( ) ) {
124
+ // we are a standalone app, so our code is here
125
+ includer . options . snippetSearchPaths = [ 'app' ] ;
126
+ } else {
127
+ // we are inside the addon, so our code is here
128
+ includer . options . snippetSearchPaths = [ 'tests/dummy/app' ] ;
129
+ }
130
+ }
100
131
includer . options . snippetRegexes = Object . assign ( { } , {
101
132
begin : / { { # (?: d o c s - s n i p p e t | d e m o .e x a m p l e ) \s n a m e = (?: " | ' ) ( \S + ) (?: " | ' ) / ,
102
133
end : / { { \/ (?: d o c s - s n i p p e t | d e m o .e x a m p l e ) } } / ,
@@ -187,8 +218,8 @@ module.exports = {
187
218
188
219
treeForAddon ( tree ) {
189
220
let dummyAppFiles = new FindDummyAppFiles ( [ this . app . trees . app ] ) ;
190
- let addonFiles = new FindAddonFiles ( [ 'addon' ] . filter ( dir => fs . existsSync ( dir ) ) ) ;
191
-
221
+ let addonToDocument = this . _documentingAddon ( ) ;
222
+ let addonFiles = new FindAddonFiles ( [ path . join ( addonToDocument . root , 'addon' ) ] ) ;
192
223
return this . _super ( new MergeTrees ( [ tree , dummyAppFiles , addonFiles ] ) ) ;
193
224
} ,
194
225
@@ -209,17 +240,16 @@ module.exports = {
209
240
} ,
210
241
211
242
postprocessTree ( type , tree ) {
212
- let parentAddon = this . parent . findAddonByName ( this . parent . name ( ) ) ;
213
- if ( ! parentAddon || type !== 'all' ) { return tree ; }
243
+ let addonToDocument = this . _documentingAddon ( ) ;
244
+ if ( ! addonToDocument || type !== 'all' ) { return tree ; }
214
245
215
246
let PluginRegistry = require ( './lib/models/plugin-registry' ) ;
216
247
let DocsCompiler = require ( './lib/broccoli/docs-compiler' ) ;
217
248
let SearchIndexer = require ( './lib/broccoli/search-indexer' ) ;
218
249
219
250
let project = this . project ;
220
251
let docsTrees = [ ] ;
221
-
222
- this . addonOptions . projects . main = this . addonOptions . projects . main || generateDefaultProject ( parentAddon ) ;
252
+ this . addonOptions . projects . main = this . addonOptions . projects . main || generateDefaultProject ( addonToDocument ) ;
223
253
224
254
for ( let projectName in this . addonOptions . projects ) {
225
255
let addonSourceTree = this . addonOptions . projects [ projectName ] ;
@@ -229,12 +259,12 @@ module.exports = {
229
259
let docsGenerators = pluginRegistry . createDocsGenerators ( addonSourceTree , {
230
260
destDir : 'docs' ,
231
261
project,
232
- parentAddon
262
+ parentAddon : addonToDocument
233
263
} ) ;
234
264
235
265
docsTrees . push (
236
266
new DocsCompiler ( docsGenerators , {
237
- name : projectName === 'main' ? parentAddon . name : projectName ,
267
+ name : projectName === 'main' ? addonToDocument . name : projectName ,
238
268
project
239
269
} )
240
270
) ;
@@ -276,6 +306,33 @@ module.exports = {
276
306
this . _repoRoot = require ( 'git-repo-info' ) ( ) . root ;
277
307
}
278
308
return this . _repoRoot ;
309
+ } ,
310
+
311
+ // returns the absolute path to the addon we're documenting when
312
+ // ember-cli-addon-docs is being used by an *app* (not an addon) that has
313
+ // explicitly set `documentingAddonAt`.
314
+ _documentingAddonAt ( ) {
315
+ if ( this . _cachedDocumentingAddonAt === undefined && this . app ) {
316
+ if ( this . app . options [ 'ember-cli-addon-docs' ] && this . app . options [ 'ember-cli-addon-docs' ] . documentingAddonAt ) {
317
+ this . _cachedDocumentingAddonAt = path . resolve ( this . project . root , this . app . options [ 'ember-cli-addon-docs' ] . documentingAddonAt ) ;
318
+ } else {
319
+ this . _cachedDocumentingAddonAt = null ;
320
+ }
321
+ }
322
+ return this . _cachedDocumentingAddonAt ;
323
+ } ,
324
+
325
+ _documentingAddon ( ) {
326
+ let addon ;
327
+ if ( this . _documentingAddonAt ( ) ) {
328
+ addon = this . project . addons . find ( a => a . root === this . _documentingAddonAt ( ) )
329
+ if ( ! addon ) {
330
+ throw new Error ( `You set documentingAddonAt to point at ${ this . _documentingAddonAt ( ) } but that addon does not appear to be present in this app.` ) ;
331
+ }
332
+ } else {
333
+ addon = this . parent . findAddonByName ( this . parent . name ( ) ) ;
334
+ }
335
+ return addon ;
279
336
}
280
337
} ;
281
338
0 commit comments