|
| 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 }; |
0 commit comments