Skip to content

Commit

Permalink
Scripts: Change the plugin-zip command to add a folder in the plugin …
Browse files Browse the repository at this point in the history
…zip file by default

1. The plugin-zip command will now create a root folder in the zip file matching the name of the plugin. This aligns with the documentation stating that "By default, it uses Plugin Handbook best practices to discover files."
2. `--zip-root-folder` argument from my previous commit is now named `--root-folder`
3. The --root-folder argument will allow specifying a custom folder, or no folder for backwards compatibility.

Example usage:

{{{
npm run plugin-zip --root-folder='foo'
}}}

Follow-up to [61375], [60481].

Props justlevine, gziolo.
  • Loading branch information
nicolasgalvez committed Oct 23, 2024
1 parent 73fa82b commit a3ae55a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
12 changes: 6 additions & 6 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ 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 zip for use with a custom plugin update system

- `--zip-root-folder` - When updating a plugin, WordPress expects a folder in the root of the zip file which matches the plugin name. The `--zip-root-folder` parameter will create a folder in the root of the zip file that matches your plugin name instead of adding all files to the root.

- `npm run plugin-zip --zip-root-folder` - which will use your plugin name as the folder.
- `npm run plugin-zip --zip-root-folder=plugin-name` - which will allow you to specify a plugin name.
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.
- `--root-folder` - Add a custom root folder to the zip file.
- `npm run plugin-zip` - By default, unzipping your plugin will result in a folder with the same name as your plugin.
- `npm run plugin-zip --root-folder=''` - This will create a zip file that has no folder inside, your plugin files will be unzipped directly into the target directory.
- `npm run plugin-zip --root-folder='custom-directory'` - Your plugin will be unzipped into a folder named `custom-directory`.

### `start`

Expand Down
23 changes: 11 additions & 12 deletions packages/scripts/scripts/plugin-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ 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( '--zip-root-folder' );
let zipRootFolder = null;
const zipRootFolderArg = getArgFromCLI( '--root-folder' );
let zipRootFolder = `${ name }/`;
let files = [];

if ( hasPackageProp( 'files' ) ) {
Expand Down Expand Up @@ -50,21 +50,20 @@ if ( hasPackageProp( 'files' ) ) {
}

if ( zipRootFolderArg !== undefined ) {
if ( zipRootFolderArg === null ) {
const trimmedZipRootFolderArg =
typeof zipRootFolderArg === 'string' ? zipRootFolderArg.trim() : null;
if ( ! trimmedZipRootFolderArg ) {
stdout.write(
'No value provided for `--zip-root-folder`. Using the plugin name as the root folder.\n\n'
'Plugin files will be zipped without a root folder.\n\n'
);
zipRootFolder = `${ name }/`;
zipRootFolder = '';
} else {
zipRootFolder = `${ zipRootFolderArg }/`;
zipRootFolder = `${ trimmedZipRootFolderArg }/`;
stdout.write(
`Adding the provided folder \`${ zipRootFolder }\` to the root of the package.\n\n`
);
}
stdout.write(
`Adding the provided folder \`${ zipRootFolder }\` to the root of the package.\n\n`
);
} else {
zipRootFolder = '';
}

files.forEach( ( file ) => {
stdout.write( ` Adding \`${ file }\`.\n` );
const zipDirectory = dirname( file );
Expand Down

0 comments on commit a3ae55a

Please sign in to comment.