Skip to content

Commit ec8b378

Browse files
Merge pull request #55 from antstackio/ROV-59
fix: Add module/components to existing resources CLI
2 parents 427e193 + 333c699 commit ec8b378

File tree

3 files changed

+160
-26
lines changed

3 files changed

+160
-26
lines changed

bin/index.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,60 @@ import {
66
addModuleCLI,
77
} from "../src/roverCLI/roverGenerateCLI"
88
import { deployCLI } from "../src/roverCLI/roverDeployCLI"
9-
109
import * as cliConfig from "../src/configs/cliConfig"
10+
import { version } from "../package.json"
1111
import * as util from "../src/utilities/cliUtil"
1212

13-
import { version } from "../package.json"
13+
function handleInitCommand(): Promise<void> {
14+
return new Promise( (resolve) => {
15+
const editedSam = <string>(<unknown>util.confirmation())
16+
if ( editedSam === "create new SAM project") {
17+
createSAMCLI()
18+
} else if (editedSam === "add components to existing SAM") {
19+
addComponentCLI()
20+
} else if (editedSam === "add modules to existing SAM") {
21+
addModuleCLI()
22+
}
23+
resolve()
24+
})
25+
}
26+
27+
async function handleDeployCommand(): Promise<void> {
28+
await deployCLI()
29+
}
30+
31+
function handleVersionCommand(): void {
32+
console.log(version)
33+
}
34+
35+
function handleUnknownCommand(): void {
36+
console.log(cliConfig.commandError(process.argv.slice(2)))
37+
}
1438

1539
async function run(argv: Array<string>): Promise<void> {
1640
try {
1741
if (!roverHelpers.npmrootTest()) {
1842
throw new Error(cliConfig.globalError)
1943
}
20-
const commandErrors = cliConfig.commandError(argv)
21-
if (argv.length !== 1) {
22-
throw new Error(commandErrors)
23-
}
24-
if (argv[0] === "init") {
25-
const editedSam = await util.confirmation()
26-
if (editedSam === "create new SAM project") {
27-
await createSAMCLI()
28-
} else if (editedSam === "add components to existing SAM") {
29-
await addComponentCLI()
30-
} else if (editedSam === "add modules to existing SAM") {
31-
await addModuleCLI()
32-
}
33-
} else if (argv[0] === "deploy") {
34-
await deployCLI()
35-
} else if (argv[0] === "-v" || argv[0] === "--version") {
36-
// show current package version in the console
37-
console.log(version)
38-
} else {
39-
console.log(commandErrors)
44+
45+
switch (argv[0]) {
46+
case "init":
47+
await handleInitCommand()
48+
break
49+
case "deploy":
50+
await handleDeployCommand()
51+
break
52+
case "-v":
53+
case "--version":
54+
handleVersionCommand()
55+
break
56+
default:
57+
handleUnknownCommand()
4058
}
4159
} catch (error) {
4260
console.log("Error: ", error as Error)
4361
//.message)
4462
}
4563
}
64+
4665
run(process.argv.slice(2))

src/utilities/cliUtil.ts

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const inputType = async function (
118118
return takeInput
119119
}
120120

121-
export const confirmation = async function () {
121+
export const confirmation = async function ():Promise<string> {
122122
const r = await inquirer.prompt([
123123
{
124124
type: "rawlist",
@@ -195,7 +195,7 @@ export const password = async function (userName: string, message = "") {
195195
return r
196196
}
197197

198-
export const samBuild = async function (lang: string) {
198+
export const samBuilds = async function (lang: string) {
199199
try {
200200
const obj = buildConfig.samConfig
201201
const choices = <Record<string, Array<string>>>buildConfig.samConfig.choices
@@ -206,7 +206,7 @@ export const samBuild = async function (lang: string) {
206206
""
207207
)
208208
const temp: Record<string, Record<string, string>> = {}
209-
Object.values(sam).map((ele) => {
209+
Object.values(sam).forEach((ele) => {
210210
Object.assign(temp, ele)
211211
})
212212
sam = temp
@@ -308,6 +308,121 @@ export const samBuild = async function (lang: string) {
308308
}
309309
}
310310

311+
export const samBuild = async (lang: string) => {
312+
try {
313+
const { samConfig } = buildConfig
314+
315+
const sam = await inputCli(
316+
samConfig,
317+
<Array<Record<string, string>>>samConfig.samBuild,
318+
""
319+
)
320+
321+
const { choices } = samConfig
322+
323+
const language = { language: lang }
324+
325+
const no_of_env = await inputNumber("no_of_env", "environments")
326+
const envs = []
327+
const steps = {}
328+
const stacknames = {}
329+
const deploymentregion = <Record<string, string>>{}
330+
const deploymentparameters = {}
331+
const depBucketNames = {}
332+
333+
const branches = { branches: ["main"] }
334+
335+
for (let i = 1; i <= no_of_env; i++) {
336+
const envName = <string>(
337+
(await inputString(`env${i}`, "", false, `Environment ${i}:`))[
338+
`env${i}`
339+
]
340+
)
341+
envs.push(envName)
342+
343+
const stepsChoice = (<Record<string, Array<string>>>choices).dev
344+
const stepData = await multichoice(
345+
`Steps required for ${envName} environment`,
346+
stepsChoice,
347+
""
348+
)
349+
const step = Object.fromEntries(
350+
Object.entries(stepData).map(([key, value]) => {
351+
const name = key
352+
.replace("steps required for ", "")
353+
.replace(" environment ", "")
354+
return [name, value]
355+
})
356+
)
357+
Object.assign(steps, step)
358+
359+
const stackname = (
360+
await inputString(
361+
`${envName}`,
362+
"",
363+
true,
364+
`Stack Name (optional) --> ${envName}:`
365+
)
366+
)[envName]
367+
const deploymentbucket = (
368+
await inputString(
369+
`${envName}`,
370+
"",
371+
true,
372+
`Deployment Bucket (optional) --> ${envName}:`
373+
)
374+
)[envName]
375+
376+
const regionChoice = (<Record<string, Array<string>>>choices)
377+
.deploymentregion
378+
const deployment_region = <string>(
379+
(await inputType(`${envName}`, regionChoice, "Deployment Region"))[
380+
`${envName}`
381+
]
382+
)
383+
deploymentregion[`${envName}`] = deployment_region
384+
385+
const deployment_parameter = (
386+
await inputString(
387+
`${envName}`,
388+
"",
389+
true,
390+
`Deployment Parameter (optional) --> ${envName}:`
391+
)
392+
)[envName]
393+
394+
Object.assign(stacknames, { [envName]: stackname })
395+
Object.assign(depBucketNames, { [envName]: deploymentbucket })
396+
Object.assign(deploymentparameters, { [envName]: deployment_parameter })
397+
}
398+
399+
const deployment_choice = (<Record<string, Array<string>>>choices)
400+
.deployment
401+
const deploymentEvent = await multichoice(
402+
`Deployment Events`,
403+
deployment_choice,
404+
""
405+
)
406+
407+
return {
408+
...sam,
409+
...language,
410+
no_of_env,
411+
envs,
412+
...branches,
413+
framework: "sam",
414+
steps,
415+
stackname: { ...stacknames },
416+
deploymentbucket: { ...depBucketNames },
417+
deploymentregion,
418+
deploymentparameters,
419+
...deploymentEvent,
420+
}
421+
} catch (error) {
422+
console.log(error)
423+
}
424+
}
425+
311426
export const appType = async function (message = "") {
312427
const r = await inquirer.prompt([
313428
{

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
1212

1313
/* Language and Environment */
14-
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
14+
"target": "es2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
1515
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
1616
// "jsx": "preserve", /* Specify what JSX code is generated. */
1717
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */

0 commit comments

Comments
 (0)