forked from andrewzlchen/realm-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
104 lines (89 loc) · 2.99 KB
/
Copy pathinstall.js
File metadata and controls
104 lines (89 loc) · 2.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
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
const fs = require('fs');
const path = require('path');
const request = require('request');
const versionMetadata = require('./version');
const basedownloadURL = `https://s3.amazonaws.com/realm-clis/${versionMetadata.baseDirectory}`;
const linuxdownloadURL = `${basedownloadURL}/linux-amd64/realm-cli`;
const macdownloadURL = `${basedownloadURL}/macos-amd64/realm-cli`;
const windowsdownloadURL = `${basedownloadURL}/windows-amd64/realm-cli.exe`;
function getdownloadURL() {
const platform = process.platform;
let downloadURL;
if (platform === 'linux') {
if (process.arch === 'x64') {
downloadURL = linuxdownloadURL;
} else {
throw new Error('Only Linux 64 bits supported.');
}
} else if (platform === 'darwin' || platform === 'freebsd') {
if (process.arch === 'x64' || process.arch === 'arm64') {
downloadURL = macdownloadURL;
} else {
throw new Error('Only Mac 64 bits supported.');
}
} else if (platform === 'win32') {
if (process.arch === 'x64') {
downloadURL = windowsdownloadURL;
} else {
throw new Error('Only Windows 64 bits supported.');
}
} else {
throw new Error(`Unexpected platform or architecture: ${process.platform} ${process.arch}`);
}
return downloadURL;
}
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');
}
}
}
function requstBinary(downloadURL, baseName) {
console.log(`downloading "${baseName}" from "${downloadURL}"`);
return new Promise((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');
const requestOptions = {
uri: downloadURL,
method: 'GET',
};
const client = request(requestOptions);
client.on('error', err => {
reject(new Error(`Error with http(s) request: ${err}`));
});
client.on('data', 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;
}
});
client.on('end', () => {
console.log(`Received ${Math.floor(count / 1024)} K total.`);
fs.closeSync(outFile);
fixFilePermissions(filePath);
resolve(true);
});
});
}
// Install script starts here
let downloadURL;
try {
downloadURL = getdownloadURL();
} catch (err) {
console.error('Realm CLI installation failed:', err);
process.exit(1);
}
requstBinary(downloadURL, 'realm-cli').catch(err => {
console.error('failed to download Realm CLI:', err);
process.exit(1);
});