forked from JS-DevTools/version-bump-prompt
/
normalize-options.ts
171 lines (143 loc) · 4.44 KB
/
normalize-options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import globby, { hasMagic } from 'globby'
import type { ReleaseType } from './release-type'
import { isReleaseType } from './release-type'
import type { VersionBumpOptions } from './types/version-bump-options'
interface Interface {
input?: NodeJS.ReadableStream | NodeJS.ReadStream | false
output?: NodeJS.WritableStream | NodeJS.WriteStream | false
[key: string]: unknown
}
/**
* A specific version release.
*/
export interface VersionRelease {
type: 'version'
version: string
}
/**
* Prompt the user for the release number.
*/
export interface PromptRelease {
type: 'prompt'
preid: string
}
/**
* A bump release, relative to the current version number.
*/
export interface BumpRelease {
type: ReleaseType
preid: string
}
/**
* One of the possible Release types.
*/
export type Release = VersionRelease | PromptRelease | BumpRelease
/**
* Normalized and sanitized options
*/
export interface NormalizedOptions {
release: Release
commit?: {
message: string
noVerify: boolean
all: boolean
}
tag?: {
name: string
}
push: boolean
files: string[]
cwd: string
interface: Interface
ignoreScripts: boolean
execute?: string
}
/**
* Converts raw VersionBumpOptions to a normalized and sanitized Options object.
*/
export async function normalizeOptions(raw: VersionBumpOptions): Promise<NormalizedOptions> {
// Set the simple properties first
const preid = typeof raw.preid === 'string' ? raw.preid : 'beta'
const push = Boolean(raw.push)
const all = Boolean(raw.all)
const noVerify = Boolean(raw.noVerify)
const cwd = raw.cwd || process.cwd()
const ignoreScripts = Boolean(raw.ignoreScripts)
const execute = raw.execute
let release: Release
if (!raw.release || raw.release === 'prompt')
release = { type: 'prompt', preid }
else if (isReleaseType(raw.release))
release = { type: raw.release, preid }
else
release = { type: 'version', version: raw.release }
let tag
if (typeof raw.tag === 'string')
tag = { name: raw.tag }
else if (raw.tag)
tag = { name: 'v' }
// NOTE: This must come AFTER `tag` and `push`, because it relies on them
let commit
if (typeof raw.commit === 'string')
commit = { all, noVerify, message: raw.commit }
else if (raw.commit || tag || push)
commit = { all, noVerify, message: 'chore: release v' }
let files
if (Array.isArray(raw.files) && raw.files.length > 0) {
files = await strictGlobMatches(raw.files, { cwd })
}
else {
// Try to find these files by default.
// If they don't exist, then they will NOT be included in the `files` array.
files = await globby(['package.json', 'package-lock.json'], { cwd })
}
let ui: Interface
if (raw.interface === false) {
ui = { input: false, outut: false }
}
else if (raw.interface === true || !raw.interface) {
ui = { input: process.stdin, output: process.stdout }
}
else {
// eslint-disable-next-line prefer-const
let { input, output, ...other } = raw.interface
if (input === true || (input !== false && !input))
input = process.stdin
if (output === true || (output !== false && !output))
output = process.stdout
ui = { input, output, ...other }
}
if (release.type === 'prompt' && !(ui.input && ui.output))
throw new Error('Cannot prompt for the version number because input or output has been disabled.')
return { release, commit, tag, push, files, cwd, interface: ui, ignoreScripts, execute }
}
/**
* Returns all files that match the given glob patterns.
* An error is thrown if any pattern matches zero files.
*/
async function strictGlobMatches(files: string[], options: object): Promise<string[]> {
// Match all glob patterns simultaneously
const matches = await Promise.all(files.map(file => strictGlobMatch(file, options)))
// Get all the unique files
const matchedFiles = new Set<string>()
for (const match of matches) {
for (const file of match)
matchedFiles.add(file)
}
return [...matchedFiles]
}
/**
* Returns all files that match the given glob pattern.
* An error is thrown if the pattern matches zero files.
*/
async function strictGlobMatch(file: string, options: object): Promise<string[]> {
const matches = await globby(file, options)
if (matches.length === 0) {
if (hasMagic(file))
throw new Error(`Could not find any files matching "${file}".`)
else
throw new Error(`Could not find file: ${file}.`)
}
// Return files in a predictable order
return matches.sort()
}