UNPKG

4.29 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5require('./readme');
6const fs = require('fs');
7const path = require('path');
8const colors = require('colors/safe');
9const yaml = require('js-yaml');
10const prompt = require('prompt');
11const program = require('commander');
12const kes = require('../index');
13const pckg = require('../package.json');
14
15const baseDir = process.cwd();
16const kesFolder = path.join(baseDir, '.kes');
17const distFolder = path.join(baseDir, 'dist');
18
19program.version(pckg.version);
20
21function extractCommanderOptions(program) {
22 const options = {};
23 Object.keys(program).forEach(property => {
24 if (typeof program[property] === 'string' || typeof program[property] === 'boolean') {
25 options[property] = program[property];
26 }
27 });
28 return options;
29}
30
31const init = function () {
32 if (fs.existsSync(kesFolder)) {
33 console.log('.kes folder already exists!');
34 process.exit(1);
35 }
36
37 const promptSchema = {
38 properties: {
39 stack: {
40 message: colors.white('Name the CloudFormation stack:'),
41 default: 'kes-cf-template'
42 },
43 bucket: {
44 message: colors.white('Bucket name used for deployment (required):'),
45 required: true
46 }
47 }
48 };
49
50 prompt.get(promptSchema, function (err, result) {
51 if (err) {
52 console.log(err);
53 process.exit(1);
54 }
55
56 fs.mkdirSync(kesFolder);
57
58 // only create dist folder if it doesn't exist
59 try {
60 fs.statSync(distFolder);
61 }
62 catch (e) {
63 fs.mkdirSync(distFolder);
64 }
65
66 console.log(`.kes folder created at ${kesFolder}`);
67
68 // copy simple config file and template
69 const config = yaml.safeLoad(fs.readFileSync(
70 path.join(__dirname, '..', 'examples/lambdas/config.yml'), 'utf8'));
71 config.default.stackName = result.stack;
72
73 if (!config.default.buckets) {
74 config.default.buckets = {};
75 }
76
77 config.default.system_bucket = result.bucket;
78 fs.writeFileSync(path.join(kesFolder, 'config.yml'), yaml.safeDump(config));
79
80 fs.createReadStream(
81 path.join(__dirname, '..', 'examples/lambdas/cloudformation.template.yml')
82 ).pipe(fs.createWriteStream(path.join(kesFolder, 'cloudformation.template.yml')));
83 console.log('config files were copied');
84 });
85};
86
87//const configureProgram = function () {
88program
89 .usage('init')
90 .description('Start a Kes project')
91 .action(() => {
92 init();
93 });
94
95// the CLI activation
96program
97 .usage('TYPE COMMAND [options]')
98 .option('-p, --profile <profile>', 'AWS profile name to use for authentication', null)
99 .option('--role <role>', 'AWS role arn to be assumed for the deployment', null)
100 .option('-c, --config <config>', 'Path to config file')
101 .option('--env-file <envFile>', 'Path to env file')
102 .option('--cf-file <cfFile>', 'Path to CloudFormation template')
103 .option('-t, --template <template>', 'A kes application template used as the base for the configuration')
104 .option('--kes-class <kesClass>', 'Kes Class override', null)
105 .option('-k, --kes-folder <kesFolder>', 'Path to config folder')
106 .option('-r, --region <region>', 'AWS region', null)
107 .option('--stack <stack>', 'stack name, defaults to the config value')
108 .option('--showOutputs', 'Show the list of a CloudFormation template outputs')
109 .option('--yes', 'Skip all confirmation prompts')
110 .option('-d, --deployment <deployment>', 'Deployment name, default to default');
111
112program
113 .command('cf [create|update|upsert|deploy|validate|compile|delete]')
114 .description(`CloudFormation Operations:
115 create Creates the CF stack (deprecated, start using deploy)
116 update Updates the CF stack (deprecated, start using deploy)
117 upsert Creates the CF stack and Update if already exists (deprecated, start using deploy)
118 deploy Creates the CF stack and Update if already exists
119 delete Delete the CF stack
120 validate Validates the CF stack
121 compile Compiles the CF stack`)
122 .action((cmd, o) => {
123 const options = extractCommanderOptions(program);
124 kes.buildCf(options, cmd).then(r => kes.utils.success(r)).catch(e => kes.utils.failure(e));
125 });
126
127program
128 .command('lambda <lambdaName>')
129 .description('uploads a given lambda function to Lambda service')
130 .action((cmd, options) => {
131 kes.buildLambda(program, cmd);
132 });
133
134program
135 .parse(process.argv);