@reelout/core 0.1.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/analyzer.js +33 -0
- package/dist/analyzer.js.map +1 -0
- package/dist/builder.js +64 -0
- package/dist/builder.js.map +1 -0
- package/dist/changelog.js +63 -0
- package/dist/changelog.js.map +1 -0
- package/dist/formatError-BXNdyRfu.js +23 -0
- package/dist/formatError-BXNdyRfu.js.map +1 -0
- package/dist/index.js +225 -1
- package/dist/index.js.map +1 -0
- package/dist/types-la_KkjCS.js +1 -0
- package/dist/types.js +2 -0
- package/dist/workflow-CroJ6v4g.js +2736 -0
- package/dist/workflow-CroJ6v4g.js.map +1 -0
- package/package.json +35 -6
- package/src/analyzer.ts +59 -0
- package/src/builder.ts +116 -0
- package/src/cache/TTLCache.ts +100 -0
- package/src/cache/index.ts +4 -0
- package/src/changelog.ts +107 -0
- package/src/config/const.ts +104 -0
- package/src/config/createPlugin.ts +41 -0
- package/src/config/findReleaseRule.ts +26 -0
- package/src/config/index.ts +15 -0
- package/src/config/isSemVer.ts +31 -0
- package/src/config/isSemVerGitTag.ts +25 -0
- package/src/config/normalizeCommitFilter.ts +37 -0
- package/src/config/normalizeReleaseRule.ts +66 -0
- package/src/config/normalizeWorkspace.ts +34 -0
- package/src/config/resolvePlugin.ts +57 -0
- package/src/config/resolveReleaseChannel.ts +47 -0
- package/src/config/resolveVersionQualifier.ts +53 -0
- package/src/config/sanitizePrereleaseLabel.ts +18 -0
- package/src/git/branch/getCurrentGitBranch.ts +18 -0
- package/src/git/branch/index.ts +4 -0
- package/src/git/commit/createGitCommit.ts +29 -0
- package/src/git/commit/extractMentions.ts +34 -0
- package/src/git/commit/getCurrentGitCommit.ts +19 -0
- package/src/git/commit/getParsedGitCommit.ts +49 -0
- package/src/git/commit/index.ts +9 -0
- package/src/git/commit/parseGitCommit.ts +114 -0
- package/src/git/commit/parseGitPrettyFormat.ts +76 -0
- package/src/git/history/countGitHistory.ts +28 -0
- package/src/git/history/expandGitHistory.ts +112 -0
- package/src/git/history/index.ts +9 -0
- package/src/git/history/mergeGitHistory.ts +76 -0
- package/src/git/history/restoreFullGitHistory.ts +19 -0
- package/src/git/history/showGitHistory.ts +80 -0
- package/src/git/history/showParsedGitHistory.ts +28 -0
- package/src/git/index.ts +9 -0
- package/src/git/repository/ensureGitUserConfig.ts +50 -0
- package/src/git/repository/getGitConfigValue.ts +27 -0
- package/src/git/repository/getGitRemote.ts +20 -0
- package/src/git/repository/index.ts +10 -0
- package/src/git/repository/isGitShallowRepository.ts +21 -0
- package/src/git/repository/pushGitChanges.ts +26 -0
- package/src/git/repository/resolveGitRepository.ts +40 -0
- package/src/git/repository/withShallowLock.ts +62 -0
- package/src/git/revision/detectGitRelationship.ts +43 -0
- package/src/git/revision/index.ts +7 -0
- package/src/git/revision/isGitAncestor.ts +33 -0
- package/src/git/revision/readGitFile.ts +29 -0
- package/src/git/revision/resolveGitRevision.ts +23 -0
- package/src/git/tag/createGitTag.ts +25 -0
- package/src/git/tag/deleteGitTag.ts +17 -0
- package/src/git/tag/ensureGitTag.ts +63 -0
- package/src/git/tag/generateGitTagName.ts +43 -0
- package/src/git/tag/getCurrentGitTag.ts +23 -0
- package/src/git/tag/getGitTagMessage.ts +35 -0
- package/src/git/tag/getLatestGitTag.ts +22 -0
- package/src/git/tag/index.ts +13 -0
- package/src/git/tag/isGitTagConnected.ts +33 -0
- package/src/git/tag/isGitTagPointed.ts +38 -0
- package/src/git/tag/isGitTagPresent.ts +17 -0
- package/src/index.ts +9 -2
- package/src/plugin/HookManager.ts +128 -0
- package/src/plugin/index.ts +4 -0
- package/src/types.ts +259 -0
- package/src/util/buildTrailerPattern.ts +20 -0
- package/src/util/compact.ts +14 -0
- package/src/util/execCommand.ts +196 -0
- package/src/util/execGit.ts +27 -0
- package/src/util/formatError.ts +21 -0
- package/src/util/formatFile.ts +41 -0
- package/src/util/index.ts +19 -0
- package/src/util/interpolate.ts +141 -0
- package/src/util/isCI.ts +18 -0
- package/src/util/isClass.ts +34 -0
- package/src/util/isDefined.ts +12 -0
- package/src/util/isMatch.ts +43 -0
- package/src/util/isQuietEnvironment.ts +35 -0
- package/src/util/isTruthyString.ts +19 -0
- package/src/util/readJson.ts +47 -0
- package/src/util/unique.ts +26 -0
- package/src/util/writeJson.ts +28 -0
- package/src/workflow/analysis/deriveChangeReleases.ts +76 -0
- package/src/workflow/analysis/deriveTransitionReleases.ts +134 -0
- package/src/workflow/analysis/index.ts +10 -0
- package/src/workflow/analysis/isCommitForPackage.ts +23 -0
- package/src/workflow/analysis/promotePrereleaseVersions.ts +51 -0
- package/src/workflow/analysis/propagateDependencyUpdates.ts +71 -0
- package/src/workflow/analysis/sliceUnreleased.ts +36 -0
- package/src/workflow/analysis/streamPackageCommits.ts +34 -0
- package/src/workflow/changelog/buildChangelog.ts +79 -0
- package/src/workflow/changelog/buildReleaseNotes.ts +50 -0
- package/src/workflow/changelog/conventional-changelog-conventionalcommits.d.ts +7 -0
- package/src/workflow/changelog/extractLatestVersion.ts +22 -0
- package/src/workflow/changelog/extractPlainText.ts +38 -0
- package/src/workflow/changelog/extractPreviousVersion.ts +77 -0
- package/src/workflow/changelog/extractSemVer.ts +33 -0
- package/src/workflow/changelog/index.ts +12 -0
- package/src/workflow/changelog/iterateChangelogVersions.ts +70 -0
- package/src/workflow/changelog/prependChangelog.ts +59 -0
- package/src/workflow/changelog/writeChangelog.ts +27 -0
- package/src/workflow/committing/generateReleaseCommitMessage.ts +121 -0
- package/src/workflow/committing/index.ts +5 -0
- package/src/workflow/committing/isReleaseCommit.ts +56 -0
- package/src/workflow/discovery/buildDependencyGraph.ts +43 -0
- package/src/workflow/discovery/discoverPackageTag.ts +98 -0
- package/src/workflow/discovery/index.ts +9 -0
- package/src/workflow/discovery/resolvePackageStableTag.ts +88 -0
- package/src/workflow/discovery/resolvePackageTag.ts +45 -0
- package/src/workflow/discovery/sortPackages.ts +75 -0
- package/src/workflow/discovery/verifyPackageTag.ts +49 -0
- package/src/workflow/executeReleaseWorkflow.ts +130 -0
- package/src/workflow/index.ts +10 -0
- package/src/workflow/report/generateReleaseReport.ts +54 -0
- package/src/workflow/report/index.ts +5 -0
- package/src/workflow/report/printReleaseReport.ts +97 -0
- package/src/workflow/versioning/bumpVersion.ts +76 -0
- package/src/workflow/versioning/deriveReleaseType.ts +33 -0
- package/src/workflow/versioning/determineReleaseType.ts +35 -0
- package/src/workflow/versioning/extractVersionQualifier.ts +29 -0
- package/src/workflow/versioning/index.ts +10 -0
- package/src/workflow/versioning/isSameReleaseCycle.ts +36 -0
- package/src/workflow/versioning/isStableVersion.ts +31 -0
- package/src/workflow/versioning/pickPriorityReleaseType.ts +27 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import type { ConsolaInstance } from 'consola/core'
|
|
5
|
+
import type { CommitBase, CommitNote } from 'conventional-commits-parser'
|
|
6
|
+
import type { Hosts } from 'hosted-git-info'
|
|
7
|
+
import type { HookHandler } from './plugin'
|
|
8
|
+
|
|
9
|
+
export type GitRefType = 'branch'
|
|
10
|
+
|
|
11
|
+
export interface GitRef {
|
|
12
|
+
type: GitRefType
|
|
13
|
+
name: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type GitCommitType =
|
|
17
|
+
| 'build'
|
|
18
|
+
| 'chore'
|
|
19
|
+
| 'ci'
|
|
20
|
+
| 'docs'
|
|
21
|
+
| 'feat'
|
|
22
|
+
| 'fix'
|
|
23
|
+
| 'perf'
|
|
24
|
+
| 'refactor'
|
|
25
|
+
| 'revert'
|
|
26
|
+
| 'style'
|
|
27
|
+
| 'test'
|
|
28
|
+
| (string & {})
|
|
29
|
+
|
|
30
|
+
export type GitCommitNote = CommitNote
|
|
31
|
+
|
|
32
|
+
export interface GitCommitTrailer {
|
|
33
|
+
key: string
|
|
34
|
+
value: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface GitCommit extends CommitBase {
|
|
38
|
+
hash: string
|
|
39
|
+
parent: string[]
|
|
40
|
+
authorDate: string
|
|
41
|
+
committerDate: string
|
|
42
|
+
type: GitCommitType | null
|
|
43
|
+
scope: string | null
|
|
44
|
+
subject: string | null
|
|
45
|
+
header: string
|
|
46
|
+
body: string | null
|
|
47
|
+
footer: string | null
|
|
48
|
+
trailers: GitCommitTrailer[]
|
|
49
|
+
files: readonly string[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface GitRepository {
|
|
53
|
+
host: Hosts
|
|
54
|
+
owner: string
|
|
55
|
+
repo: string
|
|
56
|
+
url: string
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface GitAuthor {
|
|
60
|
+
name: string
|
|
61
|
+
email: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type CommitClassifier = (
|
|
65
|
+
commit: GitCommit
|
|
66
|
+
) => ReleaseType | null | undefined
|
|
67
|
+
|
|
68
|
+
export type ReleaseType = 'major' | 'minor' | 'patch'
|
|
69
|
+
|
|
70
|
+
export type ReleaseTypeHint = 'auto' | ReleaseType
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Specify the strategy used to calculate the next semantic version.
|
|
74
|
+
*
|
|
75
|
+
* - `advance`: Bumps the target core version segment (`major`, `minor`, or
|
|
76
|
+
* `patch`). Applies when stepping into a new release milestone or switching
|
|
77
|
+
* to a new prerelease channel.
|
|
78
|
+
* - Release → Release (e.g., `1.2.3` → `1.3.0`).
|
|
79
|
+
* - Release → Prerelease (e.g., `1.2.3` → `1.3.0-dev.0`).
|
|
80
|
+
* - Prerelease → Next Prerelease Channel (e.g., `1.3.0-dev.5` → `1.4.0-dev.0`).
|
|
81
|
+
* - `continue`: Retains the target core version segment without bumping it.
|
|
82
|
+
* Applies when issuing sequential prerelease builds within the same channel or
|
|
83
|
+
* graduating an active prerelease directly to its stable target release.
|
|
84
|
+
* - Prerelease → Prerelease (e.g., `1.3.0-dev.5` → `1.3.0-dev.6`).
|
|
85
|
+
* - Prerelease → Release (e.g., `1.3.0-dev.5` → `1.3.0`).
|
|
86
|
+
*/
|
|
87
|
+
export type VersionTransition = 'advance' | 'continue'
|
|
88
|
+
|
|
89
|
+
export interface ReleaseRule {
|
|
90
|
+
type?: GitRefType | undefined
|
|
91
|
+
pattern?: string | undefined
|
|
92
|
+
qualifier?: string | boolean | undefined
|
|
93
|
+
channel?: string | boolean | undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface ResolvedReleaseRule {
|
|
97
|
+
type: GitRefType | undefined
|
|
98
|
+
pattern: string
|
|
99
|
+
versionQualifier: string | undefined
|
|
100
|
+
releaseChannel: string | undefined
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type ReleasePluginFactory<P extends Package = Package> = (
|
|
104
|
+
options?: unknown
|
|
105
|
+
) => ReleasePlugin<P> | Promise<ReleasePlugin<P>>
|
|
106
|
+
|
|
107
|
+
export type ReleasePluginConstructor<P extends Package = Package> = new (
|
|
108
|
+
options?: unknown
|
|
109
|
+
) => ReleasePlugin<P>
|
|
110
|
+
|
|
111
|
+
export type ReleasePluginDefinition<P extends Package = Package> =
|
|
112
|
+
ReleasePlugin<P> | ReleasePluginFactory<P> | ReleasePluginConstructor<P>
|
|
113
|
+
|
|
114
|
+
export type ReleasePluginTuple<P extends Package = Package> = [
|
|
115
|
+
plugin: ReleasePluginDefinition<P>,
|
|
116
|
+
options?: unknown,
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
export interface ReleaseConfig<P extends Package = Package> {
|
|
120
|
+
author?: Partial<GitAuthor>
|
|
121
|
+
classifier?: CommitClassifier | Partial<Record<ReleaseType, GitCommitType[]>>
|
|
122
|
+
rules?: (ReleaseRule | string | false | null | undefined)[]
|
|
123
|
+
plugins?: (
|
|
124
|
+
| [plugin: string, options?: unknown]
|
|
125
|
+
| [plugin: ReleasePluginDefinition<P>, options?: unknown]
|
|
126
|
+
| string
|
|
127
|
+
| ReleasePluginDefinition<P>
|
|
128
|
+
| false
|
|
129
|
+
| null
|
|
130
|
+
| undefined
|
|
131
|
+
)[]
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface ResolvedReleaseConfig<P extends Package = Package> {
|
|
135
|
+
author: GitAuthor
|
|
136
|
+
classifier: CommitClassifier
|
|
137
|
+
rules: ResolvedReleaseRule[]
|
|
138
|
+
plugins: (ReleasePluginDefinition<P> | ReleasePluginTuple<P>)[]
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type ReleaseStatus = 'success' | 'failure'
|
|
142
|
+
|
|
143
|
+
export interface ReleasePhase {
|
|
144
|
+
hook: ReleaseHookName
|
|
145
|
+
plugin: string
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ReleaseContext<P extends Package = Package> {
|
|
149
|
+
cwd: string
|
|
150
|
+
env: Readonly<NodeJS.ProcessEnv>
|
|
151
|
+
today: string
|
|
152
|
+
signal: AbortSignal | undefined
|
|
153
|
+
stdout: NodeJS.WritableStream
|
|
154
|
+
stderr: NodeJS.WritableStream
|
|
155
|
+
logger: ConsolaInstance
|
|
156
|
+
repository: GitRepository | null
|
|
157
|
+
ref: GitRef
|
|
158
|
+
commit: GitCommit
|
|
159
|
+
author: GitAuthor
|
|
160
|
+
classifier: CommitClassifier
|
|
161
|
+
rules: ResolvedReleaseRule[]
|
|
162
|
+
type: ReleaseTypeHint
|
|
163
|
+
versionQualifier: string | undefined
|
|
164
|
+
releaseChannel: string | undefined
|
|
165
|
+
allowEmpty: boolean
|
|
166
|
+
dryRun: boolean
|
|
167
|
+
packages: P[]
|
|
168
|
+
plans: ReleasePlan<P>[]
|
|
169
|
+
status: ReleaseStatus | null
|
|
170
|
+
phase: ReleasePhase | null
|
|
171
|
+
error: unknown
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface Workspace {
|
|
175
|
+
entry: string
|
|
176
|
+
members: readonly string[] | null
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface Package {
|
|
180
|
+
provider?: string | undefined
|
|
181
|
+
name: string | undefined
|
|
182
|
+
version: string
|
|
183
|
+
private?: boolean | undefined
|
|
184
|
+
root: boolean
|
|
185
|
+
entry: boolean
|
|
186
|
+
dependencies: string[]
|
|
187
|
+
location: string
|
|
188
|
+
changelogFile?: string | undefined
|
|
189
|
+
files?: string | string[] | undefined
|
|
190
|
+
tag?: Tag | null
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface Tag {
|
|
194
|
+
name: string
|
|
195
|
+
message?: string | undefined
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface Release {
|
|
199
|
+
type: ReleaseType
|
|
200
|
+
transition?: VersionTransition | null
|
|
201
|
+
version?: string | null
|
|
202
|
+
tag?: Tag | null
|
|
203
|
+
changelog?: string | null
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface ReleasePlan<P extends Package = Package> {
|
|
207
|
+
package: P
|
|
208
|
+
history: GitCommit[]
|
|
209
|
+
release: Release | null
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export interface ReleaseHookDefinition<P extends Package = Package> {
|
|
213
|
+
priority?: number | undefined
|
|
214
|
+
handler: HookHandler<ReleaseContext<P>>
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export type ReleaseHook<P extends Package = Package> =
|
|
218
|
+
HookHandler<ReleaseContext<P>> | ReleaseHookDefinition<P>
|
|
219
|
+
|
|
220
|
+
export interface ReleasePlugin<P extends Package = Package> {
|
|
221
|
+
name: string
|
|
222
|
+
version: string
|
|
223
|
+
configure?: ReleaseHook<P> | null | undefined
|
|
224
|
+
discover?: ReleaseHook<P> | null | undefined
|
|
225
|
+
analyze?: ReleaseHook<P> | null | undefined
|
|
226
|
+
build?: ReleaseHook<P> | null | undefined
|
|
227
|
+
write?: ReleaseHook<P> | null | undefined
|
|
228
|
+
publish?: ReleaseHook<P> | null | undefined
|
|
229
|
+
finalize?: ReleaseHook<P> | null | undefined
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export type ReleaseHookName = Exclude<keyof ReleasePlugin, 'name' | 'version'>
|
|
233
|
+
|
|
234
|
+
export interface ReleaseReport {
|
|
235
|
+
status: ReleaseStatus | null
|
|
236
|
+
phase: ReleasePhase | null
|
|
237
|
+
error: string | null
|
|
238
|
+
packages: {
|
|
239
|
+
location: string
|
|
240
|
+
name: string | undefined
|
|
241
|
+
version: string
|
|
242
|
+
}[]
|
|
243
|
+
plans: {
|
|
244
|
+
package: {
|
|
245
|
+
provider: string | undefined
|
|
246
|
+
entry: boolean
|
|
247
|
+
location: string
|
|
248
|
+
name: string | undefined
|
|
249
|
+
version: string
|
|
250
|
+
private: boolean
|
|
251
|
+
}
|
|
252
|
+
release: {
|
|
253
|
+
type: ReleaseType
|
|
254
|
+
version: string
|
|
255
|
+
tag: string | null | undefined
|
|
256
|
+
changelog: string | null | undefined
|
|
257
|
+
}
|
|
258
|
+
}[]
|
|
259
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build a regular expression to match trailer lines using custom markers.
|
|
6
|
+
*
|
|
7
|
+
* Combines the provided marker strings with a standard alphanumeric pattern
|
|
8
|
+
* to construct a regular expression that matches trailer definitions.
|
|
9
|
+
*
|
|
10
|
+
* @param markers Optional array of custom trailer marker strings
|
|
11
|
+
* @returns A regular expression matching trailer lines
|
|
12
|
+
*/
|
|
13
|
+
export function buildTrailerPattern(markers: readonly string[] = []) {
|
|
14
|
+
return new RegExp(
|
|
15
|
+
`^(${markers
|
|
16
|
+
.map(RegExp.escape)
|
|
17
|
+
.concat('[A-Za-z0-9_-]+')
|
|
18
|
+
.join('|')})[ \\t]*:[ \\t]*(.*)$`
|
|
19
|
+
)
|
|
20
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import { isDefined } from './isDefined'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Remove `null` and `undefined` values from an array.
|
|
8
|
+
*
|
|
9
|
+
* @param array The array to compact
|
|
10
|
+
* @returns A new array that does not contain `null` or `undefined` values
|
|
11
|
+
*/
|
|
12
|
+
export function compact<T>(array: readonly (T | null | undefined)[]): T[] {
|
|
13
|
+
return array.filter(isDefined)
|
|
14
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import { spawn, type ChildProcessByStdio } from 'node:child_process'
|
|
5
|
+
import type { Stream } from 'node:stream'
|
|
6
|
+
|
|
7
|
+
export interface PromiseWithChild<T> extends Promise<T> {
|
|
8
|
+
child: ChildProcessByStdio<null, Stream.Readable, Stream.Readable>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const MUTATING_GIT_COMMANDS = new Set([
|
|
12
|
+
'commit',
|
|
13
|
+
'tag',
|
|
14
|
+
'cherry-pick',
|
|
15
|
+
'rebase',
|
|
16
|
+
'merge',
|
|
17
|
+
'revert',
|
|
18
|
+
'commit-tree',
|
|
19
|
+
'mktag',
|
|
20
|
+
])
|
|
21
|
+
|
|
22
|
+
let deterministicCounter = 0
|
|
23
|
+
|
|
24
|
+
export interface ExecCommandOptions {
|
|
25
|
+
cwd: string
|
|
26
|
+
env?: Readonly<NodeJS.ProcessEnv> | undefined
|
|
27
|
+
signal?: AbortSignal | undefined
|
|
28
|
+
stdout?: NodeJS.WritableStream | undefined
|
|
29
|
+
stderr?: NodeJS.WritableStream | undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Execute a system command with arguments as a child process.
|
|
34
|
+
*
|
|
35
|
+
* Spawns the target process, collects output streams, and intercepts mutating
|
|
36
|
+
* `git` operations in test mode to inject deterministic commit timestamps.
|
|
37
|
+
*
|
|
38
|
+
* @param command Executable command name or path to spawn
|
|
39
|
+
* @param args Array of string arguments passed to the command
|
|
40
|
+
* @param options Execution environment, streaming, and signal options
|
|
41
|
+
* @returns Promise attached to the child process resolving to standard output
|
|
42
|
+
* @throws If process execution fails or exits with a non-zero status
|
|
43
|
+
*/
|
|
44
|
+
export function execCommand(
|
|
45
|
+
command: string,
|
|
46
|
+
args: readonly string[],
|
|
47
|
+
options: ExecCommandOptions
|
|
48
|
+
): PromiseWithChild<string> {
|
|
49
|
+
const { cwd, signal } = options
|
|
50
|
+
|
|
51
|
+
const env: NodeJS.ProcessEnv = {
|
|
52
|
+
HOME: process.env.HOME,
|
|
53
|
+
PATH: process.env.PATH,
|
|
54
|
+
...options.env,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (import.meta.env.MODE === 'test' && command === 'git') {
|
|
58
|
+
const primaryArg = args.find((arg) => MUTATING_GIT_COMMANDS.has(arg))
|
|
59
|
+
|
|
60
|
+
if (primaryArg) {
|
|
61
|
+
const base = Date.parse('2026-01-01T00:00:00Z')
|
|
62
|
+
const date = new Date(base + deterministicCounter * 1000).toISOString()
|
|
63
|
+
deterministicCounter += 1
|
|
64
|
+
env.GIT_AUTHOR_DATE = date
|
|
65
|
+
env.GIT_COMMITTER_DATE = date
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const child = spawn(command, args, {
|
|
70
|
+
cwd,
|
|
71
|
+
env,
|
|
72
|
+
signal,
|
|
73
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const promise = new Promise<string>((resolve, reject) => {
|
|
77
|
+
const stdout: Buffer[] = []
|
|
78
|
+
const stderr: Buffer[] = []
|
|
79
|
+
let settled = false
|
|
80
|
+
|
|
81
|
+
function safeResolve(value: string) {
|
|
82
|
+
/* istanbul ignore next */
|
|
83
|
+
if (settled) return
|
|
84
|
+
settled = true
|
|
85
|
+
cleanup()
|
|
86
|
+
resolve(value)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function safeReject(err: Error) {
|
|
90
|
+
/* istanbul ignore next */
|
|
91
|
+
if (settled) return
|
|
92
|
+
settled = true
|
|
93
|
+
cleanup()
|
|
94
|
+
reject(err)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function cleanup() {
|
|
98
|
+
child.off('error', onError)
|
|
99
|
+
child.off('close', onClose)
|
|
100
|
+
|
|
101
|
+
child.stdout.removeAllListeners('data')
|
|
102
|
+
child.stderr.removeAllListeners('data')
|
|
103
|
+
|
|
104
|
+
if (options.stdout) {
|
|
105
|
+
child.stdout.unpipe(options.stdout)
|
|
106
|
+
options.stdout.off('error', onError)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (options.stderr) {
|
|
110
|
+
child.stderr.unpipe(options.stderr)
|
|
111
|
+
options.stderr.off('error', onError)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function onError(err: Error) {
|
|
116
|
+
safeReject(err)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function onClose(code: number | null, signal: NodeJS.Signals | null) {
|
|
120
|
+
/* istanbul ignore next */
|
|
121
|
+
if (settled) return
|
|
122
|
+
|
|
123
|
+
if (code === 0) {
|
|
124
|
+
return safeResolve(bufferArrayToString(stdout))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const quotedCommand = quoteCommandForMessage([command, ...args].join(' '))
|
|
128
|
+
|
|
129
|
+
/* istanbul ignore next */
|
|
130
|
+
const exitStatus =
|
|
131
|
+
code !== null
|
|
132
|
+
? `exit code ${code}`
|
|
133
|
+
: signal
|
|
134
|
+
? `signal ${signal}`
|
|
135
|
+
: 'unknown status'
|
|
136
|
+
|
|
137
|
+
const errorDiagnostics =
|
|
138
|
+
bufferArrayToString(stderr).trim() || bufferArrayToString(stdout).trim()
|
|
139
|
+
|
|
140
|
+
const errorMessage = [
|
|
141
|
+
`Command ${quotedCommand} failed (${exitStatus})`,
|
|
142
|
+
errorDiagnostics,
|
|
143
|
+
]
|
|
144
|
+
.filter(Boolean)
|
|
145
|
+
.join('\n')
|
|
146
|
+
|
|
147
|
+
const err = new Error(errorMessage, {
|
|
148
|
+
cause: { code, signal, diagnostics: errorDiagnostics },
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
/* istanbul ignore else */
|
|
152
|
+
if (code != null) {
|
|
153
|
+
Object.defineProperty(err, 'code', { value: code })
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
safeReject(err)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
child.on('error', onError)
|
|
160
|
+
child.on('close', onClose)
|
|
161
|
+
|
|
162
|
+
if (options.stdout) {
|
|
163
|
+
options.stdout.on('error', onError)
|
|
164
|
+
child.stdout.pipe(options.stdout)
|
|
165
|
+
} else {
|
|
166
|
+
child.stdout.on('data', (chunk) => stdout.push(chunk))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (options.stderr) {
|
|
170
|
+
options.stderr.on('error', onError)
|
|
171
|
+
child.stderr.pipe(options.stderr)
|
|
172
|
+
} else {
|
|
173
|
+
child.stderr.on('data', (chunk) => stderr.push(chunk))
|
|
174
|
+
}
|
|
175
|
+
}) as PromiseWithChild<string>
|
|
176
|
+
|
|
177
|
+
promise.child = child
|
|
178
|
+
|
|
179
|
+
return promise
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function bufferArrayToString(chunks: Buffer[]): string {
|
|
183
|
+
return Buffer.concat(chunks).toString('utf-8')
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function quoteCommandForMessage(command: string): string {
|
|
187
|
+
if (!command.includes("'")) {
|
|
188
|
+
return `'${command}'`
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!command.includes('"')) {
|
|
192
|
+
return `"${command}"`
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return JSON.stringify(command)
|
|
196
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
execCommand,
|
|
6
|
+
type ExecCommandOptions,
|
|
7
|
+
type PromiseWithChild,
|
|
8
|
+
} from './execCommand'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Execute a Git command inside a target directory.
|
|
12
|
+
*
|
|
13
|
+
* Wraps `execCommand` to invoke the `git` executable using the provided
|
|
14
|
+
* arguments and execution options.
|
|
15
|
+
*
|
|
16
|
+
* @param cwd Working directory path where the Git command runs
|
|
17
|
+
* @param args Command-line arguments passed to Git
|
|
18
|
+
* @param options Additional execution, environment, and stream options
|
|
19
|
+
* @returns Promise attached to the child process resolving to stdout
|
|
20
|
+
*/
|
|
21
|
+
export function execGit(
|
|
22
|
+
cwd: string,
|
|
23
|
+
args: readonly string[],
|
|
24
|
+
options?: Partial<ExecCommandOptions>
|
|
25
|
+
): PromiseWithChild<string> {
|
|
26
|
+
return execCommand('git', args, { ...options, cwd })
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Normalize an unknown thrown value for logging or display.
|
|
6
|
+
*
|
|
7
|
+
* - If `err` is an `Error`, returns its `message` string.
|
|
8
|
+
* - Otherwise returns the original `err` value unchanged (may be a string,
|
|
9
|
+
* object, number, `null`, or `undefined`).
|
|
10
|
+
*
|
|
11
|
+
* This function intentionally does not coerce non-Error values to strings so
|
|
12
|
+
* callers can decide how to render structured values.
|
|
13
|
+
*
|
|
14
|
+
* @param err The thrown value to normalize
|
|
15
|
+
* @returns The error message when `err` is an `Error`, otherwise the original value
|
|
16
|
+
*/
|
|
17
|
+
export function formatError<T>(err: T): T | string {
|
|
18
|
+
if (Error.isError(err)) return err.message
|
|
19
|
+
if (typeof err === 'string') return err
|
|
20
|
+
return err
|
|
21
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import prettier from 'prettier'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Format `content` for `filePath` using Prettier when possible.
|
|
8
|
+
*
|
|
9
|
+
* Resolves project/editorconfig settings and formats the provided content.
|
|
10
|
+
* Returns the original content on error or when no parser is inferred.
|
|
11
|
+
*
|
|
12
|
+
* @param filePath Path used to infer parser and config
|
|
13
|
+
* @param content Source content to format
|
|
14
|
+
* @returns Formatted content or original content on failure
|
|
15
|
+
*/
|
|
16
|
+
export async function formatFile(
|
|
17
|
+
filePath: string,
|
|
18
|
+
content: string
|
|
19
|
+
): Promise<string> {
|
|
20
|
+
try {
|
|
21
|
+
const info = await prettier.getFileInfo(filePath, {
|
|
22
|
+
resolveConfig: true,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const config = await prettier.resolveConfig(filePath, {
|
|
26
|
+
editorconfig: true,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
if (info.inferredParser == null) {
|
|
30
|
+
return content
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return await prettier.format(content, {
|
|
34
|
+
...config,
|
|
35
|
+
parser: info.inferredParser,
|
|
36
|
+
filepath: filePath,
|
|
37
|
+
})
|
|
38
|
+
} catch {
|
|
39
|
+
return content
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
export * from './buildTrailerPattern'
|
|
5
|
+
export * from './compact'
|
|
6
|
+
export * from './execCommand'
|
|
7
|
+
export * from './execGit'
|
|
8
|
+
export * from './formatError'
|
|
9
|
+
export * from './formatFile'
|
|
10
|
+
export * from './interpolate'
|
|
11
|
+
export * from './isCI'
|
|
12
|
+
export * from './isClass'
|
|
13
|
+
export * from './isDefined'
|
|
14
|
+
export * from './isMatch'
|
|
15
|
+
export * from './isQuietEnvironment'
|
|
16
|
+
export * from './isTruthyString'
|
|
17
|
+
export * from './readJson'
|
|
18
|
+
export * from './unique'
|
|
19
|
+
export * from './writeJson'
|