forked from OKEAMAH/bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocks.js
More file actions
77 lines (65 loc) · 1.99 KB
/
locks.js
File metadata and controls
77 lines (65 loc) · 1.99 KB
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
// @flow
import Package from '../Package';
import * as logger from './logger';
import * as messages from './messages';
import * as npm from './npm';
import { settleAll } from './promises';
import { BoltError } from './errors';
const LOCK_DIST_TAG = 'bolt-lock';
export async function lock(packages: Array<Package>) {
let locks = [];
let promises = [];
logger.info(messages.lockingAllPackages());
for (let pkg of packages) {
let name = pkg.config.getName();
let version = pkg.config.getVersion();
let promise = npm.infoAllow404(name).then(response => {
if (response.published) {
let pkgInfo = response.pkgInfo || {};
if (pkgInfo['dist-tags'][LOCK_DIST_TAG]) {
throw new BoltError(
`Unable to get lock as a lock already exists for '${name}'`
);
}
return npm.addTag(name, pkgInfo.version, LOCK_DIST_TAG).then(() => {
locks.push(pkg);
});
}
});
promises.push(promise);
}
try {
await settleAll(promises);
} catch (err) {
logger.error(err.message);
// Note: We only unlock the locks *we* just locked, as the other ones are currently being used
await _unlock(locks);
throw new BoltError(
'Unable to lock all packages, someone else may be releasing'
);
}
}
async function _unlock(packages: Array<Package>) {
let promises = [];
for (let pkg of packages) {
promises.push(npm.removeTag(pkg.config.getName(), LOCK_DIST_TAG));
}
await Promise.all(promises);
}
export async function unlock(packages: Array<Package>) {
let promises = [];
for (let pkg of packages) {
let name = pkg.config.getName();
let promise = npm.infoAllow404(name).then(response => {
if (response.published) {
let pkgInfo = response.pkgInfo || {};
if (!pkgInfo['dist-tags'][LOCK_DIST_TAG]) {
return;
}
return npm.removeTag(pkg.config.getName(), LOCK_DIST_TAG);
}
});
promises.push(promise);
}
await Promise.all(promises);
}