-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart-up.js
180 lines (160 loc) · 4.71 KB
/
dart-up.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#! /usr/bin/env node
'use strict';
var https = require('https');
var fs = require('fs');
var path = require('path');
var AdmZip = require('adm-zip');
var program = require('commander');
function checkPlatform() {
var platforms = ['darwin', 'linux', 'win32'];
if (platforms.indexOf(process.platform) === -1) {
throw 'Platform (' + process.platform + ') not supported!';
}
}
function checkArch() {
var architectures = ['x64', 'ia32'];
if (architectures.indexOf(process.arch) === -1) {
throw 'Architecture (' + process.arch + ') not supported!';
}
}
function getUrl(version, thing) {
var channel = program.stable ? 'stable' : 'dev';
var parts = [];
parts.push({
SDK: 'sdk/dartsdk',
Dartium: 'dartium/dartium',
docs: 'api-docs/dart-api-docs'
}[thing]);
if (thing !== 'docs') {
// current platform
var platform = {
darwin: 'macos',
linux: 'linux',
win32: 'windows'
}[process.platform];
parts.push(platform);
// current arch
var arch = process.arch;
if (thing === 'Dartium' && (platform === 'windows' || platform === 'macos')) {
// there are no 64bit versions of Dartium for Windows or Mac OS
arch = 'ia32';
}
parts.push(arch);
parts.push('release');
}
return 'https://storage.googleapis.com/dart-archive/channels/' + channel + '/release/' + version + '/' + parts.join('-') + '.zip';
}
function fetchRemoteVersion() {
var channel = program.stable ? 'stable' : 'dev';
var versionUrl = 'https://storage.googleapis.com/dart-archive/channels/' + channel + '/release/latest/VERSION';
return new Promise(function(resolve, reject) {
https.get(versionUrl, function(response) {
var json = '';
response.on('data', function(data) {
json += data;
});
response.on('end', function() {
resolve(JSON.parse(json));
});
response.on('error', function(e) {
reject(e.message);
});
});
});
}
function fetchLocalVersion() {
return new Promise(function(resolve, reject) {
fs.readFile('.dart-up', function(err, data) {
if (err) {
resolve({});
} else {
resolve(JSON.parse(data));
}
});
});
}
function download(url, fileName) {
return new Promise(function(resolve, reject) {
var file = fs.createWriteStream(fileName);
https.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(function(){
resolve(true);
});
});
});
});
}
function downloadAndExtract(url) {
var fileName = url.split('/').pop(); // XXX: use os.tmpdir()?
console.log('[' + fileName + '] downloading...');
return new Promise(function(resolve, reject) {
download(url, fileName).then(function() {
console.log('[' + fileName + '] extracting...');
try {
var zip = new AdmZip(fileName);
// Special case for Dartium.
// We need to put the stuff from the "dartium-xxx-full-dev-xxx" directory into "chromium".
if (fileName.indexOf('dartium') !== -1) {
var entry = zip.getEntries()[0];
zip.extractEntryTo(entry.entryName, 'chromium', /*maintainEntryPath*/false, /*overwrite*/true);
} else {
zip.extractAllTo('', /*overwrite*/true);
}
resolve(true);
} catch(e) {
reject(e);
}
});
});
}
function getPackageVersion() {
return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'))).version;
}
(function main() {
checkPlatform();
checkArch();
program
.version(getPackageVersion())
.option('-s, --stable', 'use the stable channel')
.option('-d, --docs', 'include docs')
.option('-D, --no-dartium', 'exclude Dartium')
.option('-f, --force', 'update even if the version numbers are identical');
program.on('--help', function() {
console.log(' By default, dart-up uses the "dev" channel. The SDK is always downloaded. ');
console.log(' Dartium can be excluded while the docs can be included.');
});
program.parse(process.argv);
Promise.all([fetchLocalVersion(), fetchRemoteVersion()]).then(function(versions) {
var local = versions[0];
var remote = versions[1];
if (local.revision === remote.revision && !program.force) {
console.log(local.version + ' (' + local.revision + ') is already installed.');
console.log('Nothing to update.');
} else {
if (local.version) {
console.log('Updating [' + local.version + '] to [' + remote.version + '].');
} else {
console.log('Installing [' + remote.version + '].');
}
var tools = ['SDK'];
if (program.dartium) {
tools.push('Dartium');
}
if (program.docs) {
tools.push('docs');
}
Promise.all(
tools
.map(getUrl.bind(null, remote.version))
.map(downloadAndExtract)
).then(function() {
fs.writeFileSync('.dart-up', JSON.stringify(remote));
console.log('[' + tools.join(', ') + '] successfully updated!');
}).catch(function(e) {
console.log(e);
});
}
});
}());