Repository navigation
Only include icon library SVGs listed as public in the Zip file published to GitHub Container Registry for wordpress-develop
#79338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
04f1a21
Icons: Extract shared getPublicIcons helper
desrosj 022a3c5
Icons: Group internal require under Internal dependencies block
desrosj c546433
Icons: Add prune-non-public-icons build script
desrosj 3db2852
Icons: Assert exact prune log message with toHaveLoggedWith
desrosj afd0623
Build: Prune non-public icons from wordpress-develop artifact
desrosj ec99f4a
Adjust inline documentation.
desrosj ddb6288
Merge branch 'trunk' into fix/prune-non-public-icons-core-build
t-hamano 3a41514
Make use of `jq` to simplify the related logic.
desrosj cf358d9
Restore non-public icons after creating zip file.
desrosj cd5f671
Merge branch 'trunk' into fix/prune-non-public-icons-core-build
desrosj 2812470
Output details to the log.
desrosj 828584e
Merge remote-tracking branch 'origin/fix/prune-non-public-icons-core-…
desrosj 26a724d
Remove `jq-node` requirement.
desrosj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * Filters the list of registered icons marked as public. | ||
| * | ||
| * Public icons are those explicitly marked `public: true` in the manifest file. | ||
| * | ||
| * @param {Array<Object>} manifest Parsed manifest.json array. | ||
| * @return {Array<Object>} Icons with `public === true`. | ||
| */ | ||
| function getPublicIcons( manifest ) { | ||
| return manifest.filter( ( item ) => item.public === true ); | ||
| } | ||
|
desrosj marked this conversation as resolved.
Resolved
|
||
|
|
||
| module.exports = { getPublicIcons }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| const path = require( 'path' ); | ||
| const { readFile, readdir, unlink } = require( 'fs' ).promises; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| const { getPublicIcons } = require( './get-public-icons.cjs' ); | ||
|
|
||
| const MANIFEST_JSON_PATH = path.join( __dirname, '..', 'src', 'manifest.json' ); | ||
| const LIBRARY_DIR = path.join( __dirname, '..', 'src', 'library' ); | ||
|
|
||
| /** | ||
| * Deletes every non-public SVG in the icon library. | ||
| * | ||
| * Non-public icons are those not explicitly marked as `public: true` in the manifest. Must run after icon collection | ||
| * validation, which expects the manifest and library to match 1:1. | ||
| * | ||
| * @param {Object} [options] Options. | ||
| * @param {string} [options.libraryDir] Directory containing the SVG files. | ||
| * @param {string} [options.manifestPath] Path to manifest.json. | ||
| * @return {Promise<{pruned: number, retained: number}>} Prune summary. | ||
| */ | ||
| async function pruneNonPublicIcons( { | ||
| libraryDir = LIBRARY_DIR, | ||
| manifestPath = MANIFEST_JSON_PATH, | ||
| } = {} ) { | ||
| const manifest = JSON.parse( await readFile( manifestPath, 'utf8' ) ); | ||
| const publicFiles = new Set( | ||
| getPublicIcons( manifest ).map( ( item ) => | ||
| path.basename( item.filePath ) | ||
| ) | ||
| ); | ||
|
|
||
| const svgFiles = ( await readdir( libraryDir ) ).filter( ( file ) => | ||
| file.endsWith( '.svg' ) | ||
| ); | ||
|
|
||
| let pruned = 0; | ||
| for ( const file of svgFiles ) { | ||
| if ( ! publicFiles.has( file ) ) { | ||
| await unlink( path.join( libraryDir, file ) ); | ||
|
desrosj marked this conversation as resolved.
Resolved
|
||
| pruned++; | ||
| } | ||
| } | ||
|
|
||
| const retained = svgFiles.length - pruned; | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| `Pruned ${ pruned } non-public icon(s); ${ retained } retained.` | ||
| ); | ||
|
|
||
| return { pruned, retained }; | ||
| } | ||
|
|
||
| module.exports = { pruneNonPublicIcons }; | ||
|
|
||
| if ( module === require.main ) { | ||
| pruneNonPublicIcons(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { getPublicIcons } from '../get-public-icons.cjs'; | ||
|
|
||
| describe( 'getPublicIcons', () => { | ||
| it( 'returns only entries marked public: true', () => { | ||
| const manifest = [ | ||
| { slug: 'a', public: true }, | ||
| { slug: 'b' }, | ||
| { slug: 'c', public: false }, | ||
| { slug: 'd', public: true }, | ||
| ]; | ||
|
|
||
| expect( getPublicIcons( manifest ).map( ( i ) => i.slug ) ).toEqual( [ | ||
| 'a', | ||
| 'd', | ||
| ] ); | ||
| } ); | ||
|
|
||
| it( 'returns an empty array when no entry is public', () => { | ||
| expect( getPublicIcons( [ { slug: 'a' }, { slug: 'b' } ] ) ).toEqual( | ||
| [] | ||
| ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { mkdtemp, mkdir, writeFile, readdir, rm } from 'fs/promises'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { pruneNonPublicIcons } from '../prune-non-public-icons.cjs'; | ||
|
|
||
| describe( 'pruneNonPublicIcons', () => { | ||
| let workDir; | ||
| let libraryDir; | ||
| let manifestPath; | ||
|
|
||
| beforeEach( async () => { | ||
| workDir = await mkdtemp( path.join( os.tmpdir(), 'icons-prune-' ) ); | ||
| libraryDir = path.join( workDir, 'library' ); | ||
| manifestPath = path.join( workDir, 'manifest.json' ); | ||
| await mkdir( libraryDir ); | ||
|
|
||
| for ( const name of [ 'public-a', 'public-b', 'private-c' ] ) { | ||
| await writeFile( | ||
| path.join( libraryDir, `${ name }.svg` ), | ||
| '<svg/>' | ||
| ); | ||
| } | ||
|
|
||
| const manifest = [ | ||
| { | ||
| slug: 'public-a', | ||
| label: 'A', | ||
| filePath: 'library/public-a.svg', | ||
| public: true, | ||
| }, | ||
| { | ||
| slug: 'public-b', | ||
| label: 'B', | ||
| filePath: 'library/public-b.svg', | ||
| public: true, | ||
| }, | ||
| { | ||
| slug: 'private-c', | ||
| label: 'C', | ||
| filePath: 'library/private-c.svg', | ||
| }, | ||
| ]; | ||
| await writeFile( manifestPath, JSON.stringify( manifest ) ); | ||
| } ); | ||
|
|
||
| afterEach( async () => { | ||
| await rm( workDir, { recursive: true, force: true } ); | ||
| } ); | ||
|
|
||
| it( 'deletes non-public SVGs and keeps public ones', async () => { | ||
| const result = await pruneNonPublicIcons( { | ||
| libraryDir, | ||
| manifestPath, | ||
| } ); | ||
|
|
||
| const remaining = ( await readdir( libraryDir ) ).sort(); | ||
| expect( remaining ).toEqual( [ 'public-a.svg', 'public-b.svg' ] ); | ||
| expect( result ).toEqual( { pruned: 1, retained: 2 } ); | ||
| expect( console ).toHaveLoggedWith( | ||
| 'Pruned 1 non-public icon(s); 2 retained.' | ||
| ); | ||
| } ); | ||
| } ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.