Skip to content

Commit

Permalink
Scripts: Add plugin-zip command to create a zip file for a WordPres…
Browse files Browse the repository at this point in the history
…s plugin
  • Loading branch information
gziolo committed Jan 3, 2022
1 parent e68e4ca commit 3d45a3b
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 14 deletions.
3 changes: 3 additions & 0 deletions bin/test-create-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ status "Lintig CSS files..."

status "Linting JavaScript files..."
../node_modules/.bin/wp-scripts lint-js

status "Creating a plugin zip file..."
../node_modules/.bin/wp-scripts plugin-zip
95 changes: 83 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Integrated a new `plugin-zip` command to create a zip file for a WordPress plugin ([#37687](https://github.com/WordPress/gutenberg/pull/37687)).

### Enhancement

- Speed up scaffolding process by omitting WordPress dependencies in the template ([#37639](https://github.com/WordPress/gutenberg/pull/37639)).
Expand Down
3 changes: 2 additions & 1 deletion packages/create-block/lib/init-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ module.exports = async ( {
format: 'wp-scripts format',
'lint:css': 'wp-scripts lint-style',
'lint:js': 'wp-scripts lint-js',
start: 'wp-scripts start',
'packages-update': 'wp-scripts packages-update',
'plugin-zip': 'wp-scripts plugin-zip',
start: 'wp-scripts start',
},
},
isEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ node_modules/
# Optional eslint cache
.eslintcache

# Output of 'npm pack'
# Output of `npm pack`
*.tgz

# Output of `wp-scripts plugin-zip`
*.zip

# dotenv environment variables file
.env
1 change: 1 addition & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### New Features

- Added a new `plugin-zip` command to create a zip file for a WordPress plugin ([#37687](https://github.com/WordPress/gutenberg/pull/37687)).
- Added optional support for React Fast Refresh in the `start` command. It can be activated with `--hot` CLI argument ([#28273](https://github.com/WordPress/gutenberg/pull/28273)).

### Bug Fixes
Expand Down
29 changes: 29 additions & 0 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ _Example:_
"lint:md:js": "wp-scripts lint-md-js",
"lint:pkg-json": "wp-scripts lint-pkg-json",
"packages-update": "wp-scripts packages-update",
"plugin-zip": "wp-scripts plugin-zip",
"start": "wp-scripts start",
"test:e2e": "wp-scripts test-e2e",
"test:unit": "wp-scripts test-unit-js"
Expand Down Expand Up @@ -303,6 +304,34 @@ _Example:_

The command checks which packages whose name starts with `@wordpress/` are used in the project by reading the package.json file, and then executes `npm install @wordpress/package1@latest @wordpress/package2@latest ... --save` to change the package versions to the latest one.

### `packages-update`

Creates a zip file for a WordPress plugin.

_Example:_

```json
{
"scripts": {
"plugin-zip": "wp-scripts plugin-zip"
}
}
```

By default, it uses [Plugin Handbook best practices](https://developer.wordpress.org/plugins/plugin-basics/best-practices/#file-organization) to discover files.

#### Advanced information

In the case where the plugin author wants to customize the files included in the zip file, they can provide the `files` field in the `package.json` file as documented in the [`npm-packlist`](https://www.npmjs.com/package/npm-packlist) package, example:

```json
{
"files": [ "dir" ]
}
```

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

### `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. The entry point for your project’s code should be located in `src/index.js` (other supported extensions: `.jsx`, `.ts`, and `.tsx`). The output generated will be written to `build/index.js`. For single builds, better suited for production, see the [build](#build) script.
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@wordpress/postcss-plugins-preset": "file:../postcss-plugins-preset",
"@wordpress/prettier-config": "file:../prettier-config",
"@wordpress/stylelint-config": "file:../stylelint-config",
"adm-zip": "^0.5.9",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.3",
"browserslist": "^4.17.6",
Expand All @@ -70,6 +71,7 @@
"mini-css-extract-plugin": "^2.1.0",
"minimist": "^1.2.0",
"npm-package-json-lint": "^5.0.0",
"npm-packlist": "^3.0.0",
"postcss": "^8.2.15",
"postcss-loader": "^6.1.1",
"prettier": "npm:[email protected]",
Expand Down
56 changes: 56 additions & 0 deletions packages/scripts/scripts/plugin-zip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* External dependencies
*/
const AdmZip = require( 'adm-zip' );
const { sync: glob } = require( 'fast-glob' );
const { sync: packlist } = require( 'npm-packlist' );
const { dirname } = require( 'path' );
const { stdout } = require( 'process' );

/**
* Internal dependencies
*/
const { hasPackageProp, getPackageProp } = require( '../utils' );

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

let files = [];
if ( hasPackageProp( 'files' ) ) {
stdout.write(
'Using the `files` field from `package.json` to detect files:\n\n'
);
files = packlist();
} else {
stdout.write(
'Using Plugin Handbook best practices to discover files:\n\n'
);
// See https://developer.wordpress.org/plugins/plugin-basics/best-practices/#file-organization.
files = glob(
[
'admin/**',
'build/**',
'includes/**',
'languages/**',
'public/**',
`${ name }.php`,
'uninstall.php',
'block.json',
'changelog.*',
'license.*',
'readme.*',
],
{
caseSensitiveMatch: false,
}
);
}

files.forEach( ( file ) => {
stdout.write( ` Adding \`${ file }\`.\n` );
zip.addLocalFile( file, dirname( file ) );
} );

zip.writeZip( `./${ name }.zip` );
stdout.write( `\nDone. \`${ name }.zip\` is ready! 🎉\n` );

0 comments on commit 3d45a3b

Please sign in to comment.