Skip to content

Commit

Permalink
Merge pull request #878 from ckeditor/ck/14135-async
Browse files Browse the repository at this point in the history
Internal: Make release tools asynchronous. Closes ckeditor/ckeditor5#14135.
  • Loading branch information
pomek committed May 16, 2023
2 parents 1b26a06 + 7341cf3 commit d53e284
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 446 deletions.
31 changes: 17 additions & 14 deletions packages/ckeditor5-dev-release-tools/lib/tasks/cleanuppackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const fs = require( 'fs-extra' );
const upath = require( 'upath' );
const { globSync } = require( 'glob' );
const { glob } = require( 'glob' );
const { logger } = require( '@ckeditor/ckeditor5-dev-utils' );

/**
Expand All @@ -26,30 +26,31 @@ const { logger } = require( '@ckeditor/ckeditor5-dev-utils' );
* @param {String} options.packagesDirectory Relative path to a location of packages to be cleaned up.
* @param {Array.<String>} [options.packageJsonFieldsToRemove] Fields to remove from `package.json`. If not set, a predefined list is used.
* @param {String} [options.cwd] Current working directory from which all paths will be resolved.
* @returns {Promise}
*/
module.exports = function cleanUpPackages( options ) {
module.exports = async function cleanUpPackages( options ) {
const log = logger();

log.info( 'Task: cleanUpPackages()' );

const { packagesDirectory, packageJsonFieldsToRemove, cwd } = parseOptions( options );

const packageJsonPaths = globSync( '*/package.json', {
const packageJsonPaths = await glob( '*/package.json', {
cwd: upath.join( cwd, packagesDirectory ),
nodir: true,
absolute: true
} );

for ( const packageJsonPath of packageJsonPaths ) {
const packagePath = upath.dirname( packageJsonPath );
const packageJson = fs.readJsonSync( packageJsonPath );
const packageJson = await fs.readJson( packageJsonPath );

log.info( `Cleaning up: "${ packagePath }".` );

cleanUpPackageDirectory( packageJson, packagePath );
await cleanUpPackageDirectory( packageJson, packagePath );
cleanUpPackageJson( packageJson, packageJsonFieldsToRemove );

fs.writeJsonSync( packageJsonPath, packageJson, { spaces: 2 } );
await fs.writeJson( packageJsonPath, packageJson, { spaces: 2 } );
}
};

Expand Down Expand Up @@ -81,11 +82,12 @@ function parseOptions( options ) {
*
* @param {Object} packageJson
* @param {String} packagePath
* @returns {Promise}
*/
function cleanUpPackageDirectory( packageJson, packagePath ) {
async function cleanUpPackageDirectory( packageJson, packagePath ) {
if ( packageJson.files ) {
// Find and remove files that don't match the `files` field in the `package.json`.
const files = globSync( '**', {
const files = await glob( '**', {
cwd: packagePath,
absolute: true,
nodir: true,
Expand All @@ -99,29 +101,30 @@ function cleanUpPackageDirectory( packageJson, packagePath ) {
} );

for ( const file of files ) {
fs.removeSync( file );
await fs.remove( file );
}
}

// Find and remove empty directories in the package directory.
const directories = globSync( '**/', {
const globResults = await glob( '**/', {
cwd: packagePath,
absolute: true,
dot: true
} )
} );
const directories = globResults
.map( path => upath.normalize( path ) )
.sort( sortPathsFromDeepestFirst );

for ( const directory of directories ) {
const isEmpty = fs.readdirSync( directory ).length === 0;
const isEmpty = ( await fs.readdir( directory ) ).length === 0;

if ( isEmpty ) {
fs.removeSync( directory );
await fs.remove( directory );
}
}

// Remove `node_modules`.
fs.removeSync( upath.join( packagePath, 'node_modules' ) );
await fs.remove( upath.join( packagePath, 'node_modules' ) );
}

/**
Expand Down
16 changes: 8 additions & 8 deletions packages/ckeditor5-dev-release-tools/lib/tasks/commitandtag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

'use strict';

const { shExec } = require( '@ckeditor/ckeditor5-dev-utils/lib/tools' );
const { tools } = require( '@ckeditor/ckeditor5-dev-utils' );
const { toUnix } = require( 'upath' );
const { globSync } = require( 'glob' );
const { glob } = require( 'glob' );

/**
* Creates a commit and a tag for specified version.
Expand All @@ -16,13 +16,13 @@ const { globSync } = require( 'glob' );
* @param {String} options.version The commit will contain this param in its message and the tag will have a `v` prefix.
* @param {Array.<String>} options.files Array of glob patterns for files to be added to the release commit.
* @param {String} [options.cwd=process.cwd()] Current working directory from which all paths will be resolved.
* @returns {Promise}
*/
module.exports = function commitAndTag( { version, files, cwd = process.cwd() } ) {
module.exports = async function commitAndTag( { version, files, cwd = process.cwd() } ) {
const normalizedCwd = toUnix( cwd );
const filePathsToAdd = globSync( files, { cwd: normalizedCwd, absolute: true, nodir: true } );
const filePathsToAddStr = filePathsToAdd.join( ' ' );
const filePathsToAdd = await glob( files, { cwd: normalizedCwd, absolute: true, nodir: true } );

shExec( `git add ${ filePathsToAddStr }`, { cwd: normalizedCwd } );
shExec( `git commit --message "Release: v${ version }."`, { cwd: normalizedCwd } );
shExec( `git tag v${ version }`, { cwd: normalizedCwd } );
await tools.shExec( `git add ${ filePathsToAdd.join( ' ' ) }`, { cwd: normalizedCwd, async: true } );
await tools.shExec( `git commit --message "Release: v${ version }."`, { cwd: normalizedCwd, async: true } );
await tools.shExec( `git tag v${ version }`, { cwd: normalizedCwd, async: true } );
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const fs = require( 'fs-extra' );
const { globSync } = require( 'glob' );
const { glob } = require( 'glob' );
const { logger } = require( '@ckeditor/ckeditor5-dev-utils' );
const normalizePath = require( '../utils/normalizepath' );

Expand All @@ -28,8 +28,9 @@ const normalizePath = require( '../utils/normalizepath' );
* @param {String} [options.packagesDirectory] Relative path to a location of packages to update their dependencies. If not specified,
* only the root package is checked.
* @param {String} [options.cwd=process.cwd()] Current working directory from which all paths will be resolved.
* @returns {Promise}
*/
module.exports = function updateDependencies( options ) {
module.exports = async function updateDependencies( options ) {
const log = logger();

log.info( 'Task: updateDependencies()' );
Expand All @@ -55,18 +56,18 @@ module.exports = function updateDependencies( options ) {
globPatterns.push( packagesDirectoryPattern );
}

const pkgJsonPaths = globSync( globPatterns, globOptions );
const pkgJsonPaths = await glob( globPatterns, globOptions );

for ( const pkgJsonPath of pkgJsonPaths ) {
log.info( `Updating dependencies in "${ pkgJsonPath }".` );

const pkgJson = fs.readJsonSync( pkgJsonPath );
const pkgJson = await fs.readJson( pkgJsonPath );

updateVersion( version, shouldUpdateVersionCallback, pkgJson.dependencies );
updateVersion( version, shouldUpdateVersionCallback, pkgJson.devDependencies );
updateVersion( version, shouldUpdateVersionCallback, pkgJson.peerDependencies );

fs.writeJsonSync( pkgJsonPath, pkgJson, { spaces: 2 } );
await fs.writeJson( pkgJsonPath, pkgJson, { spaces: 2 } );
}
};

Expand Down
62 changes: 38 additions & 24 deletions packages/ckeditor5-dev-release-tools/lib/tasks/updateversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

'use strict';

const { globSync } = require( 'glob' );
const { glob } = require( 'glob' );
const fs = require( 'fs-extra' );
const semver = require( 'semver' );
const { normalizeTrim, toUnix, dirname } = require( 'upath' );
const { normalizeTrim, toUnix, dirname, join } = require( 'upath' );
const { tools } = require( '@ckeditor/ckeditor5-dev-utils' );
const getPackageJson = require( '../utils/getpackagejson' );

/**
* The purpose of the script is to update the version of a root package found in the current working
Expand All @@ -28,30 +27,44 @@ const getPackageJson = require( '../utils/getpackagejson' );
* @param {String} [options.packagesDirectory] Relative path to a location of packages to update. If not specified,
* only the root package is checked.
* @param {String} [options.cwd=process.cwd()] Current working directory from which all paths will be resolved.
* @returns {Promise}
*/
module.exports = function updateVersions( { packagesDirectory, version, cwd = process.cwd() } ) {
module.exports = async function updateVersions( { packagesDirectory, version, cwd = process.cwd() } ) {
const normalizedCwd = toUnix( cwd );
const normalizedPackagesDir = packagesDirectory ? normalizeTrim( packagesDirectory ) : null;
const globPatterns = [ 'package.json' ];

if ( packagesDirectory ) {
globPatterns.push( normalizedPackagesDir + '/*/package.json' );
}

const pkgJsonPaths = globSync( globPatterns, { cwd: normalizedCwd, absolute: true, nodir: true } );
const globPatterns = getGlobPatterns( normalizedPackagesDir );
const pkgJsonPaths = await glob( globPatterns, { cwd: normalizedCwd, absolute: true, nodir: true } );
const randomPackagePath = getRandomPackagePath( pkgJsonPaths, normalizedPackagesDir );

checkIfVersionIsValid( version, getPackageJson( normalizedCwd ).version );
checkVersionAvailability( version, getPackageJson( randomPackagePath ).name );
const rootPackageJson = join( normalizedCwd, 'package.json' );
const randomPackageJson = join( randomPackagePath, 'package.json' );

checkIfVersionIsValid( version, ( await fs.readJson( rootPackageJson ) ).version );
await checkVersionAvailability( version, ( await fs.readJson( randomPackageJson ) ).name );

for ( const pkgJsonPath of pkgJsonPaths ) {
const pkgJson = getPackageJson( pkgJsonPath );
const pkgJson = await fs.readJson( pkgJsonPath );

pkgJson.version = version;
fs.writeJsonSync( pkgJsonPath, pkgJson, { spaces: 2 } );
await fs.writeJson( pkgJsonPath, pkgJson, { spaces: 2 } );
}
};

/**
* @param {String|null} packagesDirectory
* @returns {Array.<String>}
*/
function getGlobPatterns( packagesDirectory ) {
const patterns = [ 'package.json' ];

if ( packagesDirectory ) {
patterns.push( packagesDirectory + '/*/package.json' );
}

return patterns;
}

/**
* @param {Array.<String>} pkgJsonPaths
* @param {String|null} packagesDirectory
Expand Down Expand Up @@ -89,15 +102,16 @@ function checkIfVersionIsValid( newVersion, currentVersion ) {
*
* @param {String} version
* @param {String} packageName
* @returns {Promise}
*/
function checkVersionAvailability( version, packageName ) {
try {
tools.shExec( `npm show ${ packageName }@${ version } version`, { verbosity: 'silent' } );

throw new Error( `Provided version ${ version } is already used in npm by ${ packageName }.` );
} catch ( e ) {
if ( !e.toString().includes( 'is not in this registry' ) ) {
throw e;
}
}
async function checkVersionAvailability( version, packageName ) {
return tools.shExec( `npm show ${ packageName }@${ version } version`, { verbosity: 'silent', async: true } )
.then( () => {
throw new Error( `Provided version ${ version } is already used in npm by ${ packageName }.` );
} )
.catch( err => {
if ( !err.toString().includes( 'is not in this registry' ) ) {
throw err;
}
} );
}
Loading

0 comments on commit d53e284

Please sign in to comment.