rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
52 lines (51 loc) โข 1.98 kB
JavaScript
import { $ } from "../lib/$.mjs";
export const debugSync = async (opts) => {
const { targetDir, dev, watch, build } = opts;
if (!targetDir) {
console.error("โ Please provide a target directory as an argument.");
process.exit(1);
}
const syncCommand = `echo ๐๏ธ rebuilding... && pnpm build && rm -rf ${targetDir}/node_modules/rwsdk/dist ${targetDir}/node_modules/rwsdk/package.json && echo ๐ syncing sdk from ${process.cwd()} to ${targetDir}/node_modules/rwsdk/... && cp -r package.json dist ${targetDir}/node_modules/rwsdk/ && echo โ
done syncing`;
// Run initial sync
await $({ stdio: "inherit", shell: true }) `${syncCommand}`;
if (!process.env.NO_CLEAN_VITE) {
console.log("๐งน Cleaning Vite cache...");
await $({
stdio: "inherit",
shell: true,
cwd: targetDir,
}) `rm -rf node_modules/.vite`;
}
// If dev flag is present, clean vite cache and start dev server
if (dev) {
console.log("๐ Starting dev server...");
await $({ stdio: "inherit", shell: true, cwd: targetDir }) `npm run dev`;
}
// Start watching if watch flag is present
else if (watch) {
console.log("๐ Watching for changes...");
$({
stdio: "inherit",
shell: true,
}) `npx chokidar-cli './src/**' './package.json' -c "${syncCommand}"`;
}
else if (build) {
console.log("๐๏ธ Running build in target directory...");
await $({
stdio: "inherit",
shell: true,
cwd: targetDir,
}) `npm run build`;
}
};
if (import.meta.url === new URL(process.argv[1], import.meta.url).href) {
const args = process.argv.slice(2);
const targetDir = args[0];
const flags = new Set(args.slice(1));
debugSync({
targetDir,
dev: flags.has("--dev"),
watch: flags.has("--watch"),
build: flags.has("--build"),
});
}