-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
208 lines (179 loc) · 5.58 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
#!/usr/bin/env node
var app = require('commander');
var fs = require('fs');
var path = require('path');
var semver = require('semver');
var glob = require('glob');
var cp = require('child_process');
var CWD = process.cwd();
var ROOT = path.dirname(__filename);
var DX_FLOW_HOOKS_DIRECTORY = path.resolve(ROOT, 'hooks');
var GITFLOW_PATH = path.resolve(ROOT, 'gitflow', 'git-flow');
var LAUNCHER_PATH = path.resolve(ROOT, 'launcher.sh');
var AVAILABLE_HOOKS = getHooks(DX_FLOW_HOOKS_DIRECTORY);
var HOOKS_DIRECTORY_POSTFIX = '-hooks'; //this is used in launcher.sh
var DESTINATION_HOOKS_DIRECTORY = path.resolve(CWD, '.git', 'hooks');
var DESTINATION_PACKAGE_JSON = path.resolve(CWD, 'package.json');
var DESTINATION_BOWER_JSON = path.resolve(CWD, 'bower.json');
var bump = require(path.resolve(DX_FLOW_HOOKS_DIRECTORY, 'post-checkout/autobump.js'));
var BRANCH_MAP = {
major: 'release',
minor: 'release',
patch: 'hotfix'
};
if (ROOT === CWD) {
throw new Error('Do not run dx-flow in its own directory');
}
app
.version(require(path.resolve(ROOT, 'package.json')).version, '')
.usage('<command> [options]');
app
.command('list')
.description('Lists all available hooks')
.action(function(options) {
AVAILABLE_HOOKS.forEach(function(hook) {
console.log(hook);
});
});
app
.command('hook [name]')
.description('Install hook by its name')
.action(function(name, options) {
if (!name) {
throw new Error('Please specify hook name');
}
hook(name);
});
app
.command('clean')
.description('Removes all hooks')
.action(function(options) {
getHooks(DESTINATION_HOOKS_DIRECTORY).forEach(function(name) {
console.log('Removing hook', name + '...');
var destination = path.resolve(DESTINATION_HOOKS_DIRECTORY, name);
exec('rm ' + destination);
});
console.log('Done');
});
app
.command('install')
.description('Installs npm dependencies')
.action(function(options) {
if (fs.existsSync(DESTINATION_PACKAGE_JSON)) {
var packageJson = require(DESTINATION_PACKAGE_JSON);
var dependencies = Object
.keys(packageJson.dependencies)
.map(key => `${key}@${packageJson.dependencies[key]}`)
.join(' ');
console.log('Installing', dependencies);
exec('npm i ' + dependencies);
}
});
app
.command('start <version>')
.description('Starts new branch (release/hotfix) with bumped version from package.json (major|minor|patch)')
.action(function(version, options) {
if (['major', 'minor', 'patch'].indexOf(version) === -1) {
throw new Error('Unknown version, should be one of (major|minor|patch)');
}
if (fs.existsSync(DESTINATION_PACKAGE_JSON)) {
var packageJson = require(DESTINATION_PACKAGE_JSON);
var bumpedVersion = semver.inc(packageJson.version, version);
console.log('Starting ' + version + ' from ' + packageJson.version + ' to ' + bumpedVersion);
gitflow(BRANCH_MAP[version] + ' start ' + bumpedVersion);
var changedFiles = [DESTINATION_PACKAGE_JSON, DESTINATION_BOWER_JSON].filter(function(file) {
return bump(file, bumpedVersion);
});
console.log('Bumped ' + changedFiles.join(' '));
commit('bump version ' + bumpedVersion, changedFiles);
console.log('Commited ' + changedFiles.join(' '));
switch (version) {
case 'major': //fall through
case 'minor':
gitflow(BRANCH_MAP[version] + ' finish ' + bumpedVersion + ' -n');
break;
case 'patch':
break;
}
} else {
throw new Error(DESTINATION_PACKAGE_JSON + ' is not found');
}
});
app
.command('*')
.description('Proxy to git-flow')
.action(function(name, options) {
return gitflow(process.argv.slice(2).join(' '));
});
app
.parse(process.argv);
if (!app.args.length) {
app.help();
}
/**
* Install hook by its name (without extension)
* @param {String} name
*/
function hook(name) {
var split = name.split('/');
var namespace = split[0];
var part = split[1];
//check if hook exists
var file = path.resolve(DX_FLOW_HOOKS_DIRECTORY, name);
if (!fs.existsSync(file)) {
throw new Error('Hook ' + name + ' not found!');
}
//resolve paths
var destinationDirectory = path.resolve(DESTINATION_HOOKS_DIRECTORY, namespace + HOOKS_DIRECTORY_POSTFIX);
//install launcher
console.log('Installing launcher ' + namespace + '...');
exec('ln -fs ' + LAUNCHER_PATH + ' ' + path.resolve(DESTINATION_HOOKS_DIRECTORY, namespace));
//install hook
console.log('Installing hook ' + name + '...');
if (!fs.existsSync(destinationDirectory)) {
exec('mkdir ' + destinationDirectory);
}
var destination = path.resolve(destinationDirectory, part);
exec('ln -fs ' + file + ' ' + destination);
}
/**
* Gets all available hooks
* @param {String} directory
* @returns {Array.<String>}
*/
function getHooks(directory) {
return glob.sync(path.join(directory, '*/*.*')).map(function(file) {
return path.relative(directory, file).replace(/\\/g, '/');
});
}
/**
* Launches gitflow with specified argument string, inherits stdio
* @param {String} argsString
* @returns {*}
*/
function gitflow(argsString) {
return exec('bash ' + GITFLOW_PATH + ' ' + argsString);
}
/**
* Synchronously executes passed command inheriting stdio
* @param {String} command
* @returns {*}
*/
function exec(command) {
return cp.execSync(command, {
stdio: 'inherit'
});
}
/**
* Commits specified files under specified message using git
* @param {String} message
* @param {Array.<String>} files
*/
function commit(message, files) {
if (files.length !== 0) {
var escapedMessage = '"' + message.replace(/"/g, '\"') + '"';
var scapedFiles = '"' + files.join('" "') + '"';
var gitDir = '--git-dir "' + CWD + '/.git"';
exec('git ' + gitDir + ' commit -m ' + escapedMessage + ' ' + scapedFiles);
}
}