|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +// ---- **IMPORTANT** ---- |
| 10 | +// This command is part of our external commands invoked by the release publish |
| 11 | +// command. Before making changes, keep in mind that more recent `ng-dev` versions |
| 12 | +// can still invoke this command. |
| 13 | +// ------------------------ |
| 14 | + |
| 15 | +import * as semver from 'semver'; |
| 16 | +import {CommandModule} from 'yargs'; |
| 17 | + |
| 18 | +import {assertPassingReleasePrechecks} from './index'; |
| 19 | +import {getConfig} from '../../utils/config'; |
| 20 | +import {readBufferFromStdinUntilClosed} from '../../utils/read-stdin-until-closed'; |
| 21 | +import {assertValidReleaseConfig, BuiltPackageWithInfo} from '../config/index'; |
| 22 | +import {error, red} from '../../utils/console'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Type describing the JSON stdin input of this command. The release tool will |
| 26 | + * deliver this information through `stdin` because command line arguments are |
| 27 | + * less reliable and have max-length limits. |
| 28 | + * |
| 29 | + * @important When changing this, make sure the release action |
| 30 | + * invocation is updated as well. |
| 31 | + */ |
| 32 | +export interface ReleasePrecheckJsonStdin { |
| 33 | + /** Package output that has been built and can be checked. */ |
| 34 | + builtPackagesWithInfo: BuiltPackageWithInfo[]; |
| 35 | + /** New version that is intended to be released. */ |
| 36 | + newVersion: string; |
| 37 | +} |
| 38 | + |
| 39 | +async function handler() { |
| 40 | + // Note: Stdin input is buffered until we start reading from it. This allows us to |
| 41 | + // asynchronously start reading the `stdin` input. With the default `readableFlowing` |
| 42 | + // value of `null`, data is buffered. See: https://nodejs.org/api/stream.html#three-states. |
| 43 | + const stdin = await readBufferFromStdinUntilClosed(); |
| 44 | + const config = getConfig(); |
| 45 | + assertValidReleaseConfig(config); |
| 46 | + |
| 47 | + // Parse the JSON metadata read from `stdin`. |
| 48 | + const {builtPackagesWithInfo, newVersion: newVersionRaw} = JSON.parse( |
| 49 | + stdin.toString('utf8'), |
| 50 | + ) as ReleasePrecheckJsonStdin; |
| 51 | + |
| 52 | + if (!Array.isArray(builtPackagesWithInfo)) { |
| 53 | + error(red(` ✘ Release pre-checks failed. Invalid list of built packages was provided.`)); |
| 54 | + process.exitCode = 1; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const newVersion = semver.parse(newVersionRaw); |
| 59 | + if (newVersion === null) { |
| 60 | + error(red(` ✘ Release pre-checks failed. Invalid new version was provided.`)); |
| 61 | + process.exitCode = 1; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (!(await assertPassingReleasePrechecks(config.release, newVersion, builtPackagesWithInfo))) { |
| 66 | + process.exitCode = 1; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** CLI command module for running checks before releasing. */ |
| 71 | +export const ReleasePrecheckCommandModule: CommandModule<{}, {}> = { |
| 72 | + handler, |
| 73 | + command: 'precheck', |
| 74 | + describe: false, |
| 75 | +}; |
0 commit comments