-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathinstall.js
117 lines (108 loc) · 4.3 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
115
116
117
#! /usr/bin/env node
const extract = require('extract-zip');
const url = require('url');
const HttpsProxyAgent = require('https-proxy-agent');
const https = require('https');
const version = require('../package.json').version;
const consolidatedBuildId = "4.0." + require('../package.json').consolidatedBuildId;
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const ProgressBar = require('progress');
const os = require('os');
function getPath() {
const bin = path.resolve(path.join(path.dirname(__filename), '..', 'bin'));
if (fs.existsSync(bin)) {
rimraf.sync(bin);
}
return bin
}
let platform = '';
if (os.platform() === 'win32') {
if (os.arch() === 'arm64') {
platform = 'win-arm64';
} else {
platform = 'win-x64';
}
} else if (os.platform() === 'darwin') {
if (os.arch() === 'arm64') {
platform = 'osx-arm64';
} else {
platform = 'osx-x64';
}
} else if (os.platform() === 'linux') {
platform = 'linux-x64';
} else {
throw Error('platform ' + os.platform() + ' isn\'t supported');
}
const fileName = 'Azure.Functions.Cli.' + platform + '.' + version + '.zip';
const endpoint = 'https://functionscdn.azureedge.net/public/' + consolidatedBuildId + '/' + fileName;
console.log('attempting to GET %j', endpoint);
const options = url.parse(endpoint);
// npm config preceed system environment
// https://github.com/npm/npm/blob/19397ad523434656af3d3765e80e22d7e6305f48/lib/config/reg-client.js#L7-L8
// https://github.com/request/request/blob/b12a6245d9acdb1e13c6486d427801e123fdafae/lib/getProxyFromURI.js#L66-L71
const proxy = process.env.npm_config_https_proxy ||
process.env.npm_config_proxy ||
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy;
const telemetryInfo = os.EOL
+ 'Telemetry' + os.EOL
+ '---------' + os.EOL
+ 'The Azure Functions Core tools collect usage data in order to help us improve your experience. ' + os.EOL
+ 'The data is anonymous and doesn\'t include any user specific or personal information. The data is collected by Microsoft.' + os.EOL
+ os.EOL
+ 'You can opt-out of telemetry by setting the FUNCTIONS_CORE_TOOLS_TELEMETRY_OPTOUT environment variable to \'1\' or \'true\' using your favorite shell.' + os.EOL
if (proxy) {
console.log('using proxy server %j', proxy);
options.agent = new HttpsProxyAgent(proxy);
}
https.get(options, response => {
const bar = new ProgressBar('[:bar] Downloading Azure Functions Core Tools', {
total: Number(response.headers['content-length']),
width: 18
});
if (response.statusCode === 200) {
const installPath = getPath();
const downloadPath = installPath + '/' + fileName;
response.on('data', data => bar.tick(data.length));
if (!fs.existsSync(installPath)) {
fs.mkdirSync(installPath);
}
const file = fs.createWriteStream(downloadPath);
response.pipe(file);
file.on('finish', function() {
file.close(() => {
extract(file.path, {
dir: installPath
}).then(() => {
try {
fs.closeSync(fs.openSync(`${installPath}/telemetryDefaultOn.sentinel`, 'w'));
console.log(telemetryInfo);
fs.unlinkSync(downloadPath);
}
catch (err) {
// That's alright.
}
if (os.platform() === 'linux' || os.platform() === 'darwin') {
fs.chmodSync(`${installPath}/func`, 0o755);
fs.chmodSync(`${installPath}/gozip`, 0o755);
fs.chmodSync(`${installPath}/in-proc8/func`, 0o755);
fs.chmodSync(`${installPath}/in-proc6/func`, 0o755);
}
});
});
});
} else {
console.error(chalk.red('Error downloading zip file from ' + endpoint));
console.error(chalk.red('Expected: 200, Actual: ' + response.statusCode));
process.exit(1);
}
})
.on('error', err => {
console.error(chalk.red(err));
process.exit(1);
});