@ckeditor/ckeditor5-dev-release-tools 62.2.0 → 62.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -33,3 +33,4 @@ export { default as validateRepositoryToRelease } from './utils/validatereposito
33
33
  export { default as getNpmTagFromVersion } from './utils/getnpmtagfromversion.js';
34
34
  export { default as provideToken } from './utils/providetoken.js';
35
35
  export { default as validateGithubToken } from './utils/validategithubtoken.js';
36
+ export { default as getNpmIdToken } from './utils/getnpmidtoken.js';
@@ -119,7 +119,8 @@ export default async function publishPackages( options ) {
119
119
  listrTask,
120
120
  taskToExecute: publishPackageOnNpmCallback,
121
121
  taskOptions: {
122
- npmTag
122
+ npmTag,
123
+ useOidc
123
124
  },
124
125
  signal,
125
126
  concurrency,
@@ -10,6 +10,7 @@ import { styleText, promisify } from 'node:util';
10
10
  import columns from 'cli-columns';
11
11
  import shellEscape from 'shell-escape';
12
12
  import assertNpmAuthorization from '../utils/assertnpmauthorization.js';
13
+ import getNpmIdToken from '../utils/getnpmidtoken.js';
13
14
  import { exec } from 'node:child_process';
14
15
 
15
16
  const execPromise = promisify( exec );
@@ -52,7 +53,14 @@ export default async function reassignNpmTags( options ) {
52
53
 
53
54
  const updateTagPromises = packages.map( async packageName => {
54
55
  const command = `npm dist-tag add ${ shellEscape( [ packageName ] ) }@${ shellEscape( [ version ] ) } ${ npmTag }`;
55
- const updateLatestTagRetryable = retry( () => execPromise( command ) );
56
+ const updateLatestTagRetryable = retry( async () => {
57
+ if ( !useOidc ) {
58
+ return execPromise( command );
59
+ }
60
+
61
+ // The calls run in parallel, so the fresh token is passed only to this npm process.
62
+ return execPromise( command, { env: { ...process.env, NPM_ID_TOKEN: await getNpmIdToken() } } );
63
+ } );
56
64
  await updateLatestTagRetryable()
57
65
  .then( response => {
58
66
  if ( response.stdout ) {
@@ -4,12 +4,13 @@
4
4
  */
5
5
 
6
6
  import { tools } from '@ckeditor/ckeditor5-dev-utils';
7
+ import getNpmIdToken from './getnpmidtoken.js';
7
8
 
8
9
  /**
9
10
  * Checks whether a user is logged to npm as the provided account name.
10
11
  *
11
- * When using npm Trusted Publishing (`useOidc`), the check verifies that the `NPM_ID_TOKEN` environment variable is set
12
- * instead. npm exchanges the OIDC token only during supported operations, such as `npm publish` or `npm dist-tag`,
12
+ * When using npm Trusted Publishing (`useOidc`), the check verifies that an OIDC token can be obtained instead
13
+ * (see `getNpmIdToken()`). npm exchanges the OIDC token only during supported operations, such as `npm publish` or `npm dist-tag`,
13
14
  * and `npm whoami` does not reflect Trusted Publishing authentication.
14
15
  *
15
16
  * @param {string} [npmOwner] Expected npm account name that should be logged into npm. Required unless `useOidc` is enabled.
@@ -19,10 +20,9 @@ import { tools } from '@ckeditor/ckeditor5-dev-utils';
19
20
  */
20
21
  export default async function assertNpmAuthorization( npmOwner, { useOidc = false } = {} ) {
21
22
  if ( useOidc ) {
22
- // Only the token presence can be verified upfront.
23
- if ( !process.env.NPM_ID_TOKEN ) {
24
- throw new Error( 'The "NPM_ID_TOKEN" environment variable is required when using npm Trusted Publishing (OIDC).' );
25
- }
23
+ // A token is requested again before each npm command. Requesting one here stops the release before any package
24
+ // is processed when no token can be obtained (neither from the CircleCI CLI nor from `NPM_ID_TOKEN`).
25
+ await getNpmIdToken();
26
26
 
27
27
  return;
28
28
  }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md.
4
+ */
5
+
6
+ import { execFile } from 'node:child_process';
7
+
8
+ /**
9
+ * The audience that npm expects in an OIDC token used in the npm Trusted Publishing token exchange.
10
+ */
11
+ export const NPM_OIDC_AUDIENCE = 'npm:registry.npmjs.org';
12
+
13
+ /**
14
+ * Returns an OIDC token that the npm CLI exchanges for a short-lived npm token (npm Trusted Publishing).
15
+ *
16
+ * npm accepts a CircleCI OIDC token in the exchange only for a few minutes after it was issued, even though the token
17
+ * itself is valid longer. A release that publishes many packages takes longer than that. Hence, on CircleCI, a fresh
18
+ * token is requested each time, right before an npm command that needs it.
19
+ *
20
+ * When the CircleCI CLI is not available, or the command does not run on CircleCI, the `NPM_ID_TOKEN` environment
21
+ * variable is used, if it is set.
22
+ *
23
+ * @returns {Promise<string>}
24
+ */
25
+ export default async function getNpmIdToken() {
26
+ if ( process.env.CIRCLECI ) {
27
+ const token = await requestCircleciToken();
28
+
29
+ if ( token ) {
30
+ return token;
31
+ }
32
+ }
33
+
34
+ if ( process.env.NPM_ID_TOKEN ) {
35
+ return process.env.NPM_ID_TOKEN;
36
+ }
37
+
38
+ throw new Error(
39
+ 'Cannot get an OIDC token for npm Trusted Publishing (OIDC). ' +
40
+ 'Run the command on CircleCI or set the "NPM_ID_TOKEN" environment variable.'
41
+ );
42
+ }
43
+
44
+ /**
45
+ * Requests a fresh OIDC token using the CircleCI CLI. Resolves `null` when the CLI is not installed.
46
+ *
47
+ * @returns {Promise<string|null>}
48
+ */
49
+ async function requestCircleciToken() {
50
+ const args = [ 'run', 'oidc', 'get', '--claims', JSON.stringify( { aud: NPM_OIDC_AUDIENCE } ) ];
51
+ let stdout;
52
+
53
+ try {
54
+ stdout = await new Promise( ( resolve, reject ) => {
55
+ execFile( 'circleci', args, ( error, output ) => error ? reject( error ) : resolve( output ) );
56
+ } );
57
+ } catch ( error ) {
58
+ if ( error.code === 'ENOENT' ) {
59
+ return null;
60
+ }
61
+
62
+ throw error;
63
+ }
64
+
65
+ const token = String( stdout ).trim();
66
+
67
+ if ( !token ) {
68
+ throw new Error( 'The `circleci run oidc get` command returned an empty OIDC token.' );
69
+ }
70
+
71
+ return token;
72
+ }
@@ -9,17 +9,26 @@
9
9
  * @param {string} packagePath
10
10
  * @param {object} taskOptions
11
11
  * @param {string} taskOptions.npmTag
12
+ * @param {boolean} [taskOptions.useOidc=false] Whether to request a fresh OIDC token for npm Trusted Publishing before publishing.
12
13
  * @returns {Promise}
13
14
  */
14
15
  export default async function publishPackageOnNpmCallback( packagePath, taskOptions ) {
15
- const { tools } = await import( '@ckeditor/ckeditor5-dev-utils' );
16
+ // The callback runs in a worker thread, so dependencies are imported dynamically and resolve from the project root.
17
+ const { exec } = await import( 'node:child_process' );
16
18
  const { rm } = await import( 'node:fs/promises' );
19
+ const { getNpmIdToken } = await import( '@ckeditor/ckeditor5-dev-release-tools' );
17
20
 
18
21
  try {
19
- await tools.shExec( `npm publish --access=public --tag ${ taskOptions.npmTag }`, {
20
- cwd: packagePath,
21
- async: true,
22
- verbosity: 'silent'
22
+ const env = taskOptions.useOidc ? { ...process.env, NPM_ID_TOKEN: await getNpmIdToken() } : process.env;
23
+
24
+ if ( !/^[a-z0-9][a-z0-9._-]*$/i.test( taskOptions.npmTag ) ) {
25
+ throw new Error( `Invalid npm tag: "${ taskOptions.npmTag }".` );
26
+ }
27
+
28
+ const command = `npm publish --access=public --tag ${ taskOptions.npmTag }`;
29
+
30
+ await new Promise( ( resolve, reject ) => {
31
+ exec( command, { cwd: packagePath, env }, error => error ? reject( error ) : resolve() );
23
32
  } );
24
33
 
25
34
  await rm( packagePath, { recursive: true, force: true } );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-dev-release-tools",
3
- "version": "62.2.0",
3
+ "version": "62.3.0",
4
4
  "description": "Tools used for releasing CKEditor 5 and related packages.",
5
5
  "keywords": [],
6
6
  "author": "CKSource (http://cksource.com/)",
@@ -22,7 +22,7 @@
22
22
  "lib"
23
23
  ],
24
24
  "dependencies": {
25
- "@ckeditor/ckeditor5-dev-utils": "^62.2.0",
25
+ "@ckeditor/ckeditor5-dev-utils": "^62.3.0",
26
26
  "@octokit/rest": "^22.0.1",
27
27
  "cli-columns": "^4.0.0",
28
28
  "glob": "^13.0.6",