|
| 1 | +import * as inquirer from "inquirer" |
| 2 | +import * as cliConfig from "./cliConfig" |
| 3 | +import * as buildConfig from "./buildConfig" |
| 4 | +import * as rover from "@rover-tools/engine/dist/bin/index" |
| 5 | +import { |
| 6 | + IroverDeploymentObject, |
| 7 | + Iroverdescription, |
| 8 | + IroverCLIparamModule, |
| 9 | +} from "./rover.types" |
| 10 | +const moduleParams = rover.modules.Modules |
| 11 | +const envpattern = new RegExp(/^env\d\d+$/g) |
| 12 | +const apipathpattern = new RegExp(/^\/[a-zA-Z]*(\/[a-zA-Z]*-*)*/g) |
| 13 | +const stringpattern = new RegExp(/^[A-Za-z]+$/g) |
| 14 | + |
| 15 | +export const multichoice = async function ( |
| 16 | + name: string, |
| 17 | + choice: Array<string> |
| 18 | +) { |
| 19 | + const messages = `Please select your ${name |
| 20 | + .charAt(0) |
| 21 | + .toUpperCase()} ${name.slice(1)} :` |
| 22 | + const r = await inquirer.prompt([ |
| 23 | + { |
| 24 | + type: "checkbox", |
| 25 | + message: messages, |
| 26 | + name: name, |
| 27 | + choices: choice, |
| 28 | + validate(answer) { |
| 29 | + if (answer.length < 1) { |
| 30 | + return "You must choose at least one option." |
| 31 | + } |
| 32 | + |
| 33 | + return true |
| 34 | + }, |
| 35 | + }, |
| 36 | + ]) |
| 37 | + return r |
| 38 | +} |
| 39 | + |
| 40 | +export const jsonCreation = async function ( |
| 41 | + obj: Record<string, IroverDeploymentObject> |
| 42 | +) { |
| 43 | + try { |
| 44 | + const content = JSON.stringify(obj, null, 2) |
| 45 | + return content |
| 46 | + } catch (err) { |
| 47 | + console.log(err) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export const inputString = async function ( |
| 52 | + userName: string, |
| 53 | + defaults: string, |
| 54 | + optional: boolean, |
| 55 | + messages = "" |
| 56 | +) { |
| 57 | + const takeInput = await inquirer.prompt([ |
| 58 | + { |
| 59 | + type: "input", |
| 60 | + name: userName, |
| 61 | + message: messages, |
| 62 | + validate: function (value) { |
| 63 | + let message = "" |
| 64 | + if (userName == "path") { |
| 65 | + if (apipathpattern.test(value)) return true |
| 66 | + else message = "Please enter a valid path" |
| 67 | + } else if (envpattern.test(userName)) { |
| 68 | + if (value !== "" && value !== undefined) return true |
| 69 | + else message = "environment values cannot be empty" |
| 70 | + } else { |
| 71 | + if (!optional) { |
| 72 | + if (stringpattern.test(value)) return true |
| 73 | + else message = `${messages} should have only alphanumeric values` |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (message !== "") return message |
| 78 | + else return true |
| 79 | + }, |
| 80 | + }, |
| 81 | + ]) |
| 82 | + |
| 83 | + return { ...takeInput } |
| 84 | +} |
| 85 | + |
| 86 | +export const languageChoice = async function () { |
| 87 | + const lang = await inquirer.prompt([ |
| 88 | + { |
| 89 | + type: "rawlist", |
| 90 | + name: "language", |
| 91 | + message: "Choose your language", |
| 92 | + choices: cliConfig.app.choices.language, |
| 93 | + }, |
| 94 | + ]) |
| 95 | + |
| 96 | + if (lang.language === "Node") { |
| 97 | + return "node" |
| 98 | + } else return "python" |
| 99 | +} |
| 100 | + |
| 101 | +export const inputType = async function ( |
| 102 | + userName: string, |
| 103 | + choices: Array<string> | string, |
| 104 | + message = "" |
| 105 | +) { |
| 106 | + const takeInput = await inquirer.prompt([ |
| 107 | + { |
| 108 | + type: "rawlist", |
| 109 | + name: `${userName}`, |
| 110 | + message: message, |
| 111 | + choices: |
| 112 | + typeof choices === "string" ? cliConfig.app.choices[choices] : choices, |
| 113 | + }, |
| 114 | + ]) |
| 115 | + |
| 116 | + return takeInput |
| 117 | +} |
| 118 | + |
| 119 | +export const confirmation = async function () { |
| 120 | + const r = await inquirer.prompt([ |
| 121 | + { |
| 122 | + type: "rawlist", |
| 123 | + name: "choice", |
| 124 | + message: `Hey, what do you want ?`, |
| 125 | + choices: [ |
| 126 | + "create new SAM project", |
| 127 | + "add components to existing SAM", |
| 128 | + "add modules to existing SAM", |
| 129 | + ], |
| 130 | + }, |
| 131 | + ]) |
| 132 | + |
| 133 | + return r.choice |
| 134 | +} |
| 135 | + |
| 136 | +export const inputNumber = async function (userName: string, message: string) { |
| 137 | + let displayname = userName |
| 138 | + if (message !== undefined) { |
| 139 | + displayname = message |
| 140 | + } |
| 141 | + const takeInput = await inquirer.prompt([ |
| 142 | + { |
| 143 | + type: "input", |
| 144 | + message: `Please enter the required number of ${displayname} you want ?`, |
| 145 | + name: `${userName}`, |
| 146 | + validate: function (value) { |
| 147 | + const pass = !isNaN(value) && value > 0 |
| 148 | + if (pass) { |
| 149 | + return true |
| 150 | + } |
| 151 | + return "Please enter a valid number greater than 0" |
| 152 | + }, |
| 153 | + }, |
| 154 | + ]) |
| 155 | + |
| 156 | + return parseInt(takeInput[`${userName}`], 10) |
| 157 | +} |
| 158 | + |
| 159 | +export const inputCli = async function ( |
| 160 | + obj: Record< |
| 161 | + string, |
| 162 | + Record<string, Array<string>> | Array<Record<string, string>> |
| 163 | + >, |
| 164 | + subObj: Array<Record<string, string>>, |
| 165 | + choiceOption: string |
| 166 | +): Promise<Record<string, Record<string, string>>> { |
| 167 | + let res: Record<string, Record<string, string>> = {} |
| 168 | + for (const sobj of subObj) { |
| 169 | + if (sobj.value === "object") { |
| 170 | + const resp = await inputCli( |
| 171 | + obj, |
| 172 | + <Array<Record<string, string>>>(<unknown>obj[sobj.key]), |
| 173 | + choiceOption |
| 174 | + ) |
| 175 | + res = <Record<string, Record<string, string>>>{ ...res, [sobj.key]: resp } |
| 176 | + } else if (sobj.value === "choice") { |
| 177 | + const choices = <Record<string, Array<string>>>obj["choices"] |
| 178 | + const choice = choices[sobj.key] |
| 179 | + const r = await inputType(sobj.key, choice, sobj.message) |
| 180 | + res[`${sobj.key}`] = r |
| 181 | + } |
| 182 | + } |
| 183 | + return res |
| 184 | +} |
| 185 | +export const password = async function (userName: string, message = "") { |
| 186 | + const r = await inquirer.prompt([ |
| 187 | + { |
| 188 | + type: "password", |
| 189 | + message: message, |
| 190 | + name: userName, |
| 191 | + }, |
| 192 | + ]) |
| 193 | + return r |
| 194 | +} |
| 195 | + |
| 196 | +export const samBuild = async function (lang: string) { |
| 197 | + try { |
| 198 | + const obj = buildConfig.samConfig |
| 199 | + const choices = <Record<string, Array<string>>>buildConfig.samConfig.choices |
| 200 | + const subObj = <Array<Record<string, string>>>buildConfig.samConfig.samBuild |
| 201 | + let sam: Record<string, Record<string, string>> = await inputCli( |
| 202 | + obj, |
| 203 | + subObj, |
| 204 | + "" |
| 205 | + ) |
| 206 | + const temp: Record<string, Record<string, string>> = {} |
| 207 | + Object.values(sam).map((ele) => { |
| 208 | + Object.assign(temp, ele) |
| 209 | + }) |
| 210 | + sam = temp |
| 211 | + const langs = { language: lang } |
| 212 | + const no_of_env = await inputNumber("no_of_env", "environments") |
| 213 | + const envs: string[] = [] |
| 214 | + let steps: Record<string, Array<string>> = {} |
| 215 | + let stacknames: Record<string, string> = {} |
| 216 | + const deploymentregion: Record<string, string> = {} |
| 217 | + let deploymentparameters: Record<string, string> = {} |
| 218 | + let depBucketNames: Record<string, string> = {} |
| 219 | + |
| 220 | + const branches = { branches: ["main"] } |
| 221 | + for (let i = 1; i <= no_of_env; i++) { |
| 222 | + const env = await inputString(`env${i}`, "", false, `Envrionment ${i} :`) |
| 223 | + const envName = env[`env${i}`] |
| 224 | + envs.push(envName) |
| 225 | + |
| 226 | + const stepsChoice = choices.dev |
| 227 | + let step = await multichoice( |
| 228 | + `steps required for ${envName} environment `, |
| 229 | + stepsChoice |
| 230 | + ) |
| 231 | + const steps1: Record<string, Array<string>> = {} |
| 232 | + step = Object.keys(step).map((ele) => { |
| 233 | + let name: string = ele.replace("steps required for ", "") |
| 234 | + name = name.replace(" environment ", "") |
| 235 | + steps1[name] = step[ele] |
| 236 | + }) |
| 237 | + |
| 238 | + const stackname = await inputString( |
| 239 | + `${envName}`, |
| 240 | + "", |
| 241 | + true, |
| 242 | + |
| 243 | + `Stack Name(optional) --> ${envName} :` |
| 244 | + ) |
| 245 | + const deploymentbucket = await inputString( |
| 246 | + `${envName}`, |
| 247 | + "", |
| 248 | + true, |
| 249 | + `Deployment Bucket(optional) --> ${envName} :` |
| 250 | + ) |
| 251 | + const regionChoice = choices.deploymentregion |
| 252 | + const deployment_region = await inputType( |
| 253 | + `${envName}`, |
| 254 | + regionChoice, |
| 255 | + "Deployment Region" |
| 256 | + ) |
| 257 | + const deployment_parameter = await inputString( |
| 258 | + `${envName}`, |
| 259 | + "", |
| 260 | + true, |
| 261 | + `Deployment Parameter(optional) --> ${envName} :` |
| 262 | + ) |
| 263 | + steps = { ...steps, ...steps1 } |
| 264 | + |
| 265 | + stacknames = { ...stacknames, ...stackname } |
| 266 | + |
| 267 | + depBucketNames = { |
| 268 | + ...depBucketNames, |
| 269 | + ...deploymentbucket, |
| 270 | + } |
| 271 | + deploymentregion[`${envName}`] = deployment_region[`${envName}`] |
| 272 | + deploymentparameters = { |
| 273 | + ...deploymentparameters, |
| 274 | + ...deployment_parameter, |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + const deployment_choice = choices.deployment |
| 279 | + const deploymentEvent = await multichoice( |
| 280 | + `deploymentevents`, |
| 281 | + deployment_choice |
| 282 | + ) |
| 283 | + const framework = { framework: "sam" } |
| 284 | + let result: IroverDeploymentObject = <IroverDeploymentObject>{} |
| 285 | + result = { |
| 286 | + ...sam, |
| 287 | + ...langs, |
| 288 | + no_of_env, |
| 289 | + envs, |
| 290 | + ...branches, |
| 291 | + ...framework, |
| 292 | + steps, |
| 293 | + stackname: { ...stacknames }, |
| 294 | + deploymentbucket: { |
| 295 | + ...depBucketNames, |
| 296 | + }, |
| 297 | + deploymentregion, |
| 298 | + deploymentparameters, |
| 299 | + ...deploymentEvent, |
| 300 | + } |
| 301 | + return result |
| 302 | + } catch (error) { |
| 303 | + console.log(error) |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +export const appType = async function (message = "") { |
| 308 | + const r = await inquirer.prompt([ |
| 309 | + { |
| 310 | + type: "rawlist", |
| 311 | + name: "app_Type", |
| 312 | + message: message, |
| 313 | + choices: cliConfig.app.choices.type, |
| 314 | + }, |
| 315 | + ]) |
| 316 | + const stackModule = cliConfig.moduleDescription |
| 317 | + for (const smodule of stackModule) { |
| 318 | + if (smodule.value === r["app_Type"]) { |
| 319 | + return smodule.key |
| 320 | + } |
| 321 | + } |
| 322 | +} |
| 323 | +export const moreStack = async function (message: string) { |
| 324 | + const r = await inquirer.prompt([ |
| 325 | + { |
| 326 | + type: "list", |
| 327 | + name: "stack", |
| 328 | + message: message, |
| 329 | + choices: ["Yes", "No"], |
| 330 | + }, |
| 331 | + ]) |
| 332 | + return r["stack"] |
| 333 | +} |
| 334 | + |
| 335 | +export const params = async function (module: string) { |
| 336 | + const choice: Record<string, Array<string> | Array<Iroverdescription>> = |
| 337 | + cliConfig.app.choices |
| 338 | + let name: Record<string, string> = {} |
| 339 | + let res: IroverCLIparamModule = <IroverCLIparamModule>{} |
| 340 | + if (module === "CRUDModule") { |
| 341 | + const modulesParams = moduleParams.CRUDModule["params"].params |
| 342 | + const paramslength = modulesParams.length |
| 343 | + |
| 344 | + if (paramslength > 0) { |
| 345 | + for (let i = 0; i < paramslength; i++) { |
| 346 | + if (modulesParams[i].value === "choice") { |
| 347 | + const r = await inputType( |
| 348 | + modulesParams[i].key, |
| 349 | + <Array<string>>choice[modulesParams[i].key], |
| 350 | + modulesParams[i].message |
| 351 | + ) |
| 352 | + |
| 353 | + res = { ...res, ...r } |
| 354 | + } else if (modulesParams[i].value === "multichoice") { |
| 355 | + const r = await multichoice( |
| 356 | + modulesParams[i].key, |
| 357 | + <Array<string>>choice.methods |
| 358 | + ) |
| 359 | + res = { ...res, ...r } |
| 360 | + } else { |
| 361 | + if (modulesParams[i].key === "name") { |
| 362 | + const r = await inputString( |
| 363 | + "name", |
| 364 | + "", |
| 365 | + false, |
| 366 | + modulesParams[i].message |
| 367 | + ) |
| 368 | + name = r |
| 369 | + } else { |
| 370 | + const r = await inputString( |
| 371 | + modulesParams[i].key, |
| 372 | + "", |
| 373 | + false, |
| 374 | + modulesParams[i].message |
| 375 | + ) |
| 376 | + res = { ...res, ...r } |
| 377 | + } |
| 378 | + } |
| 379 | + } |
| 380 | + return { |
| 381 | + res, |
| 382 | + name: name["name"], |
| 383 | + } |
| 384 | + } else { |
| 385 | + return {} |
| 386 | + } |
| 387 | + } else { |
| 388 | + return {} |
| 389 | + } |
| 390 | +} |
0 commit comments