/
cli.js
230 lines (184 loc) 路 5.43 KB
/
cli.js
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
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const chalk = require('chalk');
const prompt = inquirer.createPromptModule();
const yaml = require('js-yaml');
const slugify = require('slugify');
const { execSync } = require('child_process');
const swaggerRepo = require('swagger-repo');
const {
copy,
copyDirSync,
render,
getGhPagesBaseUrl,
validateSpecFileName,
getCurrentGitHubRepo
} = require('./lib/utils');
const { installDeps } = require('./lib/install-deps');
const REDOCLY_RC = '.redoclyrc';
async function ask() {
console.log('Welcome to the ' + chalk.green('OpenAPI-Repo') + ' generator!');
const { haveSpec } = await prompt({
type: 'confirm',
name: 'haveSpec',
message: 'Do you already have OpenAPI/Swagger spec for your API?',
default: false
});
let specFileName;
if (haveSpec) {
specFileName = (await prompt({
type: 'input',
name: 'specFileName',
message: 'Please specify the path to the OpenAPI spec (local file):',
validate(fileName) {
return validateSpecFileName(fileName);
}
})).specFileName;
}
let spec;
if (haveSpec) {
spec = yaml.safeLoad(fs.readFileSync(specFileName, 'utf8'));
}
const { apiTitle } = await prompt({
type: 'input',
name: 'apiTitle',
message: 'API Name:',
default: haveSpec ? spec.title : undefined,
validate: i => (i.length > 0 ? true : `API Name can't be empty`)
});
const { splitSpec } = await prompt({
type: 'confirm',
name: 'splitSpec',
message: `Split spec into separate files: paths/*, definitions/* ${chalk.yellow(
'[Experimental]'
)}?`,
default: true
});
const { codeSamples } = await prompt({
type: 'confirm',
name: 'codeSamples',
message: `Prepare manual code samples folder?`,
default: true
});
const { swaggerUI } = await prompt({
type: 'confirm',
name: 'swaggerUI',
message: `Install SwaggerUI?`,
default: false
});
const { travis } = await prompt({
type: 'confirm',
name: 'travis',
message: `Set up Travis CI?`,
default: true
});
let repo;
if (travis) {
repo = (await prompt({
type: 'input',
name: 'repo',
message: `Specify name of GitHub repo in format ${chalk.blue('User/Repo')}:`,
default: getCurrentGitHubRepo,
validate: function(input) {
return input.indexOf('/') > 0 ? true : 'Repo Name must contain "/"';
}
})).repo;
}
return {
specFileName,
apiTitle,
splitSpec,
codeSamples,
swaggerUI,
travis,
repo
};
}
function printSuccess(opts, root) {
let travisNote = '';
if (opts.travis) {
travisNote = `We generated ${chalk.blue('.travis')} for you. Follow steps from ${chalk.blue(
'README.md'
)} to finish Travis CI setup`;
}
console.log(
`${chalk.green('Success!')} Created ${chalk.green(path.basename(root))} at ${chalk.blue(root)}
Inside that directory, you can run several commands:
${chalk.blue(`npm start`)}
Starts the development server.
${chalk.blue(`npm run build`)}
Bundles the spec and prepares ${chalk.blue('web_deploy')} folder with static assets.
${chalk.blue(`npm test`)}
Validates the spec.
${chalk.blue(`npm run gh-pages`)}
Deploys docs to GitHub Pages. You don't need to run it manually if you have Travis CI configured.
We suggest that you begin by typing:
${chalk.blue('cd')} ${path.basename(root)}
${chalk.blue('npm start')}` + (travisNote ? '\n\n' + travisNote : '')
);
}
async function run() {
const specRoot = process.argv[2];
if (!specRoot) {
console.log(`Please specify the spec directory:
${chalk.blue('create-openapi-repo')} <spec-directory>
For example:
${chalk.blue('create-openapi-repo')} my-spec`);
process.exit(1);
}
if (fs.existsSync(specRoot) && fs.existsSync(path.join(specRoot, REDOCLY_RC))) {
console.log(`The directory ${chalk.green(specRoot)} already contains ${chalk.green(REDOCLY_RC)}
Choose another directory or remove contents.
`);
process.exit(1);
}
if (!fs.existsSync(specRoot)) {
fs.mkdirSync(specRoot);
}
const opts = await ask();
const data = {
...opts,
packageName: slugify(opts.apiTitle).toLowerCase(),
ghPagesBaseUrl: opts.repo ? getGhPagesBaseUrl(opts.repo) : undefined
};
let { specFileName } = opts;
if (!specFileName) {
specFileName = require.resolve('openapi-template');
}
process.chdir(specRoot);
console.log(`\nCreating a new OpenAPI repo in ${chalk.blue(path.resolve('.'))}\n`);
await copy('.gitignore');
await copy('LICENSE');
await render('package.json', data);
await render('README.md', data);
await copy('spec/README.md');
if (opts.splitSpec) {
copyDirSync('spec/definitions');
copyDirSync('spec/paths');
}
if (opts.codeSamples) {
copyDirSync('spec/code_samples');
}
if (opts.travis) {
await copy('.travis.yml');
}
copyDirSync('web');
swaggerRepo.syncWithSpec(fs.readFileSync(specFileName).toString());
fs.writeFileSync(REDOCLY_RC, yaml.safeDump(opts, { skipInvalid: true }));
console.log('Installing packages. This might take a couple of minutes.\n');
await installDeps('@^2.0.0-rc.2');
console.log();
try {
execSync(`git init`, { stdio: 'inherit' });
execSync(`git add . && git commit -m "Initial commit from create-openapi-repo"`);
} catch (e) {
// skip error
}
printSuccess(opts, path.resolve('.'));
}
try {
run();
} catch (e) {
console.log(e);
}