|
| 1 | +const { execSync } = require('child_process'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +// Helper to run commands |
| 6 | +const run = (command, options = {}) => { |
| 7 | + console.log(`Running: ${command}`); |
| 8 | + execSync(command, { stdio: 'inherit', ...options }); |
| 9 | +}; |
| 10 | + |
| 11 | +const distDir = path.resolve(__dirname, '../dist'); |
| 12 | +const storybookStaticDir = path.resolve(__dirname, '../storybook-static'); |
| 13 | +const storybookTargetDir = path.join(distDir, 'storybook'); |
| 14 | + |
| 15 | +try { |
| 16 | + // 1. Build Main App |
| 17 | + console.log('-------------------------------------------------------'); |
| 18 | + console.log('Step 1: Building Main App (Production)...'); |
| 19 | + console.log('-------------------------------------------------------'); |
| 20 | + run('npm run build:production'); |
| 21 | + |
| 22 | + // 2. Build Storybook |
| 23 | + console.log('\n-------------------------------------------------------'); |
| 24 | + console.log('Step 2: Building Storybook...'); |
| 25 | + console.log('-------------------------------------------------------'); |
| 26 | + // Set env var for base href. |
| 27 | + // Homepage is https://wkylin.github.io/pro-react-admin |
| 28 | + // So root is /pro-react-admin/ |
| 29 | + // Storybook should be at /pro-react-admin/storybook/ |
| 30 | + const env = { ...process.env, STORYBOOK_BASE_HREF: '/pro-react-admin/storybook/' }; |
| 31 | + run('npm run build-storybook', { env }); |
| 32 | + |
| 33 | + // 3. Move Storybook to dist/storybook |
| 34 | + console.log('\n-------------------------------------------------------'); |
| 35 | + console.log('Step 3: Moving Storybook to dist/storybook...'); |
| 36 | + console.log('-------------------------------------------------------'); |
| 37 | + |
| 38 | + if (!fs.existsSync(distDir)) { |
| 39 | + throw new Error('dist directory does not exist! Main app build might have failed.'); |
| 40 | + } |
| 41 | + |
| 42 | + // Clean previous storybook dir in dist if exists |
| 43 | + if (fs.existsSync(storybookTargetDir)) { |
| 44 | + console.log(`Removing existing ${storybookTargetDir}...`); |
| 45 | + fs.rmSync(storybookTargetDir, { recursive: true, force: true }); |
| 46 | + } |
| 47 | + |
| 48 | + // Ensure parent dir exists (it should, as dist exists) |
| 49 | + if (!fs.existsSync(storybookStaticDir)) { |
| 50 | + throw new Error('storybook-static directory does not exist! Storybook build might have failed.'); |
| 51 | + } |
| 52 | + |
| 53 | + console.log(`Moving ${storybookStaticDir} to ${storybookTargetDir}...`); |
| 54 | + |
| 55 | + // Try rename first (fastest) |
| 56 | + try { |
| 57 | + fs.renameSync(storybookStaticDir, storybookTargetDir); |
| 58 | + } catch (err) { |
| 59 | + if (err.code === 'EXDEV') { |
| 60 | + // Cross-device link, fall back to copy |
| 61 | + console.log('Cross-device link detected, copying files instead...'); |
| 62 | + fs.cpSync(storybookStaticDir, storybookTargetDir, { recursive: true }); |
| 63 | + fs.rmSync(storybookStaticDir, { recursive: true, force: true }); |
| 64 | + } else { |
| 65 | + throw err; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // 4. Deploy |
| 70 | + console.log('\n-------------------------------------------------------'); |
| 71 | + console.log('Step 4: Deploying to GitHub Pages...'); |
| 72 | + console.log('-------------------------------------------------------'); |
| 73 | + // We use npx to ensure we use the local gh-pages binary |
| 74 | + run('npx gh-pages -d dist'); |
| 75 | + |
| 76 | + console.log('\n======================================================='); |
| 77 | + console.log('Deployment Complete! 🚀'); |
| 78 | + console.log('Main App: https://wkylin.github.io/pro-react-admin/'); |
| 79 | + console.log('Storybook: https://wkylin.github.io/pro-react-admin/storybook/'); |
| 80 | + console.log('======================================================='); |
| 81 | + |
| 82 | +} catch (error) { |
| 83 | + console.error('\n❌ Deployment Failed:', error.message); |
| 84 | + process.exit(1); |
| 85 | +} |
0 commit comments