MCPcopy Create free account
hub / github.com/EmNudge/watlings / patchFile

Function patchFile

scripts/utils/patchFile.mjs:8–42  ·  view source on GitHub ↗
(stub = process.argv[2])

Source from the content-addressed store, hash-verified

6import { colors } from "./colors.mjs";
7
8export async function patchFile(stub = process.argv[2]) {
9 const sourceFileNameWithExt = await findFile(stub, "patch");
10 if (!sourceFileNameWithExt) {
11 console.log(`No file matching ${stub} found in the exercises folder.`);
12 process.exit(1);
13 }
14
15 const nameBase = basename(sourceFileNameWithExt, ".patch");
16
17 const patchFilePath = fileURLToPath(
18 new URL(`../../patch/${nameBase}.patch`, import.meta.url)
19 );
20 const patchFile = await readFile(patchFilePath, "utf-8").catch(() => {
21 console.error(`No patch file found under patch/${nameBase}.patch`);
22 process.exit(1);
23 });
24
25 const sourceFilePath = fileURLToPath(
26 new URL(`../../exercises/${nameBase}.wat`, import.meta.url)
27 );
28 const sourceFile = await readFile(sourceFilePath, "utf-8");
29
30 try {
31 const patchedContent = patch(patchFile, sourceFile);
32
33 await writeFile(sourceFilePath, patchedContent);
34
35 console.log(`Patch applied successfully to: ${colors.bold(nameBase)}`);
36 } catch (e) {
37 console.error(`Error applying patch: ${colors.red(e.message)}`);
38 console.info(
39 "\nYou may want to stash your changes to the file before trying again,"
40 );
41 }
42}

Callers 1

solve.mjsFile · 0.90

Calls 2

findFileFunction · 0.90
patchFunction · 0.90

Tested by

no test coverage detected