forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.js
More file actions
executable file
·265 lines (203 loc) · 6.44 KB
/
docs.js
File metadata and controls
executable file
·265 lines (203 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env node
const path = require('path');
const ionicPkg = require('../packages/ionic');
const utilsPkg = require('../packages/cli-utils');
const stripAnsi = utilsPkg.load('strip-ansi');
async function run() {
const env = await ionicPkg.generateIonicEnvironment(process.argv.slice(2), process.env);
const mPath = path.resolve(__dirname, '..', 'packages');
const ionicModules = (await utilsPkg.readDir(mPath))
.filter(m => m.startsWith('cli-plugin-'))
.map(m => require(path.resolve(mPath, m)));
for (let mod of ionicModules) {
utilsPkg.installPlugin(env, mod);
}
const nsPath = path.resolve(__dirname, '..', 'docs', 'index.md');
const nsDoc = formatIonicPage(env.namespace);
await utilsPkg.fsMkdirp(path.dirname(nsPath));
await utilsPkg.fsWriteFile(nsPath, nsDoc, { encoding: 'utf8' });
const commands = env.namespace.getCommandMetadataList().filter(cmd => cmd.visible !== false);
const commandPromises = commands.map(async (cmd) => {
if (!cmd.fullName) {
console.error(`${cmd.name} has no fullName`);
return;
}
const cmdPath = path.resolve(__dirname, '..', 'docs', ...cmd.fullName.split(' '), 'index.md');
const cmdDoc = formatCommandDoc(cmd);
await utilsPkg.fsMkdirp(path.dirname(cmdPath));
await utilsPkg.fsWriteFile(cmdPath, cmdDoc, { encoding: 'utf8' });
});
await Promise.all(commandPromises);
await copyToIonicSite(commands);
env.close();
}
run().then(() => console.log('done!')).catch((err) => console.error(err));
function formatIonicPage(ns) {
let headerLine = formatNamespaceHeader(ns);
function listCommandLink(cmdData) {
if (!cmdData.fullName) {
console.error(`${cmdData.name} has no fullName`);
return;
}
return `[${cmdData.fullName}](${path.join(...cmdData.fullName.split(' '))}) | ${stripAnsi(cmdData.description)}`;
}
const commands = ns.getCommandMetadataList();
return `${headerLine}
The Ionic CLI is your go-to tool for developing Ionic apps. You can follow CLI
development on [Github](https://github.com/driftyco/ionic-cli).
## Installation
Please make sure
[Node](https://ionicframework.com/docs/resources/what-is/#node) 6+ and
[NPM](https://ionicframework.com/docs/resources/what-is/#npm) 3+ are
installed.
Then, install the CLI globally (you may need sudo):
\`\`\`bash
$ npm install -g ionic@latest
\`\`\`
## Getting Started
Start a new Ionic project using \`ionic start\`:
\`\`\`bash
ionic start myNewProject tabs
cd ./myNewProject
\`\`\`
This will create a new app named \`myNewProject\`. Once you \`cd\` into your
project's directory, a few new commands become available to you, such as
\`serve\`:
\`\`\`bash
ionic serve
\`\`\`
## Commands
Here is a full list of Ionic commands. You can also see the list on the command
line with \`ionic --help\`.
Command | Description
------- | -----------
${commands.filter(cmd => cmd.visible !== false).map(listCommandLink).join(`
`)}
`;
}
function formatNamespaceHeader(ns) {
return `---
layout: fluid/docs_base
category: cli
id: cli-intro
title: Ionic CLI Documentation
---
# ${ns.name}
`;
}
function formatCommandHeader(cmd) {
if (!cmd.fullName) {
console.error(`${cmd.name} has no fullName`);
return;
}
return `---
layout: fluid/docs_base
category: cli
id: cli-${cmd.fullName.split(' ').join('-')}
command_name: ${cmd.fullName}
title: ${cmd.fullName}
header_sub_title: Ionic CLI
---
# \`$ ionic ${cmd.fullName}\`
`;
}
function formatCommandDoc(cmdMetadata) {
let description = stripAnsi(cmdMetadata.description).split('\n').join('\n ');
return formatCommandHeader(cmdMetadata) +
formatName(cmdMetadata.fullName || '', description) +
formatSynopsis(cmdMetadata.inputs, cmdMetadata.fullName) +
formatDescription(cmdMetadata.inputs, cmdMetadata.options, description) +
formatExamples(cmdMetadata.exampleCommands, cmdMetadata.fullName);
}
function formatName(fullName, description) {
return description;
}
function formatSynopsis(inputs, commandName) {
const headerLine = `## Synopsis`;
const usageLine =
`${commandName} ${
(inputs || [])
.map(input => {
// TODO: handle required arguments (there are none currently)
// if (input.required) {
// return '<' + input.name + '>';
// }
return '[' + input.name + ']';
})
.join(' ')}`;
return `
${headerLine}
\`\`\`bash
$ ionic ${usageLine}
\`\`\`
`;
}
function formatDescription(inputs = [], options = [], description = '') {
const headerLine = `## Details`;
function inputLineFn(input, index) {
const name = input.name;
const description = stripAnsi(input.description);
const optionList = `\`${name}\``;
return `${optionList} | ${description}`;
}
function optionLineFn(option) {
const name = option.name;
const aliases = option.aliases;
const description = stripAnsi(option.description);
const optionList = `\`--${name}\`` +
(aliases && aliases.length > 0 ? ', ' +
aliases
.map((alias) => `\`-${alias}\``)
.join(', ') : '');
return `${optionList} | ${description}`;
}
return `
${headerLine}
${inputs.length > 0 ? `
Input | Description
----- | ----------` : ``}
${inputs.map(inputLineFn).join(`
`)}
${options.length > 0 ? `
Option | Description
------ | ----------` : ``}
${options.map(optionLineFn).join(`
`)}
`;
}
function formatExamples(exampleCommands, commandName) {
if (!Array.isArray(exampleCommands)) {
return '';
}
const headerLine = `## Examples`;
const exampleLines = exampleCommands.map(cmd => `$ ionic ${commandName} ${cmd}`);
return `
${headerLine}
\`\`\`bash
${exampleLines.join('\n')}
\`\`\`
`;
}
async function copyToIonicSite(commands) {
const ionicSitePath = path.resolve(__dirname, '..', '..', 'ionic-site');
let dirData = await utilsPkg.fsStat(ionicSitePath);
if (!dirData.size) {
// ionic-site not present, fail silently
return;
}
// get a list of commands for the nav
await utilsPkg.fsWriteFile(
path.resolve(ionicSitePath, 'content', '_data', 'cliData.json'),
JSON.stringify(
commands.map((command) => {
return {
id: `cli-${command.fullName.split(' ').join('-')}`,
name: command.fullName,
url: command.fullName.split(' ').join('/')
};
}).sort((a, b) => a.name.localeCompare(b.name))
), { encoding: 'utf8' });
return utilsPkg.copyDirectory(
path.resolve(__dirname, '..', 'docs'),
path.resolve(ionicSitePath, 'content', 'docs', 'cli'));
}