|
| 1 | +import { BuilderContext, createBuilder } from '@angular-devkit/architect'; |
| 2 | +import semanticRelease from 'semantic-release'; |
| 3 | + |
| 4 | +import { ReleaseExecutorSchema } from './schema'; |
| 5 | +import { releaseNotesGenerator } from './release-notes-generator'; |
| 6 | +import { commitAnalyzer } from './commit-analyzer'; |
| 7 | +import { npm } from './npm'; |
| 8 | +import { plugins } from './plugins'; |
| 9 | +// import { preparePlugin } from './prepare'; |
| 10 | +import { platformPlugin } from './platform-plugin'; |
| 11 | + |
| 12 | +async function runRelease( |
| 13 | + options: ReleaseExecutorSchema, |
| 14 | + builderContext: BuilderContext |
| 15 | +) { |
| 16 | + const { |
| 17 | + npm: { pkgRoot }, |
| 18 | + dryRun, |
| 19 | + publishable, |
| 20 | + branches, |
| 21 | + } = options; |
| 22 | + |
| 23 | + const { project } = builderContext.target; |
| 24 | + |
| 25 | + const { outputPath } = await builderContext |
| 26 | + .getTargetOptions({ |
| 27 | + project, |
| 28 | + target: 'build', |
| 29 | + }) |
| 30 | + .catch(() => ({ outputPath: null })); |
| 31 | + |
| 32 | + const publishPath = outputPath ?? pkgRoot; |
| 33 | + |
| 34 | + if (publishable && !publishPath) { |
| 35 | + return { |
| 36 | + success: false, |
| 37 | + error: `Builder can't detect output path for the '${project}' project automatically. Please, provide the 'npm.pkgRoot' option`, |
| 38 | + }; |
| 39 | + } else if (publishable) { |
| 40 | + builderContext.logger.info( |
| 41 | + `The directory ${publishPath} will be used for publishing` |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + return semanticRelease( |
| 46 | + { |
| 47 | + tagFormat: `${project}@\${version}`, |
| 48 | + branches, |
| 49 | + extends: undefined, |
| 50 | + dryRun, |
| 51 | + plugins: plugins([ |
| 52 | + // preparePlugin({ publishable, publishPath }), |
| 53 | + commitAnalyzer({ project }), |
| 54 | + releaseNotesGenerator({ project }), |
| 55 | + npm({ publishable, publishPath }), |
| 56 | + platformPlugin(options, builderContext), |
| 57 | + ]), |
| 58 | + }, |
| 59 | + { |
| 60 | + env: { ...process.env }, |
| 61 | + cwd: '.', |
| 62 | + } |
| 63 | + ) |
| 64 | + .then((result) => { |
| 65 | + if (result) { |
| 66 | + const { |
| 67 | + nextRelease: { version }, |
| 68 | + } = result; |
| 69 | + |
| 70 | + builderContext.logger.info( |
| 71 | + `The '${project}' project released with version ${version}` |
| 72 | + ); |
| 73 | + } else { |
| 74 | + builderContext.logger.info( |
| 75 | + `No new release for the '${project}' project` |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + return { success: true }; |
| 80 | + }) |
| 81 | + .catch((err) => { |
| 82 | + builderContext.logger.error(err); |
| 83 | + |
| 84 | + return { |
| 85 | + success: false, |
| 86 | + error: `The automated release failed with error: ${err}`, |
| 87 | + }; |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +export const SemrelBuilder = createBuilder(runRelease); |
| 92 | + |
| 93 | +export default SemrelBuilder; |
0 commit comments