hello-lights 0.3.8 → 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 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 }` |
@@ -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
- function createApp(commander) {
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(req.body || '', reset);
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
- commander.runDefinitions(req.body || '');
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, () => console.log(`Server listening on port ${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
- console.error(`Error: port ${options.port} is already in use`);
92
+ logger.error(`port ${options.port} is already in use`);
63
93
  } else {
64
- console.error(`Error: ${err.message}`);
94
+ logger.error(err.message);
65
95
  }
66
96
  process.exit(1);
67
97
  });
@@ -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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hello-lights",
3
- "version": "0.3.8",
3
+ "version": "0.3.10",
4
4
  "description": "Commands to control a traffic light",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -16,7 +16,7 @@
16
16
  "build:doc": "jsdoc src -c jsdoc.json",
17
17
  "build:web": "npm run peg && npm run browserify && npm run build:doc",
18
18
  "doc": "npm run build:doc && open-cli web/doc/index.html",
19
- "lint": "coffeelint -f coffeelint.json test && eslint src web dev",
19
+ "lint": "coffeelint -f coffeelint.json test && eslint src web",
20
20
  "peg": "pegjs -o src/commands/peg-parser.js src/commands/grammar.pegjs && pegjs -o src/commands/doc-peg-parser.js src/commands/doc-grammar.pegjs && pegjs -o src/commands/formatter-peg-parser.js src/commands/formatter-grammar.pegjs",
21
21
  "mocha-solo": "mocha --require coffeescript/register",
22
22
  "mocha-grep": "mocha --require coffeescript/register \"test/**/*.coffee\" --grep",
@@ -31,23 +31,22 @@
31
31
  "web": "open-cli web/index.html",
32
32
  "cpy-lib": "cpy \"**/*.*\" \"!*.pegjs\" ../lib/ --cwd=src --parents",
33
33
  "prepublishOnly": "npm run cpy-lib",
34
- "dev:install": "cd dev && npm install",
35
34
  "precli": "npm run cpy-lib",
36
35
  "cli": "node cli.js"
37
36
  },
38
37
  "devDependencies": {
39
- "browserify": "^16.5.2",
38
+ "browserify": "^17.0.1",
40
39
  "chai": "^4.3.4",
41
40
  "chai-as-promised": "^7.1.1",
42
41
  "chai-string": "^1.5.0",
43
42
  "coffeelint": "^2.1.0",
44
43
  "coffeescript": "^2.5.1",
45
- "cpy-cli": "^3.1.1",
44
+ "cpy-cli": "^7.0.0",
46
45
  "docdash": "^1.2.0",
47
46
  "eslint": "^6.8.0",
48
47
  "jsdoc": "^3.6.6",
49
- "mocha": "^7.2.0",
50
- "nyc": "^15.1.0",
48
+ "mocha": "^11.7.5",
49
+ "nyc": "^17.1.0",
51
50
  "open-cli": "^6.0.1",
52
51
  "pegjs": "^0.10.0",
53
52
  "sinon": "^9.2.4"