@peachy/plugin-runner 0.0.9 → 0.0.11

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/dist/index.d.mts CHANGED
@@ -4,21 +4,30 @@ import { Plugin } from "rolldown";
4
4
  declare function gjsRunner({
5
5
  sendMessage,
6
6
  debug,
7
- setReloadHandler
7
+ setReloadHandler,
8
+ options: runnerOptions
8
9
  }: ReactGJSPluginOptions): Plugin;
10
+ type ReloadHandler = () => Promise<void>;
9
11
  interface ReactGJSPluginOptions {
10
12
  /**
11
- * Whether to print debug messages
12
- */
13
+ * Whether to print debug messages
14
+ */
13
15
  debug?: boolean;
14
16
  /**
15
- * Broadcast message to all connected clients
16
- */
17
+ * Broadcast message to all connected clients
18
+ */
17
19
  sendMessage: (message: HMRMessage) => void;
18
20
  /**
19
- * Called when a reload is needed
20
- */
21
- setReloadHandler: (handler: () => void) => void;
21
+ * Called when a reload handler is needed
22
+ */
23
+ setReloadHandler: (handler: ReloadHandler) => void;
24
+ /**
25
+ * Runner options
26
+ */
27
+ options: RunnerOptions;
28
+ }
29
+ interface RunnerOptions {
30
+ env: Record<string, string>;
22
31
  }
23
32
  interface HMRUpdate {
24
33
  type: "update";
@@ -30,4 +39,4 @@ interface HMRMessage {
30
39
  updates: HMRUpdate[];
31
40
  }
32
41
  //#endregion
33
- export { ReactGJSPluginOptions, gjsRunner };
42
+ export { ReactGJSPluginOptions, ReloadHandler, RunnerOptions, gjsRunner };
package/dist/index.mjs CHANGED
@@ -2,57 +2,74 @@ import { spawn } from "node:child_process";
2
2
  import { join, resolve } from "node:path";
3
3
 
4
4
  //#region src/index.ts
5
- let gjs = null;
6
- async function spawnConsole(entry, debug = false) {
7
- gjs = spawn("gjs", ["-m", entry], {
8
- stdio: "inherit",
9
- env: {
10
- ...process.env,
11
- G_MESSAGES_DEBUG: debug ? "all" : void 0
12
- }
13
- });
14
- gjs.on("close", (code) => {
15
- if (code !== null && code !== 0) console.error(`\n[HMR] GJS exited with error code ${code}`);
16
- gjs = null;
5
+ let gjs = null, restarting = false;
6
+ async function spawnConsole(entry, debug = false, env) {
7
+ return new Promise((resolve, reject) => {
8
+ if (!restarting) reject();
9
+ gjs = spawn("gjs", ["-m", entry], {
10
+ stdio: "inherit",
11
+ env: {
12
+ ...process.env,
13
+ G_MESSAGES_DEBUG: debug ? "all" : void 0,
14
+ ...env
15
+ }
16
+ });
17
+ gjs.on("close", (code) => {
18
+ if (!restarting) return process.exit(code ?? 0);
19
+ if (code !== null && code !== 0) console.error(`\n[HMR] GJS exited with error code ${code}`);
20
+ gjs = null;
21
+ });
22
+ gjs.once("spawn", () => {
23
+ resolve();
24
+ });
17
25
  });
18
26
  }
19
- function killConsole() {
20
- if (!gjs || gjs.killed) return;
21
- console.log("[HMR] Restarting the application");
22
- if (!gjs.kill()) gjs.kill("SIGKILL");
23
- gjs = null;
27
+ async function killConsole() {
28
+ return new Promise((resolve) => {
29
+ if (!gjs || gjs.killed) return resolve();
30
+ gjs.once("exit", () => {
31
+ gjs = null;
32
+ resolve();
33
+ });
34
+ console.log("[HMR] Restarting the application");
35
+ if (!gjs.kill()) gjs.kill("SIGKILL");
36
+ });
24
37
  }
25
- function reloadConsole(...args) {
26
- killConsole();
27
- spawnConsole(...args);
38
+ async function reloadConsole(...args) {
39
+ try {
40
+ restarting = true;
41
+ await killConsole();
42
+ await spawnConsole(...args);
43
+ restarting = false;
44
+ } catch {}
28
45
  }
29
- function gjsRunner({ sendMessage, debug, setReloadHandler }) {
30
- const changedFiles = /* @__PURE__ */ new Set();
46
+ function gjsRunner({ sendMessage, debug, setReloadHandler, options: runnerOptions }) {
47
+ let firstRun = true;
31
48
  return {
32
49
  name: "@peachy/plugin-runner",
33
- watchChange(id) {
34
- changedFiles.add(id);
35
- },
36
- writeBundle(options, bundle) {
50
+ async writeBundle(options, bundle) {
37
51
  const entryName = Object.keys(bundle)[0];
38
52
  const entry = options.file || resolve(options.dir || "dist", entryName);
39
53
  setReloadHandler(() => {
40
- reloadConsole(entry, debug);
54
+ return reloadConsole(entry, debug, runnerOptions.env);
41
55
  });
42
- if (!this.meta.watchMode || changedFiles.size === 0) {
43
- reloadConsole(entry, debug);
56
+ if (!this.meta.watchMode || firstRun) {
57
+ reloadConsole(entry, debug, runnerOptions.env);
58
+ firstRun = false;
44
59
  return;
45
60
  }
46
61
  const inputToOutput = getInputToOutputMap(bundle);
47
62
  sendMessage({
48
63
  type: "hmr",
49
- updates: Array.from(changedFiles).filter((id) => inputToOutput.has(id)).map((id) => ({
64
+ updates: Array.from(inputToOutput.entries()).filter(([id]) => {
65
+ if (!id.startsWith(process.cwd()) || !id.match(/\.[tj]sx?$/) || id.includes("node_modules") || id.startsWith("gi://*") || id === join(process.cwd(), "src", "index.tsx") || this.getModuleInfo(id)?.isEntry) return false;
66
+ return true;
67
+ }).map(([id, outputPath]) => ({
50
68
  type: "update",
51
69
  id,
52
- url: resolve(join(process.cwd(), options.dir || "."), inputToOutput.get(id))
70
+ url: resolve(options.dir || ".", outputPath)
53
71
  }))
54
72
  });
55
- changedFiles.clear();
56
73
  }
57
74
  };
58
75
  }
package/package.json CHANGED
@@ -1,22 +1,24 @@
1
1
  {
2
2
  "name": "@peachy/plugin-runner",
3
- "version": "0.0.9",
4
- "type": "module",
3
+ "version": "0.0.11",
5
4
  "description": "Run your GJS applications",
6
- "main": "./dist/index.mjs",
7
- "author": "",
8
5
  "license": "MIT",
6
+ "author": "",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "type": "module",
11
+ "main": "./dist/index.mjs",
9
12
  "devDependencies": {
10
- "rolldown": "1.0.0-beta.60",
11
- "tsdown": "0.20.0-beta.4",
12
- "typescript": "^5.9.3"
13
+ "@types/node": "^25.2.2",
14
+ "rolldown": "1.0.0-rc.3",
15
+ "tsdown": "0.20.3",
16
+ "typescript": "^5.9.3",
17
+ "@peachy/internal-utilities": "0.0.0"
13
18
  },
14
19
  "peerDependencies": {
15
20
  "rolldown": "1.0.0-beta.58"
16
21
  },
17
- "files": [
18
- "dist"
19
- ],
20
22
  "scripts": {
21
23
  "build": "tsdown src/index.ts --dts"
22
24
  },