Skip to content

Commit 18629a7

Browse files
authored
update modulebuilder with new patterns (#306)
* update modulebuilder with new patterns
1 parent a1c13cb commit 18629a7

30 files changed

Lines changed: 80 additions & 49 deletions

src/modulebuilder/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ This folder is used to provide templates for BUG modulebuilder.
44

55
Each folder should contain the following items:
66

7-
- a `builder.json` file containing metadata for the template
8-
- a `src` folder containing:
9-
- `container` - optional files to be built into a docker container
10-
- `client` - ReactJS files to be compiled into front-end code
11-
- `module.json` - metadata file for the module
7+
- a `builder.json` file containing metadata for the template
8+
- a `src` folder containing:
9+
- `container` - optional files to be built into a docker container
10+
- `Dockerfile.test` - optional Docker test image definition for module container tests
11+
- `client` - ReactJS files to be compiled into front-end code
12+
- `module.json` - metadata file for the module
1213

13-
modulebuilder will prompt for the items listed in `fields`, and search `templatedfiles` for matching template tags replacing any that it finds.
14+
modulebuilder will prompt for the items listed in `fields`, and search `files` for matching template tags replacing any that it finds.
1415

1516
Here's an example `builder.json` file:
1617

@@ -20,7 +21,7 @@ Here's an example `builder.json` file:
2021
"description": "Example Template",
2122
"version": "1.0.0",
2223
"author": "Your Name",
23-
"templatedfiles": [
24+
"files": [
2425
"module.json"
2526
],
2627
"fields": [

src/modulebuilder/cli.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ejs from "ejs";
22
import fs from "fs-extra";
3-
import glob from "glob";
3+
import { glob } from "glob";
44
import inquirer from "inquirer";
55
import path from "path";
66

src/modulebuilder/generic-with-jscontainer/builder.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
"description": "Generic module with NodeJS backend container",
44
"version": "1.0.0",
55
"author": "Geoff House",
6-
"files": ["module.json", "container/package.json", "client/components/ExampleList.jsx"],
6+
"files": [
7+
"module.json",
8+
"container/package.json",
9+
"container/package-lock.json",
10+
"container/Dockerfile.test",
11+
"client/components/ExampleList.jsx"
12+
],
713
"fields": [
814
{
915
"name": "name",

src/modulebuilder/generic-with-jscontainer/src/container/jest.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ const moduleNameMapper = Object.entries(_moduleAliases).reduce((acc, [alias, tar
4242
module.exports = {
4343
testEnvironment: "node",
4444
moduleNameMapper,
45+
testPathIgnorePatterns: ["<rootDir>/core/"],
4546
};

src/modulebuilder/generic-with-jscontainer/src/container/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"production": "node ./api/server",
66
"development": "nodemon ./api/server",
7-
"test": "jest"
7+
"test": "docker build -f ./Dockerfile.test -t bug-module-test ../../../.. && docker run --rm --name bug-module-test-run bug-module-test sh -lc 'if find . -path ./node_modules -prune -o -type f -name \"*.test.js\" -print | grep -q . || find . -path ./node_modules -prune -o -type f -name \"*.spec.js\" -print | grep -q .; then if [ -f ./jest.config.cjs ]; then npx jest --runInBand --config ./jest.config.cjs; else npx jest --runInBand; fi; else echo \"No tests found, skipping.\"; fi'"
88
},
99
"nodemonConfig": {
1010
"ignore": [

src/modulebuilder/generic-with-jscontainer/src/container/services/example-enable.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
const configGet = require("@core/config-get");
44
const yourApi = require("@utils/your-api");
55
const deviceSetPending = require("@services/device-setpending");
6+
const logger = require("@core/logger")(module);
67

78
module.exports = async (id) => {
89
try {
910
const config = await configGet();
1011

11-
console.log(`example-enable: enabling item ${id} ...`);
12+
logger.info(`enabling item ${id} ...`);
1213

1314
// enable the item on the device
1415
await yourApi({
@@ -20,7 +21,7 @@ module.exports = async (id) => {
2021
commands: ["enable"],
2122
});
2223

23-
console.log(`example-enable: success - updating DB`);
24+
logger.info("success - updating DB");
2425

2526
// update the DB to reflect enabled interface
2627
const itemCollection = await mongoCollection("items");
@@ -29,7 +30,7 @@ module.exports = async (id) => {
2930
{ $set: { enabled: true } }
3031
);
3132

32-
console.log(`example-enable: ${JSON.stringify(dbResult.result)}`);
33+
logger.info(`${JSON.stringify(dbResult.result)}`);
3334
await deviceSetPending(true);
3435
return true;
3536

src/modulebuilder/generic-with-jscontainer/src/container/workers/worker-example.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const register = require("module-alias/register");
66
const mongoDb = require("@core/mongo-db");
77
const mongoSingle = require("@core/mongo-single");
88
const axios = require("axios");
9+
const logger = require("@core/logger")(module);
910

1011
// Tell the manager the things you care about
1112
// make sure you add an array of config fields in 'restartOn' - the worker will restart whenever these are updated
@@ -20,7 +21,7 @@ const main = async () => {
2021
await mongoDb.connect(workerData.id);
2122

2223
// Kick things off
23-
console.log(`worker-example: doing something ...`);
24+
logger.debug("doing something ...");
2425

2526
// use an infinite loop
2627
while (true) {
@@ -33,14 +34,14 @@ const main = async () => {
3334
await delay(300000);
3435
}
3536
} catch (err) {
36-
console.error(`worker-example: fatal error`);
37-
console.error(err.stack || err.message || err);
37+
logger.error("fatal error");
38+
logger.error(err.stack || err.message || err);
3839
process.exit();
3940
}
4041
};
4142

4243
main().catch(err => {
43-
console.error("worker-example: startup failure");
44-
console.error(err.stack || err.message || err);
44+
logger.error("startup failure");
45+
logger.error(err.stack || err.message || err);
4546
process.exit(1);
4647
});

src/modulebuilder/network-switch/builder.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Generic network switch module",
44
"version": "1.0.0",
55
"author": "Geoff House",
6-
"files": ["module.json", "container/package.json", "client/components/ExampleList.jsx"],
6+
"files": ["module.json", "container/package.json", "container/package-lock.json", "container/Dockerfile.test"],
77
"fields": [
88
{
99
"name": "name",

src/modulebuilder/network-switch/src/container/package-lock.json

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

src/modulebuilder/network-switch/src/container/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"production": "node ./api/server",
66
"development": "nodemon ./api/server",
7-
"test": "jest"
7+
"test": "docker build -f ./Dockerfile.test -t bug-module-test ../../../.. && docker run --rm --name bug-module-test-run bug-module-test sh -lc 'if find . -path ./node_modules -prune -o -type f -name \"*.test.js\" -print | grep -q . || find . -path ./node_modules -prune -o -type f -name \"*.spec.js\" -print | grep -q .; then if [ -f ./jest.config.cjs ]; then npx jest --runInBand --config ./jest.config.cjs; else npx jest --runInBand; fi; else echo \"No tests found, skipping.\"; fi'"
88
},
99
"nodemonConfig": {
1010
"ignore": [
@@ -35,7 +35,7 @@
3535
"jest": "^30.2.0",
3636
"nodemon": "^3.1.11",
3737
"superagent": "^10.2.3",
38-
"supertest": "^6.1.3"
38+
"supertest": "^7.1.4"
3939
},
4040
"jest": {
4141
"testEnvironment": "node",

0 commit comments

Comments
 (0)