-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.ts
102 lines (86 loc) · 2.59 KB
/
release.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
import { access, copyFile } from 'fs/promises'
import { constants } from 'fs'
import { resolve } from 'path'
interface Success {
kind: 'success'
message: string
}
interface Failure {
kind: 'failure'
reason: string
}
type Result = Success | Failure
type Actions = Record<string, () => Promise<Result>>
async function actions(actions: Actions): Promise<Result> {
const [passed] = process.argv.slice(2)
const resolved = actions[passed]
if (resolved) {
return await resolved()
}
return {
kind: 'failure',
reason: `Action '${passed}' is not defined.`
}
}
async function run(result: Result) {
switch (result.kind) {
case 'success': {
console.log(result.message)
return
}
case 'failure': {
console.error(result.reason)
process.exit(1)
}
}
}
async function main() {
const cwd = process.cwd()
await run(
await actions({
/** This should be called in `prerelease`. */
async prepare() {
try {
// Just double-check for `dist` directory.
await access(`${cwd}/package.json`, constants.F_OK)
// Copy necessary files.
await copyFile(`${cwd}/package.json`, `${cwd}/dist/package.json`)
await copyFile(`${cwd}/package-lock.json`, `${cwd}/dist/package-lock.json`)
await copyFile(`${cwd}/CHANGELOG.md`, `${cwd}/dist/CHANGELOG.md`)
await copyFile(`${cwd}/README.md`, `${cwd}/dist/README.md`)
await copyFile(`${cwd}/LICENSE`, `${cwd}/dist/LICENSE`)
// This is kinda needed for local publishes to `verdaccio`, so I do not accidentally
// publish my experiments to the actual `npm` registry.
await copyFile(`${cwd}/.npmrc`, `${cwd}/dist/.npmrc`)
return {
kind: 'success',
message: 'Successfully prepared files for a release.'
}
} catch (error) {
return {
kind: 'failure',
reason: (error as Error).message
}
}
},
/** This should be called in `postversion`. Here we actually are inside the `dist`. */
async restore() {
try {
// Copy back.
await copyFile(`${cwd}/package.json`, resolve(cwd, '..', 'package.json'))
await copyFile(`${cwd}/package-lock.json`, resolve(cwd, '..', 'package-lock.json'))
return {
kind: 'success',
message: 'Successfully restored `package.json`.'
}
} catch (error) {
return {
kind: 'failure',
reason: (error as Error).message
}
}
}
})
)
}
main()