|
| 1 | +const { execSync } = require('child_process') |
| 2 | +const { existsSync } = require('fs') |
| 3 | + |
| 4 | +// Skip if explicitly disabled |
| 5 | +if (process.env.SKIP_BUILD === '1' || process.env.SKIP_POSTINSTALL === '1') { |
| 6 | + process.exit(0) |
| 7 | +} |
| 8 | + |
| 9 | +// Detect if this is a git install via pnpm's synthetic script names |
| 10 | +// pnpm creates 'npm-install', 'yarn-install', or 'pnpm-install' scripts |
| 11 | +// ONLY when preparing git dependencies |
| 12 | +const lifecycleEvent = process.env.npm_lifecycle_event |
| 13 | + |
| 14 | +// Detect git install via pnpm's store tmp directory pattern |
| 15 | +// pnpm prepares git dependencies in /pnpm/store/v{version}/tmp/ |
| 16 | +const cwd = process.cwd() |
| 17 | +const isPnpmStoreTmp = /[/\\]pnpm[/\\]store[/\\]v\d+[/\\]tmp[/\\]/.test(cwd) |
| 18 | + |
| 19 | +const isGitInstall = |
| 20 | + lifecycleEvent === 'npm-install' || |
| 21 | + lifecycleEvent === 'yarn-install' || |
| 22 | + lifecycleEvent === 'pnpm-install' || |
| 23 | + isPnpmStoreTmp |
| 24 | + |
| 25 | +// Skip if installed as sub-dependency in local development |
| 26 | +// (Don't skip for git installs - the ../../package.json would be the consuming project) |
| 27 | +if (!isGitInstall) { |
| 28 | + const isSubDependency = existsSync('../../package.json') |
| 29 | + if (isSubDependency) { |
| 30 | + process.exit(0) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +if (isGitInstall) { |
| 35 | + // Git install: build production files |
| 36 | + console.log('Building Leva for GitHub installation...') |
| 37 | + try { |
| 38 | + execSync('npx preconstruct build', { stdio: 'inherit' }) |
| 39 | + } catch (e) { |
| 40 | + console.error('Build failed:', e.message) |
| 41 | + process.exit(1) |
| 42 | + } |
| 43 | +} else { |
| 44 | + // Local development: use dev mode for fast linking |
| 45 | + try { |
| 46 | + execSync('npx preconstruct dev', { stdio: 'inherit' }) |
| 47 | + } catch (e) { |
| 48 | + console.error('Failed to run preconstruct dev:', e.message) |
| 49 | + process.exit(1) |
| 50 | + } |
| 51 | +} |
0 commit comments