From 10b8cf9285fd9117ea7e15e9dbeb28013e17b20a Mon Sep 17 00:00:00 2001 From: maze Date: Tue, 10 Mar 2026 10:29:45 +0100 Subject: [PATCH] Streamline package --- README.md | 9 +++++ package.json | 22 +++--------- scripts/build-if-needed.cjs | 70 +++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 scripts/build-if-needed.cjs diff --git a/README.md b/README.md index 027e88c..c4f85bd 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,15 @@ npm install git+https://git.maze.io/maze/cyberduck.git#main Note: when installing from a git repository, npm will clone the source. For consumers to get built artifacts (the `dist` bundle and compiled types) the repository must either include the `dist` output or provide a `prepare`/build step that runs on install. If you prefer not to publish, using `npm pack` from the repo owner (tarball) ensures the built `dist` is included. +Git install prerequisites + +When a consumer installs the package directly from the Git repository, npm (and yarn) will run the package `prepare` script to produce the built `dist` and type artifacts. That means the consumer environment must be able to run the build: + +- Have Node.js and npm installed (recommended Node 16+). +- Have a working `npm install` that can fetch devDependencies so the build tools (`typescript`, `sass`, `vite`) are available. + +If you don't want consumers to run the build during install, either include the `dist` output in the repository (not recommended) or distribute a tarball produced by `npm pack` from the author so consumers can install the built package without running `prepare`. + ## Including styles (optional) Styles are optional — CyberDuck does not automatically inject styles when you import components. Import styles only if you want the theme and utilities applied. diff --git a/package.json b/package.json index be63942..b52bf00 100644 --- a/package.json +++ b/package.json @@ -34,30 +34,16 @@ "./styles": "./src/styles/cyberduck.scss", "./styles/*": "./src/styles/*", "./css": "./dist/cyberduck.css", - "./Navbar": { + "./*": { "import": "./dist/cyberduck.es.js", "require": "./dist/cyberduck.cjs.js", - "types": "./dist/Navbar.d.ts" - }, - "./Footer": { - "import": "./dist/cyberduck.es.js", - "require": "./dist/cyberduck.cjs.js", - "types": "./dist/Footer.d.ts" - }, - "./Layout": { - "import": "./dist/cyberduck.es.js", - "require": "./dist/cyberduck.cjs.js", - "types": "./dist/Layout.d.ts" - }, - "./Split": { - "import": "./dist/cyberduck.es.js", - "require": "./dist/cyberduck.cjs.js", - "types": "./dist/Split.d.ts" + "types": "./dist/*.d.ts" } }, "scripts": { "dev": "vite", - "build": "tsc -p tsconfig.build.json && npm run styles:build && vite build", + "build": "node ./scripts/build-if-needed.cjs", + "prepack": "npm run build", "prepare": "npm run build", "lint": "eslint .", "lint:css": "stylelint \"src/**/*.{scss,css,sass}\"", diff --git a/scripts/build-if-needed.cjs b/scripts/build-if-needed.cjs new file mode 100644 index 0000000..f1ad2ec --- /dev/null +++ b/scripts/build-if-needed.cjs @@ -0,0 +1,70 @@ +#!/usr/bin/env node +const { execSync } = require('child_process') +const fs = require('fs') +const path = require('path') + +const cwd = process.cwd() +const tsconfig = path.join(cwd, 'tsconfig.build.json') + +function runIfAvailable(cmdCheck, cmdRun, desc) { + try { + execSync(cmdCheck, { stdio: 'ignore' }) + console.log(`Running ${desc}...`) + execSync(cmdRun, { stdio: 'inherit' }) + return true + } catch (e) { + console.log(`Skipping ${desc} (tool not available or command failed).`) + return false + } +} + +// 1) TypeScript declarations +if (fs.existsSync(tsconfig)) { + // try to use local tsc if available + runIfAvailable('npx --no-install tsc -v', 'npx --no-install tsc -p tsconfig.build.json', 'TypeScript declaration build') +} else { + console.log('No tsconfig.build.json found — skipping TypeScript declaration build') +} + +// 2) Sass build (styles) +runIfAvailable( + 'npx --no-install sass --version', + 'npx --no-install sass --no-source-map --style=compressed --load-path=node_modules src:dist', + 'Sass styles build', +) + +// 3) Vite build +runIfAvailable('npx --no-install vite --version', 'npx --no-install vite build', 'Vite build') + +// 4) Generate per-subpath declaration wrappers in `dist/` by scanning emitted types +try { + const distTypesRoot = path.join(cwd, 'dist', 'types') + if (fs.existsSync(distTypesRoot)) { + const collected = [] + const walk = (dir) => { + for (const name of fs.readdirSync(dir)) { + const full = path.join(dir, name) + const stat = fs.statSync(full) + if (stat.isDirectory()) walk(full) + else if (stat.isFile() && name.endsWith('.d.ts')) collected.push(full) + } + } + walk(distTypesRoot) + + // Create one wrapper per top-level d.ts basename (skip index.d.ts) + collected.forEach(full => { + const rel = path.relative(path.join(cwd, 'dist'), full).replace(/\\/g, '/') + const base = path.basename(full, '.d.ts') + if (base === 'index') return + const wrapperPath = path.join(cwd, 'dist', `${base}.d.ts`) + const target = `./${rel.replace(/\.d\.ts$/, '')}` + const content = `export { default } from '${target}';\n` + fs.writeFileSync(wrapperPath, content) + }) + console.log('Generated per-subpath .d.ts wrappers into dist/') + } else { + console.log('No dist/types found — skipping generation of per-subpath wrappers') + } +} catch (e) { + console.warn('Failed to generate per-subpath wrappers:', e && e.message) +}