-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
85 lines (71 loc) · 2.36 KB
/
deploy.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
'use strict';
const childProcess = require('child_process');
const fs = require('fs');
const pRequest = require('util').promisify(require('request'));
const config = {
GEMFURY_API_TOKEN: `${process.env.GEMFURY_API_TOKEN}`.trim(),
GEMFURY_USERNAME: `${process.env.GEMFURY_USERNAME}`.trim(),
};
function fail(...msg) {
console.error(...msg);
process.exit(1);
}
if (!config.GEMFURY_API_TOKEN) {
fail(`error GEMFURY_API_TOKEN environment variable must be set`);
}
if (!config.GEMFURY_USERNAME) {
fail(`error GEMFURY_USERNAME environment variable must be set`);
}
const repoDir = './repo';
const packageFile = `${repoDir}/package.json`;
async function main() {
let packageJson = null;
try {
packageJson = JSON.parse(fs.readFileSync(packageFile));
} catch (e) {
fail(`error finding ${packageFile}, ensure the repo directory is attached to the container as a volume`);
}
if (!packageJson.name) {
fail(`error finding a "name" property in ${packageFile}`);
}
if (!packageJson.version) {
fail(`error finding a "version" property in ${packageFile}`);
}
let remoteVersions;
try {
const remotePackage = await pRequest({
url: `https://npm.fury.io/${encodeURIComponent(config.GEMFURY_API_TOKEN)}/${encodeURIComponent(config.GEMFURY_USERNAME)}/${encodeURIComponent(packageJson.name)}`,
json: true
});
remoteVersions = Object.keys(remotePackage.body.versions);
} catch (err) {
fail(`error determining remote repo version\n${err}`);
}
if (remoteVersions.includes(packageJson.version)) {
console.log(`no publish - ${packageJson.version} already exists on Gemfury ${remoteVersions.join(', ')}`);
process.exit(0);
}
const npmPackSpawn = childProcess.spawnSync('npm', ['pack'], {cwd: repoDir});
if (npmPackSpawn.stderr.length || npmPackSpawn.status !== 0) {
fail(`error packing repo\n${npmPackSpawn.stderr.toString().trim()}`);
}
let packFile = npmPackSpawn.stdout.toString().trim();
console.log(`publishing ${packageJson.version}`);
try {
await pRequest({
method: 'POST',
url: `https://${config.GEMFURY_API_TOKEN}@push.fury.io/${config.GEMFURY_USERNAME}/`,
formData: {
packageJson: fs.createReadStream(`${repoDir}/${packFile}`)
}
});
} catch (err) {
fail(`error publish failed\n${err}`);
}
try {
fs.unlinkSync(`${repoDir}/${packFile}`);
} catch (err) {
fail(`error unable to delete pack file ${repoDir}/${packFile}`)
}
}
main();