Skip to content

Commit

Permalink
[Playground][Watch] Reload katas and samples while local development (#…
Browse files Browse the repository at this point in the history
…1487)

### Context
Previously, katas and samples in playground didnt change as a result of
changes in katas and samples directory. Hence I am following a similar
pattern to the rust builds.

### Verification
Playground gets updated during local development.

@tcNickolas , @minestarks could you please look into this small PR.
  • Loading branch information
Manvi-Agrawal committed May 7, 2024
1 parent 1d8f285 commit 0411535
Showing 1 changed file with 35 additions and 0 deletions.
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

0 comments on commit 0411535

Please sign in to comment.