-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.js
274 lines (235 loc) · 6.3 KB
/
repo.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
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
const debug = require('diagnostics');
const NPM = require('npm-shizzle');
const rmrf = require('rimraf');
const path = require('path');
const fs = require('fs');
/**
* Representation of a single repo.
*
* @param {Mono} mono Reference to the wrapping mono instance.
* @param {String} name Name of the repo we represent.
* @public
*/
class Repo {
constructor(mono, name) {
this.mono = mono;
this.name = name;
const options = this.configure();
this.log = debug('mono:repo:'+ this.name);
this.root = mono.resolve(this.name);
this.npm = new NPM(this.root, {
silent: 'silent' in options ? options.silent : false
});
this.manifest = path.join(this.root, 'package.json');
}
/**
* Create a configuration object with the options supplied to the function
* and those of our `mono` instance.
*
* @param {Object} options Supplied options.
* @returns {Object} Merged options.
* @public
*/
configure(options = {}) {
return Object.assign({}, this.mono.options, options);
}
/**
* Read the `package.json` of the repo.
*
* @returns {Object} The package.json
* @public
*/
read() {
return JSON.parse(fs.readFileSync(this.manifest, 'utf-8'));
}
/**
* Install all the dependencies.
*
* @returns {Boolean} Indication of successful installation.
* @public
*/
install() {
this.log('installing dependencies');
try { this.npm.install(); }
catch (e) {
this.log('failed to `npm install`', e);
return false;
}
//
// After installation we want to make sure that our `npm` client knows this
// library as a candidate for `npm link <name>` so we can symlink all
// projects together after installation if needed.
//
this.log('registering project with `npm link`');
try { this.npm.link(); }
catch (e) {
this.log('failed to `npm link`', e);
return false;
}
return true;
}
/**
* Run the tests of the repo.
*
* @returns {Boolean} Indication if tests pass or fail.
* @public
*/
test() {
this.log('running test suite');
try { this.npm.runScript('test'); }
catch (e) {
this.log('failed to `npm test`', e);
return false;
}
return true;
}
/**
* Symlink all known packages/repos.
*
* @returns {Boolean} Successful execution.
* @public
*/
link() {
this.log('connecting all mono-repo packages together with symlinks');
const { dependencies, devDependencies } = this.read();
const packages = this.mono.packages();
let success = true;
//
// Search the dependencies and devDependencies for package name that match
// with the names of our hosted mono packages. All matching names will be
// symlinked.
//
[
...Object.keys(dependencies || {}),
...Object.keys(devDependencies || {})
]
.filter(Boolean)
.filter((name, i, arr) => arr.indexOf(name) === i)
.filter((name) => !!~packages.indexOf(name))
.forEach((name) => {
this.log(`found package that exists in the mono repo: ${name}`);
try { this.npm.link(name); }
catch (e) {
this.log('failed to `npm link` '+ name, e);
success = false;
}
});
return success;
}
/**
* Clean up the directory.
*
* @returns {Boolean} Successful execution.
* @public
*/
uninstall() {
this.log('unstalling the dependencies');
let success = true;
[
path.join(this.root, 'node_modules')
].forEach((dir) => {
this.log(`rm-rf directory ${dir}`);
try { rmrf.sync(dir); }
catch (e) { success = false; }
});
return success;
}
/**
* Cuts a new release.
*
* @param {Object} options Optional configuration.
* @public
*/
publish(options) {
options = this.configure(options);
const pkg = this.read();
const name = pkg.name;
const git = this.mono.git;
const version = options.version || this.bump(pkg.version, options.release);
//
// Step 1: Update the package.json to the new version.
//
pkg.version = version;
this.log('step (1) updating package.json{version} to %s', version);
fs.writeFileSync(this.manifest, JSON.stringify(pkg, null, 2));
//
// Step 2: Commit the change
//
const message = JSON.stringify(`[dist] Release ${name}@${version} ${options.message || ''}`.trim());
//
// We want to make sure that we only commit changes of this repo not the
// other packages so we cannot do a `commit -a` but need to add our folder
// manually.
//
this.log('step (2) adding commit message to git history', message);
try {
git.add(this.root);
git.commit(`-nm ${message}`);
} catch (e) {
this.log('step (2) failed to `git commit`', e);
return false;
}
//
// Step 3: Tag the release.
//
const tag = `${name}@${version}`;
this.log('step (3) generating git', version);
try { git.tag(`-a "${tag}" -m ${message}`); }
catch (e) {
this.log('step (3) failed to add git tag', e);
return false;
}
//
// Step 4: Push the release to the server.
//
this.log('step (4) Pushing tags and commits to master');
try {
git.push('origin master');
git.push('--tags');
} catch (e) {
this.log('step (4) failed to push to master', e);
return false;
}
//
// Step 5: Publish the bundle to the registery.
//
this.log('step (5) publishing package');
try { this.npm.publish(); }
catch (e) {
this.log('step (5) failed publish package', e);
return false;
}
return true;
}
/**
* Bump the version number to the next release.
*
* @param {String} version The version number to bump.
* @param {String} release Type of release we need to cut.
* @returns {String} Updated version string.
* @private
*/
bump(version, release = '') {
version = version.split('.');
switch (release.toLowerCase()) {
case 'major':
version[0] = Number(version[0]) + 1;
version[1] = 0;
version[2] = 0;
break;
case 'minor':
version[1] = Number(version[1]) + 1;
version[2] = 0;
break;
case 'patch':
default:
version[2] = Number(version[2]) + 1;
break;
}
return version.join('.');
}
}
//
// Export the Repo class.
//
module.exports = Repo;