Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bin/packages/build-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ const babel = require( '@babel/core' );
const makeDir = require( 'make-dir' );
const sass = require( 'sass' );
const postcss = require( 'postcss' );

/**
* Internal dependencies
*/
const getBabelConfig = require( './get-babel-config' );
const iconsBuildUtils = require( '../../packages/icons/lib/build-worker-utils' );

const isDev = process.env.NODE_ENV === 'development';

Expand Down Expand Up @@ -179,6 +181,14 @@ async function buildJS( file ) {
}
}

// This build task is only meant for regenerating icons in the icons library at
// packages/icons/src/library. Let it delegate to that package's own handler.
async function buildSVG( file ) {
if ( ! ( await iconsBuildUtils.buildSVG( file ) ) ) {
throw new Error( `No handler for SVG file: ${ file }` );
}
}

/**
* Object of build tasks per file extension.
*
Expand All @@ -189,6 +199,7 @@ const BUILD_TASK_BY_EXTENSION = {
'.js': buildJS,
'.ts': buildJS,
'.tsx': buildJS,
'.svg': buildSVG,
};

module.exports = async ( file, callback ) => {
Expand Down
3 changes: 2 additions & 1 deletion bin/packages/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ function isSourceFile( filename ) {
.replace( /\\/g, '/' );

return (
/\/src\/.+\.(js|json|scss|ts|tsx)$/.test( relativePath ) &&
// Include .svg for automatic generation of icons in packages/icons
/\/src\/.+\.(js|json|scss|ts|tsx|svg)$/.test( relativePath ) &&
! [
/\/(benchmark|__mocks__|__tests__|test|storybook|stories|e2e-test-utils-playwright)\/.+/,
/.\.(spec|test)\.js$/,
Expand Down
49 changes: 49 additions & 0 deletions packages/icons/lib/build-worker-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* External dependencies
*/
const path = require( 'path' );
const { promisify } = require( 'util' );
const execFile = promisify( require( 'child_process' ).execFile );

/**
* Internal dependencies
*/
const { generateTsxFiles } = require( './generate-library' );

const ICON_LIBRARY_DIR = path.join( __dirname, '..', 'src', 'library' );

/**
* A build-worker task to be used by the monorepo's watcher-builder.
*
* @see bin/packages/build-worker.js
*
* @param {string} file File to build.
*/
async function buildSVG( file ) {
if ( path.dirname( file ) !== ICON_LIBRARY_DIR ) {
return false;
}

const FgRed = '\x1b[31m';
const Reset = '\x1b[0m';

try {
await execFile( 'git', [ 'ls-files', '--error-unmatch', file ] );
} catch {
throw new Error(
`${ FgRed }Cannot generate icon from untracked SVG file '${ path.basename(
file
) }'.${ Reset }
${ FgRed }Please add it to Git, then restart:${ Reset }

${ FgRed } git add ${ path.relative( '', file ) }${ Reset }
${ FgRed } npm run dev${ Reset }
`
);
}

await generateTsxFiles( [ path.basename( file ) ] );
return true;
}

module.exports = { buildSVG };
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ async function cleanup() {
}

// Generate src/library/*.tsx based on the available SVG files.
async function generateTsxFiles() {
const svgFiles = ( await readdir( ICON_LIBRARY_DIR ) ).filter( ( file ) =>
// Stricter than just checking for SVG suffix, thereby avoiding hidden
// files and characters that would get in the way of camel-casing.
file.match( /^[a-z0-9--]+\.svg$/ )
);
async function generateTsxFiles( svgFiles ) {
if ( svgFiles ) {
// Argument passed by caller at ./build-worker-utils.js
} else {
svgFiles = ( await readdir( ICON_LIBRARY_DIR ) ).filter( ( file ) =>
// Stricter than just checking for SVG suffix, thereby avoiding hidden
// files and characters that would get in the way of camel-casing.
file.match( /^[a-z0-9--]+\.svg$/ )
);
}

await Promise.all(
svgFiles.map( async ( svgFile ) => {
Expand Down Expand Up @@ -189,4 +193,10 @@ ${ jsxContent }
`;
}

main();
if ( module === require.main ) {
main();
}

module.exports = {
generateTsxFiles,
};
2 changes: 1 addition & 1 deletion packages/icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
},
"scripts": {
"prelint": "npm run build",
"build": "node bin/generate-library"
"build": "node lib/generate-library"
}
}
Loading