-
Notifications
You must be signed in to change notification settings - Fork 26
/
cli.js
224 lines (219 loc) · 6.72 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
import { Command } from "commander";
import cfonts from "cfonts";
import readline from "readline";
import inquirer from "inquirer";
import install_dependencies from "./utilities/install_dependencies.js";
import copyFolder from "./utilities/copy_templates.js";
import run_in_localhost from "./utilities/run_in_localhost.js";
import fs from "fs-extra";
import path from "path";
import push_to_github_remote from "./utilities/push_to_github.js";
import checkAndInstall from "./utilities/check_install.js";
import os from "os";
import { execSync } from "child_process";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const program = new Command();
cfonts.say("Salt App", {
font: "block",
align: "center",
gradient: ["red", "blue"],
lineHeight: 1,
space: false,
letterSpacing: 1,
});
cfonts.say("Designed using Salt Design System", {
font: "tiny",
align: "center",
gradient: ["magenta", "cyan"],
lineHeight: 1,
space: false,
letterSpacing: 1,
});
program
.version("1.0.0")
.description(
"A simple CLI tool to create a salt App using Salt Design System By J.P.Morgan Chase & Co."
);
const platform = os.platform();
let installGitCommand = "choco install git -y";
let installGhCommand = "choco install gh -y";
switch (platform) {
case "win32":
installGitCommand = "choco install git -y";
installGhCommand = "choco install gh -y";
break;
case "darwin":
installGitCommand = "brew install git";
installGhCommand = "brew install gh";
break;
case "linux":
installGitCommand = "sudo apt-get update && sudo apt-get install -y git";
installGhCommand = "sudo apt-get update && sudo apt-get install -y gh";
break;
default:
console.error("Unsupported OS");
}
const questions = [
{
type: "input",
name: "appName",
message: "Please input the app Name",
default: "salt_app",
validate: (input) => {
if (input.trim() === "") {
return "App name cannot be empty. Please provide a valid app name.";
}
return true;
},
filter: (input) => input.trim() || "salt_app",
},
{
type: "checkbox",
name: "template_choices",
message: "Choose the templates that you need in your app",
choices: ["Form","AgGrid","Login","Carousel","Accordian","Calender","Notification"],
},
{
type: "confirm",
name: "push_to_github",
message: "Do you want to push to github?",
default: false,
},
{
type: "input",
name: "github_username",
message: "Please input the github username..",
when: (answers) => answers.push_to_github,
validate: (input) => {
if (input.trim() === "") {
return "Github Username cannot be empty. Please provide your username";
}
return true;
},
filter: (input) => input.trim() || "",
},
{
type: "password",
name: "token",
message:
"Please input the github Personal Token(token should have the following access:\n1. repo:status\n2. repo_deployment\n3. public_repo\n4.repo:invite\n5.read:org(under admin:org)\n",
mask: "*",
when: (answers) => answers.push_to_github,
validate: (input) => {
if (input.trim() === "") {
return "Github token cannot be empty. Please provide your personal token";
}
return true;
},
filter: (input) => input.trim() || "",
},
{
type: "input",
name: "github_repository_name",
message: "Please input the repository name..",
when: (answers) => answers.push_to_github,
validate: (input) => {
if (input.trim() === "") {
return "Github repo name cannot be empty. Please provide the repo name";
}
return true;
},
filter: (input) => input.trim() || "",
},
{
type: "list",
name: "github_branch_name",
message: "Please select the branch name..",
choices: [
"master",
"develop",
new inquirer.Separator(),
"Custom Branch Name",
],
default: "master",
when: (answers) => answers.push_to_github,
},
{
type: "input",
name: "github_custom_branch_name",
message: "Please input the branch name..",
when: (answers) => answers.github_branch_name === "Custom Branch Name",
validate: (input) => {
if (input.trim() === "") {
return "Branch name cannot be empty.Please provide branch name.";
}
return true;
},
filter: (input) => input.trim() || "develop",
},
{
type: "input",
name: "github_commitMessage",
message: "Please give the commit message..",
default: "Initial_Commit",
when: (answers) => answers.push_to_github,
filter: (input) => input.trim() || "Initial Commit",
},
];
program
.command("ask")
.description("Please provide the information for the below questions")
.action(() => {
let responses = {};
const askQuestion = async (index) => {
await inquirer
.prompt(questions)
.then(async (answers) => {
responses = answers;
try {
await install_dependencies(responses.appName);
await copyFolder(responses.appName, responses.template_choices);
if (responses.push_to_github) {
checkAndInstall("git", installGitCommand, platform);
checkAndInstall("gh", installGhCommand, platform);
let content = `GITHUB_TOKEN=${responses.token}`;
let envPath = process.cwd() + path.sep + ".env";
fs.writeFile(envPath, content, "utf-8", (err) => {
if (err) {
console.error(`Error writing to .env: ${err.message}`);
return;
}
console.log(
`Content has been successfully written to ${envPath}`
);
});
await push_to_github_remote(
responses.appName,
responses.token,
responses.github_username,
responses.github_repository_name,
responses.github_branch_name,
responses.github_commitMessage
);
}
await run_in_localhost(responses.appName);
} catch (error) {
console.log("Error : " + error.message);
}
})
.catch((error) => {
if (error.isTtyError) {
console.log("ttyerror");
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
console.log(`Program has been terminated. ${error}`);
}
});
};
askQuestion(0);
});
if (!process.argv.slice(2).length) {
program.outputHelp();
process.argv.push("ask");
}
program.parse(process.argv);