|
| 1 | +import * as globby from "globby"; |
| 2 | +import { isReleaseType, ReleaseType } from "./release-type"; |
| 3 | +import { VersionBumpOptions } from "./version-bump-options"; |
| 4 | + |
| 5 | +interface Interface { |
| 6 | + input?: NodeJS.ReadableStream | NodeJS.ReadStream | false; |
| 7 | + output?: NodeJS.WritableStream | NodeJS.WriteStream | false; |
| 8 | + [key: string]: unknown; |
| 9 | +} |
| 10 | + |
| 11 | +type Release = "prompt" | ReleaseType | { version: string }; |
| 12 | + |
| 13 | +/** |
| 14 | + * Normalized and sanitized options |
| 15 | + */ |
| 16 | +export interface NormalizedOptions { |
| 17 | + release: Release; |
| 18 | + preid: string; |
| 19 | + commit?: { |
| 20 | + message: string; |
| 21 | + noVerify: boolean; |
| 22 | + all: boolean; |
| 23 | + }; |
| 24 | + tag?: { |
| 25 | + name: string; |
| 26 | + }; |
| 27 | + push: boolean; |
| 28 | + files: string[]; |
| 29 | + cwd: string; |
| 30 | + interface: Interface; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Converts raw VersionBumpOptions to a normalized and sanitized Options object. |
| 35 | + */ |
| 36 | +export async function normalizeOptions(raw: VersionBumpOptions): Promise<NormalizedOptions> { |
| 37 | + // Set the simple properties first |
| 38 | + let preid = typeof raw.preid === "string" ? raw.preid : "beta"; |
| 39 | + let push = Boolean(raw.push); |
| 40 | + let all = Boolean(raw.all); |
| 41 | + let noVerify = Boolean(raw.noVerify); |
| 42 | + let cwd = raw.cwd || process.cwd(); |
| 43 | + |
| 44 | + let release: Release; |
| 45 | + if (!raw.release || raw.release === "prompt") { |
| 46 | + release = "prompt"; |
| 47 | + } |
| 48 | + else if (isReleaseType(raw.release)) { |
| 49 | + release = raw.release; |
| 50 | + } |
| 51 | + else { |
| 52 | + release = { version: raw.release }; |
| 53 | + } |
| 54 | + |
| 55 | + let tag; |
| 56 | + if (typeof raw.tag === "string") { |
| 57 | + tag = { name: raw.tag }; |
| 58 | + } |
| 59 | + else if (raw.tag) { |
| 60 | + tag = { name: "v" }; |
| 61 | + } |
| 62 | + |
| 63 | + // NOTE: This must come AFTER `tag` and `push`, because it relies on them |
| 64 | + let commit; |
| 65 | + if (typeof raw.commit === "string") { |
| 66 | + commit = { all, noVerify, message: raw.commit }; |
| 67 | + } |
| 68 | + else if (raw.commit || tag || push) { |
| 69 | + commit = { all, noVerify, message: "release v" }; |
| 70 | + } |
| 71 | + |
| 72 | + let files; |
| 73 | + if (Array.isArray(raw.files) && raw.files.length > 0) { |
| 74 | + files = await globby(raw.files, { cwd }); |
| 75 | + } |
| 76 | + else { |
| 77 | + files = ["package.json", "package-lock.json"]; |
| 78 | + } |
| 79 | + |
| 80 | + let ui: Interface; |
| 81 | + if (raw.interface === false) { |
| 82 | + ui = { input: false, outut: false }; |
| 83 | + } |
| 84 | + else if (raw.interface === true || !raw.interface) { |
| 85 | + ui = { input: process.stdin, output: process.stdout }; |
| 86 | + } |
| 87 | + else { |
| 88 | + let { input, output, ...other } = raw.interface; |
| 89 | + |
| 90 | + if (input === true || (input !== false && !input)) { |
| 91 | + input = process.stdin; |
| 92 | + } |
| 93 | + |
| 94 | + if (output === true || (output !== false && !output)) { |
| 95 | + output = process.stdout; |
| 96 | + } |
| 97 | + |
| 98 | + ui = { input, output, ...other }; |
| 99 | + } |
| 100 | + |
| 101 | + if (release === "prompt" && !(ui.input && ui.output)) { |
| 102 | + throw new Error(`Cannot prompt for the version number because input or output has been disabled.`); |
| 103 | + } |
| 104 | + |
| 105 | + return { release, preid, commit, tag, push, files, cwd, interface: ui }; |
| 106 | +} |
0 commit comments