Skip to content

Commit

Permalink
Merge pull request #1040 from ckeditor/i/17333
Browse files Browse the repository at this point in the history
Fix (release-tools): The `publishPackages()` task should not throw an error after trying to publish packages after reaching an attempted limit. Instead, it should verify if the last try was successfully completed and throw the error if it wasn't. Closes ckeditor/ckeditor5#17333.

Other (release-tools): Increased the attempts limit from 3 to 5.

Other (release-tools): Created a decorated version of utils (`manifest()`, `packument()`) exposed by the `pacote` package. It prevents from using any cache when checking the npm registry. Direct calls have been replaced with the decorated version.
  • Loading branch information
pomek authored Oct 28, 2024
2 parents 7910f50 + 0f2a58f commit 937c093
Show file tree
Hide file tree
Showing 10 changed files with 721 additions and 412 deletions.
76 changes: 37 additions & 39 deletions packages/ckeditor5-dev-release-tools/lib/tasks/publishpackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ import checkVersionAvailability from '../utils/checkversionavailability.js';
import findPathsToPackages from '../utils/findpathstopackages.js';

/**
* The purpose of the script is to validate the packages prepared for the release and then release them on npm.
* The purpose of the script is to publish the prepared packages. However, before, it executes a few checks that
* prevent from publishing an incomplete package.
*
* The validation contains the following steps in each package:
* - User must be logged to npm on the specified account.
* - The package directory must contain `package.json` file.
* - All other files expected to be released must exist in the package directory.
* - The npm tag must match the tag calculated from the package version.
* The validation contains the following steps:
*
* - A user (a CLI session) must be logged to npm on the specified account (`npmOwner`).
* - A package directory must contain `package.json` file.
* - All files defined in the `optionalEntryPointPackages` option must exist in a package directory.
* - An npm tag (dist-tag) must match the tag calculated from the package version.
* A stable release can be also published as `next` or `staging.
*
* When the validation for each package passes, packages are published on npm. Optional callback is called for confirmation whether to
* continue.
*
* If a package has already been published, the script does not try to publish it again. Instead, it treats the package as published.
* Whenever a communication between the script and npm fails, it tries to re-publish a package (up to three attempts).
* Whenever a communication between the script and npm fails, it tries to re-publish a package (up to five attempts).
*
* @param {object} options
* @param {string} options.packagesDirectory Relative path to a location of packages to release.
Expand Down Expand Up @@ -63,26 +66,43 @@ export default async function publishPackages( options ) {
optionalEntryPointPackages = [],
cwd = process.cwd(),
concurrency = 2,
attempts = 3
attempts = 5
} = options;

const remainingAttempts = attempts - 1;
await assertNpmAuthorization( npmOwner );

// Find packages that would be published...
const packagePaths = await findPathsToPackages( cwd, packagesDirectory );

await assertPackages( packagePaths, { requireEntryPoint, optionalEntryPointPackages } );
await assertFilesToPublish( packagePaths, optionalEntries );
await assertNpmTag( packagePaths, npmTag );
// ...and filter out those that have already been processed.
// In other words, check whether a version per package (it's read from a `package.json` file)
// is not available. Otherwise, a package is ignored.
await removeAlreadyPublishedPackages( packagePaths );

// Once again, find packages to publish after the filtering operation.
const packagesToProcess = await findPathsToPackages( cwd, packagesDirectory );

if ( !packagesToProcess.length ) {
listrTask.output = 'All packages have been published.';

return Promise.resolve();
}

// No more attempts. Abort.
if ( attempts <= 0 ) {
throw new Error( 'Some packages could not be published.' );
}

await assertPackages( packagesToProcess, { requireEntryPoint, optionalEntryPointPackages } );
await assertFilesToPublish( packagesToProcess, optionalEntries );
await assertNpmTag( packagesToProcess, npmTag );

const shouldPublishPackages = confirmationCallback ? await confirmationCallback() : true;

if ( !shouldPublishPackages ) {
return Promise.resolve();
}

await removeAlreadyPublishedPackages( packagePaths );

await executeInParallel( {
cwd,
packagesDirectory,
Expand All @@ -95,39 +115,17 @@ export default async function publishPackages( options ) {
concurrency
} );

const packagePathsAfterPublishing = await findPathsToPackages( cwd, packagesDirectory );

// All packages have been published. No need for re-executing.
if ( !packagePathsAfterPublishing.length ) {
return Promise.resolve();
}

// No more attempts. Abort.
if ( remainingAttempts <= 0 ) {
throw new Error( 'Some packages could not be published.' );
}

listrTask.output = 'Let\'s give an npm a moment for taking a breath (~10 sec)...';

// Let's give an npm a moment for taking a breath...
await wait( 1000 * 10 );

listrTask.output = 'Done. Let\'s continue.';
listrTask.output = 'Done. Let\'s continue. Re-executing.';

// ...and try again.
return publishPackages( {
packagesDirectory,
npmOwner,
listrTask,
signal,
npmTag,
optionalEntries,
requireEntryPoint,
optionalEntryPointPackages,
cwd,
concurrency,
...options,
confirmationCallback: null, // Do not ask again if already here.
attempts: remainingAttempts
attempts: attempts - 1
} );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

import pacote from 'pacote';
import { manifest } from './pacotecacheless.js';

/**
* Checks if the provided version for the package exists in the npm registry.
Expand All @@ -15,9 +15,9 @@ import pacote from 'pacote';
* @returns {Promise}
*/
export default async function checkVersionAvailability( version, packageName ) {
return pacote.manifest( `${ packageName }@${ version }`, { cache: null, preferOnline: true } )
return manifest( `${ packageName }@${ version }` )
.then( () => {
// If `pacote.manifest` resolves, a package with the given version exists.
// If `manifest` resolves, a package with the given version exists.
return false;
} )
.catch( () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import semver from 'semver';
import pacote from 'pacote';
import { manifest } from './pacotecacheless.js';

/**
* This util aims to verify if the given `packageName` can be published with the given `version` on the `npmTag`.
Expand All @@ -15,7 +15,7 @@ import pacote from 'pacote';
* @returns {Promise.<boolean>}
*/
export default async function isVersionPublishableForTag( packageName, version, npmTag ) {
const npmVersion = await pacote.manifest( `${ packageName }@${ npmTag }`, { cache: null, preferOnline: true } )
const npmVersion = await manifest( `${ packageName }@${ npmTag }` )
.then( ( { version } ) => version )
// An `npmTag` does not exist, or it's a first release of a package.
.catch( () => null );
Expand Down
33 changes: 33 additions & 0 deletions packages/ckeditor5-dev-release-tools/lib/utils/pacotecacheless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

import os from 'os';
import { randomUUID } from 'crypto';
import upath from 'upath';
import fs from 'fs-extra';
import pacote from 'pacote';

export const manifest = cacheLessPacoteFactory( pacote.manifest );
export const packument = cacheLessPacoteFactory( pacote.packument );

function cacheLessPacoteFactory( callback ) {
return async ( description, options = {} ) => {
const uuid = randomUUID();
const cacheDir = upath.join( os.tmpdir(), `pacote--${ uuid }` );

await fs.ensureDir( cacheDir );

try {
return await callback( description, {
...options,
cache: cacheDir,
memoize: false,
preferOnline: true
} );
} finally {
await fs.remove( cacheDir );
}
};
}
4 changes: 2 additions & 2 deletions packages/ckeditor5-dev-release-tools/lib/utils/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { tools } from '@ckeditor/ckeditor5-dev-utils';
import pacote from 'pacote';
import { packument } from './pacotecacheless.js';
import getChangelog from './getchangelog.js';
import getPackageJson from './getpackagejson.js';

Expand Down Expand Up @@ -38,7 +38,7 @@ export function getLastFromChangelog( cwd = process.cwd() ) {
export function getLastPreRelease( releaseIdentifier, cwd = process.cwd() ) {
const packageName = getPackageJson( cwd ).name;

return pacote.packument( packageName, { cache: null, preferOnline: true } )
return packument( packageName )
.then( result => {
const lastVersion = Object.keys( result.versions )
.filter( version => version.startsWith( releaseIdentifier ) )
Expand Down
Loading

0 comments on commit 937c093

Please sign in to comment.