Skip to content

Commit b7224c3

Browse files
scalvertsamselikoff
authored andcommitted
Adding docs-page blueprint. (ember-learn#343)
* 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
1 parent ccfc0d5 commit b7224c3

File tree

4 files changed

+160
-4
lines changed

4 files changed

+160
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# <%= templateName %>
2+
3+
<%= templateName %> content

blueprints/docs-page/index.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"broccoli-plugin": "^1.3.1",
3737
"broccoli-source": "^1.1.0",
3838
"broccoli-stew": "^2.0.0",
39+
"chalk": "^2.4.2",
3940
"ember-auto-import": "^1.2.19",
4041
"ember-cli-autoprefixer": "^0.8.1",
4142
"ember-cli-babel": "^6.16.0",
@@ -44,6 +45,7 @@
4445
"ember-cli-htmlbars-inline-precompile": "^1.0.3",
4546
"ember-cli-sass": "10.0.0",
4647
"ember-cli-string-helpers": "^1.9.0",
48+
"ember-cli-string-utils": "^1.1.0",
4749
"ember-cli-tailwind": "^0.6.2",
4850
"ember-code-snippet": "^2.4.0",
4951
"ember-component-css": "^0.6.7",
@@ -55,6 +57,7 @@
5557
"ember-keyboard": "^4.0.0",
5658
"ember-modal-dialog": "3.0.0-beta.3",
5759
"ember-responsive": "^3.0.0-beta.1",
60+
"ember-router-generator": "^1.2.3",
5861
"ember-router-scroll": "^1.0.0",
5962
"ember-svg-jar": "^1.2.2",
6063
"ember-tether": "^1.0.0-beta.2",

tests/dummy/app/pods/docs/quickstart/template.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,39 @@ all docs pages in your site.
6565
documentation for your addon and live in the folder
6666
`tests/dummy/app/templates/docs`. Since Addon Docs supports Markdown out
6767
of the box we will create two `.md` files (one for your docs `index` and one
68-
for the `usage` page).
68+
for the `usage` page). Addon Docs includes a `docs-page` **blueprint** to make
69+
adding docs routes easier. The blueprint will generate:
70+
71+
- the **markdown file** in the `tests/dummy/app/templates/docs` directory
72+
- the **nav item entry** in `tests/dummy/app/templates/docs.md` _if it exists_
73+
- the **`route` entry** in `tests/dummy/app/router.js` _for non-`index` routes_
74+
75+
Generate an `index` route using the following:
76+
77+
```bash
78+
ember generate docs-page index
79+
```
80+
81+
This will generate the following markdown file.
6982

7083
{{#docs-snippet name='quickstart-markdown-index.md' title='tests/dummy/app/templates/docs/index.md' language='markdown'}}
71-
# Introduction
84+
# Index
7285

73-
This is my new addon, and it rocks!
86+
Index content
7487
{{/docs-snippet}}
7588

89+
Generate a `usage` route using the same blueprint as above.
90+
91+
```bash
92+
ember generate docs-page usage
93+
```
94+
95+
This will generate and modify the files for your `usage` docs page.
96+
7697
{{#docs-snippet name='quickstart-markdown-subpage.md' title='tests/dummy/app/templates/docs/usage.md' language='markdown'}}
7798
# Usage
7899

79-
So easy to use, sweet!
100+
Usage content
80101
{{/docs-snippet}}
81102

82103
6. **Create your marketing homepage**. Addon Docs comes with a set of

0 commit comments

Comments
 (0)