Skip to content

Commit d62a6dd

Browse files
Fix up Smart Start (#71)
* Smart Start * improve build * add lint scripts * Bump schema 40
1 parent 233343a commit d62a6dd

File tree

12 files changed

+426
-174
lines changed

12 files changed

+426
-174
lines changed

.github/workflows/release.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Release Workflow
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build:
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
os: [ubuntu-latest, windows-latest, macos-latest]
13+
node-version: [18]
14+
include:
15+
- os: ubuntu-latest
16+
output-path: build/dist/server
17+
output-name: server.psi.ubuntu
18+
- os: windows-latest
19+
output-path: build/dist/server.exe
20+
output-name: server.psi.win
21+
- os: macos-latest
22+
output-path: build/dist/server
23+
output-name: server.psi.macos
24+
25+
steps:
26+
- name: Checkout repository
27+
uses: actions/checkout@v3
28+
29+
- name: Set up Node.js
30+
uses: actions/setup-node@v3
31+
with:
32+
node-version: ${{ matrix.node-version }}
33+
34+
- name: Install dependencies
35+
run: npm install
36+
37+
- name: Run script from package.json
38+
run: |
39+
cd PSI
40+
npm run build
41+
42+
- name: Upload build artifact
43+
uses: actions/upload-release-asset@v1
44+
env:
45+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46+
with:
47+
upload_url: ${{ github.event.release.upload_url }}
48+
asset_path: ${{ matrix.output-path }}
49+
asset_name: ${{ matrix.output-name }}
50+
asset_content_type: application/octet-stream

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# Node Stuff
1010
**/node_modules/*
11-
**/dist/*
11+
**/build/*
1212

1313
# Yarn config and stuff
1414
**/.yarn/*
@@ -21,3 +21,4 @@
2121

2222
PSI/server.js
2323
PSI/package-lock.json
24+
package-lock.json

PSI/.eslintrc.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
module.exports = {
2+
extends: ['eslint:recommended'],
3+
parserOptions: {
4+
ecmaVersion: 2020,
5+
sourceType: 'module',
6+
allowImportExportEverywhere: true
7+
},
8+
env: {
9+
node: true,
10+
browser: true,
11+
es2021: true
12+
},
13+
rules: {
14+
'@typescript-eslint/no-this-alias': 0,
15+
'@typescript-eslint/no-non-null-assertion': 0,
16+
quotes: ['error', 'single', { avoidEscape: true }],
17+
'prefer-const': 2,
18+
'constructor-super': 2,
19+
'for-direction': 2,
20+
'getter-return': 2,
21+
'no-async-promise-executor': 0,
22+
'no-class-assign': 2,
23+
'no-compare-neg-zero': 2,
24+
'no-cond-assign': 2,
25+
'no-const-assign': 2,
26+
'no-constant-condition': 2,
27+
'no-control-regex': 2,
28+
'no-debugger': 2,
29+
'no-delete-var': 2,
30+
'no-dupe-args': 2,
31+
'no-dupe-class-members': 2,
32+
'no-dupe-else-if': 2,
33+
'no-dupe-keys': 2,
34+
'no-duplicate-case': 2,
35+
'no-empty': 2,
36+
'no-empty-character-class': 2,
37+
'no-empty-pattern': 2,
38+
'no-ex-assign': 2,
39+
'no-extra-boolean-cast': 2,
40+
'no-extra-semi': 2,
41+
'no-fallthrough': 2,
42+
'no-func-assign': 2,
43+
'no-global-assign': 2,
44+
'no-import-assign': 2,
45+
'no-inner-declarations': 2,
46+
'no-invalid-regexp': 2,
47+
'no-irregular-whitespace': 2,
48+
'no-misleading-character-class': 2,
49+
'no-mixed-spaces-and-tabs': 0,
50+
'no-new-symbol': 2,
51+
'no-obj-calls': 2,
52+
'no-octal': 2,
53+
'no-prototype-builtins': 2,
54+
'no-redeclare': 2,
55+
'no-regex-spaces': 2,
56+
'no-self-assign': 2,
57+
'no-setter-return': 2,
58+
'no-shadow-restricted-names': 2,
59+
'no-sparse-arrays': 2,
60+
'no-this-before-super': 2,
61+
'no-undef': 2,
62+
'no-unexpected-multiline': 2,
63+
'no-unreachable': 2,
64+
'no-unsafe-finally': 2,
65+
'no-unsafe-negation': 2,
66+
'no-unused-labels': 2,
67+
'no-unused-vars': 1,
68+
'no-useless-catch': 2,
69+
'no-useless-escape': 2,
70+
'no-with': 2,
71+
'require-yield': 2,
72+
'use-isnan': 2,
73+
'valid-typeof': 2,
74+
'no-case-declarations': 2
75+
}
76+
};

PSI/.prettierrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
arrowParens: 'always',
3+
printWidth: 120,
4+
semi: true,
5+
singleQuote: true,
6+
tabWidth: 2,
7+
trailingComma: 'none',
8+
useTabs: true,
9+
endOfLine: 'lf',
10+
bracketSpacing: true
11+
};

PSI/esb.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
const esbuild = require('esbuild');
2+
const { cp, readFile, writeFile } = require('fs/promises');
3+
const { exists } = require('fs-extra');
4+
const outputDir = 'build';
5+
const outfile = `${outputDir}/server.js`;
6+
const { join } = require('path');
7+
8+
const externals = [
9+
'@serialport/bindings-cpp/prebuilds',
10+
'zwave-js/package.json',
11+
'@zwave-js/config/package.json',
12+
'@zwave-js/config/config',
13+
'@zwave-js/config/build',
14+
'@zwave-js/server/package.json'
15+
];
16+
17+
const nativeNodeModulesPlugin = {
18+
name: 'native-node-modules',
19+
setup(build) {
20+
build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => ({
21+
path: require.resolve(args.path, { paths: [args.resolveDir] }),
22+
namespace: 'node-file'
23+
}));
24+
25+
build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({
26+
contents: `
27+
import path from ${JSON.stringify(args.path)}
28+
try { module.exports = require(path) }
29+
catch {}
30+
`
31+
}));
32+
33+
build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, (args) => ({
34+
path: args.path,
35+
namespace: 'file'
36+
}));
37+
38+
const opts = build.initialOptions;
39+
opts.loader = opts.loader || {};
40+
opts.loader['.node'] = 'file';
41+
}
42+
};
43+
44+
const run = async () => {
45+
const config = {
46+
entryPoints: ['./server_source.js'],
47+
plugins: [nativeNodeModulesPlugin],
48+
bundle: true,
49+
platform: 'node',
50+
target: 'node18',
51+
outfile,
52+
external: externals
53+
};
54+
await esbuild.build(config);
55+
56+
const patchedServer = (await readFile(outfile, 'utf-8'))
57+
.replace(/__dirname, "\.\.\/"/g, '__dirname, "./node_modules/@serialport/bindings-cpp"')
58+
.replace('../../package.json', './node_modules/@zwave-js/server/package.json');
59+
60+
await writeFile(outfile, patchedServer);
61+
62+
for (const ext of externals) {
63+
const path = ext.startsWith('./') ? ext : `node_modules/${ext}`;
64+
if (await exists(path)) {
65+
await cp(path, `${outputDir}/${path}`, { recursive: true });
66+
}
67+
}
68+
69+
let PKGFile;
70+
let PackagePatch;
71+
72+
const getPath = (path) => {
73+
return join(outputDir, path, 'package.json');
74+
};
75+
76+
const patch = (Package) => {
77+
delete Package.dependencies;
78+
delete Package.devDependencies;
79+
delete Package.scripts;
80+
delete Package.exports;
81+
delete Package.optionalDependencies;
82+
};
83+
84+
/* Root Package File */
85+
PKGFile = './package.json';
86+
PackagePatch = require(PKGFile);
87+
delete PackagePatch.dependencies;
88+
delete PackagePatch.devDependencies;
89+
delete PackagePatch.optionalDependencies;
90+
PackagePatch.scripts = {
91+
start: 'node server.js'
92+
};
93+
PackagePatch.bin = 'server.js';
94+
PackagePatch.pkg = {
95+
assets: ['node_modules/**']
96+
};
97+
await writeFile(`${outputDir}/package.json`, JSON.stringify(PackagePatch, null, 2));
98+
99+
/* Config Package File */
100+
PKGFile = getPath('node_modules/@zwave-js/config');
101+
console.log(`Patching ${PKGFile}`);
102+
PackagePatch = require(`./${PKGFile}`);
103+
patch(PackagePatch);
104+
await writeFile(PKGFile, JSON.stringify(PackagePatch, null, 2));
105+
106+
/* ZwaveJS Package File */
107+
PKGFile = getPath('node_modules/zwave-js');
108+
console.log(`Patching ${PKGFile}`);
109+
PackagePatch = require(`./${PKGFile}`);
110+
patch(PackagePatch);
111+
await writeFile(PKGFile, JSON.stringify(PackagePatch, null, 2));
112+
113+
/* Server Package File */
114+
PKGFile = getPath('node_modules/@zwave-js/server');
115+
console.log(`Patching ${PKGFile}`);
116+
PackagePatch = require(`./${PKGFile}`);
117+
patch(PackagePatch);
118+
await writeFile(PKGFile, JSON.stringify(PackagePatch, null, 2));
119+
};
120+
121+
run().catch((err) => {
122+
console.error(err);
123+
process.exit(1);
124+
});

PSI/package.json

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
{
2-
"version": "4.0.0",
3-
"name": "server",
4-
"bin": "./server.js",
5-
"dependencies": {
6-
"@zwave-js/server": "1.35.0",
7-
"zwave-js": "12.5.6"
8-
},
9-
"devDependencies": {
10-
"@yao-pkg/pkg": "^5.11.5",
11-
"esbuild": "^0.20.2"
12-
},
13-
"scripts": {
14-
"build": "npm run do_esbuild && npm run do_pkgbuild",
15-
"do_esbuild": "esbuild server_source.js --bundle --outfile=server.js --platform=node --target=node18 --external:@serialport/* --external:@zwave-js/config --external:zwave-js/package.json",
16-
"do_pkgbuild": "pkg . --compress gzip -t host --targets node18"
17-
},
18-
"pkg": {
19-
"assets": "./node_modules/@serialport/bindings-cpp/prebuilds/**/*",
20-
"outputPath": "dist"
21-
}
22-
}
1+
{
2+
"version": "4.0.0",
3+
"name": "server",
4+
"bin": "./server.js",
5+
"dependencies": {
6+
"@zwave-js/server": "1.40.2",
7+
"zwave-js": "14.3.7"
8+
},
9+
"devDependencies": {
10+
"@yao-pkg/pkg": "^6.2.0",
11+
"esbuild": "^0.24.2",
12+
"eslint": "^8.57.0",
13+
"prettier": "^3.4.2"
14+
},
15+
"scripts": {
16+
"build": "npm run do_esbuild && npm run do_pkgbuild",
17+
"do_esbuild": "node esb.js",
18+
"do_pkgbuild": "pkg ./build/package.json --compress gzip -t host --targets node18 --output ./build/dist/server",
19+
"test": "export WS_PORT=7070 SERIAL_PORT=/dev/tty.usbmodem214301 CONFIG=$(<./test_config.json) && ./build/dist/server",
20+
"lint": "eslint --ext .js .",
21+
"lint:fix": "eslint --fix --ext .js . && prettier -w ."
22+
}
23+
}

0 commit comments

Comments
 (0)