@lvce-editor/file-watcher-process 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Lvce Editor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # file-watcher-process
2
+ File Watcher Process
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ import '../dist/index.js'
package/dist/index.js ADDED
@@ -0,0 +1,112 @@
1
+ import { VError } from '@lvce-editor/verror';
2
+ import * as fs from 'node:fs/promises';
3
+ import { ElectronMessagePortRpcClient, ElectronUtilityProcessRpcClient, NodeWorkerRpcClient, NodeForkedProcessRpcClient } from '@lvce-editor/rpc';
4
+
5
+ const SharedProcess = 1;
6
+
7
+ const rpcs = Object.create(null);
8
+ const set = (id, rpc) => {
9
+ rpcs[id] = rpc;
10
+ };
11
+ const get = id => {
12
+ return rpcs[id];
13
+ };
14
+
15
+ const invoke = async (method, ...params) => {
16
+ const rpc = get(SharedProcess);
17
+ await rpc.invoke(method, ...params);
18
+ };
19
+
20
+ const watchInternal = async (path, options, callback) => {
21
+ try {
22
+ const watcher = fs.watch(path, options);
23
+ for await (const event of watcher) {
24
+ await callback(event);
25
+ }
26
+ } catch (error) {
27
+ console.error(`[file-watcher-process] ${new VError(error, `Failed to watch`)}`);
28
+ }
29
+ };
30
+
31
+ const watchFile = async path => {
32
+ const callback = async event => {
33
+ await invoke('FileWatcher.handleChange', event);
34
+ };
35
+ await watchInternal(path, {}, callback);
36
+ };
37
+
38
+ const watchFolder = async path => {
39
+ const callback = async event => {
40
+ await invoke('FileWatcher.handleChange', event);
41
+ };
42
+ // TODO maybe there can be a way to wait for the watcher to be created
43
+ // but not wait until all watch events have been fired
44
+ void watchInternal(path, {
45
+ recursive: true
46
+ }, callback);
47
+ };
48
+
49
+ const commandMap = {
50
+ 'FileWatcher.watchFile': watchFile,
51
+ 'FileWatcher.watchFolder': watchFolder
52
+ };
53
+
54
+ const NodeWorker = 1;
55
+ const NodeForkedProcess = 2;
56
+ const ElectronUtilityProcess = 3;
57
+ const ElectronMessagePort = 4;
58
+ const Auto = () => {
59
+ const {
60
+ argv
61
+ } = process;
62
+ if (argv.includes('--ipc-type=node-worker')) {
63
+ return NodeWorker;
64
+ }
65
+ if (argv.includes('--ipc-type=node-forked-process')) {
66
+ return NodeForkedProcess;
67
+ }
68
+ if (argv.includes('--ipc-type=electron-utility-process')) {
69
+ return ElectronUtilityProcess;
70
+ }
71
+ throw new Error('[preview-process] unknown ipc type');
72
+ };
73
+
74
+ const getModule = method => {
75
+ switch (method) {
76
+ case NodeForkedProcess:
77
+ return NodeForkedProcessRpcClient;
78
+ case NodeWorker:
79
+ return NodeWorkerRpcClient;
80
+ case ElectronUtilityProcess:
81
+ return ElectronUtilityProcessRpcClient;
82
+ case ElectronMessagePort:
83
+ return ElectronMessagePortRpcClient;
84
+ default:
85
+ throw new Error('unexpected ipc type');
86
+ }
87
+ };
88
+
89
+ const listen$1 = async ({
90
+ method,
91
+ ...params
92
+ }) => {
93
+ const module = getModule(method);
94
+ // @ts-ignore
95
+ const rpc = await module.create(params);
96
+ // @ts-ignore
97
+ return rpc;
98
+ };
99
+
100
+ const listen = async () => {
101
+ const rpc = await listen$1({
102
+ method: Auto(),
103
+ commandMap: commandMap
104
+ });
105
+ set(SharedProcess, rpc);
106
+ };
107
+
108
+ const main = async () => {
109
+ await listen();
110
+ };
111
+
112
+ main();
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@lvce-editor/file-watcher-process",
3
+ "version": "1.0.0",
4
+ "description": "File Watcher Process",
5
+ "keywords": [
6
+ "Lvce Editor",
7
+ "File Watcher"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/lvce-editor/file-watcher-process.git"
12
+ },
13
+ "license": "MIT",
14
+ "author": "Lvce Editor",
15
+ "type": "module",
16
+ "main": "dist/index.js",
17
+ "bin": {
18
+ "file-watcher-process": "bin/fileWatcherProcess.js"
19
+ },
20
+ "dependencies": {
21
+ "@lvce-editor/rpc": "^1.21.0",
22
+ "@lvce-editor/rpc-registry": "^1.0.0",
23
+ "@lvce-editor/verror": "^1.6.0"
24
+ },
25
+ "engines": {
26
+ "node": ">=22"
27
+ }
28
+ }