-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding docs-page blueprint. Includes router invocation. * Adding updating docs.hbs with new nav item * Adding documentation for docs-page blueprint * Removing placeholder to adds pods support to adding a nav item * Refining docs to be reflective of output * Bugfixes for stupid mistakes
- Loading branch information
1 parent
ccfc0d5
commit b7224c3
Showing
4 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
blueprints/docs-page/files/__templatepath__/__templatename__.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# <%= templateName %> | ||
|
||
<%= templateName %> content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* eslint-env node */ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const chalk = require('chalk'); | ||
const EmberRouterGenerator = require('ember-router-generator'); | ||
const stringUtil = require('ember-cli-string-utils'); | ||
|
||
const DUMMY_APP_PATH = path.join('tests', 'dummy', 'app'); | ||
|
||
function dedasherize(str) { | ||
let dedasherized = str.replace(/-/g, ' '); | ||
|
||
return stringUtil.capitalize(dedasherized); | ||
} | ||
|
||
module.exports = { | ||
name: 'docs-page', | ||
description: 'Generates an ember-cli-addon-docs doc page', | ||
|
||
fileMapTokens: function() { | ||
return { | ||
__templatepath__: function(options) { | ||
if (options.pod) { | ||
return path.join( | ||
DUMMY_APP_PATH, | ||
'pods', | ||
'docs', | ||
options.dasherizedModuleName | ||
); | ||
} else { | ||
return path.join(DUMMY_APP_PATH, 'templates', 'docs'); | ||
} | ||
}, | ||
__templatename__: function(options) { | ||
if (options.pod) { | ||
return 'template'; | ||
} | ||
return options.dasherizedModuleName; | ||
} | ||
}; | ||
}, | ||
|
||
locals: function(options) { | ||
return { | ||
templateName: dedasherize(options.entity.name) | ||
}; | ||
}, | ||
|
||
afterInstall: function(options) { | ||
// eslint-disable-next-line no-debugger | ||
debugger; | ||
updateRouter.call(this, 'add', options); | ||
updateDocsTemplate.call(this, options); | ||
}, | ||
|
||
afterUninstall: function(options) { | ||
updateRouter.call(this, 'remove', options); | ||
} | ||
}; | ||
|
||
function updateRouter(action, options) { | ||
let entity = options.entity; | ||
let actionColorMap = { | ||
add: 'green', | ||
remove: 'red' | ||
}; | ||
let color = actionColorMap[action] || 'gray'; | ||
|
||
if (entity.name === 'index') { | ||
return; | ||
} | ||
|
||
writeRoute(action, entity.name, options); | ||
|
||
this.ui.writeLine('updating router'); | ||
this._writeStatusToUI(chalk[color], action + ' route', entity.name); | ||
} | ||
|
||
function findRouter(options) { | ||
let routerPathParts = [].concat([ | ||
options.project.root, | ||
DUMMY_APP_PATH, | ||
'router.js' | ||
]); | ||
|
||
return routerPathParts; | ||
} | ||
|
||
function writeRoute(action, name, options) { | ||
let routerPath = path.join.apply(null, findRouter(options)); | ||
let source = fs.readFileSync(routerPath, 'utf-8'); | ||
|
||
let routes = new EmberRouterGenerator(source); | ||
let newRoutes = routes[action](name, options); | ||
|
||
fs.writeFileSync(routerPath, newRoutes.code()); | ||
} | ||
|
||
function updateDocsTemplate(options) { | ||
let routeName = options.entity.name; | ||
let docsTemplatePath = options.pods | ||
? path.join(DUMMY_APP_PATH, 'pods', 'docs', 'template.hbs') | ||
: path.join(DUMMY_APP_PATH, 'templates', 'docs.hbs'); | ||
|
||
if (fs.existsSync(docsTemplatePath)) { | ||
let templateLines = fs | ||
.readFileSync(docsTemplatePath, 'utf-8') | ||
.toString() | ||
.split('\n'); | ||
|
||
let closingViewerNavTag = templateLines.find(line => | ||
line.includes('{{/viewer.nav}}') | ||
); | ||
|
||
templateLines.splice( | ||
templateLines.indexOf(closingViewerNavTag), | ||
0, | ||
`${''.padStart( | ||
closingViewerNavTag.search(/\S/) * 2, | ||
' ' | ||
)}{{nav.item "${dedasherize(routeName)}" "docs.${routeName}"}}` | ||
); | ||
|
||
fs.writeFileSync(docsTemplatePath, templateLines.join('\n')); | ||
|
||
this.ui.writeLine('updating docs.hbs'); | ||
this._writeStatusToUI(chalk.green, 'add nav item', 'docs.hbs'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters