@logomarks/core 0.2.1 → 0.2.2
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/CHANGELOG.md +14 -0
- package/README.md +7 -2
- package/bin/fit.js +76 -0
- package/package.json +2 -2
- package/src/fit/index.js +1 -1
- package/src/geometry/glitch.js +1 -1
- package/src/geometry/path.js +1 -1
- package/src/parts/frame.js +1 -1
- package/bin/skill.js +0 -14
- package/src/skill/bundle.d.ts +0 -6
- package/src/skill/bundle.js +0 -181
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,20 @@ All notable changes to `@logomarks/core` are documented here.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.2.2] — 2026-11-15
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **`logomarks-fit`, the fit as a command:** `fitMark()` and `checkFit()` on files. It fits a file in place, several files, or every `.svg` in a folder; `-o` (or a second path that doesn't exist yet) writes one file's fit to another; `--check` only reports and exits 1 when anything is off; a bad call exits 2 with the usage. It is the design skill's `fit.mjs` command line, on the library instead of a pasted copy: on the 5,300 Logomarks marks and the skill's six examples it writes byte-identical files and prints the same lines. See [docs/api.md](docs/api.md#the-fit-command).
|
|
13
|
+
|
|
14
|
+
### Removed
|
|
15
|
+
|
|
16
|
+
- **The `logomarks-skill` bin and `src/skill/`** (`bundleKit`, `bundleFit`, `writeSkillScripts`). They pasted `/geometry` into one `kit.mjs` and the fit into one `fit.mjs` for the design skill, which left the skill a second copy of the kit and none of the parts (`/parts`, `/sdf`, `/iso`, `/letters`, `/forms`, `/objects`, `/pixels`: their names clash, so they can't be pasted into one file). The skill now installs `@logomarks/core` and imports its subpaths, and fits with `npx logomarks-fit`. A patch: nothing outside the skill used the bin, and every export is unchanged, so the libraries on `^0.2` keep one copy of core.
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- `checkFit()`'s "runs outside the 512 canvas" problem says "run logomarks-fit on it" (it named `fit.mjs`).
|
|
21
|
+
|
|
8
22
|
## [0.2.1] — 2026-11-15
|
|
9
23
|
|
|
10
24
|
### Added
|
package/README.md
CHANGED
|
@@ -22,6 +22,11 @@ const { svg: mark } = fitMark(raw); // 512 canvas, longest side 448, centred,
|
|
|
22
22
|
checkFit(mark).problems; // []
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
```bash
|
|
26
|
+
npx logomarks-fit marks/ # fit every .svg in marks/ in place
|
|
27
|
+
npx logomarks-fit --check marks/ # only report; exits 1 if anything is off
|
|
28
|
+
```
|
|
29
|
+
|
|
25
30
|
## What's in it
|
|
26
31
|
|
|
27
32
|
- `/geometry`: shapes and real booleans, strokes, tracks, iso solids, badges and letter skeletons
|
|
@@ -41,9 +46,9 @@ checkFit(mark).problems; // []
|
|
|
41
46
|
- `/objects`: devices and code glyphs, blades, dishes, sport, cells, faces, animals and rosettes
|
|
42
47
|
- `/forms`: the Bold, Line and Track forms: discs, hooks, jogs, emblems, shatter, slats, flow grids, folds, swashes, stripes, rails, locks and track editions
|
|
43
48
|
- `/batch`: a generator's runner: seeded marks, the sidecars, the command line, `slug` (Node)
|
|
44
|
-
- `logomarks-
|
|
49
|
+
- `logomarks-fit`: the fit as a command: fits marks in place (a file, several, or every `.svg` in a folder), `-o` to another file, `--check` only reports and exits 1 when anything is off
|
|
45
50
|
|
|
46
|
-
Every export, repeatable drawings and the
|
|
51
|
+
Every export, repeatable drawings and the fit command: [API](https://github.com/Logomarks/core/blob/main/docs/api.md).
|
|
47
52
|
|
|
48
53
|
## Links
|
|
49
54
|
|
package/bin/fit.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* logomarks-fit: fits SVG marks to the Logomarks standard (a 512 canvas, the mark's longest side exactly 448, centred,
|
|
4
|
+
* filled paths in currentColor) with `fitMark()`, and reports with `checkFit()`.
|
|
5
|
+
*
|
|
6
|
+
* logomarks-fit mark.svg fit in place
|
|
7
|
+
* logomarks-fit in.svg -o out.svg fit to another file (in.svg out.svg too, while out.svg doesn't exist)
|
|
8
|
+
* logomarks-fit a.svg b.svg marks/ fit each file, and every .svg in a folder, in place
|
|
9
|
+
* logomarks-fit --check mark.svg marks/ only report; exits 1 if anything is off
|
|
10
|
+
*
|
|
11
|
+
* Reads <path d> (with fill-rule), <circle>, <ellipse>, <rect> and <polygon>; nested <g> transforms are NOT applied,
|
|
12
|
+
* so flatten those first. Measures the real outline, curves and arcs included (sampled), never the viewBox.
|
|
13
|
+
*/
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { fitMark, checkFit } from '../src/fit/index.js';
|
|
17
|
+
|
|
18
|
+
const USAGE = `Usage: logomarks-fit [--check] <file.svg | folder>... [-o out.svg]
|
|
19
|
+
Fits each file in place (a folder: every .svg in it). -o writes one file's fit to another file;
|
|
20
|
+
so does a second path that doesn't exist yet (in.svg out.svg). --check only reports.`;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Stops with a message and the usage.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} msg What is wrong.
|
|
26
|
+
* @returns {never}
|
|
27
|
+
*/
|
|
28
|
+
const fail = (msg) => { console.error(`logomarks-fit: ${msg}\n${USAGE}`); process.exit(2); };
|
|
29
|
+
|
|
30
|
+
const args = process.argv.slice(2);
|
|
31
|
+
if (args.some((a) => a === '--help' || a === '-h')) { console.log(USAGE); process.exit(0); }
|
|
32
|
+
const checking = args.includes('--check');
|
|
33
|
+
/** @type {string | null} */
|
|
34
|
+
let out = null;
|
|
35
|
+
/** @type {string[]} */
|
|
36
|
+
const paths = [];
|
|
37
|
+
for (let i = 0; i < args.length; i++) {
|
|
38
|
+
if (args[i] === '--check') continue;
|
|
39
|
+
if (args[i] === '-o' || args[i] === '--out') { out = args[++i] ?? fail(`${args[i - 1]} needs a file`); continue; }
|
|
40
|
+
if (args[i].startsWith('-')) fail(`unknown option ${args[i]}`);
|
|
41
|
+
paths.push(args[i]);
|
|
42
|
+
}
|
|
43
|
+
if (!paths.length) fail('no file or folder given');
|
|
44
|
+
for (const p of paths) if (!fs.existsSync(p)) {
|
|
45
|
+
// in.svg out.svg: the second path names the file to write, as -o does
|
|
46
|
+
if (!checking && !out && paths.length === 2 && p === paths[1] && p.endsWith('.svg')) out = /** @type {string} */ (paths.pop());
|
|
47
|
+
else fail(`${p} doesn't exist`);
|
|
48
|
+
}
|
|
49
|
+
if (out && (checking || paths.length !== 1 || fs.statSync(paths[0]).isDirectory())) fail('-o takes one .svg file to fit, without --check');
|
|
50
|
+
/** @type {string[]} */
|
|
51
|
+
const files = [];
|
|
52
|
+
for (const a of paths) {
|
|
53
|
+
if (fs.statSync(a).isDirectory()) for (const f of fs.readdirSync(a)) { if (f.endsWith('.svg')) files.push(path.join(a, f)); }
|
|
54
|
+
else files.push(a);
|
|
55
|
+
}
|
|
56
|
+
let bad = 0;
|
|
57
|
+
for (const file of files) {
|
|
58
|
+
const svg = fs.readFileSync(file, 'utf8');
|
|
59
|
+
try {
|
|
60
|
+
if (checking) {
|
|
61
|
+
const { problems, size } = checkFit(svg);
|
|
62
|
+
if (problems.length) bad++;
|
|
63
|
+
console.log(`${problems.length ? '✗' : '✓'} ${file} ${size}${problems.length ? ' — ' + problems.join('; ') : ''}`);
|
|
64
|
+
} else {
|
|
65
|
+
const r = fitMark(svg);
|
|
66
|
+
fs.writeFileSync(out ?? file, r.svg);
|
|
67
|
+
const c = checkFit(r.svg);
|
|
68
|
+
console.log(`${c.problems.length ? '✗' : '✓'} ${out ?? file} (was ${r.before.w.toFixed(1)}×${r.before.h.toFixed(1)}, now ${c.size})`);
|
|
69
|
+
if (c.problems.length) bad++;
|
|
70
|
+
}
|
|
71
|
+
} catch (e) {
|
|
72
|
+
bad++;
|
|
73
|
+
console.log(`✗ ${file} — ${/** @type {Error} */ (e).message}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
process.exit(bad ? 1 : 0);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logomarks/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Geometry, SVG reading, fitting, analysis and similarity for logomarks on a 512 grid: the kit the Logomarks set is drawn with (shapes, real booleans, strokes, tracks, iso solids, badges, letter skeletons), one SVG path parser, the 448 fit, speck and shape-tag analysis, outline signatures, signed distance fields traced to shapes, stitch plans for embroidery, and the parts the generators draw with: iso objects, letters and type, font glyphs, pixel dither, objects and the Bold, Line and Track forms, plus the batch runner.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -105,7 +105,7 @@
|
|
|
105
105
|
"./package.json": "./package.json"
|
|
106
106
|
},
|
|
107
107
|
"bin": {
|
|
108
|
-
"logomarks-
|
|
108
|
+
"logomarks-fit": "bin/fit.js"
|
|
109
109
|
},
|
|
110
110
|
"files": [
|
|
111
111
|
"bin",
|
package/src/fit/index.js
CHANGED
|
@@ -129,7 +129,7 @@ export function checkFit(svg) {
|
|
|
129
129
|
if (!vb || vb.join(' ') !== `0 0 ${CANVAS} ${CANVAS}`) problems.push(`viewBox is "${vb?.join(' ')}", not "0 0 ${CANVAS} ${CANVAS}"`);
|
|
130
130
|
if (Math.abs(long - FIT) > FIT_TOLERANCE) problems.push(`longest side ${long.toFixed(1)}, not ${FIT}`);
|
|
131
131
|
if (Math.abs(cx - CANVAS / 2) > FIT_TOLERANCE || Math.abs(cy - CANVAS / 2) > FIT_TOLERANCE) problems.push(`centre (${cx.toFixed(1)}, ${cy.toFixed(1)}), not (${CANVAS / 2}, ${CANVAS / 2})`);
|
|
132
|
-
if (b.x < -FIT_TOLERANCE || b.y < -FIT_TOLERANCE || b.x + b.w > CANVAS + FIT_TOLERANCE || b.y + b.h > CANVAS + FIT_TOLERANCE) problems.push(`runs outside the ${CANVAS} canvas — it will be cut off; run fit
|
|
132
|
+
if (b.x < -FIT_TOLERANCE || b.y < -FIT_TOLERANCE || b.x + b.w > CANVAS + FIT_TOLERANCE || b.y + b.h > CANVAS + FIT_TOLERANCE) problems.push(`runs outside the ${CANVAS} canvas — it will be cut off; run logomarks-fit on it`);
|
|
133
133
|
if (FORBIDDEN_MARKUP.test(svg)) problems.push('contains groups, transforms, strokes, styles or masks — flatten to filled paths');
|
|
134
134
|
return { problems, size: `${b.w.toFixed(1)}×${b.h.toFixed(1)}`, bounds: b };
|
|
135
135
|
}
|
package/src/geometry/glitch.js
CHANGED
|
@@ -12,7 +12,7 @@ import { slices } from './offset.js';
|
|
|
12
12
|
* { kind: 'steps', cuts: [a, b, c(, d)], shift: 22 } four or five thin bands, slid +shift, −shift alternately
|
|
13
13
|
* { kind: 'cut', cuts: [a], shift: 44 } one cut, the lower (or right) band slid
|
|
14
14
|
* { kind: 'shear', cuts: [a(, b)], deg: 30, shift: 44 } a diagonal cut (deg from horizontal); two or three bands
|
|
15
|
-
* `shifts` (one per band) overrides the pattern.
|
|
15
|
+
* `shifts` (one per band) overrides the pattern. Fit the result with fitMark() or logomarks-fit.
|
|
16
16
|
*/
|
|
17
17
|
export function glitchMark(src, { kind = 'bands', cuts, shift = 44, shifts, deg, g = 22 } = {}) {
|
|
18
18
|
const s = typeof src === 'string' ? svgShape(src) : src, n = cuts.length + 1;
|
package/src/geometry/path.js
CHANGED
|
@@ -24,7 +24,7 @@ export function toPath(mp, tol = 0.12) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
|
|
27
|
-
/** The SVG file for a shape (any size;
|
|
27
|
+
/** The SVG file for a shape (any size; fitMark(), or logomarks-fit, fits it to the standard). */
|
|
28
28
|
export const svg = (shape) => `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" fill="currentColor"><path fill-rule="evenodd" d="${toPath(shape)}"/></svg>\n`;
|
|
29
29
|
|
|
30
30
|
/** Writes a shape to an SVG file. */
|
package/src/parts/frame.js
CHANGED
|
@@ -27,7 +27,7 @@ export const centre = (g) => { const [x0, y0, x1, y1] = bbox(g); return mapPts(g
|
|
|
27
27
|
export const outline = (shape) => (typeof shape[0][0][0] === 'number' ? shape[0] : shape[0][0]);
|
|
28
28
|
/** Stacks parts back to front, each later part in front of everything before it and cut free by the gap g. */
|
|
29
29
|
export const chain = (g, ...parts) => parts.reduce((scene, p) => over(scene, p, g));
|
|
30
|
-
/** Builds a mark whose gap lands on exactly `G` once fit
|
|
30
|
+
/** Builds a mark whose gap lands on exactly `G` once the fit scales it to 448: `build(g)` draws it with gap g; it's drawn,
|
|
31
31
|
* measured, and drawn again with the gap divided by the fit's scale (twice, as the gap changes the size a little).
|
|
32
32
|
* `before()` runs before every draw (seed Math.random there, so robust()'s jitter repeats). */
|
|
33
33
|
export const exact = (build, before = () => {}, target = G) => { let g = target, s;
|
package/bin/skill.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* logomarks-skill <dir> [--url <url>]: writes the design skill's scripts/kit.mjs and scripts/fit.mjs into <dir>,
|
|
4
|
-
* generated from @logomarks/core. `{{url}}` stays in their headers unless --url fills it.
|
|
5
|
-
*/
|
|
6
|
-
import { writeSkillScripts } from '../src/skill/bundle.js';
|
|
7
|
-
|
|
8
|
-
const args = process.argv.slice(2), at = args.indexOf('--url');
|
|
9
|
-
const url = at >= 0 ? args.splice(at, 2)[1] : undefined;
|
|
10
|
-
if (!args[0]) {
|
|
11
|
-
console.log('Usage: logomarks-skill <dir> [--url <url>]');
|
|
12
|
-
process.exit(1);
|
|
13
|
-
}
|
|
14
|
-
for (const file of writeSkillScripts(args[0], { url })) console.log('wrote', file);
|
package/src/skill/bundle.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/** The skill's kit.mjs: the geometry kit as one file needing only polygon-clipping, `{{url}}` unfilled. */
|
|
2
|
-
export function bundleKit(): string;
|
|
3
|
-
/** The skill's fit.mjs: the fit and check as one script with no dependencies, `{{url}}` unfilled. */
|
|
4
|
-
export function bundleFit(): string;
|
|
5
|
-
/** Writes both scripts into a folder; `url` fills `{{url}}`. Returns the files written. */
|
|
6
|
-
export function writeSkillScripts(dir: string, o?: { url?: string }): string[];
|
package/src/skill/bundle.js
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module @logomarks/core/skill
|
|
3
|
-
*
|
|
4
|
-
* The design skill's two scripts, generated from the library so they never drift: `kit.mjs` (the geometry kit as one
|
|
5
|
-
* file, needing only polygon-clipping) and `fit.mjs` (the fit and check, no dependencies). Each module's code goes in
|
|
6
|
-
* as it is, its imports dropped and its exports kept only where the script exposes them. `{{url}}` in each header is
|
|
7
|
-
* left for the skill's build to fill.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import fs from 'node:fs';
|
|
11
|
-
import * as geometry from '../geometry/index.js';
|
|
12
|
-
|
|
13
|
-
const SRC = new URL('../', import.meta.url);
|
|
14
|
-
|
|
15
|
-
/** The geometry kit's modules, in dependency order. */
|
|
16
|
-
const KIT = ['geometry/core.js', 'geometry/booleans.js', 'geometry/polygon.js', 'geometry/random.js', 'geometry/shapes.js', 'geometry/stroke.js',
|
|
17
|
-
'geometry/path.js', 'geometry/offset.js', 'geometry/mitre.js', 'geometry/iso.js', 'geometry/modular.js', 'geometry/badges.js', 'geometry/letters.js',
|
|
18
|
-
'svg/path.js', 'svg/shapes.js', 'svg/rings.js', 'svg/nonzero.js', 'svg/shape.js', 'geometry/glitch.js', 'geometry/needle.js'];
|
|
19
|
-
|
|
20
|
-
/** The fit's modules, in dependency order. */
|
|
21
|
-
const FIT = ['svg/path.js', 'svg/shapes.js', 'svg/outline.js', 'util/number.js', 'fit/index.js'];
|
|
22
|
-
|
|
23
|
-
const KIT_HEADER = `// Logomarks geometry kit — the helpers the Logomarks set was built with.
|
|
24
|
-
// {{url}} · CC0
|
|
25
|
-
//
|
|
26
|
-
// Shapes are polygon sets (arrays of rings of [x, y] points, y pointing down) on a
|
|
27
|
-
// 512 canvas with C = 256 as the centre. Combine them with real booleans:
|
|
28
|
-
// U(...shapes) union · D(a, ...b) subtract · I(...) intersect · X(...) exclude
|
|
29
|
-
// then write the result with svg(shape) or write('name.svg', shape).
|
|
30
|
-
// Sizes don't need to be exact: scripts/fit.mjs scales every mark to the set's
|
|
31
|
-
// standard (longest side 448, centred) afterwards.
|
|
32
|
-
//
|
|
33
|
-
// Needs one package: npm install polygon-clipping
|
|
34
|
-
// Generated from @logomarks/core (its geometry) by logomarks-skill: edit the library, not this file.
|
|
35
|
-
import pc from 'polygon-clipping';
|
|
36
|
-
`;
|
|
37
|
-
|
|
38
|
-
const FIT_HEADER = `#!/usr/bin/env node
|
|
39
|
-
// Fits SVG marks to the Logomarks standard: 512×512 canvas, the mark's longest side
|
|
40
|
-
// exactly 448, centred (a 32 margin), filled paths in currentColor, nothing else.
|
|
41
|
-
// {{url}} · CC0 · no dependencies
|
|
42
|
-
//
|
|
43
|
-
// node fit.mjs mark.svg fit in place
|
|
44
|
-
// node fit.mjs in.svg -o out.svg fit to another file (in.svg out.svg too, while out.svg doesn't exist)
|
|
45
|
-
// node fit.mjs a.svg b.svg marks/ fit each file, and every .svg in a folder, in place
|
|
46
|
-
// node fit.mjs --check mark.svg marks/ only report; exits 1 if anything is off
|
|
47
|
-
//
|
|
48
|
-
// Reads <path d> (with fill-rule), <circle>, <ellipse>, <rect> and <polygon>; nested
|
|
49
|
-
// <g> transforms are NOT applied, so flatten those first. Measures the real outline,
|
|
50
|
-
// curves and arcs included (sampled), never the viewBox.
|
|
51
|
-
// Generated from @logomarks/core (its svg and fit) by logomarks-skill: edit the library, not this file.
|
|
52
|
-
|
|
53
|
-
import fs from 'node:fs';
|
|
54
|
-
import path from 'node:path';
|
|
55
|
-
`;
|
|
56
|
-
|
|
57
|
-
/** fit.mjs's command line. */
|
|
58
|
-
const FIT_MAIN = `
|
|
59
|
-
// ---------- command line ----------
|
|
60
|
-
|
|
61
|
-
const USAGE = \`Usage: node fit.mjs [--check] <file.svg | folder>... [-o out.svg]
|
|
62
|
-
Fits each file in place (a folder: every .svg in it). -o writes one file's fit to another file;
|
|
63
|
-
so does a second path that doesn't exist yet (in.svg out.svg). --check only reports.\`;
|
|
64
|
-
|
|
65
|
-
/** Stops with a message and the usage. */
|
|
66
|
-
const fail = (msg) => { console.error(\`fit.mjs: \${msg}\\n\${USAGE}\`); process.exit(2); };
|
|
67
|
-
|
|
68
|
-
const args = process.argv.slice(2);
|
|
69
|
-
if (args.some((a) => a === '--help' || a === '-h')) { console.log(USAGE); process.exit(0); }
|
|
70
|
-
const checking = args.includes('--check');
|
|
71
|
-
let out = null;
|
|
72
|
-
const paths = [];
|
|
73
|
-
for (let i = 0; i < args.length; i++) {
|
|
74
|
-
if (args[i] === '--check') continue;
|
|
75
|
-
if (args[i] === '-o' || args[i] === '--out') { out = args[++i] ?? fail(\`\${args[i - 1]} needs a file\`); continue; }
|
|
76
|
-
if (args[i].startsWith('-')) fail(\`unknown option \${args[i]}\`);
|
|
77
|
-
paths.push(args[i]);
|
|
78
|
-
}
|
|
79
|
-
if (!paths.length) fail('no file or folder given');
|
|
80
|
-
for (const p of paths) if (!fs.existsSync(p)) {
|
|
81
|
-
// in.svg out.svg: the second path names the file to write, as -o does
|
|
82
|
-
if (!checking && !out && paths.length === 2 && p === paths[1] && p.endsWith('.svg')) out = paths.pop();
|
|
83
|
-
else fail(\`\${p} doesn't exist\`);
|
|
84
|
-
}
|
|
85
|
-
if (out && (checking || paths.length !== 1 || fs.statSync(paths[0]).isDirectory())) fail('-o takes one .svg file to fit, without --check');
|
|
86
|
-
const files = [];
|
|
87
|
-
for (const a of paths) {
|
|
88
|
-
if (fs.statSync(a).isDirectory()) for (const f of fs.readdirSync(a)) { if (f.endsWith('.svg')) files.push(path.join(a, f)); }
|
|
89
|
-
else files.push(a);
|
|
90
|
-
}
|
|
91
|
-
let bad = 0;
|
|
92
|
-
for (const file of files) {
|
|
93
|
-
const svg = fs.readFileSync(file, 'utf8');
|
|
94
|
-
try {
|
|
95
|
-
if (checking) {
|
|
96
|
-
const { problems, size } = checkFit(svg);
|
|
97
|
-
if (problems.length) bad++;
|
|
98
|
-
console.log(\`\${problems.length ? '✗' : '✓'} \${file} \${size}\${problems.length ? ' — ' + problems.join('; ') : ''}\`);
|
|
99
|
-
} else {
|
|
100
|
-
const r = fitMark(svg);
|
|
101
|
-
fs.writeFileSync(out ?? file, r.svg);
|
|
102
|
-
const c = checkFit(r.svg);
|
|
103
|
-
console.log(\`\${c.problems.length ? '✗' : '✓'} \${out ?? file} (was \${r.before.w.toFixed(1)}×\${r.before.h.toFixed(1)}, now \${c.size})\`);
|
|
104
|
-
if (c.problems.length) bad++;
|
|
105
|
-
}
|
|
106
|
-
} catch (e) {
|
|
107
|
-
bad++;
|
|
108
|
-
console.log(\`✗ \${file} — \${e.message}\`);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
process.exit(bad ? 1 : 0);
|
|
112
|
-
`;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* One module's code for a single-file script: its header docblock as a section line, its imports dropped, and
|
|
116
|
-
* `export` kept only on the names in `keep`.
|
|
117
|
-
*
|
|
118
|
-
* @param {string} file The module, relative to src/.
|
|
119
|
-
* @param {ReadonlySet<string>} keep The names the script exports.
|
|
120
|
-
* @returns {{ code: string, names: string[] }} The code, and the top-level names it declares.
|
|
121
|
-
*/
|
|
122
|
-
function inline(file, keep) {
|
|
123
|
-
let s = fs.readFileSync(new URL(file, SRC), 'utf8');
|
|
124
|
-
const doc = /^\/\*\*([\s\S]*?)\*\/\n/.exec(s);
|
|
125
|
-
const title = doc ? doc[1].split('\n').map((l) => l.replace(/^\s*\*\s?/, '')).filter((l) => l && !l.startsWith('@module')).join(' ').split(/[.:](\s|$)/)[0] : file;
|
|
126
|
-
if (doc) s = s.slice(doc[0].length);
|
|
127
|
-
if (/^import \{[^}]* as /m.test(s)) throw new Error(`skill bundle: ${file} renames an import, which a single file can't keep`);
|
|
128
|
-
s = s.replace(/^import [^\n]*\n/gm, '').replace(/^\n+/, '');
|
|
129
|
-
const names = [];
|
|
130
|
-
s = s.replace(/^export (const|function|async function|let) ([\w$]+)/gm, (m, kind, name) => { names.push(name); return keep.has(name) ? m : `${kind} ${name}`; });
|
|
131
|
-
for (const m of s.matchAll(/^(?:const|function|async function|let) ([\w$]+)/gm)) if (!names.includes(m[1])) names.push(m[1]);
|
|
132
|
-
return { code: `// ---------- ${title.trim()} (${file}) ----------\n\n${s.trimEnd()}\n`, names };
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Modules joined into one script; throws when two declare the same top-level name.
|
|
137
|
-
*
|
|
138
|
-
* @param {readonly string[]} files The modules.
|
|
139
|
-
* @param {ReadonlySet<string>} keep The names the script exports.
|
|
140
|
-
* @returns {string} The code.
|
|
141
|
-
*/
|
|
142
|
-
function join(files, keep) {
|
|
143
|
-
const seen = new Map();
|
|
144
|
-
return files.map((f) => {
|
|
145
|
-
const { code, names } = inline(f, keep);
|
|
146
|
-
for (const n of names) {
|
|
147
|
-
if (seen.has(n)) throw new Error(`skill bundle: ${n} is declared in both ${seen.get(n)} and ${f}`);
|
|
148
|
-
seen.set(n, f);
|
|
149
|
-
}
|
|
150
|
-
return code;
|
|
151
|
-
}).join('\n');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* The skill's `kit.mjs`: the geometry kit as one file (every export of `@logomarks/core/geometry`).
|
|
156
|
-
*
|
|
157
|
-
* @returns {string} The script, `{{url}}` unfilled.
|
|
158
|
-
*/
|
|
159
|
-
export const bundleKit = () => KIT_HEADER + '\n' + join(KIT, new Set(Object.keys(geometry)));
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* The skill's `fit.mjs`: the fit and the check as one script with no dependencies.
|
|
163
|
-
*
|
|
164
|
-
* @returns {string} The script, `{{url}}` unfilled.
|
|
165
|
-
*/
|
|
166
|
-
export const bundleFit = () => FIT_HEADER + '\n' + join(FIT, new Set()) + FIT_MAIN;
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Writes both scripts into a folder (the skill's `scripts/`).
|
|
170
|
-
*
|
|
171
|
-
* @param {string} dir The folder.
|
|
172
|
-
* @param {{ url?: string }} [o] A URL to fill `{{url}}` with (left as is by default).
|
|
173
|
-
* @returns {string[]} The files written.
|
|
174
|
-
*/
|
|
175
|
-
export function writeSkillScripts(dir, { url } = {}) {
|
|
176
|
-
const fill = (/** @type {string} */ s) => (url ? s.replaceAll('{{url}}', url) : s);
|
|
177
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
178
|
-
const out = [[`${dir}/kit.mjs`, bundleKit()], [`${dir}/fit.mjs`, bundleFit()]];
|
|
179
|
-
for (const [file, text] of out) fs.writeFileSync(file, fill(text));
|
|
180
|
-
return out.map(([file]) => file);
|
|
181
|
-
}
|