-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.js
More file actions
executable file
·473 lines (453 loc) · 14.3 KB
/
commands.js
File metadata and controls
executable file
·473 lines (453 loc) · 14.3 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/env node
const program = require("commander");
const shell = require("shelljs");
const path = require("path");
const fs = require("fs");
const { prompt } = require("inquirer");
const packageInfo = require("./package.json");
var settings = require("./settings.json");
const utils = require("./utils");
const CWD = process.cwd();
/**
* Parent Directory in client side to hold all reference code
*/
const referenceCodeDirectoryName = ".react-node-cli";
const settingsJsonFileName = "settings.json";
const colorReference = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
};
program
.version(packageInfo.version)
.description("React Node Full Stack Application Generator & Helper");
// Generate Command
program
.command("generate")
.alias("g")
.description("Generate node application to serve react build")
.action(async () => {
/**
* This directory will have core node js code to server react build
*/
const nodeCodeDirectoryName = `${referenceCodeDirectoryName}/node`;
const clentSettingJsonFile = path.join(
process.cwd(),
referenceCodeDirectoryName,
settingsJsonFileName
);
try {
const { isCorrectDirectory } = await prompt([
{
type: "confirm",
name: "isCorrectDirectory",
message: `Make sure you are inside the react project root directory`,
},
]);
if (!isCorrectDirectory) {
console.log(
colorReference.FgRed,
"Sorry: You must be in root directoy of react project",
colorReference.Reset
);
process.exit();
}
// making release directory and copying core node code inside it to serve react build
console.log(
colorReference.FgCyan,
"Your current working directory: ",
CWD,
colorReference.Reset
);
//Storing reference code initially
if (
!fs.existsSync(nodeCodeDirectoryName) ||
!fs.existsSync(referenceCodeDirectoryName)
) {
shell.rm("-rf", referenceCodeDirectoryName);
shell.mkdir("-p", nodeCodeDirectoryName);
shell.cp(
"-R",
`${__dirname}/node/*`,
`${__dirname}/node/.*`,
nodeCodeDirectoryName
);
shell.cp(
"-R",
`${__dirname}/settings.json`,
referenceCodeDirectoryName
);
await prompt([
{
name: "continue",
message: `Make sure to change your node code if needed inside ${nodeCodeDirectoryName} folder before procedding. eg PORT , CORS etc: Press enter to continue`,
},
]);
}
//Storing seetings file
if (
fs.existsSync(referenceCodeDirectoryName) &&
!fs.existsSync(clentSettingJsonFile)
) {
shell.cp(
"-R",
`${__dirname}/settings.json`,
referenceCodeDirectoryName
);
}
const clientSettings = await utils
.readJsonFile(clentSettingJsonFile)
.catch(() => {});
if (clientSettings) settings = clientSettings;
var releaseFolderName = settings.generate.releaseFolderName;
var reactBuildFolderName = settings.generate.reactBuildFolderName;
const {
runBuildCommand,
userInputReleaseOutputDirectory,
userInputReactBuildDirectory,
deployToGit,
} = await prompt([
{
type: "confirm",
name: "runBuildCommand",
default: true,
message: `Do you want to run react build command ?`,
},
{
type: "input",
name: "userInputReleaseOutputDirectory",
message: `Enter your prefer react node release output directory: [default:${releaseFolderName}]: `,
},
{
type: "input",
name: "userInputReactBuildDirectory",
message: `Enter your prefer react build directory to serve: [default:${reactBuildFolderName}]: `,
},
{
type: "confirm",
name: "deployToGit",
default: true,
message: `Do you want to deploy your release output directory to github branch using git ?`,
},
]);
//Copy all fresh/modified code from code node folder to release folder
if (
userInputReleaseOutputDirectory &&
userInputReleaseOutputDirectory !== releaseFolderName
) {
releaseFolderName = userInputReleaseOutputDirectory;
//Store in user setting too
settings.generate.releaseFolderName = releaseFolderName;
}
if (
userInputReactBuildDirectory &&
userInputReactBuildDirectory !== reactBuildFolderName
) {
reactBuildFolderName = userInputReactBuildDirectory;
//Store in user setting too
settings.generate.reactBuildFolderName = reactBuildFolderName;
}
const releaseDir = path.join(CWD, releaseFolderName);
const buildDirPath = path.join(CWD, reactBuildFolderName);
if (runBuildCommand) {
console.log(
colorReference.FgMagenta,
"....Making React Build, This will take some time....",
colorReference.Reset
);
// making react build
if (shell.exec("npm run build").code !== 0) {
console.log(
colorReference.FgRed,
"Error: React build failed",
colorReference.Reset
);
shell.exit(1);
}
}
if (!fs.existsSync(buildDirPath)) {
console.log(
colorReference.FgRed,
`Error: Given folder does not exists [${buildDirPath}]`,
colorReference.Reset
);
shell.exit(1);
}
// copying build to release directory
shell.rm("-rf", releaseDir);
shell.mkdir(releaseDir);
shell.cp(
"-R",
`${nodeCodeDirectoryName}/*`,
`${nodeCodeDirectoryName}/.*`,
`${buildDirPath}/.`,
releaseDir
);
//[gitignire-operation] Add release out folder to gitignore which is auto generaterd
const gitIgnireFilePath = `${CWD}/.gitignore`;
const stringToAdd = `\n\n#Added By React-Node-Cli\n${releaseFolderName}\n`;
if (fs.existsSync(gitIgnireFilePath)) {
const data = fs.readFileSync(gitIgnireFilePath);
// Only add if it is not added previously
if (data.indexOf(stringToAdd) < 0) {
fs.appendFileSync(gitIgnireFilePath, stringToAdd);
}
}
//Only deploy to git if user wants to deploy
if (deployToGit) {
await workingOnGit(releaseDir);
}
// Save Settings into a setting file
await utils
.writeJsonFile(settings, clentSettingJsonFile)
.catch((err) => {});
console.info(
colorReference.FgGreen,
`Completed: To run locally: $cd ${releaseFolderName} && npm start`,
colorReference.Reset
);
process.exit();
} catch (error) {
console.log(
colorReference.FgRed,
"Error: error executing command ",
error,
colorReference.Reset
);
}
});
// Generate Starter Structure Command
program
.command("export")
.alias("exp")
.description("Export React Starter Structure")
.action(async () => {
const exportTemplates = [
{
name: "Export react structure only",
outFolderName: "exported_react_structure_only",
path: "/templates/react_structure_only",
},
{
name: "Export react structure with materialUI & Mobx",
outFolderName: "exported_react_mui_mobx",
path: "/templates/react_mui_mobx",
},
{
name: "Export react structure with materialUI & Redux",
outFolderName: "exported_react_mui_redux",
path: "/templates/react_mui_redux",
},
];
try {
console.log(
colorReference.FgCyan,
"Your current working directory: ",
CWD,
colorReference.Reset
);
const DEFAULT_EXTRACT_FOLDER_NAME = "/exported_react_structure";
var folderToExport = DEFAULT_EXTRACT_FOLDER_NAME;
const { exportType } = await prompt([
{
type: "list",
name: "exportType",
message: "What you want to export? (Use arrow keys)",
choices: exportTemplates.map((i) => i.name),
},
]);
const templateType = exportTemplates.find((i) => i.name == exportType);
folderToExport = templateType.outFolderName;
const { extractFolderName } = await prompt([
{
type: "input",
name: "extractFolderName",
message: `Enter your prefer folder name to export: ${colorReference.FgGreen}[default:${folderToExport}]${colorReference.Reset}, If it is your fresh project, simply enter directory as ${colorReference.FgGreen}/${colorReference.Reset}: `,
},
]);
if (extractFolderName) folderToExport = extractFolderName;
const { aggreed } = await prompt([
{
type: "confirm",
name: "aggreed",
default: false,
message: `This will replace all code that you have in ${colorReference.FgGreen}[${folderToExport}]${colorReference.Reset} directory and it is can not be Undone. Do you aggreed of this ?`,
},
]);
if (!aggreed) {
console.log(
colorReference.BgRed,
"Exited: Reason not aggreed",
colorReference.Reset
);
process.exit();
}
//Export Path: where to export
const exportPath = path.join(CWD, folderToExport);
//Template Path: from where to export
const templatePath = path.join(__dirname, templateType.path);
shell.mkdir("-p", exportPath);
shell.cp("-R", `${templatePath}/*`, `${templatePath}/.*`, exportPath);
const dirs = fs.readdirSync(templatePath);
console.log(
colorReference.FgGreen,
`Completed:: Exported Directories: ${JSON.stringify(
dirs
)} inside ${exportPath}`,
colorReference.Reset
);
console.log(
colorReference.BgRed,
`Please make sure to install all required packages to work with this structure by running command:${colorReference.FgGreen} $ npm install ${colorReference.Reset}`,
colorReference.Reset
);
process.exit();
} catch (error) {
console.log(
colorReference.FgRed,
"Error: error executing command ",
error,
colorReference.Reset
);
process.exit(1);
}
});
// Deploy any folder to github branch
program
.command("git_deploy")
.alias("gdeploy")
.description("Deploy any folder to github branch")
.action(async () => {
try {
const { userInputTargetFolderName } = await prompt([
{
type: "input",
name: "userInputTargetFolderName",
message: `Enter your prefer folderName to deploy in github: `,
},
]);
const userInputTargetFolderPath = path.join(
CWD,
userInputTargetFolderName
);
if (
!userInputTargetFolderName ||
!fs.existsSync(userInputTargetFolderPath)
) {
console.log(
colorReference.FgRed,
`Error: Given folder does not exists or invalid folder, please provide valid foldername`,
colorReference.Reset
);
shell.exit(1);
}
await workingOnGit(userInputTargetFolderPath);
console.info(colorReference.FgGreen, `Completed`, colorReference.Reset);
process.exit();
} catch (error) {
console.log(
colorReference.FgRed,
"Error: error executing command ",
error,
colorReference.Reset
);
}
});
program.parse(process.argv);
/**
* This will deploy the given folder to github branch
* @param {string} folderToDeploy
* @returns
*/
async function workingOnGit(folderToDeploy) {
console.log(
colorReference.FgMagenta,
"....Working on git....",
colorReference.Reset
);
// checking git and working on git
if (!shell.which("git")) {
console.log(
colorReference.FgRed,
"Sorry, this script requires git",
colorReference.Reset
);
shell.exit(1);
}
// Findout git origin
const CURRENT_GIT_ORIGIN_RESULT = shell.exec(
"git config --get remote.origin.url"
);
if (CURRENT_GIT_ORIGIN_RESULT.code !== 0) {
console.log(
colorReference.FgRed,
"Error: Please make sure you have git initialized and added the remote origin, Try again later..",
colorReference.Reset
);
shell.exit(1);
}
const CURRENT_GIT_ORIGIN = CURRENT_GIT_ORIGIN_RESULT.stdout.replace(
/(\n|\r)/g,
""
);
console.log(
colorReference.FgCyan,
"Your current git origin: ",
CURRENT_GIT_ORIGIN,
colorReference.Reset
);
// switching to release directory to work on git operation
shell.cd(folderToDeploy);
// initialized git
shell.exec("git init");
// Set remote origin
shell.exec(`git remote add origin "${CURRENT_GIT_ORIGIN}"`);
// Get prefer branch name and commit message
const DEFAULT_BRANCH_NAME = "frontend-release";
const DEFAULT_COMMIT_MESSAGE = "release with build";
const { branchName, commitMessage } = await prompt([
{
type: "input",
name: "branchName",
message: `Enter your prefer branch name: [default:${DEFAULT_BRANCH_NAME}]: `,
},
{
type: "input",
name: "commitMessage",
message: `Enter your commit message: [default:${DEFAULT_COMMIT_MESSAGE}]: `,
},
]);
const BRANCH_NAME = branchName || DEFAULT_BRANCH_NAME;
const COMMIT_MESSAGE = commitMessage || DEFAULT_COMMIT_MESSAGE;
// Do basic git operation to push to prefer branch
shell.exec(`git checkout -b "${BRANCH_NAME}"`);
shell.exec(`git add .`);
shell.exec(`git commit -m "${COMMIT_MESSAGE}"`);
shell.exec(`git push -f origin "${BRANCH_NAME}"`);
console.info(
colorReference.FgGreen,
`${folderToDeploy} is successfully Deployed to git 😀 ,please check on your ${BRANCH_NAME} branch in github(${CURRENT_GIT_ORIGIN})`,
colorReference.Reset
);
return { CURRENT_GIT_ORIGIN, BRANCH_NAME, COMMIT_MESSAGE };
}