Skip to content

Commit b3dadf8

Browse files
committed
build: update auto-doc to support generate command
1 parent ceeb292 commit b3dadf8

File tree

1 file changed

+108
-58
lines changed

1 file changed

+108
-58
lines changed

tools/publish/src/generate-docs.ts

Lines changed: 108 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { logging, tags } from '@angular-devkit/core';
3+
import { logging, strings, tags } from '@angular-devkit/core';
4+
import {
5+
getEngineHost,
6+
} from '../../../packages/@angular/cli/utilities/schematics';
47

58
export default async function () {
69
const commandsPath = __dirname + '/../../../packages/@angular/cli/commands';
@@ -17,70 +20,117 @@ export default async function () {
1720
continue;
1821
}
1922

20-
try {
21-
await command.initialize({});
22-
} catch (e) {
23-
console.log(`initialize failed [${commandFile}]: ` + e.toString());
24-
}
23+
generateDoc(command, commandFile);
24+
25+
if (command.name === 'generate') {
26+
const host = getEngineHost();
27+
const collection = host.createCollectionDescription('@schematics/angular');
2528

26-
let optionText;
27-
if (!command.options) {
28-
optionText = '';
29-
} else {
30-
optionText = (command.options as any[])
31-
.filter(option => !option.hidden)
32-
.map(option => {
33-
let defaultText = '';
34-
if (option.default) {
35-
defaultText = `<em>default value: ${option.default}</em>`;
36-
}
37-
let aliasText = '';
38-
if (option.aliases && option.aliases.length > 0) {
39-
aliasText = (option.aliases as string[])
40-
.map(alias => '<code>' + (alias.length === 1 ? '-' : '--') + alias + '</code>')
41-
.join(',');
42-
aliasText = ` (alias: ${aliasText})`;
43-
}
44-
45-
return tags.stripIndent`
46-
<details>
47-
<summary>${option.name}</summary>
48-
<p>
49-
<code>--${option.name}</code>${aliasText} ${defaultText}
50-
</p>
51-
<p>
52-
${option.description}
53-
</p>
54-
</details>
55-
`;
56-
}).join('\n');
29+
for (const schematicName in collection.schematics) {
30+
const schematic = collection.schematics[schematicName];
31+
if (schematic.hidden || schematic.private) {
32+
continue;
33+
}
34+
const generateCommand = new commandConstructor(
35+
{ project: { root: path.join(__dirname, '../fake_root/') } },
36+
new logging.NullLogger(),
37+
);
38+
generateDoc(
39+
generateCommand,
40+
commandFile,
41+
{ _: [`${collection.name}:${schematicName}`] },
42+
{
43+
name: strings.dasherize(schematicName),
44+
description: schematic.description,
45+
postfix: '-' + schematicName,
46+
},
47+
);
48+
}
5749
}
50+
}
51+
}
52+
53+
interface DocInfo {
54+
name: string;
55+
description: string;
56+
postfix: string;
57+
}
58+
async function generateDoc(
59+
command: any,
60+
commandFile: string,
61+
options: any = {},
62+
info?: Partial<DocInfo>,
63+
) {
64+
const docInfo = {
65+
name: command.name,
66+
description: command.description,
67+
postfix: '',
68+
...info,
69+
};
5870

59-
const docFile = path.join(
60-
__dirname,
61-
'../../../docs/documentation/',
62-
path.basename(commandFile, '.ts') + '.md');
71+
try {
72+
await command.initialize(options);
73+
} catch (e) {
74+
console.log(`initialize failed [${commandFile}]: ` + e);
75+
}
6376

64-
let docText;
65-
if (fs.existsSync(docFile)) {
66-
docText = fs.readFileSync(docFile, 'utf8');
67-
docText = docText.slice(0, docText.indexOf('## Options') + 10);
68-
} else {
69-
// tslint:disable:max-line-length
70-
docText = tags.stripIndent`
71-
<!-- Links in /docs/documentation should NOT have \`.md\` at the end, because they end up in our wiki at release. -->
77+
let optionText;
78+
if (!command.options) {
79+
optionText = '';
80+
} else {
81+
optionText = (command.options as any[])
82+
.filter(option => !option.hidden)
83+
.map(option => {
84+
let defaultText = '';
85+
if (option.default) {
86+
defaultText = ` <em>default value: ${option.default}</em>`;
87+
}
88+
let aliasText = '';
89+
if (option.aliases && option.aliases.length > 0) {
90+
aliasText = (option.aliases as string[])
91+
.map(alias => '<code>' + (alias.length === 1 ? '-' : '--') + alias + '</code>')
92+
.join(',');
93+
aliasText = ` (alias: ${aliasText})`;
94+
}
7295

73-
# ng ${command.name}
96+
return tags.stripIndent`
97+
<details>
98+
<summary>${option.name}</summary>
99+
<p>
100+
<code>--${option.name}</code>${aliasText}${defaultText}
101+
</p>
102+
<p>
103+
${option.description}
104+
</p>
105+
</details>
106+
`;
107+
}).join('\n');
108+
}
74109

75-
## Overview
76-
${command.description}
110+
const docFile = path.join(
111+
__dirname,
112+
'../../../docs/documentation/',
113+
path.basename(commandFile, '.ts') + docInfo.postfix + '.md');
77114

78-
## Options
79-
`;
80-
// tslint:enable:max-line-length
81-
}
115+
let docText;
116+
if (fs.existsSync(docFile)) {
117+
docText = fs.readFileSync(docFile, 'utf8');
118+
docText = docText.slice(0, docText.indexOf('## Options') + 10);
119+
} else {
120+
// tslint:disable:max-line-length
121+
docText = tags.stripIndent`
122+
<!-- Links in /docs/documentation should NOT have \`.md\` at the end, because they end up in our wiki at release. -->
82123
83-
const finalText = docText + '\n' + (optionText ? optionText : 'None.') + '\n';
84-
fs.writeFileSync(docFile, finalText);
124+
# ng ${docInfo.name}
125+
126+
## Overview
127+
${docInfo.description}
128+
129+
## Options
130+
`;
131+
// tslint:enable:max-line-length
85132
}
133+
134+
const finalText = docText + '\n' + (optionText ? optionText : 'None.') + '\n';
135+
fs.writeFileSync(docFile, finalText);
86136
}

0 commit comments

Comments
 (0)