|
3 | 3 | 'use strict'; |
4 | 4 |
|
5 | 5 | require('./readme'); |
6 | | -const get = require('lodash.get'); |
7 | 6 | const fs = require('fs'); |
8 | 7 | const path = require('path'); |
9 | 8 | const colors = require('colors/safe'); |
10 | 9 | const yaml = require('js-yaml'); |
11 | 10 | const prompt = require('prompt'); |
12 | 11 | const program = require('commander'); |
13 | | -const deprecate = require('deprecate'); |
| 12 | +const kes = require('../index'); |
14 | 13 | const pckg = require('../package.json'); |
15 | | -const Config = require('../src/config'); |
16 | 14 |
|
17 | 15 | const baseDir = process.cwd(); |
18 | 16 | const kesFolder = path.join(baseDir, '.kes'); |
19 | 17 | const distFolder = path.join(baseDir, 'dist'); |
20 | 18 |
|
21 | | -const success = (r) => process.exit(0); |
22 | | - |
23 | 19 | program.version(pckg.version); |
24 | 20 |
|
25 | | -/** |
26 | | - * @name failure |
27 | | - * @private |
28 | | - */ |
29 | | -const failure = (e) => { |
30 | | - if (e.message) { |
31 | | - console.log(e.message); |
32 | | - } |
33 | | - else { |
34 | | - console.log(e); |
35 | | - } |
36 | | - process.exit(1); |
37 | | -}; |
38 | | - |
39 | | -const determineKesClass = function () { |
40 | | - let Kes; |
41 | | - |
42 | | - // if there is a kes class specified use that |
43 | | - const kesClass = get(program, 'kesClass'); |
44 | | - if (kesClass) { |
45 | | - Kes = require(path.join(process.cwd(), kesClass)); |
46 | | - } |
47 | | - else { |
48 | | - // check if there is kes.js in the kes-folder |
49 | | - try { |
50 | | - let kesFolder; |
51 | | - if (program.kesFolder) { |
52 | | - kesFolder = program.kesFolder; |
53 | | - } |
54 | | - else { |
55 | | - kesFolder = path.join(process.cwd(), '.kes'); |
56 | | - } |
57 | | - Kes = require(path.join(process.cwd(), kesFolder, 'kes.js')); |
| 21 | +function extractCommanderOptions(program) { |
| 22 | + const options = {}; |
| 23 | + Object.keys(program).forEach(property => { |
| 24 | + if (typeof program[property] === 'string') { |
| 25 | + options[property] = program[property]; |
58 | 26 | } |
59 | | - catch (e) { |
60 | | - // check if there is a template and the template has kes class |
61 | | - const template = get(program, 'template', '/path/to/nowhere'); |
62 | | - try { |
63 | | - const kesPath = path.join(process.cwd(), template, 'kes.js'); |
64 | | - fs.lstatSync(kesPath); |
65 | | - Kes = require(kesPath); |
66 | | - } |
67 | | - catch (e) { |
68 | | - Kes = require('../index').Kes; |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - return Kes; |
74 | | -}; |
| 27 | + }); |
| 28 | + return options; |
| 29 | +} |
75 | 30 |
|
76 | 31 | const init = function () { |
77 | 32 | if (fs.existsSync(kesFolder)) { |
@@ -161,50 +116,16 @@ program |
161 | 116 | deploy Creates the CF stack and Update if already exists |
162 | 117 | validate Validates the CF stack |
163 | 118 | compile Compiles the CF stack`) |
164 | | - .action((cmd) => { |
165 | | - const Kes = determineKesClass(program); |
166 | | - const config = new Config(program); |
167 | | - const kes = new Kes(config); |
168 | | - switch (cmd) { |
169 | | - case 'create': |
170 | | - deprecate('"kes cf create" command is deprecated. Use "kes cf deploy" instead'); |
171 | | - kes.createStack().then(r => success(r)).catch(e => failure(e)); |
172 | | - break; |
173 | | - case 'update': |
174 | | - deprecate('"kes cf update" command is deprecated. Use "kes cf deploy" instead'); |
175 | | - kes.updateStack().then(r => success(r)).catch(e => failure(e)); |
176 | | - break; |
177 | | - case 'upsert': |
178 | | - deprecate('"kes cf upsert" command is deprecated. Use "kes cf deploy" instead'); |
179 | | - kes.upsertStack().then(r => success(r)).catch(e => failure(e)); |
180 | | - break; |
181 | | - case 'deploy': |
182 | | - kes.deployStack().then(r => success(r)).catch(e => failure(e)); |
183 | | - break; |
184 | | - case 'validate': |
185 | | - kes.validateTemplate().then(r => success(r)).catch(e => failure(e)); |
186 | | - break; |
187 | | - case 'compile': |
188 | | - kes.compileCF().then(r => success(r)).catch(e => failure(e)); |
189 | | - break; |
190 | | - default: |
191 | | - console.log('Wrong choice. Accepted arguments: [create|update|upsert|deploy|validate|compile]'); |
192 | | - } |
| 119 | + .action((cmd, o) => { |
| 120 | + const options = extractCommanderOptions(program); |
| 121 | + kes.buildCf(options ,cmd); |
193 | 122 | }); |
194 | 123 |
|
195 | 124 | program |
196 | 125 | .command('lambda <lambdaName>') |
197 | 126 | .description('uploads a given lambda function to Lambda service') |
198 | 127 | .action((cmd, options) => { |
199 | | - if (cmd) { |
200 | | - const Kes = require('../index').Kes; |
201 | | - const config = new Config(program); |
202 | | - const kes = new Kes(config); |
203 | | - kes.updateSingleLambda(cmd).then(r => success(r)).catch(e => failure(e)); |
204 | | - } |
205 | | - else { |
206 | | - console.log('Lambda name is missing'); |
207 | | - } |
| 128 | + kes.buildLambda(program, cmd); |
208 | 129 | }); |
209 | 130 |
|
210 | 131 | program |
|
0 commit comments