forked from antfu-collective/bumpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize-options.ts
137 lines (116 loc) · 3.35 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
import process from 'node:process'
import fg from 'fast-glob'
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 & { oldVersion?: string }): 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' }
const files = await fg(
raw.files?.length
? raw.files
: ['package.json', 'package-lock.json'],
{
cwd,
onlyFiles: true,
ignore: [
'**/{.git,node_modules,bower_components,__tests__,fixtures,fixture}/**',
],
},
)
let ui: Interface
if (raw.interface === false) {
ui = { input: false, output: false }
}
else if (raw.interface === true || !raw.interface) {
ui = { input: process.stdin, output: process.stdout }
}
else {
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 }
}