|
| 1 | +/* eslint-env node */ |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | +const chalk = require('chalk'); |
| 5 | +const EmberRouterGenerator = require('ember-router-generator'); |
| 6 | +const stringUtil = require('ember-cli-string-utils'); |
| 7 | + |
| 8 | +const DUMMY_APP_PATH = path.join('tests', 'dummy', 'app'); |
| 9 | + |
| 10 | +function dedasherize(str) { |
| 11 | + let dedasherized = str.replace(/-/g, ' '); |
| 12 | + |
| 13 | + return stringUtil.capitalize(dedasherized); |
| 14 | +} |
| 15 | + |
| 16 | +module.exports = { |
| 17 | + name: 'docs-page', |
| 18 | + description: 'Generates an ember-cli-addon-docs doc page', |
| 19 | + |
| 20 | + fileMapTokens: function() { |
| 21 | + return { |
| 22 | + __templatepath__: function(options) { |
| 23 | + if (options.pod) { |
| 24 | + return path.join( |
| 25 | + DUMMY_APP_PATH, |
| 26 | + 'pods', |
| 27 | + 'docs', |
| 28 | + options.dasherizedModuleName |
| 29 | + ); |
| 30 | + } else { |
| 31 | + return path.join(DUMMY_APP_PATH, 'templates', 'docs'); |
| 32 | + } |
| 33 | + }, |
| 34 | + __templatename__: function(options) { |
| 35 | + if (options.pod) { |
| 36 | + return 'template'; |
| 37 | + } |
| 38 | + return options.dasherizedModuleName; |
| 39 | + } |
| 40 | + }; |
| 41 | + }, |
| 42 | + |
| 43 | + locals: function(options) { |
| 44 | + return { |
| 45 | + templateName: dedasherize(options.entity.name) |
| 46 | + }; |
| 47 | + }, |
| 48 | + |
| 49 | + afterInstall: function(options) { |
| 50 | + // eslint-disable-next-line no-debugger |
| 51 | + debugger; |
| 52 | + updateRouter.call(this, 'add', options); |
| 53 | + updateDocsTemplate.call(this, options); |
| 54 | + }, |
| 55 | + |
| 56 | + afterUninstall: function(options) { |
| 57 | + updateRouter.call(this, 'remove', options); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +function updateRouter(action, options) { |
| 62 | + let entity = options.entity; |
| 63 | + let actionColorMap = { |
| 64 | + add: 'green', |
| 65 | + remove: 'red' |
| 66 | + }; |
| 67 | + let color = actionColorMap[action] || 'gray'; |
| 68 | + |
| 69 | + if (entity.name === 'index') { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + writeRoute(action, entity.name, options); |
| 74 | + |
| 75 | + this.ui.writeLine('updating router'); |
| 76 | + this._writeStatusToUI(chalk[color], action + ' route', entity.name); |
| 77 | +} |
| 78 | + |
| 79 | +function findRouter(options) { |
| 80 | + let routerPathParts = [].concat([ |
| 81 | + options.project.root, |
| 82 | + DUMMY_APP_PATH, |
| 83 | + 'router.js' |
| 84 | + ]); |
| 85 | + |
| 86 | + return routerPathParts; |
| 87 | +} |
| 88 | + |
| 89 | +function writeRoute(action, name, options) { |
| 90 | + let routerPath = path.join.apply(null, findRouter(options)); |
| 91 | + let source = fs.readFileSync(routerPath, 'utf-8'); |
| 92 | + |
| 93 | + let routes = new EmberRouterGenerator(source); |
| 94 | + let newRoutes = routes[action](name, options); |
| 95 | + |
| 96 | + fs.writeFileSync(routerPath, newRoutes.code()); |
| 97 | +} |
| 98 | + |
| 99 | +function updateDocsTemplate(options) { |
| 100 | + let routeName = options.entity.name; |
| 101 | + let docsTemplatePath = options.pods |
| 102 | + ? path.join(DUMMY_APP_PATH, 'pods', 'docs', 'template.hbs') |
| 103 | + : path.join(DUMMY_APP_PATH, 'templates', 'docs.hbs'); |
| 104 | + |
| 105 | + if (fs.existsSync(docsTemplatePath)) { |
| 106 | + let templateLines = fs |
| 107 | + .readFileSync(docsTemplatePath, 'utf-8') |
| 108 | + .toString() |
| 109 | + .split('\n'); |
| 110 | + |
| 111 | + let closingViewerNavTag = templateLines.find(line => |
| 112 | + line.includes('{{/viewer.nav}}') |
| 113 | + ); |
| 114 | + |
| 115 | + templateLines.splice( |
| 116 | + templateLines.indexOf(closingViewerNavTag), |
| 117 | + 0, |
| 118 | + `${''.padStart( |
| 119 | + closingViewerNavTag.search(/\S/) * 2, |
| 120 | + ' ' |
| 121 | + )}{{nav.item "${dedasherize(routeName)}" "docs.${routeName}"}}` |
| 122 | + ); |
| 123 | + |
| 124 | + fs.writeFileSync(docsTemplatePath, templateLines.join('\n')); |
| 125 | + |
| 126 | + this.ui.writeLine('updating docs.hbs'); |
| 127 | + this._writeStatusToUI(chalk.green, 'add nav item', 'docs.hbs'); |
| 128 | + } |
| 129 | +} |
0 commit comments