|
|
#!/usr/bin/env node |
|
|
|
|
|
import { exec, spawnSync } from 'node:child_process'; |
|
|
import { EventEmitter } from 'node:events'; |
|
|
import { writeFile, copyFile } from 'node:fs/promises'; |
|
|
import path from 'node:path'; |
|
|
import chokidar from 'chokidar'; |
|
|
import runAll from 'npm-run-all'; |
|
|
import treeKill from 'tree-kill'; |
|
|
import yargs from 'yargs'; |
|
|
import { hideBin } from 'yargs/helpers'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EventEmitter.setMaxListeners( 30 ); |
|
|
|
|
|
|
|
|
if ( process.env.NODE_ENV == null ) { |
|
|
process.env.NODE_ENV = 'development'; |
|
|
} |
|
|
if ( process.env.BROWSERSLIST_ENV == null ) { |
|
|
process.env.BROWSERSLIST_ENV = 'wpcom'; |
|
|
} |
|
|
if ( process.env.DISABLE_DUPLICATE_PACKAGE_CHECK == null ) { |
|
|
process.env.DISABLE_DUPLICATE_PACKAGE_CHECK = true; |
|
|
} |
|
|
|
|
|
const { argv } = yargs( hideBin( process.argv ) ).options( { |
|
|
sync: { type: 'boolean', default: false, implies: 'remotePath' }, |
|
|
remotePath: { type: 'string' }, |
|
|
localPath: { |
|
|
type: 'string', |
|
|
required: true, |
|
|
coerce: ( distPath ) => ( distPath ? `${ process.cwd() }/${ distPath }/` : undefined ), |
|
|
}, |
|
|
verbose: { type: 'boolean', default: false, alias: 'v' }, |
|
|
watch: { type: 'boolean', default: process.env.NODE_ENV === 'development', alias: 'w' }, |
|
|
} ); |
|
|
const VERBOSE = argv.verbose; |
|
|
|
|
|
try { |
|
|
await runBuilder( argv ); |
|
|
} catch ( e ) { |
|
|
|
|
|
if ( process.env.IS_CI !== 'true' ) { |
|
|
const { pid } = process; |
|
|
if ( VERBOSE ) { |
|
|
console.log( `Removing children of PID: ${ pid }` ); |
|
|
} |
|
|
treeKill( pid ); |
|
|
} |
|
|
showTips( e.tasks ); |
|
|
console.error( e.message ); |
|
|
} |
|
|
|
|
|
async function runBuilder( args ) { |
|
|
const { sync, localPath, remotePath, watch } = args; |
|
|
|
|
|
if ( VERBOSE ) { |
|
|
console.log( JSON.stringify( args ) ); |
|
|
} |
|
|
|
|
|
const runOpts = { |
|
|
stdout: process.stdout, |
|
|
stderr: process.stderr, |
|
|
printLabel: true, |
|
|
parallel: true, |
|
|
}; |
|
|
console.log( 'Cleaning...' ); |
|
|
await runAll( [ 'clean' ], runOpts ); |
|
|
|
|
|
console.log( 'Starting webpack...' ); |
|
|
await Promise.all( [ |
|
|
runAll( [ `build:*${ watch ? ' --watch' : '' }` ], runOpts ).then( () => { |
|
|
console.log( 'Build completed!' ); |
|
|
const translate = runAll( 'translate', runOpts ).catch( () => {} ); |
|
|
translate.then( () => { |
|
|
if ( ! watch && sync ) { |
|
|
|
|
|
setupRemoteSync( localPath, remotePath ); |
|
|
} |
|
|
} ); |
|
|
} ), |
|
|
|
|
|
watch && sync && setupRemoteSync( localPath, remotePath, true ), |
|
|
] ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! watch ) { |
|
|
await copyMetaFiles( localPath ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setupRemoteSync( localPath, remotePath, shouldWatch = false ) { |
|
|
const remoteHost = process.env.WPCOM_SANDBOX || 'wpcom-sandbox'; |
|
|
|
|
|
return new Promise( ( resolve, reject ) => { |
|
|
let rsync = null; |
|
|
const debouncedSync = debouncer( () => { |
|
|
if ( VERBOSE ) { |
|
|
console.log( 'Attempting sync...' ); |
|
|
} |
|
|
if ( rsync ) { |
|
|
|
|
|
rsync.kill( 'SIGINT' ); |
|
|
} |
|
|
rsync = exec( |
|
|
`rsync -ahz --exclude=".*" ${ localPath } ${ remoteHost }:${ remotePath }`, |
|
|
( err ) => { |
|
|
rsync = null; |
|
|
|
|
|
const wasRsyncCancelled = err && ( err.signal === 'SIGINT' || err.code === 20 ); |
|
|
if ( err && ! wasRsyncCancelled ) { |
|
|
|
|
|
reject( err ); |
|
|
return; |
|
|
} else if ( wasRsyncCancelled ) { |
|
|
if ( VERBOSE ) { |
|
|
console.log( 'Restarting sync.' ); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
console.log( 'Sync to sandbox completed.' ); |
|
|
if ( ! shouldWatch ) { |
|
|
|
|
|
resolve(); |
|
|
} |
|
|
} |
|
|
); |
|
|
} ); |
|
|
|
|
|
if ( shouldWatch ) { |
|
|
chokidar.watch( localPath ).on( 'all', debouncedSync ); |
|
|
} else { |
|
|
debouncedSync(); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function debouncer( cb ) { |
|
|
let timeout = null; |
|
|
return () => { |
|
|
|
|
|
|
|
|
clearTimeout( timeout ); |
|
|
timeout = setTimeout( cb, 1000 ); |
|
|
}; |
|
|
} |
|
|
|
|
|
function showTips( tasks ) { |
|
|
if ( ! Array.isArray( tasks ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const numFailed = tasks.reduce( ( total, { code } ) => total + ( code ? 1 : 0 ), 0 ); |
|
|
if ( numFailed === tasks.length ) { |
|
|
console.log( |
|
|
'Since all builds failed, there is likely an issue with webpack or node modules.' |
|
|
); |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
function git( cmd ) { |
|
|
return spawnSync( 'git', cmd.split( ' ' ), { |
|
|
encoding: 'utf8', |
|
|
} ).stdout.replace( '\n', '' ); |
|
|
} |
|
|
|
|
|
async function copyMetaFiles( archiveDir ) { |
|
|
const buildNumber = process.env.build_number; |
|
|
|
|
|
|
|
|
|
|
|
const commitHash = process.env.commit_sha ?? git( 'rev-parse HEAD' ); |
|
|
|
|
|
const cacheBuster = commitHash.slice( 0, 11 ); |
|
|
|
|
|
const buildMeta = { |
|
|
build_number: buildNumber ?? 'dev', |
|
|
cache_buster: cacheBuster, |
|
|
commit_hash: commitHash, |
|
|
commit_url: `https://github.com/Automattic/wp-calypso/commit/${ commitHash }`, |
|
|
}; |
|
|
|
|
|
await writeFile( |
|
|
path.join( archiveDir, 'build_meta.json' ), |
|
|
JSON.stringify( buildMeta, null, 2 ) |
|
|
); |
|
|
|
|
|
|
|
|
await copyFile( path.join( process.cwd(), 'README.md' ), path.join( archiveDir, 'README.md' ) ); |
|
|
} |
|
|
|