Skip to content

Commit 7ca0607

Browse files
committed
fix: simplified the index.ts
1 parent 0c66ec0 commit 7ca0607

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

bin/index.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,61 @@ 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+
// eslint-disable-next-line no-async-promise-executor
15+
return new Promise(async (resolve) => {
16+
const editedSam = await util.confirmation()
17+
if (editedSam === "create new SAM project") {
18+
await createSAMCLI()
19+
} else if (editedSam === "add components to existing SAM") {
20+
await addComponentCLI()
21+
} else if (editedSam === "add modules to existing SAM") {
22+
await addModuleCLI()
23+
}
24+
resolve()
25+
})
26+
}
27+
28+
async function handleDeployCommand(): Promise<void> {
29+
await deployCLI()
30+
}
31+
32+
function handleVersionCommand(): void {
33+
console.log(version)
34+
}
35+
36+
function handleUnknownCommand(): void {
37+
console.log(cliConfig.commandError(process.argv.slice(2)))
38+
}
1439

1540
async function run(argv: Array<string>): Promise<void> {
1641
try {
1742
if (!roverHelpers.npmrootTest()) {
1843
throw new Error(cliConfig.globalError)
1944
}
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)
45+
46+
switch (argv[0]) {
47+
case "init":
48+
await handleInitCommand()
49+
break
50+
case "deploy":
51+
await handleDeployCommand()
52+
break
53+
case "-v":
54+
case "--version":
55+
handleVersionCommand()
56+
break
57+
default:
58+
handleUnknownCommand()
4059
}
4160
} catch (error) {
4261
console.log("Error: ", error as Error)
4362
//.message)
4463
}
4564
}
65+
4666
run(process.argv.slice(2))

0 commit comments

Comments
 (0)