-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
294 lines (267 loc) Β· 6.22 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use strict';
const join = require('path').join;
const lazyReq = require('lazy-req')(require);
const request = lazyReq('request');
const semver = lazyReq('semver');
const del = lazyReq('del');
const manager = lazyReq('bin-manager');
const osFilterObj = lazyReq('os-filter-obj');
const BIN_PATH = 'bin';
const VERSION_LIST_ENDPOINT = 'https://arduino-cli.github.io/arduino-version/list';
const MIRRORS = [{
os: 'win32',
uri: 'https://github.com/arduino-cli/arduino-version/releases/download/{{version}}/arduino-{{version}}-windows.zip',
bin: 'arduino_debug.exe'
}, {
os: 'darwin',
uri: 'https://github.com/arduino-cli/arduino-version/releases/download/{{version}}/arduino-{{version}}-macosx.zip',
bins: [{
bin: 'Contents/MacOS/Arduino',
version: '>=1.6.0'
}, {
bin: 'Contents/MacOS/JavaAppLauncher',
version: '<=1.5.8'
}]
}, {
os: 'linux',
arch: 'x32',
uri: 'https://github.com/arduino-cli/arduino-version/releases/download/{{version}}/arduino-{{version}}-linux32.zip',
bin: 'arduino'
}, {
os: 'linux',
arch: 'x64',
uri: 'https://github.com/arduino-cli/arduino-version/releases/download/{{version}}/arduino-{{version}}-linux64.zip',
bin: 'arduino'
}, {
os: 'linux',
arch: 'arm',
uri: 'https://github.com/arduino-cli/arduino-version/releases/download/{{version}}/arduino-{{version}}-linuxarm.zip',
bin: 'arduino'
}];
const SPLASH_SCREEN = [{
os: 'win32',
img: 'lib/splash.png'
}, {
os: 'darwin',
img: 'Contents/Java/lib/splash.png'
}];
/**
* Initialize a new `Arduino`
*
* @param {object} [opts]
* @api public
*/
function arduino(opts) {
const version = opts.version || 'latest';
const binPath = opts.path || BIN_PATH;
const binSlug = opts.tag ? ('-' + opts.tag) : '';
let inited = false;
let bin;
/**
* Runs the arduino binary
*
* @param {array} [argv]
* @param {function} callback
* @api public
*/
function run(argv, callback) {
if (callback === undefined) {
callback = argv;
argv = [];
}
if (inited) {
bin.run(argv, callback);
return;
}
load(err => {
if (err) {
callback(err);
return;
}
bin.run(argv, callback);
});
}
/**
* Load/download the current version of the binary
*
* @param {function} callback
* @api public
*/
function load(callback) {
const opts = {
extract: true,
strip: 1
};
if (inited) {
bin.load(opts, err => callback(err));
return;
}
init(err => {
if (err) {
callback(err);
return;
}
bin.load(opts, err => {
if (err) {
callback(err);
return;
}
// HACK: remove the splash.png file because the arduino IDE does not
// hide it in command line mode.
// See https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc
const splash = osFilterObj()(SPLASH_SCREEN);
if (splash.length === 0) {
callback();
return;
}
const dirs = splash.map(s => {
return join(bin.path(), s.img);
});
del()(dirs)
.then(() => callback())
.catch(err => callback(err));
});
});
}
/**
* Removes the current version of the binary
*
* @param {function} callback
* @api public
*/
function unload(callback) {
if (inited) {
bin.unload(callback);
return;
}
init(err => {
if (err) {
callback(err);
return;
}
bin.unload(callback);
});
}
/**
* Checks if a version is available.
*
* @param {function} callback
* @api private
*/
function getFullVersion(callback) {
getVersionsList((err, vers) => {
if (err) {
callback(err);
return;
}
if (version === 'latest') {
callback(null, vers[vers.length - 1]);
return;
}
const valid = vers.some(v => v === version);
if (!valid) {
callback(new Error('The version provided is not available'));
return;
}
callback(null, version);
});
}
/**
* Retrieves the list of arduino ide versions available
*
* @param {function} callback
* @api private
*/
function getVersionsList(callback) {
request()(VERSION_LIST_ENDPOINT, (err, res, body) => {
if (err) {
callback('Cannot get the version list: ' + err);
return;
}
if (res.statusCode !== 200) {
callback('Cannot get the version list: ' + res.statusCode);
return;
}
const vers = body.split(',').map(v => semver().clean(v));
callback(null, vers);
});
}
/**
* Initializes the binary mirrors
*
* @param {string} version
* @api private
*/
function init(callback) {
getFullVersion((err, version) => {
if (err) {
callback(err);
return;
}
const slug = 'arduino-' + version + binSlug;
bin = manager()(binPath, slug);
MIRRORS.forEach(mirror => {
bin.src(mirror.uri.replace(/{{version}}/g, version), mirror.os, mirror.arch);
if (mirror.os === process.platform) {
if (Array.isArray(mirror.bins)) {
mirror.bins.some(b => {
if (semver().satisfies(version, b.version)) {
bin.use(b.bin.replace(/{{version}}/g, version));
return true;
}
return false;
});
return;
}
bin.use(mirror.bin.replace(/{{version}}/g, version));
}
});
inited = true;
callback();
});
}
/**
* Gets the path to the binary
*
* @api public
*/
function binary(callback) {
if (inited) {
callback(null, bin.bin());
return;
}
init(err => {
if (err) {
callback(err);
return;
}
callback(null, bin.bin());
});
}
/**
* Gets the path of the folder containing the Arduino IDE in use
*
* @api public
*/
function path(callback) {
if (inited) {
callback(null, bin.path());
return;
}
init(err => {
if (err) {
callback(err);
return;
}
callback(null, bin.path());
});
}
return {
run,
load,
unload,
binary,
path
};
}
module.exports = arduino;