Skip to content

Commit 188cce3

Browse files
Implement FS Driver, to better handle PKG paths
1 parent af8a22a commit 188cce3

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

PSI/PKGFSDriver.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const { fs: nodeFs } = require('@zwave-js/core/bindings/fs/node');
2+
const path = require('path');
3+
4+
const CONFIG_PATH = path.resolve(__filename, '/config');
5+
const CONFIG_PATH_IN_PKG = path.join(__dirname, `node_modules/@zwave-js/config/config`);
6+
7+
class PKGFSDriver {
8+
readFile(filePath) {
9+
filePath = path.normalize(filePath);
10+
if (filePath.startsWith(CONFIG_PATH)) {
11+
filePath = filePath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
12+
}
13+
return nodeFs.readFile(filePath);
14+
}
15+
16+
writeFile(filePath, data) {
17+
filePath = path.normalize(filePath);
18+
if (filePath.startsWith(CONFIG_PATH)) {
19+
// The pkg assets are readonly
20+
return;
21+
}
22+
return nodeFs.writeFile(filePath, data);
23+
}
24+
25+
copyFile(source, dest) {
26+
source = path.normalize(source);
27+
dest = path.normalize(dest);
28+
if (dest.startsWith(CONFIG_PATH)) {
29+
// The pkg assets are readonly
30+
return;
31+
}
32+
if (source.startsWith(CONFIG_PATH)) {
33+
source = source.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
34+
}
35+
return nodeFs.copyFile(source, dest);
36+
}
37+
38+
open(filePath, flags) {
39+
filePath = path.normalize(filePath);
40+
if (filePath.startsWith(CONFIG_PATH) && flags.write) {
41+
// The pkg assets are readonly
42+
throw new Error(`${filePath} is not writable`);
43+
}
44+
if (filePath.startsWith(CONFIG_PATH)) {
45+
filePath = filePath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
46+
}
47+
return nodeFs.open(filePath, flags);
48+
}
49+
50+
readDir(dirPath) {
51+
dirPath = path.normalize(dirPath);
52+
if (dirPath.startsWith(CONFIG_PATH)) {
53+
dirPath = dirPath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
54+
}
55+
return nodeFs.readDir(dirPath);
56+
}
57+
58+
stat(filePath) {
59+
filePath = path.normalize(filePath);
60+
if (filePath.startsWith(CONFIG_PATH)) {
61+
filePath = filePath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
62+
}
63+
return nodeFs.stat(filePath);
64+
}
65+
66+
ensureDir(dirPath) {
67+
dirPath = path.normalize(dirPath);
68+
if (dirPath.startsWith(CONFIG_PATH)) {
69+
// The pkg assets are readonly
70+
return;
71+
}
72+
return nodeFs.ensureDir(dirPath);
73+
}
74+
75+
deleteDir(dirPath) {
76+
dirPath = path.normalize(dirPath);
77+
if (dirPath.startsWith(CONFIG_PATH)) {
78+
// The pkg assets are readonly
79+
return;
80+
}
81+
return nodeFs.deleteDir(dirPath);
82+
}
83+
84+
makeTempDir(prefix) {
85+
return nodeFs.makeTempDir(prefix);
86+
}
87+
}
88+
89+
module.exports = { PKGFSDriver };

PSI/esb.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ const run = async () => {
5656

5757
const patchedServer = (await readFile(outfile, 'utf-8'))
5858
.replace(/__dirname, "\.\.\/"/g, '__dirname, "./node_modules/@serialport/bindings-cpp"')
59-
.replace('"../.."', '"./node_modules/@zwave-js/config"')
6059
.replace('../../package.json', './node_modules/@zwave-js/server/package.json');
6160

6261
await writeFile(outfile, patchedServer);

PSI/server_source.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const { Driver } = require('zwave-js');
22
const { ZwavejsServer } = require('@zwave-js/server');
3+
const { PKGFSDriver } = require('./PKGFSDriver');
34

45
const serialPort = process.env.SERIAL_PORT;
56
const wsPort = parseInt(process.env.WS_PORT);
67
const driverOptions = JSON.parse(process.env.CONFIG);
8+
driverOptions.host = {
9+
fs: new PKGFSDriver()
10+
};
711
let ServerStarted = false;
812
let DriverStarted = false;
913

0 commit comments

Comments
 (0)