-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (173 loc) · 4.62 KB
/
index.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const inquirer = require('./lib/inquirer');
const git = require('./lib/git');
const github = require('./lib/github');
const program = require('commander');
const conf = new (require('configstore'))('gitflow');
const exec = require('util').promisify(require('child_process').exec);
program.version(
JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json')))['version'] || 'test-mode', '-v, --version'
);
program
.command('finish-feature')
.description('Finish feature')
.action(async () => {
try {
await git.bailIfNotGitDirectory();
await git.finishFeature(git.getConfig(process.cwd()));
} catch (e) {
console.log(e.message);
}
});
program
.command('finish-hotfix')
.description('Finish hotfix')
.action(async () => {
try {
await git.bailIfNotGitDirectory();
await git.finishHotfix(git.getConfig(process.cwd()));
} catch (e) {
console.log(e.message);
}
});
program
.command('finish-sprint')
.description('Finish Sprint')
.action(async () => {
try {
await git.bailIfNotGitDirectory();
await git.finishSprint(git.getConfig(process.cwd()));
} catch (e) {
console.log(e.message);
}
});
program
.command('generate-token')
.description('Generate Github personal access token to be used by gitflow')
.action(async () => {
try {
await github.generateToken();
} catch (e) {
console.log(e.message);
}
});
program
.command('get-config')
.description('Set default project and sprint number')
.action(() => {
try {
console.log(git.getConfig(process.cwd()));
} catch (e) {
console.log(e.message);
}
});
program
.command('remove-token')
.description('Remove Github personal access token from your local machine')
.action(async () => {
try {
await github.removeToken();
} catch (e) {
console.log(e.message);
}
});
program
.command('set-config')
.description('Set default project and sprint number')
.action(async () => {
try {
const config = await inquirer.askProjectNameAndSprintNumber({projectName: null, sprintNumber: null});
git.setConfig(process.cwd(), config);
} catch (e) {
console.log(e.message);
}
});
program
.command('start-feature')
.arguments('[feature-branch]')
.description('Create new feature branch')
.action(async (featureBranch) => {
try {
await git.bailIfNotGitDirectory();
await git.startFeature(git.getConfig(process.cwd()), featureBranch);
} catch (e) {
console.log(e.message);
}
});
program
.command('start-hotfix')
.arguments('[hotfix-branch]')
.description('Create new hotfix branch')
.action(async (hotfixBranch) => {
try {
await git.bailIfNotGitDirectory();
await git.startHotfix(hotfixBranch);
} catch (e) {
console.log(e.message);
}
});
program
.command('start-sprint')
.arguments('[project] [sprint]')
.description('Create new sprint branch')
.action(async (project, sprint) => {
try {
await git.bailIfNotGitDirectory();
await git.startSprint(git.getConfig(process.cwd()), project, sprint);
} catch (e) {
console.log(e.message);
}
});
program
.command('alias')
.arguments('<alias> <cmd...>')
.description('Create new alias for a command')
.action(async (alias, cmd) => {
const cmdAlias = {[`alias:${alias}`]: cmd.join(' ')};
try {
conf.set(cmdAlias);
console.log(cmdAlias);
} catch (e) {
console.log(e.message);
}
});
program
.command('soft-push')
.description('Perform soft push to trigger CI build')
.action(async () => {
try {
await git.bailIfNotGitDirectory();
await git.softPush();
} catch (e) {
console.log(e.message);
}
});
program.on('command:*', async (args) => {
const alias = args.shift();
let cmd = conf.get(`alias:${alias}`);
const pkg = path.join(process.cwd(), '/package.json');
// package.json alias overrides the common alias
if (fs.existsSync(pkg)) {
const {gitflow: {alias: aliases = {}} = {}} = require(pkg);
cmd = aliases[alias] || cmd;
}
try {
if (!cmd) {
throw new Error(`Unknown command or alias: ${alias}`);
}
console.log(`> ${cmd} ${args.join(' ')}`);
const proc = await exec(`${cmd} ${args.join(' ')}`).catch((err) => err);
if (proc.stdout !== '') {
console.log(proc.stdout);
}
if (proc.stderr !== '') {
console.error(proc.stderr);
}
process.exit(proc.exitCode || proc.code);
} catch (e) {
console.log(e.message);
}
});
program.parse(process.argv);