forked from spaceneedle/Chromeduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.js
134 lines (126 loc) · 5.64 KB
/
arduino.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
const { exec } = require('child_process');
const fs = require('fs');
const properties = require ("properties");
const tmp = require('tmp');
const errorCodes = {
0: "Success",
1: "Build failed or upload failed",
2: "Sketch not found",
3: "Invalid (argument for) commandline option",
4: "Preference passed to --get-pref does not exist"
};
const process_file = (path, cb)=>{
fs.readFile(path, 'utf8', (err, file)=>{
if(err) return cb(err);
// make boards.txt file compatible
file = file.replace('\n\r', '\n');
file = file.replace('\r', '\n');
file = file.split('\n');
for(let i in file){
if(file[i].indexOf('=') === -1) continue;
let key = file[i].replace(/=.*/, '');
if(key.search(/menu.cpu.[^.]*$/) !== -1) file[i] = file[i].replace('=', '.name=');
if(key.search(/.vid.\d+$/) !== -1) file[i] = file[i].replace('=', '.value=');
}
file = file.join('\n');
properties.parse(file,{namespaces: true}, cb);
});
};
class Arduino {
constructor (arduino_dir) {
this.dir = arduino_dir;
this.buildcmd = `"${this.dir}/arduino-builder" -hardware "${this.dir}/hardware" -libraries "${this.dir}/libraries"`;
let tools = ['/hardware/tools', '/tools', '/tools-builder'];
for(let i in tools) this.buildcmd += ` -tools "${this.dir}${tools[i]}"`;
this.libraries = [];
this.boards = {};
this.loadLibraries().catch(console.error);
this.loadBoards().catch(console.error);
}
loadLibraries(){
return new Promise((resolve, reject) => {
fs.readdir(`${this.dir}/libraries`, (err, files)=>{
if(err) reject(err);
if(files.length < 1) return resolve([]);
let count = 0;
let libs = [];
let done = (err)=>{
count++;
if(err) console.error(err);
if(count === files.length){
this.libraries = libs;
resolve(libs);
}
};
files.forEach(file=>{
process_file(`${this.dir}/libraries/${file}/library.properties`, (err, data)=>{
if(err) return done(err);
if(!data.name || !data.version) return done(`The library ${file} is missing a name and/or version in it's properties file.`);
libs.push(data);
done();
});
});
});
});
}
loadBoards(){ //this will need to become more dynamic
return new Promise((resolve, reject) => {
process_file(`${this.dir}/hardware/arduino/avr/boards.txt`, (err, data)=>{
if(err) return reject(err);
let boards = {};
for(let i in data){
if(i === 'menu') continue;
let board = 'arduino:avr:' + i;
let name = data[i].name || i;
if(data[i].menu && data[i].menu.cpu){
for(let j in data[i].menu.cpu){
let boardcpu = board + ':cpu=' + j;
let cpuname = data[i].menu.cpu[j].name || j;
boards[boardcpu] = {name: name + ' / ' + cpuname, upload_speed: data[i].menu.cpu[j].upload.speed};
}
} else {
boards[board] = {name: name, upload_speed: data[i].upload.speed};
}
}
this.boards = boards;
resolve(boards);
});
});
}
compile(code, board="arduino:avr:uno", verbose){
return new Promise((resolve, reject) => {
if(!this.boards[board]) return resolve({success: false, msg: "Unknown board provided"});
if(/#\s*include\s*<\.*\/.*>/.test(code)) resolve({success: false, msg: 'Security out-of-bounds'});
verbose = verbose ? '-verbose' : '';
tmp.dir({prefix: 'chromeduino-', unsafeCleanup: true}, (err, path, cleanup)=>{
if(err) return reject(err);
console.log(path);
let codeFile = path.replace(/^.*\//, '')+'.ino';
fs.writeFile(path + '/' + codeFile, code, err => {
if(err) return reject(err);
fs.mkdir(path+'/compiled', err=>{
if(err) return reject(err);
let cmd = `${this.buildcmd} ${verbose} -fqbn ${board} -build-path "${path}/compiled" "${path}/${codeFile}"`;
exec(cmd, (err, stdout, stderr)=>{
stderr = stderr.split(this.dir).join('~/arduino-1.8.5');
stdout = stdout.split(this.dir).join('~/arduino-1.8.5');
if(err){
cleanup();
return resolve({success: false, msg: errorCodes[err.code], code: err.code, stdout, stderr});
}
fs.readFile(path + '/compiled/' + codeFile + '.hex', 'base64', (err, hexcode)=>{
if(err) {
cleanup();
return reject(err);
}
cleanup();
resolve({success: true, hex: hexcode, stdout, stderr});
});
});
});
});
});
});
}
}
module.exports = Arduino;