-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
executable file
·177 lines (161 loc) · 4.89 KB
/
commands.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
#!/usr/bin/env node
const { Command } = require("commander");
const shell = require("shelljs");
const path = require("path");
const fs = require("fs");
const { prompt } = require("inquirer");
const packageInfo = require("./package.json");
const CWD = process.cwd();
const program = new Command();
var DEFAULT_BRANCH_NAME = "frontend-release";
var DEFAULT_COMMIT_MESSAGE = "release with build";
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("Git Push Cli Application");
program
.option('-gb, --gitbranch <type>', 'default branch in git', DEFAULT_BRANCH_NAME);
program
.option('-gcm, --gitcommitmessage <type>', 'default commit message in git', DEFAULT_COMMIT_MESSAGE);
// Deploy any folder to github branch
program.argument("[folderName]").action(async (folderName) => {
try {
if (!folderName) {
const { inputFolderNumber } = await prompt([
{
type: "input",
name: "inputFolderNumber",
message: `Enter your prefer folderName to deploy in github: `,
},
]);
if (!inputFolderNumber) {
console.log(
colorReference.FgRed,
`Error: foldername can not be empty, please provide valid foldername`,
colorReference.Reset
);
shell.exit(1);
}
folderName = inputFolderNumber;
}
const userInputTargetFolderPath = path.join(CWD, folderName);
if (!folderName || !fs.existsSync(userInputTargetFolderPath)) {
console.log(
colorReference.FgRed,
`Error: Given folder ${userInputTargetFolderPath} does not exists or invalid folder, please provide valid foldername`,
colorReference.Reset
);
shell.exit(1);
}
DEFAULT_BRANCH_NAME=program.opts().gitbranch
DEFAULT_COMMIT_MESSAGE=program.opts().gitcommitmessage
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 { 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 };
}