@rulecad/cad 0.7.29 → 0.7.31
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/README.md +4 -0
- package/bin/cad.mjs +12 -4
- package/lib/sandbox.mjs +3 -2
- package/lib/targets.mjs +2 -1
- package/lib/versions.mjs +6 -3
- package/lib/workspace.mjs +23 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -272,6 +272,10 @@ other items' variables, so a keycap can sit exactly where its slot meets the swi
|
|
|
272
272
|
reach it. Run only designs you trust. It gets its values as data and returns its parts; what it prints
|
|
273
273
|
(`console.log`) comes out on stderr. A workspace that trusts its designs sets `"sandbox": false` in cad.config.json
|
|
274
274
|
(or `CAD_TRUST=1`). Board designs (engine pcb) run in the tool's process, with the board engine.
|
|
275
|
+
- **Trust, asked once:** what a workspace's cad.config.json asks to run outside the sandbox (`"sandbox": false`, a
|
|
276
|
+
target that is a module of its own) isn't done until you run `cad trust` in it. That records the folder and what it
|
|
277
|
+
asked for in your own list (`~/.config/rulecad/trusted.json`, or `$CAD_TRUSTED`), so a workspace cloned from
|
|
278
|
+
elsewhere can't turn the sandbox off on its first command; one that later asks for more is asked again.
|
|
275
279
|
- **Mistakes are named:** a design that reads a variable that isn't there (`v.widht`) stops with the name it
|
|
276
280
|
probably meant, not a shape drawn from `undefined`; errors in a design carry its file and line.
|
|
277
281
|
- **Units:** `cad check` warns where an expression mixes units (millimetres added to degrees, a sine of a length) or
|
package/bin/cad.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import fs from 'node:fs';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { execFileSync } from 'node:child_process';
|
|
6
6
|
import { fmt, loadRuleSet } from '../lib/rules.mjs';
|
|
7
|
-
import { WS, TOOL, paths, config, configError, printerOf } from '../lib/workspace.mjs';
|
|
7
|
+
import { WS, TOOL, paths, config, configError, printerOf, asks, trust, trusted } from '../lib/workspace.mjs';
|
|
8
8
|
import { fromKLE } from '@rulecad/keyboard-layout';
|
|
9
9
|
import { target, hasTarget } from '../lib/targets.mjs';
|
|
10
10
|
import { locate, build, pick, lint, readObject, printer, pcbEngine, objectSets } from '../lib/design.mjs';
|
|
@@ -44,6 +44,7 @@ const HELP = `cad: JSCAD designs you can trace, check, share and print (rulecad)
|
|
|
44
44
|
cad import-kle <file> <project>/<name> a keyboard layout from keyboard-layout-editor.com, into layouts/
|
|
45
45
|
cad clone <board.kicad_pcb | GitHub link> <name> a published circuit board into rules/<name>.json, to redraw by rules [--source, --licence]
|
|
46
46
|
cad agents AGENTS.md: how an AI agent should design in this workspace
|
|
47
|
+
cad trust allow what this workspace's cad.config.json asks (no sandbox, its own targets)
|
|
47
48
|
cad rules <project>/<object> every variable: value, the rule behind it, its source
|
|
48
49
|
cad values <project>/<object> [names…] the object's variables, resolved [--json]
|
|
49
50
|
cad set <project>/<object> name=value … change variables, keeping the file's layout [--unit, --rule, --label, --new to add one]
|
|
@@ -98,7 +99,7 @@ const HELP = `cad: JSCAD designs you can trace, check, share and print (rulecad)
|
|
|
98
99
|
cad --version this tool's version`;
|
|
99
100
|
|
|
100
101
|
// Every command and option there is: a misspelt one is refused by name, not passed over
|
|
101
|
-
const COMMANDS = ['new', 'import-kle', 'clone', 'agents', 'rules', 'values', 'set', 'add', 'why', 'check', 'preview', 'animate', 'optimise', 'optimize', 'export',
|
|
102
|
+
const COMMANDS = ['new', 'import-kle', 'clone', 'agents', 'trust', 'rules', 'values', 'set', 'add', 'why', 'check', 'preview', 'animate', 'optimise', 'optimize', 'export',
|
|
102
103
|
'show', 'link', 'status', 'release', 'history', 'diff', 'printed', 'measure', 'match', 'variants', 'compare', 'help'];
|
|
103
104
|
const OPTIONS = ['all', 'along', 'at', 'bump', 'depth', 'dry-run', 'explode', 'force', 'fresh', 'json', 'licence', 'license', 'long', 'm', 'material', 'md', 'name', 'new',
|
|
104
105
|
'no-models', 'no-pictures', 'no-rules', 'note', 'open', 'part', 'place', 'pose', 'progress', 'push', 'result', 'rules', 'samples', 'set', 'short', 'source', 'steps',
|
|
@@ -336,6 +337,13 @@ async function main() {
|
|
|
336
337
|
return console.log(`made projects/${project}/objects/${name}.{js,json}`);
|
|
337
338
|
}
|
|
338
339
|
|
|
340
|
+
if (cmd === 'trust') { // this workspace may do what its cad.config.json asks (designs outside the sandbox, its own targets)
|
|
341
|
+
const what = asks();
|
|
342
|
+
if (!what.length) return console.log('this workspace asks for nothing outside the sandbox: nothing to trust');
|
|
343
|
+
trust();
|
|
344
|
+
return console.log(`trusted ${WS} for ${what.join(' and ')} (kept in your own list; a workspace that asks for more is asked again)`);
|
|
345
|
+
}
|
|
346
|
+
|
|
339
347
|
if (cmd === 'agents') { // the notes for AI agents, into this workspace (AGENTS.md, and CLAUDE.md pointing at it)
|
|
340
348
|
const to = path.join(WS, 'AGENTS.md'), ours = fs.readFileSync(path.join(TOOL, 'AGENTS.md'), 'utf8'), record = paths.cache('agents.md');
|
|
341
349
|
// An AGENTS.md as cad agents last wrote it (or, with no record of that, one that is an earlier copy of ours) is
|
|
@@ -733,7 +741,7 @@ function changelog(dir, heading, message, reasons) {
|
|
|
733
741
|
const old = fs.existsSync(f) ? fs.readFileSync(f, 'utf8').replace(/^# .*\n+/, '') : '';
|
|
734
742
|
const entry = `## ${heading}\n\n${message}\n${reasons.length ? `\n${reasons.map((r) => `- ${r.text} (${r.kind})`).join('\n')}\n` : ''}`;
|
|
735
743
|
fs.writeFileSync(f, `# ${path.basename(dir)} changelog\n\n${entry}\n${old}`);
|
|
736
|
-
return path.relative(WS, f);
|
|
744
|
+
return path.relative(WS, f).split(path.sep).join('/'); // (git's terms: forward slashes)
|
|
737
745
|
}
|
|
738
746
|
|
|
739
747
|
async function releaseObject(d, { batch = false } = {}) {
|
|
@@ -756,7 +764,7 @@ async function releaseObject(d, { batch = false } = {}) {
|
|
|
756
764
|
console.log(`${tag} (${kind}${last ? `, from ${last.version}` : ''})`);
|
|
757
765
|
for (const r of reasons) console.log(` ${r.kind.padEnd(5)} ${r.text}`);
|
|
758
766
|
if (opt('dry-run')) return;
|
|
759
|
-
const allowed = [...files(d, object), `projects/${d.project}/CHANGELOG.md`, path.relative(WS, printsFile(d))];
|
|
767
|
+
const allowed = [...files(d, object), `projects/${d.project}/CHANGELOG.md`, path.relative(WS, printsFile(d)).split(path.sep).join('/')];
|
|
760
768
|
if (!batch) onlyChanged(allowed); // releaseAll checked the whole tree first
|
|
761
769
|
fs.writeFileSync(d.json, setAt(fs.readFileSync(d.json, 'utf8'), ['version'], version)); // the object's own version (added, if it had none), nothing else
|
|
762
770
|
const log = changelog(d.dir, `${d.name} ${version} (${kind}), ${today()}`, message, reasons);
|
package/lib/sandbox.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import path from 'node:path';
|
|
|
15
15
|
import { spawn } from 'node:child_process';
|
|
16
16
|
import * as nodeModule from 'node:module';
|
|
17
17
|
import { pathToFileURL, fileURLToPath } from 'node:url';
|
|
18
|
-
import { WS, TOOL, config, paths } from './workspace.mjs';
|
|
18
|
+
import { WS, TOOL, config, paths, trusted } from './workspace.mjs';
|
|
19
19
|
import { fromFaces } from './parts.mjs';
|
|
20
20
|
import { located } from './errors.mjs';
|
|
21
21
|
|
|
@@ -27,7 +27,8 @@ const flag = (() => {
|
|
|
27
27
|
const inThread = typeof nodeModule.registerHooks === 'function'; // import hooks in this thread; older Node needs a worker for them
|
|
28
28
|
|
|
29
29
|
/** Whether designs run sandboxed here. */
|
|
30
|
-
|
|
30
|
+
// (a workspace's "sandbox": false counts only once the user has trusted it: cad trust)
|
|
31
|
+
export const sandboxed = () => !!flag && process.env.CAD_TRUST !== '1' && !(config.sandbox === false && trusted());
|
|
31
32
|
|
|
32
33
|
// What the sandbox may read: the workspace, and the packages designs and the runner load (wherever npm put them)
|
|
33
34
|
function readable() {
|
package/lib/targets.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
9
|
import { pathToFileURL } from 'node:url';
|
|
10
|
-
import { WS, config } from './workspace.mjs';
|
|
10
|
+
import { WS, config, needTrust } from './workspace.mjs';
|
|
11
11
|
|
|
12
12
|
export const hasTarget = (kind) => !!config.targets?.[kind];
|
|
13
13
|
|
|
@@ -15,6 +15,7 @@ export async function target(kind) {
|
|
|
15
15
|
const name = config.targets?.[kind];
|
|
16
16
|
if (!name) throw new Error(`no "${kind}" target: name one in cad.config.json ("targets": { "${kind}": … })`);
|
|
17
17
|
const own = new URL(`./targets/${name}.mjs`, import.meta.url);
|
|
18
|
+
if (!fs.existsSync(own)) needTrust(); // (the workspace's own code, run in the tool's process: only once it is trusted)
|
|
18
19
|
const mod = await import(fs.existsSync(own) ? own : pathToFileURL(path.resolve(WS, name)).href);
|
|
19
20
|
if (typeof mod[kind] !== 'function') throw new Error(`target ${name} has no ${kind}()`);
|
|
20
21
|
return mod[kind];
|
package/lib/versions.mjs
CHANGED
|
@@ -36,10 +36,13 @@ export function tags(project, name) {
|
|
|
36
36
|
const layoutNames = (d, object) => Object.values(object.variables || {}).filter((v) => v.type === 'layout')
|
|
37
37
|
.flatMap((v) => v.choice || [v.value]).filter((n) => hasLayout(d.project, n, d.layoutsDir));
|
|
38
38
|
|
|
39
|
+
// A file's path in git's terms: relative to the workspace, forward slashes on every system (git show tag:path needs them)
|
|
40
|
+
const rel = (f) => path.relative(WS, f).split(path.sep).join('/');
|
|
41
|
+
|
|
39
42
|
// The files a design depends on, relative to the workspace (its git repo): its .js and .json, the rule sets and the layouts it uses.
|
|
40
43
|
// (A board's engine is the tool's code, not the workspace's: its release records engineId() instead.)
|
|
41
44
|
export function files(d, object) {
|
|
42
|
-
return [
|
|
45
|
+
return [rel(d.js), rel(d.json), ...(object.uses || []).filter((u) => !u.includes('/')).map((u) => `rules/${u}.json`),
|
|
43
46
|
...layoutNames(d, object).map((n) => `projects/${d.project}/layouts/${n}.json`)];
|
|
44
47
|
}
|
|
45
48
|
|
|
@@ -49,10 +52,10 @@ export function materialise(d, version) {
|
|
|
49
52
|
try { git('rev-parse', '--verify', `refs/tags/${tag}`); } catch { throw new Error(`no release ${tag} (releases: ${tags(d.project, d.name).map((t) => t.version).join(', ') || 'none'})`); }
|
|
50
53
|
const base = paths.versions(d.project, `${d.name}@${version}`);
|
|
51
54
|
const show = (rel) => git('show', `${tag}:${rel}`);
|
|
52
|
-
const object = JSON.parse(show(
|
|
55
|
+
const object = JSON.parse(show(rel(d.json)));
|
|
53
56
|
fs.mkdirSync(path.join(base, 'rules'), { recursive: true });
|
|
54
57
|
const js = path.join(base, `${d.name}.js`), json = path.join(base, `${d.name}.json`);
|
|
55
|
-
fs.writeFileSync(js, show(
|
|
58
|
+
fs.writeFileSync(js, show(rel(d.js)));
|
|
56
59
|
fs.writeFileSync(json, JSON.stringify(object, null, 2));
|
|
57
60
|
for (const u of (object.uses || []).filter((x) => !x.includes('/'))) fs.writeFileSync(path.join(base, 'rules', `${u}.json`), show(`rules/${u}.json`)); // objects it reads are released on their own
|
|
58
61
|
// The layouts it could pick, as they were then (only files that existed in that release)
|
package/lib/workspace.mjs
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// printer the rule set a print is checked against (bed, overhang, bridges); a project.json's "printer" wins
|
|
7
7
|
// viewUrl the model page that links open (the public one by default: it reads links as data, nothing is uploaded)
|
|
8
8
|
// targets optional extras: show (put a model on display: a module of your own) and shorten (short links: "gist")
|
|
9
|
+
import fs from 'node:fs';
|
|
9
10
|
import path from 'node:path';
|
|
10
11
|
import { host, readJson } from './host.mjs';
|
|
11
12
|
import { fileURLToPath } from 'node:url';
|
|
@@ -48,3 +49,25 @@ export const printerOf = (project) => {
|
|
|
48
49
|
const f = project && path.join(paths.project(project), 'project.json');
|
|
49
50
|
return (f && host.exists(f) && readJson(f).printer) || config.printer;
|
|
50
51
|
};
|
|
52
|
+
|
|
53
|
+
// Trust: a workspace that turns the design sandbox off ("sandbox": false), or runs code of its own as a target (a
|
|
54
|
+
// module path rather than one of rulecad's), runs that code in the tool's process. A workspace cloned from elsewhere
|
|
55
|
+
// could do that on its first command, so it is asked once: cad trust records this folder, with what it asked for, in
|
|
56
|
+
// the user's own list (~/.config/rulecad/trusted.json, or $CAD_TRUSTED); asking for more later is asked again.
|
|
57
|
+
import os from 'node:os';
|
|
58
|
+
const TRUSTED = () => process.env.CAD_TRUSTED || path.join(os.homedir(), '.config', 'rulecad', 'trusted.json');
|
|
59
|
+
/** What this workspace asks to run outside the sandbox: [] when nothing. */
|
|
60
|
+
export function asks(cfg = config) {
|
|
61
|
+
const own = Object.entries(cfg.targets || {}).filter(([, v]) => typeof v === 'string' && (v.includes('/') || v.endsWith('.mjs') || v.endsWith('.js'))).map(([k, v]) => `the ${k} target ${v}`);
|
|
62
|
+
return [...(cfg.sandbox === false ? ['designs run outside the sandbox'] : []), ...own].sort();
|
|
63
|
+
}
|
|
64
|
+
const trustList = () => { try { return JSON.parse(fs.readFileSync(TRUSTED(), 'utf8')); } catch { return {}; } };
|
|
65
|
+
/** Whether the user has trusted this workspace for all it asks. */
|
|
66
|
+
export const trusted = (cfg = config) => { const a = asks(cfg); if (!a.length || process.env.CAD_TRUST === '1') return true; const t = trustList()[WS]; return !!t && a.every((x) => t.includes(x)); };
|
|
67
|
+
/** Trust this workspace for what it asks now. */
|
|
68
|
+
export function trust(cfg = config) { const list = trustList(); list[WS] = asks(cfg); fs.mkdirSync(path.dirname(TRUSTED()), { recursive: true }); fs.writeFileSync(TRUSTED(), JSON.stringify(list, null, 2) + '\n'); return list[WS]; }
|
|
69
|
+
/** Throws, saying what it asks and how to allow it, when this workspace isn't trusted for what it asks. */
|
|
70
|
+
export function needTrust(cfg = config) {
|
|
71
|
+
if (trusted(cfg)) return;
|
|
72
|
+
throw Object.assign(new Error(`this workspace asks for ${asks(cfg).join(' and ')} (cad.config.json). If you trust it, run cad trust once; until then, it isn't done`), { code: 'workspace.untrusted' });
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/cad",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.31",
|
|
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": {
|