Skip to content

Commit c9eff15

Browse files
committed
cache/hash system is working. STLs do not regenerate if the underlying scad file has not changed
1 parent 57efe9e commit c9eff15

File tree

15 files changed

+332
-324
lines changed

15 files changed

+332
-324
lines changed

dist/index.js

Lines changed: 173 additions & 177 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eleventy.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { addOpenSCADPlugin } from "./dist/index.js";
66
* @param {import("@11ty/eleventy/UserConfig").default} eleventyConfig
77
*/
88
export default function (eleventyConfig) {
9-
eleventyConfig.setQuietMode(false);
109
eleventyConfig.setInputDirectory("test/_11ty/input");
1110
eleventyConfig.setOutputDirectory("test/_11ty/output");
1211
eleventyConfig.addWatchTarget("dist/**/*");
12+
eleventyConfig.setQuietMode(false);
1313

1414
addOpenSCADPlugin(eleventyConfig, {
1515
launchPath: "nightly",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"dev": "pnpm build --watch ./src",
3636
"bump": "bumpp",
3737
"release": "bumpp && npm publish",
38-
"start": "DEBUG=eleventy:scad* eleventy --serve",
38+
"start": "eleventy --serve",
39+
"debug": "DEBUG=eleventy:scad* pnpm start",
3940
"test": "vitest",
4041
"typecheck": "tsc --noEmit"
4142
},

src/core/cache.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFile } from "node:fs/promises";
2+
import { md5 } from "../lib";
23
import Debug from "../lib/debug";
3-
import { md5 } from "../lib/hash";
44

55
const debug = Debug.extend("cache");
66
const cache = new Map<string, string>();
@@ -11,8 +11,7 @@ export function getFileHash(key: string) {
1111

1212
export function isFileRegistered(file: string) {
1313
const status = cache.has(file);
14-
debug("file: %o", file);
15-
debug("registered: %o", status);
14+
debug({ file, registered: status });
1615
return status;
1716
}
1817

@@ -21,21 +20,29 @@ export function fileNeedsRegistration(file: string) {
2120
}
2221

2322
export async function fileHashesMatch(key: string) {
24-
const currHash = getFileHash(key);
23+
const cachedHash = getFileHash(key);
2524
const scadContent = await readFile(key, "utf8");
2625
const newHash = md5(scadContent);
27-
return newHash === currHash;
26+
const hashMatch = newHash === cachedHash;
27+
// debug("comparing hashes: %O", { cached: cachedHash, current: newHash });
28+
debug("fileHashesMatch: %o", hashMatch);
29+
return hashMatch;
2830
}
2931

3032
export async function fileHashesDiffer(key: string) {
3133
return (await fileHashesMatch(key)) !== true;
3234
}
3335

34-
export async function registerFile(key: string) {
36+
export async function updateHash(key: string) {
3537
const scadContent = await readFile(key, "utf8");
3638
const hash = md5(scadContent);
3739
cache.set(key, hash);
38-
isFileRegistered(key);
40+
debug("hashed %o", key);
41+
}
42+
43+
export async function registerFile(key: string) {
44+
updateHash(key);
45+
debug({ registered: key });
3946
}
4047

4148
export async function ensureFileRegistered(file: string) {

src/core/events.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,16 @@ export function registerEventHandlers(eleventyConfig: EleventyConfig) {
2525

2626
// eleventyConfig.addTemplate;
2727
});
28+
29+
// biome-ignore lint/suspicious/noExplicitAny: merp
30+
eleventyConfig.on("eleventy.contentMap", (_event: any) => {
31+
logger.logWithOptions({
32+
message: "eleventy.after",
33+
color: "magenta",
34+
prefix: "🪵\n\t",
35+
force: true,
36+
});
37+
38+
// eleventyConfig.addTemplate;
39+
});
2840
}

src/core/generator.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { spawn } from "node:child_process";
2+
import Debug from "../lib/debug";
3+
import Timer from "../lib/timer";
4+
5+
const timer = new Timer();
6+
const debug = Debug.extend("generator");
7+
8+
/**
9+
* Generate an `.stl` from a given `.scad` file
10+
*/
11+
export async function scad2stl(
12+
launchPath: string,
13+
files: { in: string; out: string },
14+
): Promise<ScadExportResult> {
15+
return new Promise<ScadExportResult>((resolve, reject) => {
16+
const lines: string[] = [];
17+
const input = normalPath(files.in);
18+
const output = normalPath(files.out);
19+
20+
debug("input: %o", input);
21+
debug("output: %o", output);
22+
23+
const scad = spawn(launchPath, ["--o", output, input]);
24+
25+
scad.on("spawn", () => timer.start());
26+
27+
scad.stdout.on("data", (data) => {
28+
debug("[stdout] %s", data.toString());
29+
});
30+
31+
scad.stderr.on("data", (data) => {
32+
// const lines = String(data).split("\n");
33+
// lines.forEach((line) => void debug("[stderr] %s", line));
34+
lines.push(data.toString());
35+
});
36+
37+
scad.on("error", (err) => {
38+
reject({ output: err, ok: false });
39+
});
40+
41+
scad.on("close", (exitCode) => {
42+
const result: ScadExportResult = {
43+
ok: exitCode === 0,
44+
output: lines,
45+
duration: timer.duration,
46+
exitCode,
47+
};
48+
debug("result: %O", result);
49+
resolve(result);
50+
});
51+
});
52+
}
53+
54+
type ScadExportResult = {
55+
ok: boolean;
56+
output: string[];
57+
exitCode: number | null;
58+
duration: number;
59+
};
60+
61+
function normalPath(path: string) {
62+
return `./${path.replace(/^\.?\/?/, "")}`;
63+
}

src/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export * as cache from "./cache";
22
export * from "./const";
33
export * from "./events";
4+
export * from "./generator";
45
export * from "./global-data";
56
export * from "./highlighters";
67
export * from "./options";
78
export * from "./scad-bin";
8-
export * from "./scad2stl";
99
export * from "./shortcodes";
1010
export * from "./templates";
1111
export * from "./transformer";

src/core/scad2stl.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/core/transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { join } from "node:path";
22
import { getLogger } from "../lib/logger";
33
import { DOT_SCAD, DOT_STL } from "./const";
4-
import { scad2stl } from "./scad2stl";
4+
import { scad2stl } from "./generator";
55
import type { EleventyConfig, EleventyScope } from "../types";
66

77
/**

src/lib/fs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const debug = Debug.extend("fs");
66

77
export function fileExist(file: string) {
88
const state = existsSync(file);
9-
debug("file: %o", file);
10-
debug("exists: %o", state);
9+
debug({ file, exists: state });
1110
return state;
1211
}
1312

0 commit comments

Comments
 (0)