Skip to content
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

WP Scripts: Add a --root-folder argument to the plugin-zip command #61375

Merged
merged 7 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Enhancements

- Add BlueOak-1.0.0 the GPLv2-comatible licenses recognized by check-licenses ([#66139](https://github.com/WordPress/gutenberg/pull/66139)).
- Add BlueOak-1.0.0 the GPLv2-compatible licenses recognized by check-licenses ([#66139](https://github.com/WordPress/gutenberg/pull/66139)).
- Add an optional `--root-folder` argument to the `plugin-zip` command ([#61375](https://github.com/WordPress/gutenberg/pull/61375)). By default, the command will use the plugin's name as the root folder of the zip. If the change in the behavior impacted your workflow, you could pass `--no-root-folder` to remove the root folder.

### Internal

Expand Down
7 changes: 7 additions & 0 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ In the case where the plugin author wants to customize the files included in the

It reuses the same logic as `npm pack` command to create an npm package tarball.

This is how you create a custom root folder inside the zip file.
- When updating a plugin, WordPress expects a folder in the root of the zip file which matches the plugin name. So be aware that this may affect the plugin update process.
gziolo marked this conversation as resolved.
Show resolved Hide resolved
- `--root-folder` - Add a custom root folder to the zip file.
- `npm run plugin-zip` - By default, unzipping your plugin's zip file will result in a folder with the same name as your plugin.
- `npm run plugin-zip --root-folder='custom-directory'` - Your plugin's zip file will be unzipped into a folder named `custom-directory`.
- `npm run plugin-zip --no-root-folder` - This will create a zip file that has no folder inside, your plugin files will be unzipped directly into the target directory.

### `start`

Transforms your code according the configuration provided so it’s ready for development. The script will automatically rebuild if you make changes to the code, and you will see the build errors in the console.
Expand Down
29 changes: 26 additions & 3 deletions packages/scripts/scripts/plugin-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ const { stdout } = require( 'process' );
/**
* Internal dependencies
*/
const { hasPackageProp, getPackageProp } = require( '../utils' );
const { hasPackageProp, getPackageProp, getArgFromCLI } = require( '../utils' );

const name = getPackageProp( 'name' );
stdout.write( `Creating archive for \`${ name }\` plugin... 🎁\n\n` );
const zip = new AdmZip();

const zipRootFolderArg = getArgFromCLI( '--root-folder' );
const noRootFolderArg = getArgFromCLI( '--no-root-folder' );
let zipRootFolder = `${ name }/`;
let files = [];

if ( hasPackageProp( 'files' ) ) {
stdout.write(
'Using the `files` field from `package.json` to detect files:\n\n'
Expand Down Expand Up @@ -47,10 +50,30 @@ if ( hasPackageProp( 'files' ) ) {
);
}

if ( noRootFolderArg !== undefined ) {
zipRootFolder = '';
stdout.write( ' Plugin files will be zipped without a root folder.\n\n' );
} else if ( zipRootFolderArg !== undefined ) {
const trimmedZipRootFolderArg =
typeof zipRootFolderArg === 'string' ? zipRootFolderArg.trim() : null;
if ( trimmedZipRootFolderArg === null ) {
stdout.write(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this could use some formatting to better indicate that the param was misconfigured and the zip wasn't created.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the message to Unable to create zip package: please provide a --root-folder name or use `--no-root-folder.

'Unable to create zip package: please provide a `--root-folder` name or use `--no-root-folder.`\n\n'
);
process.exit( 1 );
}
zipRootFolder = `${ trimmedZipRootFolderArg }/`;
stdout.write(
` Adding the provided folder \`${ zipRootFolder }\` to the root of the package.\n\n`
);
}
files.forEach( ( file ) => {
stdout.write( ` Adding \`${ file }\`.\n` );
const zipDirectory = dirname( file );
zip.addLocalFile( file, zipDirectory !== '.' ? zipDirectory : '' );
zip.addLocalFile(
file,
zipRootFolder + ( zipDirectory !== '.' ? zipDirectory : '' )
);
} );

zip.writeZip( `./${ name }.zip` );
Expand Down
Loading