117 lines
4.8 KiB
JavaScript
117 lines
4.8 KiB
JavaScript
#!/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 --quiet-deps --silence-deprecation=if-function,global-builtin,color-functions 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)
|
|
}
|
|
|
|
// Ensure a minimal `dist/types/index.d.ts` exists so consumers installing
|
|
// from the packed tarball won't see a "could not find declaration file" error.
|
|
try {
|
|
const indexDts = path.join(cwd, 'dist', 'types', 'index.d.ts')
|
|
if (!fs.existsSync(indexDts)) {
|
|
const dir = path.dirname(indexDts)
|
|
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
|
|
const fallback = `/// <reference types="react" />
|
|
export const Navbar: import('react').FC<any>;
|
|
export const Footer: import('react').FC<any>;
|
|
export const Layout: import('react').FC<any>;
|
|
export const Split: import('react').FC<any> & { Panel: import('react').FC<any> };
|
|
export default { Navbar, Footer, Layout, Split };
|
|
`
|
|
fs.writeFileSync(indexDts, fallback)
|
|
console.log('Wrote fallback dist/types/index.d.ts')
|
|
}
|
|
} catch (e) {
|
|
console.warn('Failed to write fallback index.d.ts:', e && e.message)
|
|
}
|
|
|
|
// Write a small ESM wrapper that re-exports everything from the generated
|
|
// `cyberduck.es.js` bundle. Some browser toolchains import the package
|
|
// entry directly; having a tiny wrapper helps ensure named exports are
|
|
// preserved and visible to native ESM loaders.
|
|
try {
|
|
const esmWrapperPath = path.join(cwd, 'dist', 'esm.js')
|
|
// Re-export named bindings and build a local default object so we don't
|
|
// need to import the bundle's default (some tools choke on that).
|
|
const esmContent = `import { Navbar, Footer, Layout, Split } from './cyberduck.es.js';\nexport { Navbar, Footer, Layout, Split };\nconst __default = { Navbar, Footer, Layout, Split };\nexport default __default;\n`;
|
|
fs.writeFileSync(esmWrapperPath, esmContent)
|
|
console.log('Wrote dist/esm.js wrapper')
|
|
} catch (e) {
|
|
console.warn('Failed to write dist/esm.js wrapper:', e && e.message)
|
|
}
|
|
try {
|
|
const indexPath = path.join(cwd, 'dist', 'index.js')
|
|
// index.js mirrors esm.js to help environments that resolve package root
|
|
// to an index file instead of reading package.json fields.
|
|
const indexContent = `import { Navbar, Footer, Layout, Split } from './cyberduck.es.js';\nexport { Navbar, Footer, Layout, Split };\nconst __default = { Navbar, Footer, Layout, Split };\nexport default __default;\n`;
|
|
fs.writeFileSync(indexPath, indexContent)
|
|
console.log('Wrote dist/index.js wrapper')
|
|
} catch (e) {
|
|
console.warn('Failed to write dist/index.js wrapper:', e && e.message)
|
|
}
|