@rulecad/cad 0.7.97 → 0.8.0

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/lib/workspace.mjs CHANGED
@@ -10,6 +10,7 @@ import fs from 'node:fs';
10
10
  import path from 'node:path';
11
11
  import { host, readJson } from './host.mjs';
12
12
  import { fileURLToPath } from 'node:url';
13
+ import { plainName } from './names.mjs';
13
14
 
14
15
  export const TOOL = fileURLToPath(new URL('..', import.meta.url)).replace(/[\\/]$/, '');
15
16
 
@@ -37,28 +38,38 @@ export const RULE_DIRS = [...new Set([path.join(WS, 'rules'), path.dirname(fileU
37
38
 
38
39
  export const paths = {
39
40
  projects: path.join(WS, 'projects'),
40
- project: (p) => path.join(WS, 'projects', p),
41
+ project: (p) => path.join(WS, 'projects', plainName(p, 'project name')), // (a name from a file or the command line: never ../)
41
42
  cache: (...p) => path.join(WS, '.cache', ...p),
42
43
  preview: (...p) => path.join(WS, '.preview', ...p),
43
44
  exports: (...p) => path.join(WS, 'exports', ...p),
44
45
  versions: (...p) => path.join(WS, '.versions', ...p),
45
46
  };
46
47
 
47
- // The printer a project's prints are checked against
48
+ // The printer a project's prints are checked against: project.json's "printer", else cad.config.json's. Either is a
49
+ // rule set's name, which becomes part of a path (rules/<printer>.json, and the kit's), so it must be a plain name.
48
50
  export const printerOf = (project) => {
49
51
  const f = project && path.join(paths.project(project), 'project.json');
50
- return (f && host.exists(f) && readJson(f).printer) || config.printer;
52
+ const own = f && host.exists(f) && readJson(f).printer;
53
+ if (own) return plainName(own, '"printer" field', path.relative(WS, f));
54
+ return plainName(config.printer, '"printer" field', 'cad.config.json');
51
55
  };
52
56
 
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
57
+ // Trust: a workspace that turns the design sandbox off ("sandbox": false) or runs code of its own as a target (any
58
+ // name but one of the kit's own, below) runs that code outside the sandbox. (Board designs run in the sandbox, as
59
+ // every design does: they ask nothing.) A workspace cloned from elsewhere
55
60
  // could do that on its first command, so it is asked once: cad trust records this folder, with what it asked for, in
56
61
  // the user's own list (~/.config/rulecad/trusted.json, or $CAD_TRUSTED); asking for more later is asked again.
57
62
  import os from 'node:os';
58
63
  const TRUSTED = () => process.env.CAD_TRUSTED || path.join(os.homedir(), '.config', 'rulecad', 'trusted.json');
64
+ // The kit's own targets, by exact name. A target is the kit's own only if its name is a key here, never because of
65
+ // what its name looks like or because a file of that name happens to be in the kit: anything else in "targets" (a bare
66
+ // name, a path with / or \, a ../ name that reaches one of the kit's modules) is the workspace's code, and asked about.
67
+ export const OWN_TARGETS = Object.freeze(Object.assign(Object.create(null), { gist: new URL('./targets/gist.mjs', import.meta.url).href }));
68
+ /** Whether a target name in cad.config.json is one of the kit's own (exactly a key of OWN_TARGETS). */
69
+ export const isOwnTarget = (name) => typeof name === 'string' && Object.hasOwn(OWN_TARGETS, name);
59
70
  /** What this workspace asks to run outside the sandbox: [] when nothing. */
60
71
  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}`);
72
+ const own = Object.entries(cfg.targets || {}).filter(([, v]) => v && !isOwnTarget(v)).map(([k, v]) => `the ${k} target ${typeof v === 'string' ? v : JSON.stringify(v)}`);
62
73
  return [...(cfg.sandbox === false ? ['designs run outside the sandbox'] : []), ...own].sort();
63
74
  }
64
75
  const trustList = () => { try { return JSON.parse(fs.readFileSync(TRUSTED(), 'utf8')); } catch { return {}; } };
@@ -66,7 +77,10 @@ const trustList = () => { try { return JSON.parse(fs.readFileSync(TRUSTED(), 'ut
66
77
  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
78
  /** Trust this workspace for what it asks now. */
68
79
  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. */
80
+ /**
81
+ * Throws, saying what it asks, where it asks it and how to allow it, when this workspace isn't trusted for what it asks.
82
+ * @param {object} [cfg]
83
+ */
70
84
  export function needTrust(cfg = config) {
71
85
  if (trusted(cfg)) return;
72
86
  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' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/cad",
3
- "version": "0.7.97",
3
+ "version": "0.8.0",
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": {
@@ -61,24 +61,24 @@
61
61
  "@rulecad/variables": "^0.3.7",
62
62
  "@rulecad/keyboard-layout": "^0.1.2",
63
63
  "@rulecad/printcheck": "^0.3.3",
64
- "@rulecad/fit": "^0.4.0",
65
- "@rulecad/model-link": "^0.3.2",
64
+ "@rulecad/fit": "^0.4.1",
65
+ "@rulecad/model-link": "^0.3.3",
66
66
  "@rulecad/versions": "^0.1.6",
67
67
  "@rulecad/shapes": "^0.4.9",
68
- "@rulecad/rules": "^0.3.30",
68
+ "@rulecad/rules": "^0.3.31",
69
69
  "@jscad/modeling": "^2.12.0",
70
70
  "three": "^0.186.1",
71
- "@rulecad/fea": "^0.2.14",
72
- "@rulecad/topopt": "^0.3.8",
73
- "@rulecad/flexures": "^0.5.17",
74
- "@rulecad/gears": "^0.3.4",
71
+ "@rulecad/fea": "^0.3.0",
72
+ "@rulecad/topopt": "^0.3.9",
73
+ "@rulecad/flexures": "^0.5.18",
74
+ "@rulecad/gears": "^0.3.5",
75
75
  "@rulecad/motors": "^0.1.6",
76
- "@rulecad/fixings": "^0.1.8",
76
+ "@rulecad/fixings": "^0.1.9",
77
77
  "@rulecad/robots": "^0.1.4",
78
- "@rulecad/frames": "^0.1.13"
78
+ "@rulecad/frames": "^0.1.14"
79
79
  },
80
80
  "peerDependencies": {
81
- "@rulecad/pcb": "^0.1.0 || ^0.2.0 || ^0.3.0 || ^0.4.0 || ^0.5.0"
81
+ "@rulecad/pcb": "^0.6.0"
82
82
  },
83
83
  "peerDependenciesMeta": {
84
84
  "@rulecad/pcb": {
@@ -13,7 +13,7 @@
13
13
  "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+" },
14
14
  "description": { "type": "string" },
15
15
  "note": { "type": "string" },
16
- "uses": { "type": "array", "items": { "type": "string" }, "description": "Rule sets (x1c, m3) and other objects (project/object, optionally @version). A screw's holes need none listed: v.fixings reads the thread's set, the printer and the material itself" },
16
+ "uses": { "type": "array", "items": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9_-]*(/[A-Za-z0-9][A-Za-z0-9_-]*(:[^/@]+)?(@[^/]+)?)?$", "description": "a rule set's plain name (letters, digits, - and _, as x1c), or another object as <project>/<object>, optionally with :<variant> and @<version> (keyboard/keycap@^1.0.0), never a path to a file" }, "description": "Rule sets (x1c, m3) and other objects (project/object, optionally :variant and @version), each name plain: letters, digits, - and _. A screw's holes need none listed: v.fixings reads the thread's set, the printer and the material itself" },
17
17
  "material": { "type": "string" },
18
18
  "orientation": { "type": "string", "description": "How it sits on the bed, and anything the printer or the person assembling it should know" },
19
19
  "role": { "enum": ["part", "reference", "board"], "description": "reference: drawn faint and never printed (a switch, a tablet); board: a circuit board" },
@@ -12,7 +12,7 @@
12
12
  "title": { "type": "string", "description": "What the project is, in a few words, for its pages and lists" },
13
13
  "description": { "type": "string", "description": "A sentence or two on what it is for" },
14
14
  "material": { "type": "string", "description": "The plastic its parts print in unless a part says otherwise: PLA or PETG, or a rule set in rules/ that gives strength, modulus and density. Printed fittings are sized from it" },
15
- "printer": { "type": "string", "description": "The printer rule set its prints are checked against (bed, overhang, layer), as x1c; left out, cad.config.json's printer" },
15
+ "printer": { "type": "string", "pattern": "^([A-Za-z0-9][A-Za-z0-9_-]*)?$", "description": "The printer rule set its prints are checked against (bed, overhang, layer), as x1c; left out, cad.config.json's printer. A plain name: letters, digits, - and _" },
16
16
  "objects": { "type": "array", "items": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" }, "description": "Its objects, by name, as cad new <project>/<object> lists them" },
17
17
  "filaments": {
18
18
  "type": "object",