rwsdk 0.1.7 โ†’ 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 ADDED
@@ -0,0 +1,12 @@
1
+ #!/bin/sh
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,57 +1,165 @@
1
1
  import path from "node:path";
2
2
  import { fileURLToPath } from "node:url";
3
- import { $ } from "../lib/$.mjs";
3
+ import { $ } from "execa";
4
+ import fs from "node:fs/promises";
5
+ import { existsSync } from "node:fs";
6
+ import chokidar from "chokidar";
4
7
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
+ const getPackageManagerInfo = (targetDir) => {
9
+ if (existsSync(path.join(targetDir, "yarn.lock"))) {
10
+ return { name: "yarn", lockFile: "yarn.lock", command: "add" };
11
+ }
12
+ if (existsSync(path.join(targetDir, "pnpm-lock.yaml"))) {
13
+ return { name: "pnpm", lockFile: "pnpm-lock.yaml", command: "add" };
14
+ }
15
+ return { name: "npm", lockFile: "package-lock.json", command: "install" };
16
+ };
17
+ const performFullSync = async (sdkDir, targetDir) => {
18
+ console.log("๐Ÿ“ฆ Packing SDK...");
19
+ const packResult = await $ `npm pack`;
20
+ const tarballName = packResult.stdout?.trim() ?? "";
21
+ if (!tarballName) {
22
+ console.error("โŒ Failed to get tarball name from npm pack.");
23
+ return;
24
+ }
25
+ const tarballPath = path.resolve(sdkDir, tarballName);
26
+ console.log(`๐Ÿ’ฟ Installing ${tarballName} in ${targetDir}...`);
27
+ const pm = getPackageManagerInfo(targetDir);
28
+ const packageJsonPath = path.join(targetDir, "package.json");
29
+ const lockfilePath = path.join(targetDir, pm.lockFile);
30
+ const originalPackageJson = await fs
31
+ .readFile(packageJsonPath, "utf-8")
32
+ .catch(() => null);
33
+ const originalLockfile = await fs
34
+ .readFile(lockfilePath, "utf-8")
35
+ .catch(() => null);
36
+ try {
37
+ let installCommand = `${pm.name} ${pm.command} ${tarballPath}`;
38
+ if (pm.name === "yarn") {
39
+ installCommand = `yarn add file:${tarballPath}`;
40
+ }
41
+ await $ `${installCommand}`;
42
+ }
43
+ finally {
44
+ if (originalPackageJson) {
45
+ console.log("Restoring package.json...");
46
+ await fs.writeFile(packageJsonPath, originalPackageJson);
47
+ }
48
+ if (originalLockfile) {
49
+ console.log(`Restoring ${pm.lockFile}...`);
50
+ await fs.writeFile(lockfilePath, originalLockfile);
51
+ }
52
+ await fs.unlink(tarballPath).catch(() => {
53
+ // ignore if deletion fails
54
+ });
55
+ }
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");
92
+ };
5
93
  export const debugSync = async (opts) => {
6
- const { targetDir, sdkDir = process.cwd(), dev, watch, build } = opts;
94
+ const { targetDir, sdkDir = process.cwd(), watch } = opts;
7
95
  if (!targetDir) {
8
96
  console.error("โŒ Please provide a target directory as an argument.");
9
97
  process.exit(1);
10
98
  }
11
- const syncCommand = `echo ๐Ÿ—๏ธ rebuilding... && pnpm build && rm -rf ${targetDir}/node_modules/rwsdk/dist ${targetDir}/node_modules/rwsdk/package.json && echo ๐Ÿ“ syncing sdk from ${sdkDir} to ${targetDir}/node_modules/rwsdk/... && cp -r ${sdkDir}/package.json ${sdkDir}/dist ${targetDir}/node_modules/rwsdk/ && echo โœ… done syncing`;
12
- // Run initial sync
13
- await $({ stdio: "inherit", shell: true }) `${syncCommand}`;
14
- if (!process.env.NO_CLEAN_VITE) {
15
- console.log("๐Ÿงน Cleaning Vite cache...");
16
- await $({
17
- stdio: "inherit",
18
- shell: true,
19
- cwd: targetDir,
20
- }) `rm -rf ${targetDir}/node_modules/.vite*`;
21
- }
22
- // If dev flag is present, clean vite cache and start dev server
23
- if (dev) {
24
- console.log("๐Ÿš€ Starting dev server...");
25
- await $({ stdio: "inherit", shell: true, cwd: targetDir }) `npm run dev`;
26
- }
27
- // Start watching if watch flag is present
28
- else if (watch) {
99
+ // Initial sync
100
+ await performSync(sdkDir, targetDir);
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"));
29
105
  console.log("๐Ÿ‘€ Watching for changes...");
30
- $({
31
- stdio: "inherit",
32
- shell: true,
33
- }) `npx chokidar-cli './src/**' './package.json' -c "${syncCommand}"`;
34
- }
35
- else if (build) {
36
- console.log("๐Ÿ—๏ธ Running build in target directory...");
37
- await $({
38
- stdio: "inherit",
39
- shell: true,
40
- cwd: targetDir,
41
- }) `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();
42
142
  }
43
143
  };
44
144
  if (import.meta.url === new URL(process.argv[1], import.meta.url).href) {
45
145
  const args = process.argv.slice(2);
46
- const targetDir = args[0] ?? process.cwd();
47
- const flags = new Set(args.slice(1));
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);
157
+ }
158
+ const targetDir = cmdArgs[0] ?? process.cwd();
159
+ const sdkDir = path.resolve(__dirname, "..", "..");
48
160
  debugSync({
49
161
  targetDir,
50
- sdkDir: process.env.SDK_REPO
51
- ? path.resolve(__dirname, process.env.SDK_REPO, "sdk")
52
- : path.resolve(__dirname, "..", ".."),
53
- dev: flags.has("--dev"),
54
- watch: flags.has("--watch"),
55
- build: flags.has("--build"),
162
+ sdkDir,
163
+ watch: watchCmd,
56
164
  });
57
165
  }
package/package.json CHANGED
@@ -1,14 +1,16 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.1.7",
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": {
7
- "rw-scripts": "./bin/rw-scripts.mjs"
7
+ "rw-scripts": "./bin/rw-scripts.mjs",
8
+ "rwsync": "./bin/rwsync"
8
9
  },
9
10
  "files": [
10
11
  "./README.md",
11
- "./dist"
12
+ "./dist",
13
+ "./bin"
12
14
  ],
13
15
  "exports": {
14
16
  "./vite": {
@@ -118,6 +120,7 @@
118
120
  "@types/react-dom": "^19.1.2",
119
121
  "@types/react-is": "^19.0.0",
120
122
  "@vitejs/plugin-react": "^4.3.4",
123
+ "chokidar": "^3.6.0",
121
124
  "debug": "^4.4.0",
122
125
  "enhanced-resolve": "^5.18.1",
123
126
  "eventsource-parser": "^3.0.0",