rwsdk 0.1.7-test.20250702145135 โ†’ 0.1.8-test.20250702232536

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/bin/rwsync CHANGED
@@ -1,2 +1,12 @@
1
1
  #!/bin/sh
2
- DIR=$PWD && (cd "${@:-$SDK_REPO}/sdk" && pnpm debug:sync $DIR)
2
+ set -e
3
+
4
+ # This script is a lightweight wrapper that passes all arguments to the underlying
5
+ # debug:sync Node.js script.
6
+ # The RWSDK_REPO env var should point to the root of the sdk repo.
7
+
8
+ # Capture the current directory *before* changing it
9
+ TARGET_DIR=$PWD
10
+
11
+ cd "${RWSDK_REPO}/sdk"
12
+ pnpm debug:sync "$TARGET_DIR" "$@"
@@ -1,8 +1,6 @@
1
1
  export interface DebugSyncOptions {
2
2
  targetDir: string;
3
3
  sdkDir?: string;
4
- dev?: boolean;
5
- watch?: boolean;
6
- build?: boolean;
4
+ watch?: string | boolean;
7
5
  }
8
6
  export declare const debugSync: (opts: DebugSyncOptions) => Promise<void>;
@@ -1,8 +1,9 @@
1
1
  import path from "node:path";
2
2
  import { fileURLToPath } from "node:url";
3
- import { $ } from "../lib/$.mjs";
3
+ import { $ } from "execa";
4
4
  import fs from "node:fs/promises";
5
5
  import { existsSync } from "node:fs";
6
+ import chokidar from "chokidar";
6
7
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
8
  const getPackageManagerInfo = (targetDir) => {
8
9
  if (existsSync(path.join(targetDir, "yarn.lock"))) {
@@ -13,14 +14,16 @@ const getPackageManagerInfo = (targetDir) => {
13
14
  }
14
15
  return { name: "npm", lockFile: "package-lock.json", command: "install" };
15
16
  };
16
- const performSync = async (sdkDir, targetDir) => {
17
- console.log("๐Ÿ—๏ธ rebuilding sdk...");
18
- await $({ cwd: sdkDir, stdio: "inherit", shell: true }) `pnpm build`;
19
- console.log("๐Ÿ“ฆ packing sdk...");
20
- const packResult = await $({ cwd: sdkDir, shell: true }) `npm pack`;
17
+ const performFullSync = async (sdkDir, targetDir) => {
18
+ console.log("๐Ÿ“ฆ Packing SDK...");
19
+ const packResult = await $ `npm pack`;
21
20
  const tarballName = packResult.stdout?.trim() ?? "";
21
+ if (!tarballName) {
22
+ console.error("โŒ Failed to get tarball name from npm pack.");
23
+ return;
24
+ }
22
25
  const tarballPath = path.resolve(sdkDir, tarballName);
23
- console.log(` installing ${tarballName} in ${targetDir}...`);
26
+ console.log(`๐Ÿ’ฟ Installing ${tarballName} in ${targetDir}...`);
24
27
  const pm = getPackageManagerInfo(targetDir);
25
28
  const packageJsonPath = path.join(targetDir, "package.json");
26
29
  const lockfilePath = path.join(targetDir, pm.lockFile);
@@ -35,11 +38,7 @@ const performSync = async (sdkDir, targetDir) => {
35
38
  if (pm.name === "yarn") {
36
39
  installCommand = `yarn add file:${tarballPath}`;
37
40
  }
38
- await $({
39
- cwd: targetDir,
40
- stdio: "inherit",
41
- shell: true,
42
- }) `${installCommand}`;
41
+ await $ `${installCommand}`;
43
42
  }
44
43
  finally {
45
44
  if (originalPackageJson) {
@@ -54,66 +53,113 @@ const performSync = async (sdkDir, targetDir) => {
54
53
  // ignore if deletion fails
55
54
  });
56
55
  }
57
- console.log("โœ… done syncing");
56
+ };
57
+ const performFastSync = async (sdkDir, targetDir) => {
58
+ console.log("โšก๏ธ No dependency changes, performing fast sync...");
59
+ const sdkPackageJson = JSON.parse(await fs.readFile(path.join(sdkDir, "package.json"), "utf-8"));
60
+ const filesToSync = sdkPackageJson.files || [];
61
+ for (const file of filesToSync) {
62
+ const source = path.join(sdkDir, file);
63
+ const destination = path.join(targetDir, "node_modules/rwsdk", file);
64
+ if (existsSync(source)) {
65
+ await fs.cp(source, destination, { recursive: true, force: true });
66
+ }
67
+ }
68
+ // Always copy package.json
69
+ await fs.copyFile(path.join(sdkDir, "package.json"), path.join(targetDir, "node_modules/rwsdk/package.json"));
70
+ };
71
+ const performSync = async (sdkDir, targetDir) => {
72
+ console.log("๐Ÿ—๏ธ Rebuilding SDK...");
73
+ await $ `pnpm build`;
74
+ const sdkPackageJsonPath = path.join(sdkDir, "package.json");
75
+ const installedSdkPackageJsonPath = path.join(targetDir, "node_modules/rwsdk/package.json");
76
+ let packageJsonChanged = true;
77
+ if (existsSync(installedSdkPackageJsonPath)) {
78
+ const sdkPackageJsonContent = await fs.readFile(sdkPackageJsonPath, "utf-8");
79
+ const installedSdkPackageJsonContent = await fs.readFile(installedSdkPackageJsonPath, "utf-8");
80
+ if (sdkPackageJsonContent === installedSdkPackageJsonContent) {
81
+ packageJsonChanged = false;
82
+ }
83
+ }
84
+ if (packageJsonChanged) {
85
+ console.log("๐Ÿ“ฆ package.json changed, performing full sync...");
86
+ await performFullSync(sdkDir, targetDir);
87
+ }
88
+ else {
89
+ await performFastSync(sdkDir, targetDir);
90
+ }
91
+ console.log("โœ… Done syncing");
58
92
  };
59
93
  export const debugSync = async (opts) => {
60
- const { targetDir, sdkDir = process.cwd(), dev, watch, build } = opts;
94
+ const { targetDir, sdkDir = process.cwd(), watch } = opts;
61
95
  if (!targetDir) {
62
96
  console.error("โŒ Please provide a target directory as an argument.");
63
97
  process.exit(1);
64
98
  }
65
- const thisScriptPath = fileURLToPath(import.meta.url);
66
- const syncCommand = `tsx ${thisScriptPath} --_sync ${sdkDir} ${targetDir}`;
67
- // Run initial sync
99
+ // Initial sync
68
100
  await performSync(sdkDir, targetDir);
69
- if (!process.env.NO_CLEAN_VITE) {
70
- console.log("๐Ÿงน Cleaning Vite cache...");
71
- await $({
72
- stdio: "inherit",
73
- shell: true,
74
- cwd: targetDir,
75
- }) `rm -rf ${targetDir}/node_modules/.vite*`;
76
- }
77
- // If dev flag is present, clean vite cache and start dev server
78
- if (dev) {
79
- console.log("๐Ÿš€ Starting dev server...");
80
- await $({ stdio: "inherit", shell: true, cwd: targetDir }) `npm run dev`;
81
- }
82
- // Start watching if watch flag is present
83
- else if (watch) {
101
+ if (watch) {
102
+ const sdkPackageJson = JSON.parse(await fs.readFile(path.join(sdkDir, "package.json"), "utf-8"));
103
+ const filesToWatch = (sdkPackageJson.files || []).map((p) => path.join(sdkDir, p));
104
+ filesToWatch.push(path.join(sdkDir, "package.json"));
84
105
  console.log("๐Ÿ‘€ Watching for changes...");
85
- $({
86
- stdio: "inherit",
87
- shell: true,
88
- }) `npx chokidar-cli './src/**' './package.json' -c "${syncCommand}"`;
89
- }
90
- else if (build) {
91
- console.log("๐Ÿ—๏ธ Running build in target directory...");
92
- await $({
93
- stdio: "inherit",
94
- shell: true,
95
- cwd: targetDir,
96
- }) `npm run build`;
106
+ let childProc = null;
107
+ const runWatchedCommand = () => {
108
+ if (typeof watch === "string") {
109
+ console.log(`\n> ${watch}\n`);
110
+ childProc = $({
111
+ stdio: "inherit",
112
+ shell: true,
113
+ cwd: targetDir,
114
+ reject: false,
115
+ }) `${watch}`;
116
+ }
117
+ };
118
+ const watcher = chokidar.watch(filesToWatch, {
119
+ ignoreInitial: true,
120
+ cwd: sdkDir,
121
+ });
122
+ watcher.on("all", async () => {
123
+ console.log("\n Detected change, re-syncing...");
124
+ if (childProc && !childProc.killed) {
125
+ console.log("Stopping running process...");
126
+ childProc.kill();
127
+ await childProc.catch(() => {
128
+ /* ignore kill errors */
129
+ });
130
+ }
131
+ await performSync(sdkDir, targetDir);
132
+ runWatchedCommand();
133
+ });
134
+ const cleanup = () => {
135
+ if (childProc && !childProc.killed) {
136
+ childProc.kill();
137
+ }
138
+ };
139
+ process.on("SIGINT", cleanup);
140
+ process.on("SIGTERM", cleanup);
141
+ runWatchedCommand();
97
142
  }
98
143
  };
99
144
  if (import.meta.url === new URL(process.argv[1], import.meta.url).href) {
100
145
  const args = process.argv.slice(2);
101
- const flags = new Set(args.filter((arg) => arg.startsWith("--")));
102
- const positionalArgs = args.filter((arg) => !arg.startsWith("--"));
103
- if (flags.has("--_sync")) {
104
- const [sdkDir, targetDir] = positionalArgs;
105
- await performSync(sdkDir, targetDir);
106
- }
107
- else {
108
- const targetDir = positionalArgs[0] ?? process.cwd();
109
- debugSync({
110
- targetDir,
111
- sdkDir: process.env.SDK_REPO
112
- ? path.resolve(__dirname, process.env.SDK_REPO, "sdk")
113
- : path.resolve(__dirname, "..", ".."),
114
- dev: flags.has("--dev"),
115
- watch: flags.has("--watch"),
116
- build: flags.has("--build"),
117
- });
146
+ const watchFlagIndex = args.indexOf("--watch");
147
+ let watchCmd = watchFlagIndex !== -1;
148
+ let cmdArgs = args;
149
+ if (watchFlagIndex !== -1) {
150
+ if (watchFlagIndex + 1 < args.length &&
151
+ !args[watchFlagIndex + 1].startsWith("--")) {
152
+ watchCmd = args[watchFlagIndex + 1];
153
+ }
154
+ // remove --watch and its potential command from args
155
+ const watchArgCount = typeof watchCmd === "string" ? 2 : 1;
156
+ cmdArgs = args.filter((_, i) => i < watchFlagIndex || i >= watchFlagIndex + watchArgCount);
118
157
  }
158
+ const targetDir = cmdArgs[0] ?? process.cwd();
159
+ const sdkDir = path.resolve(__dirname, "..", "..");
160
+ debugSync({
161
+ targetDir,
162
+ sdkDir,
163
+ watch: watchCmd,
164
+ });
119
165
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.1.7-test.20250702145135",
3
+ "version": "0.1.8-test.20250702232536",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {
@@ -120,6 +120,7 @@
120
120
  "@types/react-dom": "^19.1.2",
121
121
  "@types/react-is": "^19.0.0",
122
122
  "@vitejs/plugin-react": "^4.3.4",
123
+ "chokidar": "^3.6.0",
123
124
  "debug": "^4.4.0",
124
125
  "enhanced-resolve": "^5.18.1",
125
126
  "eventsource-parser": "^3.0.0",