Skip to content

Commit

Permalink
Scripts: Add a --zip-root-folder argument to the plugin-zip command (W…
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasgalvez committed May 3, 2024
1 parent c86e77f commit 964beed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,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 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.

### `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
27 changes: 24 additions & 3 deletions packages/scripts/scripts/plugin-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ 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( '--zip-root-folder' );
let zipRootFolder = null;
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 +49,29 @@ if ( hasPackageProp( 'files' ) ) {
);
}

if ( zipRootFolderArg !== undefined ) {
if ( zipRootFolderArg === null ) {
stdout.write(
'No value provided for `--zip-root-folder`. Using the plugin name as the root folder.\n\n'
);
zipRootFolder = `${ name }/`;
} else {
zipRootFolder = `${ zipRootFolderArg }/`;
}
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 );
zip.addLocalFile( file, zipDirectory !== '.' ? zipDirectory : '' );
zip.addLocalFile(
file,
zipRootFolder + ( zipDirectory !== '.' ? zipDirectory : '' )
);
} );

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

0 comments on commit 964beed

Please sign in to comment.