Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Playground][Watch] Reload katas and samples while local development #1487

Merged
merged 1 commit into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions watch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const libsDir = join(thisDir, "library");
const vslsDir = join(thisDir, "language_service");
const wasmDir = join(thisDir, "wasm");
const npmDir = join(thisDir, "npm", "qsharp");
const katasDir = join(thisDir, "katas");
const samplesDir = join(thisDir, "samples");

const isWin = process.platform === "win32";
const npmCmd = isWin ? "npm.cmd" : "npm";
Expand Down Expand Up @@ -98,6 +100,39 @@ onRustChange();
subscribe(dir, onRustChange),
);

let katasBuildPending = false;
function onKatasAndSamplesChange() {
if (katasBuildPending) return; // Already queued
katasBuildPending = true;
setTimeout(() => {
// The build task runs sychronously, so we can clear the timeout handle and
// run the build knowing that nothing will interleave with those operations
if (katasBuildPending) {
katasBuildPending = false;
buildKatasAndSamples();
}
}, buildDelayMs);
}

function buildKatasAndSamples() {
console.log("Recompiling katas and samples content");

const result = spawnSync(npmCmd, ["run", "generate"], {
cwd: npmDir,
shell: true,
});

console.log("Katas and samples recompiled!", result.stderr.toString());
}

// Do an initial build
onKatasAndSamplesChange();

// Watch the katas directories for code changes
[katasDir, samplesDir].forEach((dir) =>
subscribe(dir, onKatasAndSamplesChange),
);

/**
*
* @param {string} dir
Expand Down
Loading