-
Notifications
You must be signed in to change notification settings - Fork 3
fix(versioner): publish with npm via OIDC #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3697854
e96045b
3ce093e
85a0689
7eb5ac9
31e9983
60b10d0
4fb8a3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import 'source-map-support'; | ||
|
|
||
| import { dirname, join, resolve } from 'path'; | ||
| import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
| import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'fs'; | ||
| import { tmpdir } from 'os'; | ||
|
|
||
| import { getLog } from '@dot/log'; | ||
| import parser from 'conventional-commits-parser'; | ||
|
|
@@ -25,6 +26,8 @@ const parserOptions = { | |
| noteKeywords: ['BREAKING CHANGE', 'Breaking Change'] | ||
| }; | ||
| const reBreaking = new RegExp(`(${parserOptions.noteKeywords.join(')|(')})`); | ||
| const NPM_CLI_SPEC = 'npm@11.5.1'; | ||
| const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org'; | ||
|
|
||
| type Commit = parser.Commit<string | number | symbol>; | ||
|
|
||
|
|
@@ -151,9 +154,64 @@ const publish = async (cwd: string) => { | |
| return; | ||
| } | ||
|
|
||
| log.info(chalk`\n{cyan Publishing to NPM}`); | ||
| if (argv.registry != null && typeof argv.registry !== 'string') { | ||
| throw new TypeError( | ||
| `--registry must be a string (e.g. "${DEFAULT_NPM_REGISTRY}"), received ${typeof argv.registry}: ${String( | ||
| argv.registry | ||
| )}` | ||
| ); | ||
| } | ||
|
|
||
| const registryOverride = typeof argv.registry === 'string' ? argv.registry.trim() : null; | ||
| if (registryOverride != null && registryOverride.length === 0) { | ||
| throw new TypeError(`--registry must be a non-empty string (e.g. "${DEFAULT_NPM_REGISTRY}")`); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CharlieHelps are you checking the type of argv.registry here twice unnecessarily? this is very sloppy
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Investigating this PR comment about the duplicated
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expand this to see my work.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — yep, that was redundant. I refactored the Changes
Verification$ pnpm install --frozen-lockfile
$ pnpm -r build
# tsc: 3 errors in @dot/config, 4 errors in @dot/cdk (preexisting; build script exits 0)
$ pnpm lint
# ESLint: 1 warning (no-param-reassign) in packages/cdk/src/methods/security.ts (preexisting)
$ AWS_REGION=us-east-1 pnpm -r test --if-present
# Ava: 65 tests passed (3 packages) |
||
|
|
||
| const registry = (registryOverride || DEFAULT_NPM_REGISTRY).replace(/\/+$/, ''); | ||
|
|
||
| log.info(chalk`\n{cyan Publishing to registry}`); | ||
| log.info(chalk`{grey Registry:} ${registry}`); | ||
|
|
||
| await execa('pnpm', ['publish', '--no-git-checks'], { cwd, stdio: 'inherit' }); | ||
| const packDir = mkdtempSync(join(tmpdir(), 'versioner-pack-')); | ||
| try { | ||
| await execa('pnpm', ['pack', '--pack-destination', packDir], { cwd, stdio: 'inherit' }); | ||
|
|
||
| const tarballs = readdirSync(packDir) | ||
| .filter((file) => file.endsWith('.tgz')) | ||
| .sort(); | ||
|
|
||
| if (tarballs.length !== 1) { | ||
| throw new RangeError( | ||
| `Expected exactly 1 packed tarball in: ${packDir} for cwd=${cwd} (found ${ | ||
| tarballs.length | ||
| }): ${tarballs.join(', ')}` | ||
| ); | ||
| } | ||
|
|
||
| const tarballPath = join(packDir, tarballs[0]); | ||
| const hasOidcEnv = | ||
| !!process.env.ACTIONS_ID_TOKEN_REQUEST_URL && !!process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN; | ||
| const provenanceArgs = hasOidcEnv ? ['--provenance'] : []; | ||
|
|
||
| log.info(chalk`{grey Using npm CLI:} ${NPM_CLI_SPEC}`); | ||
|
|
||
| await execa( | ||
| 'pnpm', | ||
| [ | ||
| 'dlx', | ||
| NPM_CLI_SPEC, | ||
| 'publish', | ||
| '--no-git-checks', | ||
| '--registry', | ||
| registry, | ||
| ...provenanceArgs, | ||
| tarballPath | ||
| ], | ||
| { cwd, stdio: 'inherit' } | ||
|
shellscape marked this conversation as resolved.
|
||
| ); | ||
|
shellscape marked this conversation as resolved.
|
||
| } finally { | ||
| rmSync(packDir, { force: true, recursive: true }); | ||
| } | ||
| }; | ||
|
|
||
| const pull = async () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.