|
| 1 | +import { Plugin } from 'release-it'; |
| 2 | + |
| 3 | +const NPM_BASE_URL = 'https://www.npmjs.com'; |
| 4 | +const NPM_PUBLIC_PATH = 'package'; |
| 5 | + |
| 6 | +class PrePublishReleaseItPlugin extends Plugin { |
| 7 | + constructor(...args) { |
| 8 | + super(...args); |
| 9 | + |
| 10 | + this.registerPrompts({ |
| 11 | + prePublishPrompt: { |
| 12 | + type: 'confirm', |
| 13 | + message: (context) => |
| 14 | + `Are you sure you want to publish ${context.npm.name}${ |
| 15 | + context.isPreRelease ? `@${context.preReleaseId}` : '@latest' |
| 16 | + } to npm?`, |
| 17 | + default: true, |
| 18 | + }, |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + disablePlugin() { |
| 23 | + return 'npm'; |
| 24 | + } |
| 25 | + |
| 26 | + getName() { |
| 27 | + return 'plonePrePublish'; |
| 28 | + } |
| 29 | + |
| 30 | + getPackageUrl() { |
| 31 | + const baseUrl = NPM_BASE_URL; |
| 32 | + const publicPath = NPM_PUBLIC_PATH; |
| 33 | + return `${baseUrl}/${publicPath}/${this.getName()}`; |
| 34 | + } |
| 35 | + |
| 36 | + async beforeRelease() { |
| 37 | + const context = this.config.getContext(); |
| 38 | + const tag = context.isPreRelease ? context.preReleaseId : 'latest'; |
| 39 | + const dryRunArg = this.config.isDryRun ? '--dry-run' : ''; |
| 40 | + const pluginOptions = this.config.options?.plonePrePublish; |
| 41 | + const publish = pluginOptions ? pluginOptions.publish ?? true : true; |
| 42 | + if (publish) { |
| 43 | + await this.step({ |
| 44 | + enabled: true, |
| 45 | + task: () => |
| 46 | + this.exec( |
| 47 | + `pnpm publish${tag ? ` --tag ${tag}` : ''}${dryRunArg ? ` ${dryRunArg}` : ''} --no-git-checks`, |
| 48 | + { |
| 49 | + options: { write: false }, |
| 50 | + }, |
| 51 | + ).then(() => { |
| 52 | + this.setContext({ isReleased: true }); |
| 53 | + }), |
| 54 | + label: 'Publishing to npm...', |
| 55 | + prompt: 'prePublishPrompt', |
| 56 | + }); |
| 57 | + } else { |
| 58 | + this.setContext({ isReleased: true }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + afterRelease() { |
| 63 | + const { isReleased } = this.getContext(); |
| 64 | + if (isReleased) { |
| 65 | + this.log.log(`🔗 ${this.getPackageUrl()}`); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export default PrePublishReleaseItPlugin; |
0 commit comments