-
Notifications
You must be signed in to change notification settings - Fork 23
/
install.js
114 lines (95 loc) · 3.2 KB
/
install.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
"use strict";
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const packageMetadata = require('./package');
const manifestURL =
'https://s3.amazonaws.com/realm-clis/versions/cloud-prod/CURRENT';
function fetchManifest() {
return axios(manifestURL)
.then(function (res) { return res.data; });
}
function getDownloadURL(manifest) {
const pastReleases = manifest.past_releases || [];
let tag = '';
if (process.platform === 'linux') {
if (process.arch !== 'x64') {
throw new Error('Only Linux 64 bits supported.');
}
tag = 'linux-amd64';
} else if (process.platform === 'darwin' || process.platform === 'freebsd') {
if (process.arch !== 'x64' && process.arch !== 'arm64') {
throw new Error('Only Mac 64 bits supported.');
}
tag = 'macos-amd64';
} else if (process.platform === 'win32') {
if (process.arch !== 'x64') {
throw new Error('Only Windows 64 bits supported.');
}
tag = 'windows-amd64';
}
if (tag === '') {
throw new Error(`Unexpected platform or architecture: ${process.platform} ${process.arch}`);
}
if (manifest.version !== packageMetadata.version) {
for (let i = 0; i < pastReleases.length; i++) {
const pastRelease = pastReleases[i];
if (pastRelease.version === packageMetadata.version) {
return pastRelease.info[tag].url;
}
}
}
return manifest.info[tag].url;
}
function requstBinary(downloadURL, baseName) {
baseName = baseName || 'realm-cli';
console.log(`downloading "${baseName}" from "${downloadURL}"`);
return new Promise(function (resolve, reject) {
let count = 0;
let notifiedCount = 0;
const binaryName =
process.platform === 'win32' ? baseName + '.exe' : baseName;
const filePath = path.join(process.cwd(), binaryName);
const outFile = fs.openSync(filePath, 'w');
axios.get(downloadURL, { responseType: 'stream' })
.then(function (res) { return res.data; })
.then(function (stream) {
stream.on('error', function (err) {
reject(new Error(`Error with http(s) request: ${err}`));
});
stream.on('data', function (data) {
fs.writeSync(outFile, data, 0, data.length, null);
count += data.length;
if (count - notifiedCount > 800000) {
process.stdout.write(`Received ${Math.floor(count / 1024)} K...\r`);
notifiedCount = count;
}
});
stream.on('end', function () {
console.log(`Received ${Math.floor(count / 1024)} K total.`);
fs.closeSync(outFile);
fixFilePermissions(filePath);
resolve(true);
});
})
.catch(reject);
});
}
function fixFilePermissions(filePath) {
// Check that the binary is user-executable and fix it if it isn't
if (process.platform !== 'win32') {
const stat = fs.statSync(filePath);
// 64 == 0100 (no octal literal in strict mode)
// eslint-disable-next-line no-bitwise
if (!(stat.mode & 64)) {
fs.chmodSync(filePath, '755');
}
}
}
fetchManifest()
.then(getDownloadURL)
.then(requstBinary)
.catch(function (err) {
console.error('failed to download Realm CLI:', err);
process.exit(1);
});