Skip to content

Commit

Permalink
Adding docs-page blueprint. (#343)
Browse files Browse the repository at this point in the history
* 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
scalvert authored and samselikoff committed Apr 5, 2019
1 parent ccfc0d5 commit b7224c3
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# <%= templateName %>

<%= templateName %> content
129 changes: 129 additions & 0 deletions blueprints/docs-page/index.js
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');
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"broccoli-plugin": "^1.3.1",
"broccoli-source": "^1.1.0",
"broccoli-stew": "^2.0.0",
"chalk": "^2.4.2",
"ember-auto-import": "^1.2.19",
"ember-cli-autoprefixer": "^0.8.1",
"ember-cli-babel": "^6.16.0",
Expand All @@ -44,6 +45,7 @@
"ember-cli-htmlbars-inline-precompile": "^1.0.3",
"ember-cli-sass": "10.0.0",
"ember-cli-string-helpers": "^1.9.0",
"ember-cli-string-utils": "^1.1.0",
"ember-cli-tailwind": "^0.6.2",
"ember-code-snippet": "^2.4.0",
"ember-component-css": "^0.6.7",
Expand All @@ -55,6 +57,7 @@
"ember-keyboard": "^4.0.0",
"ember-modal-dialog": "3.0.0-beta.3",
"ember-responsive": "^3.0.0-beta.1",
"ember-router-generator": "^1.2.3",
"ember-router-scroll": "^1.0.0",
"ember-svg-jar": "^1.2.2",
"ember-tether": "^1.0.0-beta.2",
Expand Down
29 changes: 25 additions & 4 deletions tests/dummy/app/pods/docs/quickstart/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,39 @@ all docs pages in your site.
documentation for your addon and live in the folder
`tests/dummy/app/templates/docs`. Since Addon Docs supports Markdown out
of the box we will create two `.md` files (one for your docs `index` and one
for the `usage` page).
for the `usage` page). Addon Docs includes a `docs-page` **blueprint** to make
adding docs routes easier. The blueprint will generate:

- the **markdown file** in the `tests/dummy/app/templates/docs` directory
- the **nav item entry** in `tests/dummy/app/templates/docs.md` _if it exists_
- the **`route` entry** in `tests/dummy/app/router.js` _for non-`index` routes_

Generate an `index` route using the following:

```bash
ember generate docs-page index
```

This will generate the following markdown file.

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

This is my new addon, and it rocks!
Index content
{{/docs-snippet}}

Generate a `usage` route using the same blueprint as above.

```bash
ember generate docs-page usage
```

This will generate and modify the files for your `usage` docs page.

{{#docs-snippet name='quickstart-markdown-subpage.md' title='tests/dummy/app/templates/docs/usage.md' language='markdown'}}
# Usage

So easy to use, sweet!
Usage content
{{/docs-snippet}}

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

0 comments on commit b7224c3

Please sign in to comment.