This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
157 lines (136 loc) · 4.05 KB
/
util.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
"use strict";
var gulp = require("gulp");
var git = require("gulp-git");
var fs = require("fs");
var argv = require("yargs").argv;
var readline = require("readline");
var path = require("path");
var version = require("conventional-recommended-bump");
var $ = module.exports;
var extend = require("util")._extend;
var runSequence = require("async").series;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
$.conf = {
token: null,
preset: "angular",
testTask: null,
appDir: path.dirname(require.main.filename),
versionFiles: [
"package.json"
]
};
$.createTmpBranch = function (done, opts) {
var name = "tmp-" + Math.floor(Math.random() * 10000);
var myOpts = extend({}, opts);
myOpts.args = myOpts.args?myOpts.args+" -b":"-b";
git.checkout(name, myOpts, function (err) {
done(err, name);
});
};
$.packageVersion = function (file) {
// We parse the json file instead of using require because require caches
// multiple calls so the version number won"t be updated
file = file || path.resolve($.conf.appDir, "./package.json");
return JSON.parse(fs.readFileSync(file, "utf8")).version;
};
$.commitChangesStream = function (opts) {
var myOpts = extend({}, opts);
return gulp.src($.conf.appDir)
.pipe(git.add(myOpts))
.pipe(git.commit("[Prerelease] Bumped version number", myOpts));
};
$.mergeInto = function (branch, done, opts) {
var chckOpts = extend({}, opts);
var mergeOpts = extend({}, opts);
mergeOpts.args = mergeOpts.args?mergeOpts.args + " --no-ff":" --no-ff";
if (!argv.b) {
throw new Error("You must set a branch with -b argument");
}
git.checkout(branch, chckOpts, function () {
git.merge(argv.b, mergeOpts, done);
});
};
$.calculateVersion = function (done) {
version({
preset: $.conf.preset
}, done);
};
$.askContinue = function (text, callback, must) {
must = must || false;
rl.question(text + " (Default to Yes): ", function (answer) {
if (!answer.match(/not|no|n/i)) {
callback(true);
} else if(must) {
throw new Error("The last question must be answered with a yes YES for this task keeps going");
} else {
callback(false);
}
});
};
$.askDeleteBranch = function (branch, callback, opts) {
var myOpts = extend({}, opts);
var pushOpts = extend({}, opts);
myOpts.args = myOpts.args?myOpts.args+" -d":"-d";
pushOpts.args = pushOpts.args?pushOpts.args+" --delete":"--delete";
$.askContinue("Want to delete local and remote " + branch + " branch? ", function (did) {
if (did) {
runSequence([
// Delete local branch
function(next){
git.branch(branch, myOpts, next);
},
// Delete remote branch
function(next){
git.push("origin", branch, pushOpts, next);
}
], function(err, result){
if(err) {
callback(false); // I know it's weird but I do not figured out how to test this throw in util.test.js
} else {
callback(true);
}
});
} else {
callback(false);
}
});
};
$.askPushTo = function (local, remote, callback, opts) {
var pushOpts = extend({}, opts);
pushOpts.args = pushOpts.args?pushOpts.args + " -u":"-u";
$.askContinue("Want to push the local " + local + " branch to " + remote + " repository?", function (did) {
if (did) {
git.push(remote, local, pushOpts, function(err){
if (err) {
callback(false); // I know it's weird but I do not figured out how to test this throw in util.test.js
} else {
callback(true);
}
});
} else {
callback(false);
}
});
};
/*
* Recursively merge properties of two objects
*/
$.merge = function (obj1, obj2) {
for (var p in obj2) {
try {
// Property in destination object set; update its value.
if ( obj2[p].constructor==Object ) {
obj1[p] = $.merge(obj1[p], obj2[p]);
} else {
obj1[p] = obj2[p];
}
} catch(e) {
// Property in destination object not set; create it and set its value.
obj1[p] = obj2[p];
}
}
return obj1;
};