forked from chickenbug/stitch-cli-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
executable file
·104 lines (89 loc) · 2.96 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
const fs = require('fs');
const path = require('path');
const request = require('request');
const versionMetadata = require('./version');
const baseDownloadURL = `https://s3.amazonaws.com/stitch-clis/${versionMetadata.baseDirectory}`;
const linuxDownloadURL = `${baseDownloadURL}/linux-amd64/stitch-cli`;
const macDownloadURL = `${baseDownloadURL}/macos-amd64/stitch-cli`;
const windowsDownloadURL = `${baseDownloadURL}/windows-amd64/stitch-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') {
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) {
console.log(`downloading stitch-cli from "${downloadUrl}"`);
return new Promise((resolve, reject) => {
let count = 0;
let notifiedCount = 0;
const binaryName = process.platform === 'win32' ? 'stitch-cli.exe' : 'stitch-cli';
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('Stitch CLI installation failed:', err);
process.exit(1);
}
requstBinary(downloadUrl).catch(err => {
console.error('Stitch CLI installation failed while downloading:', err);
process.exit(1);
});