Skip to content

Commit

Permalink
Let wp-scripts packages-update install packages for a specific vers…
Browse files Browse the repository at this point in the history
…ion of WordPress.

This is to mitigate the problems related to DEWP usage mentioned in WordPress#35630.

This allows a plugin developer to install to-be-extracted dependencies at the lowest version of their plugin targets. To be able to run local tests and linters against that version. To give some insight to the developer, on what is the lowest anticipated version of an individual package.
  • Loading branch information
tomalec committed Oct 27, 2021
1 parent 92bf5e6 commit 0796b6f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
12 changes: 11 additions & 1 deletion packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,17 @@ _Example:_

#### Advanced information

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.
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.

##### `--wpVersion`

You may use this command to update your local dependencies, to the versions delivered by a specific WordPress version. This is especially usefull when you use [`@wordpress/dependency-extraction-webpack-plugin`](https://www.npmjs.com/package/@wordpress/dependency-extraction-webpack-plugin). You may want to install the dependencies at versions used by the lowest WordPress version your plugin supports, for local testing, etc.

```sh
wp-scripts packages-update --wpVersion=5.6
```

Please note, this command updates all `@wordpress/*` packages that intersects your `package.json` and given WordPress version's. It will **not** check your `dependency-extraction-webpack-plugin` setup, to update only extracted ones.

### `start`

Expand Down
1 change: 1 addition & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"merge-deep": "^3.0.3",
"mini-css-extract-plugin": "^2.1.0",
"minimist": "^1.2.0",
"node-fetch": "^3.0.0",
"npm-package-json-lint": "^5.0.0",
"postcss": "^8.2.15",
"postcss-loader": "^6.1.1",
Expand Down
58 changes: 51 additions & 7 deletions packages/scripts/scripts/packages-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@
*/
const fs = require( 'fs' );
const spawn = require( 'cross-spawn' );
// CJS version of module-only: import fetch from 'node-fetch'`;
const fetch = ( ...args ) =>
import( 'node-fetch' ).then( ( { default: nodeFetch } ) =>
nodeFetch( ...args )
);

/**
* Internal dependencies
*/
const { getArgFromCLI } = require( '../utils' );

/**
* Constants
*/
const WORDPRESS_PACKAGES_PREFIX = '@wordpress/';

const sourceOfWPPackagesVersions = ( wpVersion ) =>
`https://raw.githubusercontent.com/WordPress/wordpress-develop/${ wpVersion }/package.json`;

function readJSONFile( fileName ) {
const data = fs.readFileSync( fileName, 'utf8' );
return JSON.parse( data );
Expand Down Expand Up @@ -43,11 +56,13 @@ function getPackageVersionDiff( initialPackageJSON, finalPackageJSON ) {
return diff.sort( ( a, b ) => a.dependency.localeCompare( b.dependency ) );
}

function updatePackagesToLatestVersion( packages ) {
const packagesWithLatest = packages.map(
( packageName ) => `${ packageName }@latest`
);
return spawn.sync( 'npm', [ 'install', ...packagesWithLatest, '--save' ], {
/**
* Installs the given npm packages.
*
* @param {Array<string>} packagesWithVersion List of package names and their versions in the `name@semver` format.
*/
function updatePackages( packagesWithVersion ) {
return spawn.sync( 'npm', [ 'install', ...packagesWithVersion, '--save' ], {
stdio: 'inherit',
} );
}
Expand All @@ -63,10 +78,39 @@ function outputPackageDiffReport( packageDiff ) {
);
}

function updatePackageJSON() {
/**
* Fetches `dev-` & `dependencies` for a given WordPress version.
*
* @param {string} wpVersion WordPress version to fetch the data for.
* @return {Object<string,string>} Concatenated dependencies and devDependencies.
*/
async function fetchRemoteWordPressPackages( wpVersion = 'master' ) {
const response = await fetch( sourceOfWPPackagesVersions( wpVersion ) );
const { dependencies, devDependencies } = await response.json();
return { ...dependencies, ...devDependencies };
}

async function updatePackageJSON() {
const initialPackageJSON = readJSONFile( 'package.json' );
const packages = getWordPressPackages( initialPackageJSON );
const result = updatePackagesToLatestVersion( packages );
const wpVersion = getArgFromCLI( '--wpVersion' );
let packagesWithVersion;
if ( wpVersion ) {
const remoteVersions = await fetchRemoteWordPressPackages( wpVersion );
// Get remote versions for intersecting/local packages.
const common = Object.entries(
remoteVersions
).filter( ( [ packageName ] ) => packages.includes( packageName ) );
// Stringify name & version in `npm install` format.
packagesWithVersion = common.map(
( [ packageName, version ] ) => `${ packageName }@${ version }`
);
} else {
packagesWithVersion = packages.map(
( packageName ) => `${ packageName }@latest`
);
}
const result = updatePackages( packagesWithVersion );
const finalPackageJSON = readJSONFile( 'package.json' );
outputPackageDiffReport(
getPackageVersionDiff( initialPackageJSON, finalPackageJSON )
Expand Down

0 comments on commit 0796b6f

Please sign in to comment.