hello-lights 0.3.9 → 0.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/lib/cli/serve/index.js +36 -6
- package/lib/rest-commander.js +8 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -109,10 +109,10 @@ The `serve` command starts an HTTP server that exposes the Commander interface a
|
|
|
109
109
|
|
|
110
110
|
| Endpoint | Method | Body | Response |
|
|
111
111
|
|---|---|---|---|
|
|
112
|
-
| `/run` | POST | Command string (plain text) | 202 Accepted |
|
|
113
|
-
| `/run?reset=true` | POST | Command string (plain text) | 202 Accepted (resets lights first) |
|
|
112
|
+
| `/run` | POST | Command string (plain text) | 202 Accepted, or 400 if malformed |
|
|
113
|
+
| `/run?reset=true` | POST | Command string (plain text) | 202 Accepted (resets lights first), or 400 if malformed |
|
|
114
114
|
| `/cancel` | POST | — | 200 OK |
|
|
115
|
-
| `/definitions` | POST | Definition string (plain text) | 202 Accepted |
|
|
115
|
+
| `/definitions` | POST | Definition string (plain text) | 202 Accepted, or 400 if malformed |
|
|
116
116
|
| `/commands` | GET | — | 200 + JSON array of command names |
|
|
117
117
|
| `/commands/:name` | GET | — | 200 + help text (`text/x-ansi`) or 404 |
|
|
118
118
|
| `/info` | GET | — | 200 + JSON array of `{ serialNum, status }` |
|
package/lib/cli/serve/index.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
/////////////////////////////////////////////////////////////////
|
|
2
2
|
|
|
3
3
|
const path = require('path');
|
|
4
|
+
const chalk = require('chalk');
|
|
4
5
|
const commanderOptions = require('../commander-options');
|
|
5
6
|
|
|
6
7
|
/////////////////////////////////////////////////////////////////
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
const now = () => chalk.gray(`[${new Date().toISOString()}]`);
|
|
10
|
+
|
|
11
|
+
const logger = {
|
|
12
|
+
log: (...args) =>
|
|
13
|
+
console.log(now(), '[INFO]', ...args),
|
|
14
|
+
error: (...args) =>
|
|
15
|
+
console.error(now(), chalk.red('[ERROR]'), chalk.red(...args))
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/////////////////////////////////////////////////////////////////
|
|
19
|
+
|
|
20
|
+
function createApp(commander, {logger: log = logger} = {}) {
|
|
9
21
|
const express = require('express');
|
|
10
22
|
const app = express();
|
|
11
23
|
|
|
@@ -13,8 +25,17 @@ function createApp(commander) {
|
|
|
13
25
|
app.use(express.text({type: '*/*'}));
|
|
14
26
|
|
|
15
27
|
app.post('/run', (req, res) => {
|
|
28
|
+
let body = req.body || '';
|
|
29
|
+
log.log('POST /run:', body);
|
|
30
|
+
try {
|
|
31
|
+
commander.interpreter.process(body);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
log.error('POST /run: malformed:', e.message);
|
|
34
|
+
res.status(400).send(e.message);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
16
37
|
let reset = req.query.reset === 'true';
|
|
17
|
-
commander.run(
|
|
38
|
+
commander.run(body, reset);
|
|
18
39
|
res.sendStatus(202);
|
|
19
40
|
});
|
|
20
41
|
|
|
@@ -24,7 +45,16 @@ function createApp(commander) {
|
|
|
24
45
|
});
|
|
25
46
|
|
|
26
47
|
app.post('/definitions', (req, res) => {
|
|
27
|
-
|
|
48
|
+
let body = req.body || '';
|
|
49
|
+
log.log('POST /definitions:', body);
|
|
50
|
+
try {
|
|
51
|
+
commander.interpreter.process(body);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
log.error('POST /definitions: malformed:', e.message);
|
|
54
|
+
res.status(400).send(e.message);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
commander.runDefinitions(body);
|
|
28
58
|
res.sendStatus(202);
|
|
29
59
|
});
|
|
30
60
|
|
|
@@ -56,12 +86,12 @@ function createApp(commander) {
|
|
|
56
86
|
function serve(options) {
|
|
57
87
|
const commander = commanderOptions.resolveCommander(options);
|
|
58
88
|
const app = createApp(commander);
|
|
59
|
-
const server = app.listen(options.port, () =>
|
|
89
|
+
const server = app.listen(options.port, () => logger.log(`Server listening on port ${options.port}`));
|
|
60
90
|
server.on('error', err => {
|
|
61
91
|
if (err.code === 'EADDRINUSE') {
|
|
62
|
-
|
|
92
|
+
logger.error(`port ${options.port} is already in use`);
|
|
63
93
|
} else {
|
|
64
|
-
|
|
94
|
+
logger.error(err.message);
|
|
65
95
|
}
|
|
66
96
|
process.exit(1);
|
|
67
97
|
});
|
package/lib/rest-commander.js
CHANGED
|
@@ -57,7 +57,10 @@ class RestCommander {
|
|
|
57
57
|
*/
|
|
58
58
|
async run(command, reset = false) {
|
|
59
59
|
let path = reset ? '/run?reset=true' : '/run';
|
|
60
|
-
await request(this.host, 'POST', path, command);
|
|
60
|
+
let res = await request(this.host, 'POST', path, command);
|
|
61
|
+
if (res.statusCode === 400) {
|
|
62
|
+
this.logger.error(res.body);
|
|
63
|
+
}
|
|
61
64
|
}
|
|
62
65
|
|
|
63
66
|
/**
|
|
@@ -72,7 +75,10 @@ class RestCommander {
|
|
|
72
75
|
* @param {string} command - Command with definitions to execute.
|
|
73
76
|
*/
|
|
74
77
|
async runDefinitions(command) {
|
|
75
|
-
await request(this.host, 'POST', '/definitions', command);
|
|
78
|
+
let res = await request(this.host, 'POST', '/definitions', command);
|
|
79
|
+
if (res.statusCode === 400) {
|
|
80
|
+
this.logger.error(res.body);
|
|
81
|
+
}
|
|
76
82
|
}
|
|
77
83
|
|
|
78
84
|
/**
|