Skip to content

Commit eb38f59

Browse files
committed
tweaks
1 parent 927d7f6 commit eb38f59

File tree

8 files changed

+93
-37
lines changed

8 files changed

+93
-37
lines changed

package-lock.json

Lines changed: 43 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bin/arduino-cli.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ board_manager:
22
additional_urls:
33
- http://arduino.esp8266.com/stable/package_esp8266com_index.json
44
arduino_data: './data'
5-
downloads_dir: '../../data/arduino-downloads'
5+
downloads_dir: '../../data/arduino-downloads/'

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const initFiles = [
1717

1818
io.on('connection', async (socket) => {
1919
await files.loadTempFiles(initFiles, socket);
20-
await cores.indexUpdate(socket);
20+
await cores._autoUpdate(socket);
2121
await libs.indexUpdate(socket);
2222

2323
socket.on('files.new', (fileObjects, done) => files.loadTempFiles(fileObjects, socket, 'files', done));
@@ -47,4 +47,12 @@ io.on('connection', async (socket) => {
4747
socket.on('upload.start', (data, done) => program.upload(data, socket, done));
4848

4949
socket.on('disconnect', () => socket.tmpDir.cleanup());
50+
51+
socket.emit('ready');
52+
});
53+
54+
io.of('/ping').on('connect', (socket) => {
55+
socket.on('p', done => done());
5056
});
57+
58+
console.log(`🚀 Server Launched on localhost:${process.env.PORT || 3030}`);

src/utils/arduino-exec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
const { spawn } = require('child_process');
2+
const _ = require('lodash');
23

34
export default (commands, args, socket, options) => new Promise((resolve) => {
45
const opts = Object.assign({}, {
56
emit: true,
67
}, options);
78
let res = '';
89
const log = (data) => {
9-
res += data;
10-
if (opts.emit) socket.emit('console.log', data);
10+
res += data.toString('utf-8');
11+
// console.log(data.toString('utf-8'));
12+
if (opts.emit) socket.emit('console.log', data.toString('utf-8'));
1113
};
1214

1315
const exec = spawn(`${__dirname}/../bin/arduino-cli`, [
1416
...(Array.isArray(commands) ? commands : commands.split('.')),
15-
args.map(arg => `"${`${arg}`.replace(/"/g, '')}"`),
17+
..._.castArray(args).map(arg => `${`${arg}`.replace(/"/g, '')}`),
1618
'--config-file',
1719
`${socket.tmpDir.path}/arduino-cli.yaml`,
1820
'--format',

src/utils/boards.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@ import cli from './arduino-exec';
22

33
const boards = {
44

5+
// Print details about a board.
56
details: async (fqbn = '', socket, done) => {
67
const res = await cli('board.details', [fqbn], socket, { emit: false });
78
const response = JSON.parse(res);
89
if (done) done(response);
910
return response;
1011
},
1112

13+
// List connected boards.
1214
list: async (socket, done) => {
1315
const res = await cli('board.list', [], socket, { emit: false });
1416
const response = JSON.parse(res);
1517
if (done) done(response);
1618
return response;
1719
},
1820

21+
// List all known boards and their corresponding FQBN.
1922
listall: async (socket, done) => {
20-
const res = await cli('board.listall', socket);
23+
const res = await cli('board.listall', [], socket);
2124
const response = JSON.parse(res);
2225
await Promise.all(response.boards.map(async (board) => {
2326
// eslint-disable-next-line no-param-reassign
24-
board.options = (await boards.details(board.fqbn)).ConfigOptions;
27+
board.options = (await boards.details(board.fqbn, socket)).ConfigOptions;
2528
}));
2629
if (done) done(response);
2730
return response;

src/utils/cores.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,58 @@ const cores = {
1818
await fs.readFile(`${socket.tmpDir.path}/arduino-cli.yaml`, configFile);
1919
},
2020

21+
_autoUpdate: async (socket) => {
22+
if (!socket.coresAutoUpdated) {
23+
await cores.indexUpdate(socket);
24+
// eslint-disable-next-line no-param-reassign
25+
socket.coresAutoUpdated = true;
26+
}
27+
},
2128

29+
// returns the indexes of cores used.
2230
indexList: async (socket, done) => {
2331
const config = await cores.readConfig(socket);
24-
const response = _.get(config, indPath) || [];
25-
if (done) done(response);
26-
return response;
32+
const res = _.get(config, indPath) || [];
33+
if (done) done(res);
34+
return res;
2735
},
2836

37+
// Updates the index of cores.
2938
indexUpdate: async (socket, done) => {
30-
await cli('cores.update-index', [], socket);
39+
await cli('core.update-index', [], socket);
3140
if (done) done();
3241
},
3342

43+
// adds new core indexes
3444
indexNew: async (indexes, socket, done) => {
3545
const config = await cores.readConfig(socket);
3646
const existing = _.get(config, indPath) || [];
3747
await cores.writeConfig(_.set(config, indPath, _.uniq([...existing, ...indexes])));
3848
await cores.indexUpdate(socket, done);
3949
},
4050

51+
// Search for a core in the package index.
4152
search: async (searchTerm = '', socket, done) => {
42-
const res = await cli('cores.search', [searchTerm], socket, { emit: false });
43-
const response = JSON.parse(res);
53+
await cores._autoUpdate(socket);
54+
const res = await cli('core.search', [searchTerm], socket, { emit: false });
55+
const response = res ? JSON.parse(res) : [];
4456
if (done) done(response);
4557
return response;
4658
},
4759

60+
// Shows the list of installed platforms.
4861
list: async (socket, done) => {
49-
const res = await cli('cores.list', [], socket, { emit: false });
62+
await cores._autoUpdate(socket);
63+
const res = await cli('core.list', [], socket, { emit: false });
5064
const response = JSON.parse(res);
5165
if (done) done(response);
5266
return response;
5367
},
5468

69+
// Installs one or more cores and corresponding tool dependencies.
5570
install: async (coreIds, socket, done) => {
56-
if (!socket.coresAutoUpdated) {
57-
await cores.indexUpdate();
58-
// eslint-disable-next-line no-param-reassign
59-
socket.coresAutoUpdated = true;
60-
}
61-
await cli('cores.install', coreIds, socket);
71+
await cores._autoUpdate(socket);
72+
await cli('core.install', coreIds, socket);
6273
cores.list(socket, done);
6374
},
6475
};

0 commit comments

Comments
 (0)