@rulecad/cad 0.7.26 → 0.7.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cad.mjs +36 -7
- package/lib/edit.mjs +19 -1
- package/lib/live.mjs +7 -1
- package/package.json +8 -4
- package/schemas/object.schema.json +1 -1
package/bin/cad.mjs
CHANGED
|
@@ -17,6 +17,7 @@ import { isAssembly, buildAssembly, checkAssembly, shapes, sweep, motionData } f
|
|
|
17
17
|
import { valuesOf, why } from '../lib/explain.mjs';
|
|
18
18
|
import { errorData, near } from '../lib/errors.mjs';
|
|
19
19
|
import { schemaFindings } from '../lib/schema.mjs';
|
|
20
|
+
import { readJson } from '../lib/host.mjs';
|
|
20
21
|
import { optimised, readProblem, analysePart, picture, problemFile } from '../lib/optimise.mjs';
|
|
21
22
|
import { evaluate } from '../lib/rules.mjs';
|
|
22
23
|
import { setVariables, addToAssembly, parseValue } from '../lib/edit.mjs';
|
|
@@ -134,6 +135,15 @@ const showProgress = (e) => {
|
|
|
134
135
|
process.stderr.write(`\r\x1b[K${e.stage === 'sweep' ? `sweeping ${e.joint} ${e.step}/${e.steps}` : `building ${e.item} (${e.done})`}`);
|
|
135
136
|
};
|
|
136
137
|
// Findings: as text (a mark and the words), or with --json as data: { ...about, findings: [{ level, text, code, values, refs, at }] }
|
|
138
|
+
// Every measurement says why it is what it is, and every object its name: a variable with a value and no rule (or an
|
|
139
|
+
// object with no name) is a warning, not a failure, so an older design still builds
|
|
140
|
+
function unruled(object, file) {
|
|
141
|
+
const out = [], none = Object.entries(object.variables || {}).filter(([, v]) => v && v.value !== undefined && !v.type && !v.rule).map(([n]) => n);
|
|
142
|
+
if (none.length) out.push({ level: 'warn', text: `${none.length === 1 ? 'a variable has' : `${none.length} variables have`} no rule saying why ${none.length === 1 ? 'it is' : 'they are'} what ${none.length === 1 ? 'it is' : 'they are'}: ${none.slice(0, 6).join(', ')}${none.length > 6 ? ', …' : ''}`, code: 'var.no-rule', refs: { file, variables: none } });
|
|
143
|
+
if (!object.name) out.push({ level: 'warn', text: 'the object has no "name": give it the file\'s name, so links and releases say what it is', code: 'object.no-name', refs: { file } });
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
137
147
|
function report(findings, about = {}) {
|
|
138
148
|
if (opt('json')) console.log(JSON.stringify({ ...about, findings }, null, 2));
|
|
139
149
|
else { const mark = { pass: '✓', warn: '!', fail: '✗', info: '·' }; for (const r of findings) console.log(` ${mark[r.level]} ${r.text}`); }
|
|
@@ -327,8 +337,13 @@ async function main() {
|
|
|
327
337
|
}
|
|
328
338
|
|
|
329
339
|
if (cmd === 'agents') { // the notes for AI agents, into this workspace (AGENTS.md, and CLAUDE.md pointing at it)
|
|
330
|
-
const to = path.join(WS, 'AGENTS.md');
|
|
331
|
-
|
|
340
|
+
const to = path.join(WS, 'AGENTS.md'), ours = fs.readFileSync(path.join(TOOL, 'AGENTS.md'), 'utf8'), record = paths.cache('agents.md');
|
|
341
|
+
// An AGENTS.md as cad agents last wrote it (or, with no record of that, one that is an earlier copy of ours) is
|
|
342
|
+
// brought up to date; one edited here is kept, unless --force
|
|
343
|
+
const mine = fs.existsSync(to) ? fs.readFileSync(to, 'utf8') : null, last = fs.existsSync(record) ? fs.readFileSync(record, 'utf8') : null;
|
|
344
|
+
const ourCopy = mine === null || mine === ours || (last !== null ? mine === last : mine.startsWith(ours.split('\n')[0]));
|
|
345
|
+
if (!ourCopy && !opt('force')) throw new Error('AGENTS.md here has been edited (or isn\'t rulecad\'s): it is kept as it is (cad agents --force replaces it)');
|
|
346
|
+
fs.writeFileSync(to, ours); fs.mkdirSync(path.dirname(record), { recursive: true }); fs.writeFileSync(record, ours);
|
|
332
347
|
const claude = path.join(WS, 'CLAUDE.md');
|
|
333
348
|
if (!fs.existsSync(claude)) fs.writeFileSync(claude, '@AGENTS.md\n');
|
|
334
349
|
else if (!fs.readFileSync(claude, 'utf8').includes('@AGENTS.md')) fs.appendFileSync(claude, '\n@AGENTS.md\n');
|
|
@@ -367,6 +382,11 @@ async function main() {
|
|
|
367
382
|
if (isAssembly(ref)) return assembly(cmd, ref);
|
|
368
383
|
const [base, at] = ref.split('@');
|
|
369
384
|
const d = at ? materialise(locate(base), at) : locate(ref), set = sets();
|
|
385
|
+
// A check reads the files against their schemas first: a broken .json is said as that, before a build fails on it
|
|
386
|
+
if (cmd === 'check' && !at && fs.existsSync(d.json)) {
|
|
387
|
+
const bad = schemaFindings(readJson(d.json), 'object', path.relative(WS, d.json)).filter((f) => f.level === 'fail'); // (readJson: broken JSON is named with its file)
|
|
388
|
+
if (bad.length) { say(`${d.project}/${d.name}`); report(bad, { object: `${d.project}/${d.name}` }); process.exitCode = 1; return; }
|
|
389
|
+
}
|
|
370
390
|
const b = opt('part') ? pick(await build(d, set), { part: opt('part') }) : await build(d, set);
|
|
371
391
|
const tag = `${d.variant ? `-${d.variant}` : ''}${suffix(set)}${opt('part') ? `-${opt('part')}` : ''}`; // in file names
|
|
372
392
|
const title = `${b.object.title || b.object.name}${d.variant ? ` (${d.variant})` : ''}`, version = b.object.version || '0.0.0';
|
|
@@ -390,6 +410,7 @@ async function main() {
|
|
|
390
410
|
}
|
|
391
411
|
|
|
392
412
|
if (cmd === 'rules') {
|
|
413
|
+
if (opt('json')) return console.log(JSON.stringify({ object: `${d.project}/${d.name}`, variables: b.entries.map((e) => ({ name: e.name, value: e.value, unit: e.unit || null, expr: e.expr || null, rule: e.rule || null, ...(e.source ? { source: e.source, kind: e.kind || null } : {}) })) }, null, 2));
|
|
393
414
|
for (const e of b.entries) console.log(`${e.name.padEnd(18)} ${`${fmt(e.value)} ${e.unit || ''}`.padEnd(14)} ${e.expr ? `= ${e.expr} ` : ''}${e.rule || ''}${e.source ? ` [${e.kind ? `${e.kind}: ` : ''}${e.source}]` : ''}${e.set ? ' (set)' : ''}`);
|
|
394
415
|
return;
|
|
395
416
|
}
|
|
@@ -405,6 +426,7 @@ async function main() {
|
|
|
405
426
|
const failed = report([
|
|
406
427
|
...(schemaProblems.length ? schemaProblems : [{ level: 'pass', text: 'files match their schemas', code: 'schema' }]),
|
|
407
428
|
...(b.units || []).map((u) => ({ level: 'warn', text: `units: ${u.message}`, code: 'units', refs: { file: own(d.json), path: u.path, variable: u.name } })),
|
|
429
|
+
...unruled(b.object, own(d.json)),
|
|
408
430
|
raw.length ? { level: 'fail', text: `numbers in ${path.basename(d.js)} that should be variables: ${raw.map((r) => `line ${r.line}: ${r.text}`).join(', ')}`, code: 'lint.numbers', refs: { file, numbers: raw } }
|
|
409
431
|
: { level: 'pass', text: 'every measurement is a variable', code: 'lint.numbers', refs: { file } },
|
|
410
432
|
...(b.object.role === 'reference'
|
|
@@ -444,8 +466,10 @@ async function main() {
|
|
|
444
466
|
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
445
467
|
const fil = filaments(d.project); // one object, a part per part, each in its filament
|
|
446
468
|
fs.writeFileSync(out, await threemf([{ name: `${d.name}${d.variant ? ` ${d.variant}` : ''}`, parts: b.parts.map((p) => ({ name: p.label, triangles: p.triangles, slot: slotOf(fil, p.filament) })) }], { filaments: slots(fil) }));
|
|
447
|
-
|
|
469
|
+
const notes = filamentNotes(b.parts, fil);
|
|
448
470
|
if (opt('open')) openInSlicer(out);
|
|
471
|
+
if (opt('json')) return console.log(JSON.stringify({ object: `${d.project}/${d.name}`, version, file: path.relative(WS, out), findings: notes }, null, 2)); // (one object: the file is in it, not a line after)
|
|
472
|
+
report(notes);
|
|
449
473
|
return console.log(out);
|
|
450
474
|
}
|
|
451
475
|
|
|
@@ -520,7 +544,7 @@ async function explainValue(ref) {
|
|
|
520
544
|
async function board(cmd, d, b, { title, version, tag }) {
|
|
521
545
|
const pcb = await pcbEngine(), name = `${d.name}@${version}${tag}`;
|
|
522
546
|
if (cmd === 'check') {
|
|
523
|
-
|
|
547
|
+
say(`${title} ${version}`);
|
|
524
548
|
const raw = lint(fs.readFileSync(d.js, 'utf8'));
|
|
525
549
|
// The files first: the object's JSON, and the workspace's own rule sets it uses, against their schemas
|
|
526
550
|
const own = (f) => path.relative(WS, f), ruleFiles = (b.object.uses || []).filter((u) => !u.includes('/')).map((u) => path.join(WS, 'rules', `${u}.json`)).filter((f) => fs.existsSync(f));
|
|
@@ -532,8 +556,8 @@ async function board(cmd, d, b, { title, version, tag }) {
|
|
|
532
556
|
...(b.units || []).map((u) => ({ level: 'warn', text: `units: ${u.message}`, code: 'units', refs: { file: own(d.json), path: u.path, variable: u.name } })),
|
|
533
557
|
raw.length ? { level: 'fail', text: `numbers in ${path.basename(d.js)} that should be variables: ${raw.map((r) => `line ${r.line}: ${r.text}`).join(', ')}` } : { level: 'pass', text: 'every measurement is a variable' },
|
|
534
558
|
...(await pcb.checks(b.circuit, b.values, b.matrix, b.source)),
|
|
535
|
-
]);
|
|
536
|
-
if (b.object.orientation)
|
|
559
|
+
], { object: `${d.project}/${d.name}`, title, version });
|
|
560
|
+
if (b.object.orientation) say(` ${b.object.orientation}`);
|
|
537
561
|
process.exitCode = failed ? 1 : 0;
|
|
538
562
|
return;
|
|
539
563
|
}
|
|
@@ -554,7 +578,9 @@ async function board(cmd, d, b, { title, version, tag }) {
|
|
|
554
578
|
const written = [zip];
|
|
555
579
|
for (const [f, t] of Object.entries(await pcb.kicadFiles(b.circuit, name))) { fs.writeFileSync(path.join(dir, f), t); written.push(path.join(dir, f)); }
|
|
556
580
|
if (b.matrix) { const f = path.join(dir, `${name}-matrix.json`); fs.writeFileSync(f, JSON.stringify(b.matrix, null, 2) + '\n'); written.push(f); }
|
|
557
|
-
|
|
581
|
+
const done = [{ level: 'pass', text: `fab zip: ${Object.keys(fab).length} files (${Object.keys(fab).filter((f) => !/\.(csv|DRL)$/.test(f)).length} Gerber layers, 2 drill files, BOM, placement)` }];
|
|
582
|
+
if (opt('json')) return console.log(JSON.stringify({ object: `${d.project}/${d.name}`, version, files: written.map((f) => path.relative(WS, f)), findings: done }, null, 2));
|
|
583
|
+
report(done);
|
|
558
584
|
for (const f of written) console.log(f);
|
|
559
585
|
}
|
|
560
586
|
|
|
@@ -799,6 +825,7 @@ async function releaseAll(project) {
|
|
|
799
825
|
// The object's variants, one a line: name, then its inputs
|
|
800
826
|
function variants(d) {
|
|
801
827
|
const v = readObject(d).variants || {};
|
|
828
|
+
if (opt('json')) return console.log(JSON.stringify({ object: `${d.project}/${d.name}`, variants: v }, null, 2));
|
|
802
829
|
if (!Object.keys(v).length) return console.log(`${d.name} has no variants: add "variants": { "name": { …inputs } } to its .json`);
|
|
803
830
|
for (const [n, set] of Object.entries(v)) console.log(`${`${d.project}/${d.name}:${n}`.padEnd(32)} ${Object.entries(set).map(([k, x]) => `${k}=${x}`).join(', ')}`);
|
|
804
831
|
}
|
|
@@ -832,11 +859,13 @@ async function status(project) {
|
|
|
832
859
|
async function history(ref) {
|
|
833
860
|
const [project, name] = ref.split('/');
|
|
834
861
|
if (!name) {
|
|
862
|
+
if (opt('json')) return console.log(JSON.stringify({ project, releases: tags(project).reverse() }, null, 2));
|
|
835
863
|
for (const t of tags(project).reverse()) console.log(`${t.tag} ${t.date} ${t.kind} ${t.message}`);
|
|
836
864
|
return status(project);
|
|
837
865
|
}
|
|
838
866
|
const d = locate(ref), done = prints(d);
|
|
839
867
|
const s = await since(d);
|
|
868
|
+
if (opt('json')) return console.log(JSON.stringify({ object: ref, unreleased: s.kind && s.last ? { kind: s.kind, reasons: s.reasons.map((r) => r.text) } : null, releases: tags(d.project, d.name).reverse().map((t) => ({ ...t, prints: done.filter((x) => x.version === t.version) })) }, null, 2));
|
|
840
869
|
if (s.kind && s.last) { console.log(`unreleased (needs ${s.kind}):`); for (const r of s.reasons) console.log(` ${r.text}`); }
|
|
841
870
|
for (const t of tags(d.project, d.name).reverse()) {
|
|
842
871
|
console.log(`${t.version.padEnd(8)} ${t.date} ${t.kind.padEnd(5)} ${t.message}`);
|
package/lib/edit.mjs
CHANGED
|
@@ -39,7 +39,14 @@ export async function setVariables(ref, changes, { create = false } = {}) {
|
|
|
39
39
|
}
|
|
40
40
|
const json = write(d.json, text, 'object');
|
|
41
41
|
try { // the variables must still resolve: an unknown name or a loop puts the file back as it was
|
|
42
|
-
resolve(json, { sets: (await objectSets(d, readObject(d))).sets });
|
|
42
|
+
const { values } = resolve(json, { sets: (await objectSets(d, readObject(d))).sets });
|
|
43
|
+
// and each changed value must lie within the variable's own range, where it has one (a tweak's min and max)
|
|
44
|
+
for (const name of Object.keys(changes)) {
|
|
45
|
+
const t = json.variables?.[name]?.tweak, v = values[name];
|
|
46
|
+
if (!t || typeof v !== 'number') continue;
|
|
47
|
+
const lo = typeof t.min === 'number' ? t.min : -Infinity, hi = typeof t.max === 'number' ? t.max : Infinity;
|
|
48
|
+
if (v < lo || v > hi) throw new CadError('edit.range', `${name} = ${v} is outside its range, ${t.min ?? '…'} to ${t.max ?? '…'} (change the range first if it should go there)`, { file: d.json, path: `variables.${name}` });
|
|
49
|
+
}
|
|
43
50
|
} catch (e) { fs.writeFileSync(d.json, before); throw Object.assign(e, { file: d.json, message: `not changed: ${e.message}` }); }
|
|
44
51
|
return { file: d.json, changed: Object.keys(changes) };
|
|
45
52
|
}
|
|
@@ -66,6 +73,17 @@ export function addToAssembly(ref, what, entry) {
|
|
|
66
73
|
if (getAt(text, [list]) === undefined) text = setAt(text, [list], []);
|
|
67
74
|
const ids = (getAt(text, [list]) || []).map((x) => x.id).filter(Boolean);
|
|
68
75
|
if (entry.id && ids.includes(entry.id)) throw new CadError('edit.duplicate', `${ref} already has a ${what} "${entry.id}"`, { file });
|
|
76
|
+
// a fit names parts as item.part: the item must be in the assembly, and the part one its object declares (or body)
|
|
77
|
+
if (what === 'fit') for (const side of ['a', 'b']) {
|
|
78
|
+
const named = entry[side]; if (typeof named !== 'string') continue;
|
|
79
|
+
const [item, part = 'body'] = named.split('@')[0].split('.'), it = (getAt(text, ['items']) || []).find((x) => x.id === item);
|
|
80
|
+
if (!it) throw new CadError('edit.part', `fit ${side}: no item "${item}" in ${ref} (items: ${(getAt(text, ['items']) || []).map((x) => x.id).join(', ') || 'none'})`, { file });
|
|
81
|
+
if (it.object) {
|
|
82
|
+
const [project] = ref.split('/'), obj = path.join(paths.project(project), 'objects', `${String(it.object).split(/[@:]/)[0]}.json`);
|
|
83
|
+
const parts = fs.existsSync(obj) ? Object.keys(JSON.parse(fs.readFileSync(obj, 'utf8')).parts || { body: {} }) : null;
|
|
84
|
+
if (parts && !parts.includes(part)) throw new CadError('edit.part', `fit ${side}: ${it.object} has no part "${part}" (parts: ${parts.join(', ')})`, { file });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
69
87
|
text = appendAt(text, [list], entry);
|
|
70
88
|
}
|
|
71
89
|
write(file, text, 'assembly');
|
package/lib/live.mjs
CHANGED
|
@@ -221,7 +221,13 @@ if (!isMainThread && workerData?.flag) {
|
|
|
221
221
|
} else if (req.url.startsWith('/three/') && fs.existsSync(path.join(THREE, path.basename(req.url)))) {
|
|
222
222
|
res.writeHead(200, { 'content-type': 'text/javascript' }); res.end(fs.readFileSync(path.join(THREE, path.basename(req.url))));
|
|
223
223
|
} else if (req.url.startsWith('/frame/') && frames[Number(req.url.slice(7))]) { res.writeHead(200, { 'content-type': 'application/json' }); res.end(JSON.stringify(frames[Number(req.url.slice(7))]));
|
|
224
|
-
} else if (req.url === '/stop' && req.method === 'POST') {
|
|
224
|
+
} else if (req.url === '/stop' && req.method === 'POST') {
|
|
225
|
+
// Only this page may stop the run: a request from another page (any site open in the browser can post here) comes
|
|
226
|
+
// with that page's Origin, and is refused
|
|
227
|
+
const origin = req.headers.origin, host = req.headers.host;
|
|
228
|
+
if (origin && origin !== `http://${host}`) { res.writeHead(403); return res.end('not from the live view'); }
|
|
229
|
+
Atomics.store(flag, 0, 1); res.end('stopping');
|
|
230
|
+
}
|
|
225
231
|
else if (req.url === '/') { res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' }); res.end(page(title)); }
|
|
226
232
|
else { res.writeHead(404); res.end(); }
|
|
227
233
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/cad",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.29",
|
|
4
4
|
"description": "The cad command: JSCAD designs from traceable rules, checked for printing and fit, versioned by what fits, shared as links and exported as Bambu 3MF.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -52,8 +52,12 @@
|
|
|
52
52
|
"url": "git+https://github.com/JoniBach/rulecad.git",
|
|
53
53
|
"directory": "packages/cad"
|
|
54
54
|
},
|
|
55
|
+
"homepage": "https://github.com/JoniBach/rulecad/tree/main/packages/cad#readme",
|
|
56
|
+
"bugs": {
|
|
57
|
+
"url": "https://github.com/JoniBach/rulecad/issues"
|
|
58
|
+
},
|
|
55
59
|
"dependencies": {
|
|
56
|
-
"@rulecad/variables": "^0.
|
|
60
|
+
"@rulecad/variables": "^0.3.0",
|
|
57
61
|
"@rulecad/keyboard-layout": "^0.1.2",
|
|
58
62
|
"@rulecad/printcheck": "^0.2.0",
|
|
59
63
|
"@rulecad/fit": "^0.3.0",
|
|
@@ -64,8 +68,8 @@
|
|
|
64
68
|
"@jscad/modeling": "^2.12.0",
|
|
65
69
|
"three": "^0.186.1",
|
|
66
70
|
"@rulecad/fea": "^0.2.1",
|
|
67
|
-
"@rulecad/topopt": "^0.
|
|
68
|
-
"@rulecad/flexures": "^0.
|
|
71
|
+
"@rulecad/topopt": "^0.3.0",
|
|
72
|
+
"@rulecad/flexures": "^0.4.0"
|
|
69
73
|
},
|
|
70
74
|
"peerDependencies": {
|
|
71
75
|
"@rulecad/pcb": "^0.1.0 || ^0.2.0 || ^0.3.0 || ^0.4.0 || ^0.5.0"
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"required": ["value"],
|
|
49
49
|
"additionalProperties": false,
|
|
50
50
|
"properties": {
|
|
51
|
-
"value": { "description": "A number, an expression (x1c.line * 3), or for typed variables a name or text" },
|
|
51
|
+
"value": { "description": "A number, an expression (x1c.line * 3), or for typed variables a name or text", "type": ["number", "string", "boolean", "array", "object"] },
|
|
52
52
|
"unit": { "type": "string" },
|
|
53
53
|
"rule": { "type": "string", "description": "Why the value is what it is" },
|
|
54
54
|
"label": { "type": "string" },
|