diff --git a/.gitignore b/.gitignore index ea071d8..13da4e2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ src/generated/ .DS_Store *.log .env* +lib/ diff --git a/README.md b/README.md index aef37ee..2028007 100644 --- a/README.md +++ b/README.md @@ -76,12 +76,28 @@ Open `http://127.0.0.1:3000`. ## Command line +Install the `decoder` command from npm (Node.js 22.12 or newer): + ```sh -npm run decoder -- init example.decoder.json "Example system" -npm run decoder -- validate example.decoder.json -npm run decoder -- preview example.decoder.json -npm run decoder -- render example.decoder.json plate.svg patent -npm run decoder -- package example.decoder.json exhibit.html +npm install -g primitive-decoder +``` + +```sh +decoder init example.decoder.json "Example system" +decoder validate example.decoder.json +decoder preview example.decoder.json +decoder render example.decoder.json plate.svg patent +decoder package example.decoder.json exhibit.html +``` + +Or run it once without installing: `npx primitive-decoder validate example.decoder.json`. In a checkout of this repository, use `npm run decoder -- `. + +The same engine is available as a library: + +```js +import { DecoderSDK } from 'primitive-decoder'; + +const report = new DecoderSDK().validate(project); ``` `package` writes one self-contained HTML file: the same renderer and simulation as the Studio, the project data, and a vector fallback plate for devices without WebGL. It makes no network requests. diff --git a/package-lock.json b/package-lock.json index 6cdda9d..46ad71f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,29 +9,32 @@ "version": "0.1.0", "license": "Apache-2.0", "dependencies": { - "@fontsource/geist": "^5.3.0", - "@fontsource/geist-mono": "^5.3.0", - "@fontsource/space-grotesk": "^5.3.0", + "@types/three": "^0.187.0", "ajv": "^8.20.0", - "lucide-react": "^0.546.0", - "react": "^19.0.1", - "react-dom": "^19.0.1", - "three": "^0.187.0", - "three-bvh-csg": "^0.0.19", - "three-gpu-pathtracer": "^0.0.28", - "three-mesh-bvh": "^0.10.0" + "three": "^0.187.0" + }, + "bin": { + "decoder": "lib/cli.js" }, "devDependencies": { + "@fontsource/geist": "^5.3.0", + "@fontsource/geist-mono": "^5.3.0", + "@fontsource/space-grotesk": "^5.3.0", "@tailwindcss/vite": "^4.3.3", "@types/node": "^22.14.0", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", - "@types/three": "^0.187.0", "@vitejs/plugin-react": "^6.1.1", "esbuild": "^0.28.0", + "lucide-react": "^0.546.0", "playwright": "^1.65.0", "prettier": "^3.10.1", + "react": "^19.0.1", + "react-dom": "^19.0.1", "tailwindcss": "^4.3.3", + "three-bvh-csg": "^0.0.19", + "three-gpu-pathtracer": "^0.0.28", + "three-mesh-bvh": "^0.10.0", "tsx": "^4.21.0", "typescript": "^7.0.2", "vite": "^8.3.0" @@ -44,7 +47,6 @@ "version": "0.12.0", "resolved": "https://registry.npmjs.org/@dimforge/rapier3d-compat/-/rapier3d-compat-0.12.0.tgz", "integrity": "sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow==", - "dev": true, "license": "Apache-2.0" }, "node_modules/@emnapi/core": { @@ -527,6 +529,7 @@ "version": "5.3.0", "resolved": "https://registry.npmjs.org/@fontsource/geist/-/geist-5.3.0.tgz", "integrity": "sha512-u03ftlfVwb05uijq2jGDJ03/HKGqMKaNFk7KOhkpBUex8+BhrAOfMs/XciJ2UUNUX9fEsZbZQld/RWMA2oFU7g==", + "dev": true, "license": "OFL-1.1", "funding": { "url": "https://github.com/sponsors/ayuhito" @@ -536,6 +539,7 @@ "version": "5.3.0", "resolved": "https://registry.npmjs.org/@fontsource/geist-mono/-/geist-mono-5.3.0.tgz", "integrity": "sha512-UtJ1BBBCVpMYdIcW7nEB45UAoAw5M53ZXs2t0ciPW+IokuAAIc56M8+kW5tXbRJCTpDw4XTtU7proT6NdQAHTg==", + "dev": true, "license": "OFL-1.1", "funding": { "url": "https://github.com/sponsors/ayuhito" @@ -545,6 +549,7 @@ "version": "5.3.0", "resolved": "https://registry.npmjs.org/@fontsource/space-grotesk/-/space-grotesk-5.3.0.tgz", "integrity": "sha512-ksnGizDPXIDuvqcTYTSrmZ+evx9sDlS8rp7+42BQ7wU+spt3twEoXfbJz672C+5CLg6VeUQwRy5RXshWb67LcQ==", + "dev": true, "license": "OFL-1.1", "funding": { "url": "https://github.com/sponsors/ayuhito" @@ -1285,7 +1290,6 @@ "version": "23.1.3", "resolved": "https://registry.npmjs.org/@tweenjs/tween.js/-/tween.js-23.1.3.tgz", "integrity": "sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA==", - "dev": true, "license": "MIT" }, "node_modules/@tybys/wasm-util": { @@ -1333,14 +1337,12 @@ "version": "0.17.4", "resolved": "https://registry.npmjs.org/@types/stats.js/-/stats.js-0.17.4.tgz", "integrity": "sha512-jIBvWWShCvlBqBNIZt0KAshWpvSjhkwkEu4ZUcASoAvhmrgAUI2t1dXrjSL4xXVLB4FznPrIsX3nKXFl/Dt4vA==", - "dev": true, "license": "MIT" }, "node_modules/@types/three": { "version": "0.187.0", "resolved": "https://registry.npmjs.org/@types/three/-/three-0.187.0.tgz", "integrity": "sha512-v+vKNarUtk4n2QRZ4+CN5ygMM0ARUjXjIQl0R/unMUfmStFeVqGbgqarbrpoGTXRZoulSH16ePrVJVxPJEbMUA==", - "dev": true, "license": "MIT", "dependencies": { "@dimforge/rapier3d-compat": "~0.12.0", @@ -1355,7 +1357,6 @@ "version": "0.5.24", "resolved": "https://registry.npmjs.org/@types/webxr/-/webxr-0.5.24.tgz", "integrity": "sha512-h8fgEd/DpoS9CBrjEQXR+dIDraopAEfu4wYVNY2tEPwk60stPWhvZMf4Foo5FakuQ7HFZoa8WceaWFervK2Ovg==", - "dev": true, "license": "MIT" }, "node_modules/@typescript/typescript-aix-ppc64": { @@ -1861,7 +1862,6 @@ "version": "0.8.3", "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.3.tgz", "integrity": "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA==", - "dev": true, "license": "MIT" }, "node_modules/fsevents": { @@ -2179,6 +2179,7 @@ "version": "0.546.0", "resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.546.0.tgz", "integrity": "sha512-Z94u6fKT43lKeYHiVyvyR8fT7pwCzDu7RyMPpTvh054+xahSgj4HFQ+NmflvzdXsoAjYGdCguGaFKYuvq0ThCQ==", + "dev": true, "license": "ISC", "peerDependencies": { "react": "^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0" @@ -2198,7 +2199,6 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/meshoptimizer/-/meshoptimizer-1.1.1.tgz", "integrity": "sha512-oRFNWJRDA/WTrVj7NWvqa5HqE1t9MYDj2VaWirQCzCCrAd2GHrqR/sQezCxiWATPNlKTcRaPRHPJwIRoPBAp5g==", - "dev": true, "license": "MIT" }, "node_modules/nanoid": { @@ -2318,6 +2318,7 @@ "version": "19.3.1", "resolved": "https://registry.npmjs.org/react/-/react-19.3.1.tgz", "integrity": "sha512-QNhWdt7nrgVWWQG58QuGsAR3jv3Hbe4mb4vz7w2csnHavciZJgda1CPNaldF6eVxv90ud3jUXpKlHL69gO38yw==", + "dev": true, "license": "MIT", "engines": { "node": ">=0.10.0" @@ -2327,6 +2328,7 @@ "version": "19.3.1", "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.3.1.tgz", "integrity": "sha512-Y0bARz53lhEC1NMCASs0/j+faB/zqt3R8hpvg1e8+61ZVJ4UL0UdC4cBbXfZMcIuhrfgeILTLe+ZeBCavQPtKQ==", + "dev": true, "license": "MIT", "dependencies": { "scheduler": "^0.28.0" @@ -2383,6 +2385,7 @@ "version": "0.28.0", "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.28.0.tgz", "integrity": "sha512-juorfCmIkIw8tT+p5BXSm6PJjQF/ycEYmKyzURCIt/RaZIhL+PulbQ9Yu2z1HdOJDdqDTlxA1+xKBmHXJsczAw==", + "dev": true, "license": "MIT" }, "node_modules/source-map-js": { @@ -2426,6 +2429,7 @@ "version": "0.0.19", "resolved": "https://registry.npmjs.org/three-bvh-csg/-/three-bvh-csg-0.0.19.tgz", "integrity": "sha512-aYX12cVnkBV7QZFagoilf7VhL/w5VLFD2fqAYsAU1VyadMkAxjWYPsU+wOoNFc8krp+GI6PwAbT3yEeWTOu+KA==", + "dev": true, "license": "MIT", "peerDependencies": { "three": ">=0.179.0", @@ -2436,6 +2440,7 @@ "version": "0.0.28", "resolved": "https://registry.npmjs.org/three-gpu-pathtracer/-/three-gpu-pathtracer-0.0.28.tgz", "integrity": "sha512-Ybfa6qpRIU2QP4EXQJaawYZzVN4raSFqsr9qhXXVSiCi59fjef0pyyXztYYWqAnX7bweKmD70p2xMEWehl019A==", + "dev": true, "license": "MIT", "peerDependencies": { "three": ">=0.187.0", @@ -2447,6 +2452,7 @@ "version": "0.10.0", "resolved": "https://registry.npmjs.org/three-mesh-bvh/-/three-mesh-bvh-0.10.0.tgz", "integrity": "sha512-QHKCD67pgd7YkAcPa4rvuPJjeUm1shFKcFSW8QqnJ0t2uhwi+yyadLpDWI8t0VQsivfFjhVCYvf6rVk9Z0R1gQ==", + "dev": true, "license": "MIT", "peerDependencies": { "three": ">= 0.159.0" @@ -2620,6 +2626,7 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/xatlas-web/-/xatlas-web-0.1.0.tgz", "integrity": "sha512-PprVfuXbaIskxLTLBUQRaWfgSy9xUQqAMIRooOw0P6NYqwgh6T0voeer6+Z5M7AFt5SGXUybuww/uDGs1yw8vQ==", + "dev": true, "license": "MIT", "peer": true } diff --git a/package.json b/package.json index 422f15b..5b8527e 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,40 @@ { "name": "primitive-decoder", "version": "0.1.0", - "private": true, "description": "Local-first procedural system visualisation engine", "license": "Apache-2.0", "repository": { "type": "git", "url": "https://github.com/Primitivelabai/decoder.git" }, + "homepage": "https://primitivelabai.github.io/decoder/", + "bugs": "https://github.com/Primitivelabai/decoder/issues", + "keywords": [ + "3d", + "visualisation", + "diagram", + "exploded-view", + "technical-illustration", + "three", + "cli" + ], "type": "module", + "bin": { + "decoder": "lib/cli.js" + }, + "exports": { + ".": { + "types": "./lib/types/sdk/index.d.ts", + "import": "./lib/index.js" + }, + "./package.json": "./package.json" + }, + "types": "./lib/types/sdk/index.d.ts", + "files": [ + "lib", + "skills", + "THIRD_PARTY_NOTICES.md" + ], "engines": { "node": ">=22.12" }, @@ -31,32 +57,35 @@ "pretest": "npm run bundle:standalone", "predecoder": "npm run bundle:standalone", "preverify:exports": "npm run bundle:standalone", - "prelint": "npm run bundle:standalone" + "prelint": "npm run bundle:standalone", + "build:package": "npm run bundle:standalone && node scripts/build-package.mjs", + "prepack": "npm run build:package", + "prepublishOnly": "npm run check" }, "dependencies": { - "@fontsource/geist": "^5.3.0", - "@fontsource/geist-mono": "^5.3.0", - "@fontsource/space-grotesk": "^5.3.0", + "@types/three": "^0.187.0", "ajv": "^8.20.0", - "lucide-react": "^0.546.0", - "react": "^19.0.1", - "react-dom": "^19.0.1", - "three": "^0.187.0", - "three-bvh-csg": "^0.0.19", - "three-gpu-pathtracer": "^0.0.28", - "three-mesh-bvh": "^0.10.0" + "three": "^0.187.0" }, "devDependencies": { + "@fontsource/geist": "^5.3.0", + "@fontsource/geist-mono": "^5.3.0", + "@fontsource/space-grotesk": "^5.3.0", "@tailwindcss/vite": "^4.3.3", "@types/node": "^22.14.0", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", - "@types/three": "^0.187.0", "@vitejs/plugin-react": "^6.1.1", "esbuild": "^0.28.0", + "lucide-react": "^0.546.0", "playwright": "^1.65.0", "prettier": "^3.10.1", + "react": "^19.0.1", + "react-dom": "^19.0.1", "tailwindcss": "^4.3.3", + "three-bvh-csg": "^0.0.19", + "three-gpu-pathtracer": "^0.0.28", + "three-mesh-bvh": "^0.10.0", "tsx": "^4.21.0", "typescript": "^7.0.2", "vite": "^8.3.0" diff --git a/scripts/build-package.mjs b/scripts/build-package.mjs new file mode 100644 index 0000000..e87686c --- /dev/null +++ b/scripts/build-package.mjs @@ -0,0 +1,59 @@ +// Builds the npm package: the SDK and the `decoder` command as ES modules in lib/, +// with type declarations for the SDK. +// +// Runtime dependencies stay external. The three.js add-ons (three-bvh-csg and friends) +// are bundled from their ES module builds, because Node would otherwise load their +// CommonJS builds and with them a second copy of three. +import { build } from 'esbuild'; +import { execFileSync } from 'node:child_process'; +import { existsSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs'; +import { dirname, join, resolve } from 'node:path'; + +const pkg = JSON.parse(readFileSync('package.json', 'utf8')); +const external = Object.keys(pkg.dependencies ?? {}).flatMap((name) => [name, `${name}/*`]); + +rmSync('lib', { recursive: true, force: true }); + +await build({ + entryPoints: { index: 'src/sdk/index.ts', cli: 'cli/decoder.ts' }, + outdir: 'lib', + bundle: true, + splitting: true, + format: 'esm', + platform: 'node', + target: 'node22', + mainFields: ['module', 'main'], + external, + chunkNames: 'chunks/[name]-[hash]', + legalComments: 'inline', + logLevel: 'warning', +}); + +execFileSync(process.execPath, ['node_modules/typescript/bin/tsc', '-p', 'tsconfig.package.json'], { + stdio: 'inherit', +}); + +// Node's ES module resolution needs explicit extensions in relative imports, +// including the ones inside declaration files. +const relativeImport = /(from\s+|import\s*\(\s*)(['"])(\.{1,2}\/[^'"]+)\2/g; +for (const file of walk('lib/types')) { + if (!file.endsWith('.d.ts')) continue; + const source = readFileSync(file, 'utf8'); + const fixed = source.replace(relativeImport, (match, lead, quote, specifier) => { + const target = resolve(dirname(file), specifier); + if (existsSync(`${target}.d.ts`)) return `${lead}${quote}${specifier}.js${quote}`; + if (existsSync(join(target, 'index.d.ts'))) return `${lead}${quote}${specifier}/index.js${quote}`; + return match; + }); + if (fixed !== source) writeFileSync(file, fixed); +} + +console.log('Package built in lib/'); + +function* walk(dir) { + for (const name of readdirSync(dir)) { + const path = join(dir, name); + if (statSync(path).isDirectory()) yield* walk(path); + else yield path; + } +} diff --git a/tsconfig.package.json b/tsconfig.package.json new file mode 100644 index 0000000..a0fb4f7 --- /dev/null +++ b/tsconfig.package.json @@ -0,0 +1,13 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": false, + "declaration": true, + "emitDeclarationOnly": true, + "allowImportingTsExtensions": false, + "rootDir": "./src", + "outDir": "./lib/types", + "types": [] + }, + "include": ["src/sdk/index.ts"] +}