Skip to content

Commit 460f890

Browse files
authored
Merge pull request #409 from pharindoko/feat/extend-cli-options
feat(cli): extend cli options to deploy stack without any prompt
2 parents 9e90de6 + 7b86652 commit 460f890

File tree

1 file changed

+183
-130
lines changed

1 file changed

+183
-130
lines changed

packages/cli/src/commands/create-stack.ts

Lines changed: 183 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ export class CreateStackCommand extends Command {
3737
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
3838
required: false, // make flag required (this is not common and you should probably use an argument instead)
3939
}),
40+
name: flags.string({
41+
char: 'n', // shorter flag version
42+
description: 'api name', // help description for flag
43+
hidden: false, // hide from help
44+
default: '', // default value if flag not passed (can be a function that returns a string or undefined)
45+
required: false, // make flag required (this is not common and you should probably use an argument instead)
46+
}),
47+
region: flags.string({
48+
char: 'i', // shorter flag version
49+
description: 'AWS region', // help description for flag
50+
hidden: false, // hide from help
51+
default: '', // default value if flag not passed (can be a function that returns a string or undefined)
52+
required: false, // make flag required (this is not common and you should probably use an argument instead)
53+
}),
54+
description: flags.string({
55+
char: 'd', // shorter flag version
56+
description: 'api description', // help description for flag
57+
hidden: false, // hide from help
58+
default: '', // default value if flag not passed (can be a function that returns a string or undefined)
59+
required: false, // make flag required (this is not common and you should probably use an argument instead)
60+
}),
61+
autoapprove: flags.boolean({
62+
char: 'y', // shorter flag version
63+
description: 'skip interactive approval before deployment', // help description for flag
64+
hidden: false, // hide from help
65+
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
66+
required: false, // make flag required (this is not common and you should probably use an argument instead)
67+
}),
4068
};
4169

4270
static args = [
@@ -73,25 +101,41 @@ export class CreateStackCommand extends Command {
73101
}
74102
cli.action.stop();
75103
this.log();
104+
let stackName: string | undefined;
76105

77-
const apiName = await inquirer.prompt({
78-
name: 'answer',
79-
message: `${chalk.magenta('What is the name of the api ?')}`,
80-
type: 'input',
81-
validate: Helpers.s3BucketValidator,
82-
});
83-
84-
const apiDesription = await inquirer.prompt({
85-
name: 'answer',
86-
message: `${chalk.magenta('What is this api used for ? (description)')}`,
87-
type: 'input',
88-
validate: Helpers.descriptionValidator,
89-
});
106+
if (flags.name) {
107+
stackName = flags.name;
108+
} else {
109+
const apiNameAnswer = await inquirer.prompt({
110+
name: 'answer',
111+
message: `${chalk.magenta('What is the name of the api ?')}`,
112+
type: 'input',
113+
validate: Helpers.s3BucketValidator,
114+
});
115+
stackName = apiNameAnswer.answer;
116+
}
117+
let stackDescription: string | undefined;
118+
if (flags.description) {
119+
stackDescription = flags.description;
120+
} else {
121+
const apiDesriptionAnswer = await inquirer.prompt({
122+
name: 'answer',
123+
message: `${chalk.magenta(
124+
'What is this api used for ? (description)'
125+
)}`,
126+
type: 'input',
127+
validate: Helpers.descriptionValidator,
128+
});
129+
stackDescription = apiDesriptionAnswer.answer;
130+
}
90131

91-
const stackName = apiName.answer;
92-
const stackDescription = apiDesription.answer;
93132
this.log();
94-
const region = await this.getRegion();
133+
let region: string | undefined;
134+
if (flags.region) {
135+
region = flags.region;
136+
} else {
137+
region = await this.getRegion();
138+
}
95139
let filePath = path.normalize(args.file);
96140
const templateFolder = path.normalize(
97141
this.config.root + '/node_modules/json-serverless-template/'
@@ -103,134 +147,143 @@ export class CreateStackCommand extends Command {
103147
`${chalk.blueBright.bold.underline(stackFolder)}`
104148
);
105149
this.log();
106-
await cli.confirm(`${chalk.magenta('Continue ? y/n')}`);
107-
this.log();
108-
const tasks = new Listr([
109-
{
110-
title: 'Validate Files',
111-
task: async (task) => {
112-
filePath = Helpers.validateFile(filePath);
150+
let confirm = true;
151+
if (!flags.autoapprove) {
152+
confirm = await cli.confirm(`${chalk.magenta('Continue ? y/n')}`);
153+
}
154+
155+
if (confirm) {
156+
this.log();
157+
const tasks = new Listr([
158+
{
159+
title: 'Validate Files',
160+
task: async (task) => {
161+
filePath = Helpers.validateFile(filePath);
162+
},
113163
},
114-
},
115-
{
116-
title: 'Validate StackFolder',
117-
task: (task) => {
118-
Helpers.validateStackFolder(stackFolder);
164+
{
165+
title: 'Validate StackFolder',
166+
task: (task) => {
167+
Helpers.validateStackFolder(stackFolder);
168+
},
119169
},
120-
},
121-
{
122-
title: 'Copy Template Files',
123-
task: async (task) => {
124-
await fs.copy(templateFolder, stackFolder, {
125-
dereference: true,
126-
recursive: true,
127-
overwrite: true,
128-
});
170+
{
171+
title: 'Copy Template Files',
172+
task: async (task) => {
173+
await fs.copy(templateFolder, stackFolder, {
174+
dereference: true,
175+
recursive: true,
176+
overwrite: true,
177+
});
178+
},
129179
},
130-
},
131-
{
132-
title: 'Create Appconfig',
133-
task: (ctx, task) => {
134-
const appconfig = new AppConfig();
135-
appconfig.jsonFile = filePath;
136-
appconfig.enableApiKeyAuth = flags.apikeyauth;
137-
appconfig.readOnly = flags.readonly;
138-
appconfig.enableSwagger = flags.swagger;
139-
appconfig.stackName = stackName;
140-
Helpers.createDir(stackFolder + '/config');
141-
fs.writeFileSync(
142-
path.normalize(stackFolder + '/config/appconfig.json'),
143-
JSON.stringify(appconfig, null, 2),
144-
'utf-8'
145-
);
180+
{
181+
title: 'Create Appconfig',
182+
task: (ctx, task) => {
183+
const appconfig = new AppConfig();
184+
appconfig.jsonFile = filePath;
185+
appconfig.enableApiKeyAuth = flags.apikeyauth;
186+
appconfig.readOnly = flags.readonly;
187+
appconfig.enableSwagger = flags.swagger;
188+
appconfig.stackName = stackName!;
189+
Helpers.createDir(stackFolder + '/config');
190+
fs.writeFileSync(
191+
path.normalize(stackFolder + '/config/appconfig.json'),
192+
JSON.stringify(appconfig, null, 2),
193+
'utf-8'
194+
);
195+
},
146196
},
147-
},
148-
{
149-
title: 'Create ServerlessConfig',
150-
task: (ctx, task) => {
151-
const serverlessConfig = new ServerlessConfig();
152-
serverlessConfig.awsRegion = region;
153-
serverlessConfig.stage = args.stage;
154-
Helpers.createDir(stackFolder + '/config');
155-
fs.writeFileSync(
156-
path.normalize(stackFolder + '/config/serverlessconfig.json'),
157-
JSON.stringify(serverlessConfig, null, 2),
158-
'utf-8'
159-
);
197+
{
198+
title: 'Create ServerlessConfig',
199+
task: (ctx, task) => {
200+
const serverlessConfig = new ServerlessConfig();
201+
serverlessConfig.awsRegion = region;
202+
serverlessConfig.stage = args.stage;
203+
Helpers.createDir(stackFolder + '/config');
204+
fs.writeFileSync(
205+
path.normalize(stackFolder + '/config/serverlessconfig.json'),
206+
JSON.stringify(serverlessConfig, null, 2),
207+
'utf-8'
208+
);
209+
},
160210
},
161-
},
162-
{
163-
title: 'Install Dependencies',
164-
task: async (task) => {
165-
if (process.env.NODE_ENV != 'local') {
166-
task.output = 'INSTALL DEPENDENCIES';
167-
Helpers.removeDir(stackFolder + '/node_modules');
211+
{
212+
title: 'Install Dependencies',
213+
task: async (task) => {
214+
if (process.env.NODE_ENV != 'local') {
215+
task.output = 'INSTALL DEPENDENCIES';
216+
Helpers.removeDir(stackFolder + '/node_modules');
217+
await Helpers.executeChildProcess(
218+
'npm i',
219+
{
220+
cwd: stackFolder,
221+
},
222+
false
223+
);
224+
}
225+
},
226+
},
227+
{
228+
title: 'Update Package.json',
229+
task: async (task) => {
230+
task.output = 'UPDATE PACKAGE.JSON';
231+
Helpers.updatePackageJson(
232+
stackFolder,
233+
stackName!,
234+
stackDescription!
235+
);
236+
},
237+
},
238+
{
239+
title: 'Build Code',
240+
task: async () => {
168241
await Helpers.executeChildProcess(
169-
'npm i',
242+
'npm run build',
170243
{
171244
cwd: stackFolder,
172245
},
173246
false
174247
);
175-
}
248+
},
176249
},
177-
},
178-
{
179-
title: 'Update Package.json',
180-
task: async (task) => {
181-
task.output = 'UPDATE PACKAGE.JSON';
182-
Helpers.updatePackageJson(stackFolder, stackName, stackDescription);
183-
},
184-
},
185-
{
186-
title: 'Build Code',
187-
task: async () => {
188-
await Helpers.executeChildProcess(
189-
'npm run build',
190-
{
191-
cwd: stackFolder,
192-
},
193-
false
194-
);
195-
},
196-
},
197-
{
198-
title: 'Deploy Stack on AWS',
199-
task: async () => {
200-
await Helpers.executeChildProcess(
201-
'node_modules/serverless/bin/serverless deploy',
202-
{
203-
cwd: stackFolder,
204-
},
205-
false
206-
);
250+
{
251+
title: 'Deploy Stack on AWS',
252+
task: async () => {
253+
await Helpers.executeChildProcess(
254+
'node_modules/serverless/bin/serverless deploy',
255+
{
256+
cwd: stackFolder,
257+
},
258+
false
259+
);
260+
},
207261
},
208-
},
209-
]);
210-
let slsinfo = '';
211-
try {
212-
await tasks.run();
213-
slsinfo = await Helpers.executeChildProcess2(
214-
'node_modules/serverless/bin/serverless info',
215-
{ cwd: stackFolder }
216-
);
217-
} catch (error) {
218-
this.error(`${chalk.red(error.message)}`);
219-
}
220-
try {
221-
const appConfig = JSON.parse(
222-
fs.readFileSync(stackFolder + '/config/appconfig.json', 'UTF-8')
223-
) as AppConfig;
224-
225-
Helpers.createCLIOutput(
226-
slsinfo,
227-
appConfig.enableApiKeyAuth,
228-
appConfig.enableSwagger
229-
);
262+
]);
263+
let slsinfo = '';
264+
try {
265+
await tasks.run();
266+
slsinfo = await Helpers.executeChildProcess2(
267+
'node_modules/serverless/bin/serverless info',
268+
{ cwd: stackFolder }
269+
);
270+
} catch (error) {
271+
this.error(`${chalk.red(error.message)}`);
272+
}
273+
try {
274+
const appConfig = JSON.parse(
275+
fs.readFileSync(stackFolder + '/config/appconfig.json', 'UTF-8')
276+
) as AppConfig;
230277

231-
} catch (error) {
232-
this.log(`${chalk.red(error.message)}`);
233-
this.log(slsinfo);
278+
Helpers.createCLIOutput(
279+
slsinfo,
280+
appConfig.enableApiKeyAuth,
281+
appConfig.enableSwagger
282+
);
283+
} catch (error) {
284+
this.log(`${chalk.red(error.message)}`);
285+
this.log(slsinfo);
286+
}
234287
}
235288
}
236289

0 commit comments

Comments
 (0)