Skip to content

Commit

Permalink
feat(cli): extend cli options to deploy stack without any prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuss Florian (uid10804) committed Jun 2, 2020
1 parent 9e90de6 commit 7b86652
Showing 1 changed file with 183 additions and 130 deletions.
313 changes: 183 additions & 130 deletions packages/cli/src/commands/create-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ export class CreateStackCommand extends Command {
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
name: flags.string({
char: 'n', // shorter flag version
description: 'api name', // help description for flag
hidden: false, // hide from help
default: '', // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
region: flags.string({
char: 'i', // shorter flag version
description: 'AWS region', // help description for flag
hidden: false, // hide from help
default: '', // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
description: flags.string({
char: 'd', // shorter flag version
description: 'api description', // help description for flag
hidden: false, // hide from help
default: '', // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
autoapprove: flags.boolean({
char: 'y', // shorter flag version
description: 'skip interactive approval before deployment', // help description for flag
hidden: false, // hide from help
default: false, // default value if flag not passed (can be a function that returns a string or undefined)
required: false, // make flag required (this is not common and you should probably use an argument instead)
}),
};

static args = [
Expand Down Expand Up @@ -73,25 +101,41 @@ export class CreateStackCommand extends Command {
}
cli.action.stop();
this.log();
let stackName: string | undefined;

const apiName = await inquirer.prompt({
name: 'answer',
message: `${chalk.magenta('What is the name of the api ?')}`,
type: 'input',
validate: Helpers.s3BucketValidator,
});

const apiDesription = await inquirer.prompt({
name: 'answer',
message: `${chalk.magenta('What is this api used for ? (description)')}`,
type: 'input',
validate: Helpers.descriptionValidator,
});
if (flags.name) {
stackName = flags.name;
} else {
const apiNameAnswer = await inquirer.prompt({
name: 'answer',
message: `${chalk.magenta('What is the name of the api ?')}`,
type: 'input',
validate: Helpers.s3BucketValidator,
});
stackName = apiNameAnswer.answer;
}
let stackDescription: string | undefined;
if (flags.description) {
stackDescription = flags.description;
} else {
const apiDesriptionAnswer = await inquirer.prompt({
name: 'answer',
message: `${chalk.magenta(
'What is this api used for ? (description)'
)}`,
type: 'input',
validate: Helpers.descriptionValidator,
});
stackDescription = apiDesriptionAnswer.answer;
}

const stackName = apiName.answer;
const stackDescription = apiDesription.answer;
this.log();
const region = await this.getRegion();
let region: string | undefined;
if (flags.region) {
region = flags.region;
} else {
region = await this.getRegion();
}
let filePath = path.normalize(args.file);
const templateFolder = path.normalize(
this.config.root + '/node_modules/json-serverless-template/'
Expand All @@ -103,134 +147,143 @@ export class CreateStackCommand extends Command {
`${chalk.blueBright.bold.underline(stackFolder)}`
);
this.log();
await cli.confirm(`${chalk.magenta('Continue ? y/n')}`);
this.log();
const tasks = new Listr([
{
title: 'Validate Files',
task: async (task) => {
filePath = Helpers.validateFile(filePath);
let confirm = true;
if (!flags.autoapprove) {
confirm = await cli.confirm(`${chalk.magenta('Continue ? y/n')}`);
}

if (confirm) {
this.log();
const tasks = new Listr([
{
title: 'Validate Files',
task: async (task) => {
filePath = Helpers.validateFile(filePath);
},
},
},
{
title: 'Validate StackFolder',
task: (task) => {
Helpers.validateStackFolder(stackFolder);
{
title: 'Validate StackFolder',
task: (task) => {
Helpers.validateStackFolder(stackFolder);
},
},
},
{
title: 'Copy Template Files',
task: async (task) => {
await fs.copy(templateFolder, stackFolder, {
dereference: true,
recursive: true,
overwrite: true,
});
{
title: 'Copy Template Files',
task: async (task) => {
await fs.copy(templateFolder, stackFolder, {
dereference: true,
recursive: true,
overwrite: true,
});
},
},
},
{
title: 'Create Appconfig',
task: (ctx, task) => {
const appconfig = new AppConfig();
appconfig.jsonFile = filePath;
appconfig.enableApiKeyAuth = flags.apikeyauth;
appconfig.readOnly = flags.readonly;
appconfig.enableSwagger = flags.swagger;
appconfig.stackName = stackName;
Helpers.createDir(stackFolder + '/config');
fs.writeFileSync(
path.normalize(stackFolder + '/config/appconfig.json'),
JSON.stringify(appconfig, null, 2),
'utf-8'
);
{
title: 'Create Appconfig',
task: (ctx, task) => {
const appconfig = new AppConfig();
appconfig.jsonFile = filePath;
appconfig.enableApiKeyAuth = flags.apikeyauth;
appconfig.readOnly = flags.readonly;
appconfig.enableSwagger = flags.swagger;
appconfig.stackName = stackName!;
Helpers.createDir(stackFolder + '/config');
fs.writeFileSync(
path.normalize(stackFolder + '/config/appconfig.json'),
JSON.stringify(appconfig, null, 2),
'utf-8'
);
},
},
},
{
title: 'Create ServerlessConfig',
task: (ctx, task) => {
const serverlessConfig = new ServerlessConfig();
serverlessConfig.awsRegion = region;
serverlessConfig.stage = args.stage;
Helpers.createDir(stackFolder + '/config');
fs.writeFileSync(
path.normalize(stackFolder + '/config/serverlessconfig.json'),
JSON.stringify(serverlessConfig, null, 2),
'utf-8'
);
{
title: 'Create ServerlessConfig',
task: (ctx, task) => {
const serverlessConfig = new ServerlessConfig();
serverlessConfig.awsRegion = region;
serverlessConfig.stage = args.stage;
Helpers.createDir(stackFolder + '/config');
fs.writeFileSync(
path.normalize(stackFolder + '/config/serverlessconfig.json'),
JSON.stringify(serverlessConfig, null, 2),
'utf-8'
);
},
},
},
{
title: 'Install Dependencies',
task: async (task) => {
if (process.env.NODE_ENV != 'local') {
task.output = 'INSTALL DEPENDENCIES';
Helpers.removeDir(stackFolder + '/node_modules');
{
title: 'Install Dependencies',
task: async (task) => {
if (process.env.NODE_ENV != 'local') {
task.output = 'INSTALL DEPENDENCIES';
Helpers.removeDir(stackFolder + '/node_modules');
await Helpers.executeChildProcess(
'npm i',
{
cwd: stackFolder,
},
false
);
}
},
},
{
title: 'Update Package.json',
task: async (task) => {
task.output = 'UPDATE PACKAGE.JSON';
Helpers.updatePackageJson(
stackFolder,
stackName!,
stackDescription!
);
},
},
{
title: 'Build Code',
task: async () => {
await Helpers.executeChildProcess(
'npm i',
'npm run build',
{
cwd: stackFolder,
},
false
);
}
},
},
},
{
title: 'Update Package.json',
task: async (task) => {
task.output = 'UPDATE PACKAGE.JSON';
Helpers.updatePackageJson(stackFolder, stackName, stackDescription);
},
},
{
title: 'Build Code',
task: async () => {
await Helpers.executeChildProcess(
'npm run build',
{
cwd: stackFolder,
},
false
);
},
},
{
title: 'Deploy Stack on AWS',
task: async () => {
await Helpers.executeChildProcess(
'node_modules/serverless/bin/serverless deploy',
{
cwd: stackFolder,
},
false
);
{
title: 'Deploy Stack on AWS',
task: async () => {
await Helpers.executeChildProcess(
'node_modules/serverless/bin/serverless deploy',
{
cwd: stackFolder,
},
false
);
},
},
},
]);
let slsinfo = '';
try {
await tasks.run();
slsinfo = await Helpers.executeChildProcess2(
'node_modules/serverless/bin/serverless info',
{ cwd: stackFolder }
);
} catch (error) {
this.error(`${chalk.red(error.message)}`);
}
try {
const appConfig = JSON.parse(
fs.readFileSync(stackFolder + '/config/appconfig.json', 'UTF-8')
) as AppConfig;

Helpers.createCLIOutput(
slsinfo,
appConfig.enableApiKeyAuth,
appConfig.enableSwagger
);
]);
let slsinfo = '';
try {
await tasks.run();
slsinfo = await Helpers.executeChildProcess2(
'node_modules/serverless/bin/serverless info',
{ cwd: stackFolder }
);
} catch (error) {
this.error(`${chalk.red(error.message)}`);
}
try {
const appConfig = JSON.parse(
fs.readFileSync(stackFolder + '/config/appconfig.json', 'UTF-8')
) as AppConfig;

} catch (error) {
this.log(`${chalk.red(error.message)}`);
this.log(slsinfo);
Helpers.createCLIOutput(
slsinfo,
appConfig.enableApiKeyAuth,
appConfig.enableSwagger
);
} catch (error) {
this.log(`${chalk.red(error.message)}`);
this.log(slsinfo);
}
}
}

Expand Down

0 comments on commit 7b86652

Please sign in to comment.