Package release: Handle new package validation (#39008)

This commit is contained in:
Paul Sealock 2023-06-29 13:28:19 +12:00 committed by GitHub
parent 55b2c67eda
commit b6b093225e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 9 deletions

View File

@ -3,7 +3,7 @@ on:
workflow_dispatch: workflow_dispatch:
inputs: inputs:
packages: packages:
description: 'Enter a specific package to release, or packages separated by commas, ie @woocommerce/components,@woocommerce/number. Leaving this input to the default "-a" will prepare to release all eligible packages. When releasing a package for the first time, pass the "--initialRelease" flag.' description: 'Enter a specific package to release, or packages separated by commas, ie @woocommerce/components,@woocommerce/number. Leaving this input to the default "-a" will prepare to release all eligible packages.'
required: false required: false
default: '-a' default: '-a'

View File

@ -2,6 +2,8 @@
When a package contains sufficient changes to justify a release to [NPM](https://www.npmjs.com/), follow these instructions to create a new release from the monorepo. When a package contains sufficient changes to justify a release to [NPM](https://www.npmjs.com/), follow these instructions to create a new release from the monorepo.
Below are instructions for releasing using Github Workflows (recommended) and the command line. When releasing packages for the first time, release from the command line.
## Release packages using Github Workflows (recommended) ## Release packages using Github Workflows (recommended)
### Prepare packages ### Prepare packages
@ -36,7 +38,7 @@ In order to prepare a package for release, a changelog will need to be compiled
./tools/package-release/bin/dev prepare -a ./tools/package-release/bin/dev prepare -a
``` ```
When making an initial release for a new package, pass the `--initialRelease` flag to signify a new release for a new package. When making an initial release for a new package, pass the `--initial-release` flag to signify a new release for a new package.
2. Create a pull request with the resulting changes and merge it. 2. Create a pull request with the resulting changes and merge it.
@ -48,6 +50,8 @@ See more about the prepare script using `./tools/package-release/bin/dev publish
2. Run the release script from monorepo root, first as a dry run. 2. Run the release script from monorepo root, first as a dry run.
When making an initial release for a new package, pass the `--initial-release` flag to signify a new release for a new package.
``` ```
./tools/package-release/bin/dev publish -a --dry-run ./tools/package-release/bin/dev publish -a --dry-run
``` ```

View File

@ -49,7 +49,7 @@ export default class PackagePrepare extends Command {
default: false, default: false,
description: 'Perform prepare function on all packages.', description: 'Perform prepare function on all packages.',
} ), } ),
initialRelease: Flags.boolean( { 'initial-release': Flags.boolean( {
default: false, default: false,
description: "Create a package's first release to NPM", description: "Create a package's first release to NPM",
} ), } ),
@ -72,7 +72,7 @@ export default class PackagePrepare extends Command {
const packages = args.packages.split( ',' ); const packages = args.packages.split( ',' );
if ( flags.initialRelease && packages.length > 1 ) { if ( flags[ 'initial-release' ] && packages.length > 1 ) {
this.error( this.error(
'Please release only a single package when making an initial release' 'Please release only a single package when making an initial release'
); );
@ -82,7 +82,7 @@ export default class PackagePrepare extends Command {
validatePackage( name, ( e: string ): void => this.error( e ) ) validatePackage( name, ( e: string ): void => this.error( e ) )
); );
await this.preparePackages( packages, flags.initialRelease ); await this.preparePackages( packages, flags[ 'initial-release' ] );
} }
/** /**

View File

@ -59,6 +59,10 @@ export default class PackageRelease extends Command {
description: 'Skip pnpm install', description: 'Skip pnpm install',
default: false, default: false,
} ), } ),
'initial-release': Flags.boolean( {
default: false,
description: "Create a package's first release to NPM",
} ),
}; };
/** /**
@ -112,13 +116,17 @@ export default class PackageRelease extends Command {
*/ */
private publishPackages( private publishPackages(
packages: Array< string >, packages: Array< string >,
{ 'dry-run': dryRun, branch }: { 'dry-run': boolean; branch: string } {
'dry-run': dryRun,
branch,
'initial-release': initialRelease,
}: { 'dry-run': boolean; branch: string; 'initial-release': boolean }
) { ) {
packages.forEach( ( name ) => { packages.forEach( ( name ) => {
try { try {
const verb = dryRun ? 'Performing dry run of' : 'Publishing'; const verb = dryRun ? 'Performing dry run of' : 'Publishing';
CliUx.ux.action.start( `${ verb } ${ name }` ); CliUx.ux.action.start( `${ verb } ${ name }` );
if ( isValidUpdate( name ) ) { if ( isValidUpdate( name, initialRelease ) ) {
const cwd = getFilepathFromPackageName( name ); const cwd = getFilepathFromPackageName( name );
execSync( execSync(
`SKIP_TURBO=true pnpm publish ${ `SKIP_TURBO=true pnpm publish ${

View File

@ -131,9 +131,13 @@ export const validatePackage = (
* Determine if an update is valid by comparing version numbers. * Determine if an update is valid by comparing version numbers.
* *
* @param {string} name package name. * @param {string} name package name.
* @param {boolean} initialRelease if package has not been released yet.
* @return {boolean} If an update is valid. * @return {boolean} If an update is valid.
*/ */
export const isValidUpdate = ( name: string ): boolean => { export const isValidUpdate = (
name: string,
initialRelease: boolean
): boolean => {
const packageJson = getPackageJson( name ); const packageJson = getPackageJson( name );
if ( ! packageJson ) { if ( ! packageJson ) {
@ -146,6 +150,10 @@ export const isValidUpdate = ( name: string ): boolean => {
return false; return false;
} }
if ( initialRelease ) {
return true;
}
const npmVersion = execSync( `pnpm view ${ name } version`, { const npmVersion = execSync( `pnpm view ${ name } version`, {
encoding: 'utf-8', encoding: 'utf-8',
} ); } );