2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
const fs = require( 'fs' );
|
|
|
|
const { execSync } = require( 'child_process' );
|
|
|
|
const path = require( 'path' );
|
|
|
|
const chalk = require( 'chalk' );
|
2019-01-15 18:40:13 +00:00
|
|
|
const watch = require( 'node-watch' );
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
const getPackages = require( './get-packages' );
|
|
|
|
|
2021-04-15 01:32:46 +00:00
|
|
|
const BUILD_CMD = `node ${ path
|
|
|
|
.resolve( __dirname, './build.js' )
|
|
|
|
.replace( /(\s+)/g, '\\$1' ) }`;
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
let filesToBuild = new Map();
|
|
|
|
|
|
|
|
const exists = ( filename ) => {
|
|
|
|
try {
|
|
|
|
return fs.statSync( filename ).isFile();
|
|
|
|
} catch ( e ) {}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Exclude deceitful source-like files, such as editor swap files.
|
|
|
|
const isSourceFile = ( filename ) => {
|
2021-04-15 01:32:46 +00:00
|
|
|
return /.\.(js|ts|tsx|scss)$/.test( filename );
|
2018-10-30 18:57:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const rebuild = ( filename ) => filesToBuild.set( filename, true );
|
|
|
|
|
|
|
|
getPackages().forEach( ( p ) => {
|
|
|
|
const srcDir = path.resolve( p, 'src' );
|
|
|
|
try {
|
|
|
|
fs.accessSync( srcDir, fs.F_OK );
|
2021-04-15 01:32:46 +00:00
|
|
|
watch(
|
|
|
|
path.resolve( p, 'src' ),
|
|
|
|
{ recursive: true },
|
|
|
|
( event, filename ) => {
|
|
|
|
const filePath = path.resolve( srcDir, filename );
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2021-04-15 01:32:46 +00:00
|
|
|
if ( ! isSourceFile( filename ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2021-04-15 01:32:46 +00:00
|
|
|
if (
|
|
|
|
[ 'update', 'change', 'rename' ].includes( event ) &&
|
|
|
|
exists( filePath )
|
|
|
|
) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(
|
|
|
|
chalk.green( '->' ),
|
|
|
|
`${ event }: ${ filename }`
|
|
|
|
);
|
|
|
|
rebuild( filePath );
|
|
|
|
} else {
|
|
|
|
const buildFile = path.resolve(
|
|
|
|
srcDir,
|
|
|
|
'..',
|
|
|
|
'build',
|
|
|
|
filename
|
2018-10-30 18:57:48 +00:00
|
|
|
);
|
2021-04-15 01:32:46 +00:00
|
|
|
try {
|
|
|
|
fs.unlinkSync( buildFile );
|
|
|
|
process.stdout.write(
|
|
|
|
chalk.red( ' \u2022 ' ) +
|
|
|
|
path.relative(
|
|
|
|
path.resolve( srcDir, '..', '..' ),
|
|
|
|
buildFile
|
|
|
|
) +
|
|
|
|
' (deleted)' +
|
|
|
|
'\n'
|
|
|
|
);
|
|
|
|
} catch ( e ) {}
|
|
|
|
}
|
2018-10-30 18:57:48 +00:00
|
|
|
}
|
2021-04-15 01:32:46 +00:00
|
|
|
);
|
2018-10-30 18:57:48 +00:00
|
|
|
} catch ( e ) {
|
|
|
|
// doesn't exist
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
setInterval( () => {
|
|
|
|
const files = Array.from( filesToBuild.keys() );
|
|
|
|
if ( files.length ) {
|
|
|
|
filesToBuild = new Map();
|
|
|
|
try {
|
2021-04-15 01:32:46 +00:00
|
|
|
execSync(
|
|
|
|
`${ BUILD_CMD } ${ files
|
|
|
|
.map( ( file ) => file.replace( /(\s+)/g, '\\$1' ) )
|
|
|
|
.join( ' ' ) }`,
|
|
|
|
{ stdio: [ 0, 1, 2 ] }
|
|
|
|
);
|
2018-10-30 18:57:48 +00:00
|
|
|
} catch ( e ) {}
|
|
|
|
}
|
|
|
|
}, 100 );
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log( chalk.red( '->' ), chalk.cyan( 'Watching for changes...' ) );
|