forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-node-version
executable file
·121 lines (106 loc) · 3.94 KB
/
set-node-version
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
118
119
120
121
#!/usr/bin/env node
'use strict';
let crypto = require('crypto');
let fs = require('fs');
let https = require('https');
let knox = require('knox').createClient({
key: process.env.HEROKU_RELEASE_ACCESS,
secret: process.env.HEROKU_RELEASE_SECRET,
bucket: 'heroku-cli',
region: 'us-west-2',
});
const outputPath = '/./gode/constants.go';
const version = '4.1.0';
const npmVersion = '2.14.3';
const urlBase = `https://nodejs.org/download/release/v${version}`;
let npmURL = `https://github.com/npm/npm/archive/v${npmVersion}.zip`;
let npmSha;
let targets = [
{arch: '386', os: 'linux', url: urlBase+`/node-v${version}-linux-x86.tar.gz`, base: `node-v${version}-linux-x86`},
{arch: 'amd64', os: 'linux', url: urlBase+`/node-v${version}-linux-x64.tar.gz`, base: `node-v${version}-linux-x64`},
{arch: 'arm', os: 'linux', url: urlBase+`/node-v${version}-linux-armv7.tar.gz`, base: `node-v${version}-linux-armv7`},
{arch: 'amd64', os: 'darwin', url: urlBase+`/node-v${version}-darwin-x64.tar.gz`, base: `node-v${version}-darwin-x64`},
{arch: '386', os: 'windows', url: urlBase+`/win-x86/node.exe`, base: `node-v${version}-windows-x86`},
{arch: 'amd64', os: 'windows', url: urlBase+`/win-x64/node.exe`, base: `node-v${version}-windows-x64`},
];
function sha (path) {
return new Promise(function (fulfill) {
let fd = fs.createReadStream(path);
let hash = crypto.createHash('sha256');
hash.setEncoding('hex');
fd.on('end', function () {
hash.end();
fulfill(hash.read());
});
fd.pipe(hash);
});
}
function download (url, path) {
return new Promise(function (fulfill, reject) {
https.get(url, function (res) {
if (res.statusCode >= 300 && res.statusCode <= 399) return fulfill(download(res.headers.location, path));
if (res.statusCode <= 199 || res.statusCode >= 400) return reject(new Error(res.statusCode));
res.pipe(fs.createWriteStream(path));
res.on('end', fulfill);
})
});
}
function s3upload (local, remote) {
let key = remote.replace('https://cli-assets.heroku.com/', '/');
return new Promise(function (fulfill, reject) {
knox.putFile(local, key, {
'x-amz-acl': 'public-read',
'x-amz-meta-Cache-Control': 'public,max-age=86400',
'Cache-Control': 'public,max-age=86400',
}, function (err, res) {
if (err) return reject(err);
res.resume();
fulfill();
});
});
}
function processTarget(target) {
let path = `./tmp/${target.arch}-${target.os}-v${version}`;
return download(target.url, path)
.then(() => sha(path))
.then((sha) => target.sha = sha)
.then(function () {
target.url = target.url.replace('https://nodejs.org/download/release/', 'https://cli-assets.heroku.com/node/');
return s3upload(path, target.url);
});
}
function processNpm() {
let path = './tmp/npm.zip';
return download(npmURL, path)
.then(() => sha(path))
.then((sha) => npmSha = sha)
.then(function () {
npmURL = npmURL.replace('https://github.com/npm/npm/archive/', 'https://cli-assets.heroku.com/npm/');
return s3upload(path, npmURL);
});
}
function output () {
let output = `package gode
//
// DO NOT EDIT
//
// THIS FILE IS GENERATED WITH ./set-node-version
//
// Version is the requested node version
const Version = "${version}"
// NpmVersion is the requested npm version
const NpmVersion = "${npmVersion}"
const npmSha = "${npmSha}"
const npmURL = "${npmURL}"
var targets = []Target{
`;
for (let target of targets) output += `\t{"${target.arch}", "${target.os}", "${target.url}", "${target.base}", "${target.sha}"},\n`;
output += '}\n';
return output;
}
console.log(`Setting node version to ${version} and npm to ${npmVersion}...`);
Promise.all(targets.map(processTarget))
.then(processNpm)
.then(() => fs.writeFileSync(__dirname + outputPath, output()))
.then(function () { console.log(`${outputPath} updated`); })
.catch(function (err) { console.error(err.stack); process.exit(1); });