2022-06-23 00:09:43 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-02-08 09:23:13 +00:00
|
|
|
import { existsSync, readFileSync } from 'fs';
|
2022-07-14 02:03:37 +00:00
|
|
|
import { execSync } from 'child_process';
|
|
|
|
import { gt as greaterVersionThan } from 'semver';
|
2022-06-23 00:09:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { MONOREPO_ROOT, excludedPackages } from './const';
|
|
|
|
|
2024-02-08 09:23:13 +00:00
|
|
|
/**
|
|
|
|
* Get pnpm's package data.
|
|
|
|
*
|
|
|
|
* @param {string} name package name.
|
|
|
|
* @return {Object|Array} A package object or an array of package objects.
|
|
|
|
*/
|
|
|
|
const getPackageData = ( name?: string ) => {
|
|
|
|
try {
|
|
|
|
const rawData = execSync( 'pnpm m ls --json --depth=-1', {
|
|
|
|
cwd: MONOREPO_ROOT,
|
|
|
|
encoding: 'utf-8',
|
|
|
|
} );
|
|
|
|
|
|
|
|
const data = JSON.parse( rawData );
|
|
|
|
|
|
|
|
if ( ! name ) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.find( ( p: { name: string } ) => p.name === name );
|
|
|
|
} catch ( e ) {
|
|
|
|
if ( e instanceof Error ) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log( e );
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if package is JS or PHP.
|
|
|
|
*
|
|
|
|
* @param {string} name package name.
|
|
|
|
* @return {undefined|string} Package type js or php.
|
|
|
|
*/
|
|
|
|
export const getPackageType = ( name: string ) => {
|
|
|
|
const packageData = getPackageData( name );
|
|
|
|
if ( ! packageData ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( packageData.path.includes( 'packages/js' ) ) {
|
|
|
|
return 'js';
|
|
|
|
}
|
|
|
|
if ( packageData.path.includes( 'packages/php' ) ) {
|
|
|
|
return 'php';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-23 00:09:43 +00:00
|
|
|
/**
|
|
|
|
* Get filepath for a given package name.
|
|
|
|
*
|
|
|
|
* @param {string} name package name.
|
|
|
|
* @return {string} Absolute path for the package.
|
|
|
|
*/
|
2024-02-08 09:23:13 +00:00
|
|
|
export const getFilepathFromPackageName = ( name: string ): string => {
|
|
|
|
const packageData = getPackageData( name );
|
|
|
|
return packageData?.path;
|
|
|
|
};
|
2022-06-23 00:09:43 +00:00
|
|
|
|
|
|
|
/**
|
2022-07-14 02:03:37 +00:00
|
|
|
* Get a package's package.json file in JSON format.
|
2022-06-23 00:09:43 +00:00
|
|
|
*
|
|
|
|
* @param {string} name package name.
|
2022-07-14 02:03:37 +00:00
|
|
|
* @return {Object|false} JSON object or false if it fails.
|
2022-06-23 00:09:43 +00:00
|
|
|
*/
|
2022-07-14 02:03:37 +00:00
|
|
|
export const getPackageJson = ( name: string ) => {
|
2022-06-23 00:09:43 +00:00
|
|
|
const filepath = getFilepathFromPackageName( name );
|
|
|
|
const packageJsonFilepath = `${ filepath }/package.json`;
|
|
|
|
const packageJsonExists = existsSync( packageJsonFilepath );
|
|
|
|
if ( ! packageJsonExists ) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-14 02:03:37 +00:00
|
|
|
|
|
|
|
return JSON.parse( readFileSync( packageJsonFilepath, 'utf8' ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-02-08 09:23:13 +00:00
|
|
|
* Get a package's composer.json file in JSON format.
|
2022-07-14 02:03:37 +00:00
|
|
|
*
|
|
|
|
* @param {string} name package name.
|
2024-02-08 09:23:13 +00:00
|
|
|
* @return {Object|false} JSON object or false if it fails.
|
2022-07-14 02:03:37 +00:00
|
|
|
*/
|
2024-02-08 09:23:13 +00:00
|
|
|
export const getComposerJson = ( name: string ) => {
|
|
|
|
const filepath = getFilepathFromPackageName( name );
|
|
|
|
const composerJsonFilepath = `${ filepath }/composer.json`;
|
|
|
|
const composerJsonExists = existsSync( composerJsonFilepath );
|
|
|
|
if ( ! composerJsonExists ) {
|
2022-06-23 00:09:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:23:13 +00:00
|
|
|
return JSON.parse( readFileSync( composerJsonFilepath, 'utf8' ) );
|
2022-06-23 00:09:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all releaseable package names.
|
|
|
|
*
|
|
|
|
* @return {Array<string>} Package names.
|
|
|
|
*/
|
2024-02-08 09:23:13 +00:00
|
|
|
export const getAllPackages = (): Array< string > => {
|
|
|
|
const packageData = getPackageData();
|
|
|
|
if ( ! packageData ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return packageData
|
|
|
|
.map( ( p: { name: string } ) => p.name )
|
|
|
|
.filter( ( name: string ) => ! excludedPackages.includes( name ) );
|
2022-06-23 00:09:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate a package.
|
|
|
|
*
|
|
|
|
* @param {string} name package name.
|
|
|
|
* @param {Function} error Error logging function.
|
|
|
|
*/
|
|
|
|
export const validatePackage = (
|
|
|
|
name: string,
|
|
|
|
error: ( s: string ) => void
|
|
|
|
) => {
|
2024-02-08 09:23:13 +00:00
|
|
|
const packageData = getPackageData( name );
|
2022-06-23 00:09:43 +00:00
|
|
|
|
2024-02-08 09:23:13 +00:00
|
|
|
if ( ! packageData ) {
|
|
|
|
error( `${ name } is not a valid package.` );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( packageData.private ) {
|
2022-06-23 00:09:43 +00:00
|
|
|
error(
|
2024-02-08 09:23:13 +00:00
|
|
|
`${ name } is a private package, no need to prepare for a release.`
|
2022-06-23 00:09:43 +00:00
|
|
|
);
|
|
|
|
}
|
2024-02-08 09:23:13 +00:00
|
|
|
|
|
|
|
return true;
|
2022-06-23 00:09:43 +00:00
|
|
|
};
|
2022-07-14 02:03:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if an update is valid by comparing version numbers.
|
|
|
|
*
|
2023-06-29 01:28:19 +00:00
|
|
|
* @param {string} name package name.
|
|
|
|
* @param {boolean} initialRelease if package has not been released yet.
|
2022-07-14 02:03:37 +00:00
|
|
|
* @return {boolean} If an update is valid.
|
|
|
|
*/
|
2023-06-29 01:28:19 +00:00
|
|
|
export const isValidUpdate = (
|
|
|
|
name: string,
|
|
|
|
initialRelease: boolean
|
|
|
|
): boolean => {
|
2022-07-14 02:03:37 +00:00
|
|
|
const packageJson = getPackageJson( name );
|
|
|
|
|
|
|
|
if ( ! packageJson ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextVersion = packageJson.version;
|
|
|
|
|
|
|
|
if ( ! nextVersion ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-29 01:28:19 +00:00
|
|
|
if ( initialRelease ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-22 00:08:11 +00:00
|
|
|
const npmVersion = execSync( `pnpm view ${ name } version`, {
|
2022-07-14 02:03:37 +00:00
|
|
|
encoding: 'utf-8',
|
|
|
|
} );
|
|
|
|
|
|
|
|
return greaterVersionThan( nextVersion.trim(), npmVersion.trim() );
|
|
|
|
};
|