Skip to content

Commit 19b7c51

Browse files
committed
faeture : add modules to existing SAM project
1 parent 50a4f2f commit 19b7c51

File tree

4 files changed

+138
-154
lines changed

4 files changed

+138
-154
lines changed

bin/index.ts

Lines changed: 96 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,45 @@ let stack_resource_Name: any = [];
1616
let AppType;
1717
let template = {};
1818
let config;
19-
async function run(argv: AnyObject) {
20-
try {
21-
if (rover_utilities.npmrootTest()) {
22-
const commandError = `rover ${argv.join(
23-
" "
24-
)} -is not a rover command \n rover init - creates new SAM project \n rover deploy - deploys SAM project`;
25-
if (argv.length === 1) {
26-
if (argv.length === 1 && argv[0] === "init") {
27-
let editedSam = await util.confirmation();
28-
if (editedSam === "create new SAM project") {
29-
let app_name: object = await util.inputString(
30-
"app_name",
31-
"",
32-
false,
33-
"App Name:"
34-
);
35-
await rover_utilities.checkFile(app_name["app_name"], "no");
36-
let language = await util.languageChoice();
19+
async function roverADD() {
20+
let app_name = await util.inputString("app_name", "", false, "App Name");
21+
await rover_utilities.samValidate(app_name["app_name"]);
22+
await rover_utilities.checkFile(app_name["app_name"], "yes");
23+
let language = await util.languageChoice();
24+
let file_name = await exec("ls " + app_name["app_name"] + "/" + "*.yaml ").toString();
25+
let CompStacks = await rover_utilities.checkNested(file_name);
26+
return { "appname": app_name, "language": language, "filename": file_name,"compstack":CompStacks }
27+
}
28+
async function CRUDObject(stackName,AppType) {
29+
let crud: AnyObject = {};
30+
let StackParams: AnyObject = {};
31+
let paramModule: AnyObject = {};
32+
let obj: AnyObject = {};
33+
let tempObj = {};
34+
35+
do {
36+
paramModule = await util.params(AppType);
37+
paramModule["res"]["resourcetype"] = "lambda";
38+
paramModule["res"]["methods"].push("options");
39+
crud[paramModule.name] = paramModule.res;
40+
41+
obj[stackName] = crud;
42+
tempObj = { ...tempObj, crud };
43+
moreStack = await util.moreStack("Do you want to add another API ?");
44+
} while (moreStack !== "No");
45+
StackParams = { ...obj };
46+
47+
return StackParams
48+
}
49+
async function CustomObject(i) {
50+
let customStacks: any = {};
51+
let choice = cliConfig.customizable.components;
52+
let customstack_name = await util.inputString(`customStackName${i}`,"",false,`Stack ${i} Name: `);
53+
let CustomStacks = await util.multichoice("app_type", choice);
54+
customStacks[customstack_name[`customStackName${i}`]] = CustomStacks.app_type;
55+
return customStacks
56+
}
57+
async function createModules(app_name,language) {
3758
let stack_names: any = {};
3859
let customStacks: any = {};
3960
let paramModule: AnyObject = {};
@@ -46,95 +67,76 @@ async function run(argv: AnyObject) {
4667
do {
4768
let app_Types: any = [];
4869
let AppType: string = await util.appType("Module Type :");
49-
5070
if (AppType !== "Customizable") {
51-
stackname[`stackName${i}`] =
52-
"rover" + rover_utilities.NumtoAlpabet(i);
53-
let stack_name = stackname;
54-
let stackName: string = stack_name[`stackName${i}`];
55-
56-
if (AppType === "CRUD") {
57-
let crud: AnyObject = {};
58-
let tempObj = {};
59-
do {
71+
stackname[`stackName${i}`] ="rover" + rover_utilities.makeid(5);
72+
let stack_name = stackname;
73+
let stackName: string = stack_name[`stackName${i}`];
74+
if (AppType === "CRUD") {
75+
StackParams = { ...StackParams,... await CRUDObject(stackName,AppType)};
76+
} else {
6077
paramModule = await util.params(AppType);
61-
paramModule["res"]["resourcetype"] = "lambda";
62-
paramModule["res"]["methods"].push("options");
63-
crud[paramModule.name] = paramModule.res;
64-
65-
obj[stackName] = crud;
66-
tempObj = { ...tempObj, crud };
67-
moreStack = await util.moreStack(
68-
"Do you want to add another API ?"
69-
);
70-
} while (moreStack !== "No");
71-
72-
StackParams = { ...obj };
73-
} else {
74-
paramModule = await util.params(AppType);
75-
obj[stackName] = basecrud;
76-
StackParams = { ...obj };
77-
}
78-
79-
stack_names[stack_name[`stackName${i}`]] = AppType;
78+
obj[stackName] = basecrud;
79+
StackParams = { ...obj };
80+
}
81+
stack_names[stack_name[`stackName${i}`]] = AppType;
8082
} else {
81-
let choice = cliConfig.customizable.choice;
82-
let customstack_name = await util.inputString(
83-
`customStackName${i}`,
84-
"",
85-
false,
86-
`Stack ${i} Name: `
87-
);
88-
let CustomStacks = await util.multichoice("app_type", choice);
89-
customStacks[customstack_name[`customStackName${i}`]] =
90-
CustomStacks.app_type;
83+
customStacks = {...customStacks, ... await CustomObject(i) }
9184
}
9285
moreStack = await util.moreStack(
9386
"Do you want to add one more modules ? "
9487
);
9588
i++;
9689
} while (moreStack !== "No");
97-
{
90+
9891
template = { ...app_name, language };
9992
if (stack_names !== null)
10093
template = { ...template, Stacks: stack_names, StackParams };
10194
if (customStacks !== null)
10295
template = { ...template, CustomStacks: customStacks };
103-
await rover_utilities.generationSAM({ template }["template"]);
104-
}
105-
} else if (editedSam === "add components to existing SAM") {
106-
let app_name = await util.inputString(
96+
return template
97+
98+
}
99+
async function run(argv: AnyObject) {
100+
try {
101+
if (rover_utilities.npmrootTest()) {
102+
const commandError = `rover ${argv.join(
103+
" "
104+
)} -is not a rover command \n rover init - creates new SAM project \n rover deploy - deploys SAM project\n rover -v or rover --version - gives installed rover version` ;
105+
if (argv.length === 1) {
106+
if (argv.length === 1 && argv[0] === "init") {
107+
let editedSam = await util.confirmation();
108+
if (editedSam === "create new SAM project") {
109+
let app_name: object = await util.inputString(
107110
"app_name",
108111
"",
109112
false,
110-
"App Name"
113+
"App Name:"
111114
);
112-
await rover_utilities.checkFile(app_name["app_name"], "yes");
115+
await rover_utilities.checkFile(app_name["app_name"], "no");
116+
let language = await util.languageChoice();
117+
118+
template= await createModules(app_name,language)
119+
await rover_utilities.generateSAM({ template }["template"]);
120+
121+
} else if (editedSam === "add components to existing SAM") {
113122

123+
let app_name = await util.inputString("app_name","",false,"App Name");
124+
await rover_utilities.checkFile(app_name["app_name"], "yes");
114125
let language = await util.languageChoice();
115-
let file_name = await exec(
116-
"ls " + app_name["app_name"] + "/" + "*.yaml "
117-
).toString();
126+
let file_name = await exec("ls " + app_name["app_name"] + "/" + "*.yaml ").toString();
118127
let CompStacks = await rover_utilities.checkNested(file_name);
119128
let nestedComponents: AnyObject = {};
120129
let choice = Object.keys(CompStacks["CompStacks"]);
121130
let choiceLength = 0;
131+
122132
do {
123133
let nested = CompStacks["checkNested"];
124134
choiceLength = choice.length;
125135
if (nested) {
126-
let chooseStack = await util.inputType(
127-
"Select the module to which you want to add the components ",
128-
choice
129-
);
130-
let selectedchoice = choice.filter((ele) =>
131-
Object.values(chooseStack).includes(ele)
132-
);
133-
let componentChoice = cliConfig.customizable.choice;
134-
let components = await util.multichoice(
135-
"type",
136-
componentChoice
137-
);
136+
let chooseStack = await util.inputType("Select the module to which you want to add the components ", choice);
137+
let selectedchoice = choice.filter((ele) =>Object.values(chooseStack).includes(ele));
138+
let componentChoice = cliConfig.customizable.components;
139+
let components = await util.multichoice("components",componentChoice);
138140
let path = CompStacks["CompStacks"][selectedchoice[0]];
139141
nestedComponents[selectedchoice[0]] = {
140142
...components,
@@ -148,7 +150,7 @@ async function run(argv: AnyObject) {
148150
nestedComponents
149151
};
150152
} else {
151-
let choice = cliConfig.customizable.choice;
153+
let choice = cliConfig.customizable.components;
152154
let Compnents = await util.multichoice("components", choice);
153155
template = { ...app_name, language };
154156
if (customStacks !== null)
@@ -158,26 +160,33 @@ async function run(argv: AnyObject) {
158160
...Compnents
159161
};
160162
}
161-
163+
162164
moreStack = await util.moreStack(
163165
"Do you want to add one more components to modules ?"
164166
);
165-
console.log(moreStack);
167+
166168
i++;
167169
} while (moreStack !== "No" || choiceLength === 0);
168-
169170
await rover_utilities.addComponents(template);
170171
} else if (editedSam === "add modules to existing SAM") {
171-
console.log("Work in progress...");
172+
let res = await roverADD()
173+
let app_name = res["appname"]
174+
let language =res["language"]
175+
let file_name =res["filename"]
176+
let CompStacks = res["compstack"]
177+
178+
template = await createModules(app_name , language)
179+
template["file_name"] = file_name
180+
await rover_utilities.addModules(template);
172181
}
173182
} else if (argv[0] === "deploy") {
174183
let r = await util.inputType("choice", "pipeline", "Deploy through:");
175184
r = r["choice"];
176185
if (r === "repository and pipeline") {
177186
console.log("Work in progress...");
178187
} else if (r === "generate pipeline") {
179-
await util.samValidate();
180-
let lang: string = await util.langValue();
188+
await rover_utilities.samValidate(undefined);
189+
let lang: string = await rover_utilities.langValue();
181190
let pipeline = await util.samBuild(lang);
182191
let repoConfig = { ...pipeline };
183192
template = { ...template, repoConfig };
@@ -186,7 +195,7 @@ async function run(argv: AnyObject) {
186195
await deployment.setupRepo(JSON.parse(repoconfig)["repoConfig"]);
187196
}
188197
} else {
189-
await util.samValidate();
198+
await rover_utilities.samValidate(undefined);
190199
if (fs.existsSync("samconfig.toml")) {
191200
exec("rm -rf samconfig.toml");
192201
}
@@ -255,7 +264,6 @@ async function run(argv: AnyObject) {
255264
console.log("Error: ", error.message);
256265
}
257266
}
258-
259267
export let stackNames: any = stack_resource_Name;
260268
let moreStack;
261269
let customStacks: AnyObject;

cli-main/cliConfig.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
const customStack= require("@rover-tools/engine").rover_components;
2+
const rovercomponents = require("@rover-tools/engine").rover_components;
33
const Stack = require("@rover-tools/engine").rover_modules;
44
export let LanguageSupport = {
55
node: {
@@ -13,6 +13,12 @@ export let LanguageSupport = {
1313
extension: ".py",
1414
},
1515
};
16+
let keys = []
17+
let values=[]
18+
Stack.ModuleDescription.filter(ele => {
19+
keys.push(ele["key"])
20+
values.push(ele["value"])
21+
})
1622
export let app =
1723
{
1824
choices:{
@@ -25,5 +31,9 @@ export let app =
2531

2632
}
2733
export let customizable ={
28-
choice : Object.keys(customStack.Components)
34+
components: Object.keys(rovercomponents.Components),
35+
modules: {
36+
"keys": keys,
37+
"values":values,
38+
}
2939
}

cli-main/util.ts

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,20 @@ const rover_config = require("@rover-tools/engine").rover_config;
1212
import { AnyArray, AnyObject } from "immer/dist/internal";
1313
const rover_utilities= require("@rover-tools/engine").rover_utilities;
1414
const TOML = require('@iarna/toml')
15-
let pythonpattern=new RegExp(/python[1-9]*\.[1-9]*/g)
16-
let jspattern=new RegExp(/nodejs[1-9]*\.[a-zA-Z]*/g)
17-
let yamlpattern=new RegExp(/(\.yaml$)/g)
15+
1816
let envpattern =new RegExp(/^env\d\d+$/g)
1917
let apipathpattern=new RegExp(/^\/[a-zA-Z]*(\/[a-zA-Z]*-*)*/g)
2018
let stringpattern=new RegExp(/^[A-Za-z]+$/g)
2119

22-
const crypto = require('crypto');
23-
2420
export let s3Choice:any = [];
2521
export let accesskey:any,secretkey:any;
2622

27-
export let multichoice = async function (name:string, choice:any) {
23+
export let multichoice = async function (name: string, choice: any, messages = []) {
24+
if (messages = [])"Please select your "+ name.charAt(0).toUpperCase() + name.slice(1)+" :"
2825
let r = await inquirer.prompt([
2926
{
3027
type: "checkbox",
31-
message: "Please select your "+ name.charAt(0).toUpperCase() + name.slice(1)+" :",
28+
message: messages,
3229
name: name,
3330
choices: choice,
3431
validate(answer) {
@@ -465,6 +462,7 @@ export let params = async function(module:string){
465462
if(module==="CRUD"){
466463
let modulesParams= moduleParams.ModuleParams.CRUD.params;
467464
let paramslength = modulesParams.length;
465+
468466
if(paramslength>0){
469467

470468
for(let i=0;i<paramslength;i++){
@@ -508,55 +506,6 @@ if(module==="CRUD"){
508506

509507
}
510508

511-
export let langValue=async function () {
512-
513-
let pwd =(process.cwd()+"/").trim()
514-
if(!fs.existsSync(pwd+".aws-sam/build.toml"))exec("sam build")
515-
let data=fs.readFileSync(pwd+".aws-sam/build.toml", { encoding: "utf-8" })
516-
data=TOML.parse(data)
517-
let langarray:AnyArray=[]
518-
let jsresult:AnyArray=[]
519-
let pyresult:AnyArray=[]
520-
Object.keys(data).forEach(ele=>{
521-
Object.keys(data[ele]).forEach(obj=>{
522-
if(data[ele][obj].hasOwnProperty("runtime"))langarray.push(data[ele][obj]["runtime"])})
523-
}
524-
)
525-
langarray.forEach(ele=>{
526-
if (ele.match(jspattern)!==null)jsresult.push(...ele.match(jspattern))
527-
if (ele.match(pythonpattern)!==null)pyresult.push(...ele.match(pythonpattern))
528-
529-
})
530-
if(jsresult.length>pyresult.length) return "js"
531-
else if(pyresult.length>jsresult.length) return "python"
532-
else return "js"
533-
534-
}
535509

536-
export let samValidate=async function(){
537-
try {
538-
let files:AnyArray=fs.readdirSync(exec("pwd").toString().replace("\n",""))
539-
let yamlfiles:AnyArray=[]
540-
let response:AnyArray=[]
541-
files.map(ele=>{if (ele.match(yamlpattern)!==null)yamlfiles.push(ele)})
542-
yamlfiles.map(ele=>{
543-
let data=fs.readFileSync(ele,{ encoding: "utf-8" })
544-
data=Yaml.load(rover_utilities.replaceTempTag(data))
545-
if(data.hasOwnProperty("AWSTemplateFormatVersion")
546-
&&data.hasOwnProperty("Transform")
547-
&&data.hasOwnProperty("Description")
548-
&&data.hasOwnProperty("Resources")){response.push(true)
549-
}
550-
})
551-
if (!response.includes(true)) {
552-
throw new Error("SAM Template error \n")
553-
}
554-
} catch (error) {
555-
throw new Error("Not a SAM file or "+error.message)
556-
}
557-
558-
}
559-
export let makeid =async function () {
560-
561-
return (crypto.randomBytes(1).toString("base64url").replace(/\d/g, 'd')).toLowerCase();
562-
}
510+
511+

0 commit comments

Comments
 (0)