#!/usr/bin/env node import { fileURLToPath } from 'url'; import path from 'path'; import spawn from 'cross-spawn'; const __dirname = path.dirname( fileURLToPath( import.meta.url ) ); const ROOT_DIR = path.resolve( __dirname, '../..' ); /** * Execute a command and return a promise. * * @param {string} command Command to execute. * @param {string[]} args Command arguments. * @param {Object} options Spawn options. * @return {Promise} Promise that resolves when command completes. */ function exec( command, args = [], options = {} ) { const silent = options.silent || false; const spawnOptions = { ...options }; delete spawnOptions.silent; return new Promise( ( resolve, reject ) => { const childOptions = { cwd: ROOT_DIR, stdio: silent ? 'pipe' : 'inherit', ...spawnOptions, }; const child = spawn( command, args, childOptions ); // If silent, capture output to show only on error let stdout = ''; let stderr = ''; if ( silent ) { if ( child.stdout ) { child.stdout.on( 'data', ( data ) => { stdout += data.toString(); } ); } if ( child.stderr ) { child.stderr.on( 'data', ( data ) => { stderr += data.toString(); } ); } } child.on( 'exit', ( code ) => { if ( code === 0 ) { resolve(); } else { // On error, show captured output if it was silent if ( silent && ( stdout || stderr ) ) { if ( stdout ) { process.stdout.write( stdout ); } if ( stderr ) { process.stderr.write( stderr ); } } reject( new Error( `Command failed: ${ command } ${ args.join( ' ' ) }` ) ); } } ); child.on( 'error', reject ); } ); } /** * Main build orchestration function. */ async function build() { console.log( '๐Ÿ”จ Starting build process...\n' ); const startTime = Date.now(); try { /* * Step 0: Verify node_modules is in sync with package-lock.json. * * GUTENBERG_CHECK_INSTALLED_DEPS controls when this runs: * - `BEFORE_BUILD` (default): pre-build gate. * - `NEVER`: skip entirely (sticky opt-out for power users). */ if ( process.env.GUTENBERG_CHECK_INSTALLED_DEPS === 'NEVER' ) { console.log( '๐Ÿ” Skipping dependency check (GUTENBERG_CHECK_INSTALLED_DEPS=NEVER).' ); } else { console.log( '๐Ÿ” Checking dependencies...' ); await exec( 'npm', [ 'run', 'check-installed-deps', '--workspace', '@wordpress/validation-tools', '--silent', ] ).catch( () => { throw new Error( 'Run `npm install` to update, or set GUTENBERG_CHECK_INSTALLED_DEPS=NEVER to skip this check.' ); } ); } console.log( '\n๐Ÿงน Cleaning packages...' ); await exec( 'node', [ path.join( __dirname, 'clean.mjs' ), '--packages' ], { silent: true } ); console.log( '\n๐Ÿ“ฆ Building workspaces...' ); await exec( 'npm', [ 'run', '--if-present', '--workspaces', '--silent', 'build' ], { silent: true } ); // This must happen before TypeScript compilation because some packages // (like vips) have source files that import from generated worker-code.ts await exec( 'node', [ path.join( __dirname, 'packages/generate-worker-placeholders.mjs' ), ] ); /* * Emit declarations without type checking; `npm run typecheck` owns * that. Only parse and declaration emit errors can fail this step. */ console.log( '\n๐Ÿ“˜ Building TypeScript types...\n' ); const tsStartTime = Date.now(); await exec( 'tsc', [ '--build', 'tsconfig.build.json', '--noCheck', ] ).catch( () => { console.error( '\nโŒ TypeScript declaration emit failed. Try cleaning up first: `npm run clean:package-types`' ); throw new Error( 'TypeScript declaration emit failed' ); } ); const buildTime = Date.now() - tsStartTime; console.log( ` โœ” Built TypeScript types (${ buildTime }ms)` ); console.log( '\n๐Ÿ“ฆ Building vendor files...' ); await exec( 'node', [ path.join( __dirname, 'packages/build-vendors.mjs' ), ] ); console.log( '\n๐Ÿ“ฆ Building packages (production mode)...' ); await exec( 'wp-build', process.argv.slice( 2 ), { env: { ...process.env, NODE_ENV: 'production' }, } ); console.log( '\n๐Ÿ“ฆ Building blocks manifests...' ); const blocksDirs = [ { input: 'build/scripts/block-library', output: 'build/scripts/block-library/blocks-manifest.php', }, { input: 'build/scripts/edit-widgets/blocks', output: 'build/scripts/edit-widgets/blocks/blocks-manifest.php', }, { input: 'build/scripts/widgets/blocks', output: 'build/scripts/widgets/blocks/blocks-manifest.php', }, ]; for ( const { input, output } of blocksDirs ) { await exec( 'wp-scripts', [ 'build-blocks-manifest', `--input=${ input }`, `--output=${ output }`, ], { silent: true } ); } console.log( '\n๐Ÿ“ฆ Building workspace :wp targets...' ); await exec( 'npm', [ 'run', '--if-present', '--workspaces', '--silent', 'build:wp' ], { silent: true } ); const totalTime = Date.now() - startTime; console.log( `\n๐ŸŽ‰ Build completed successfully! (${ Math.round( totalTime / 1000 ) }s total)` ); } catch ( error ) { console.error( '\nโŒ Build failed:', error.message ); process.exit( 1 ); } } build();