forked from tamagui/tamagui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release-next-beta.ts
219 lines (194 loc) · 6.17 KB
/
release-next-beta.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* eslint-disable no-console */
import * as proc from 'node:child_process'
import { join } from 'node:path'
import { promisify } from 'node:util'
import path from 'path'
import fs from 'fs-extra'
import _ from 'lodash'
import prompts from 'prompts'
// --resume would be cool here where it stores the last failed step somewhere and tries resuming
const exec = promisify(proc.exec)
const spawn = proc.spawn
const curVersion = fs.readJSONSync('./packages/tamagui/package.json').version
const nextVersion = `1.0.1-beta.${+curVersion.split('.')[3] + 1}`
const skipVersion = process.argv.includes('--skip-version')
const skipPublish = process.argv.includes('--skip-publish')
const tamaguiGitUser = process.argv.includes('--tamagui-git-user')
const isCI = process.argv.includes('--ci')
// could add only if changed checks: git diff --quiet HEAD HEAD~3 -- ./packages/core
// but at that point would be nicer to get a whole setup for this.. lerna or whatever
const spawnify = async (cmd: string, opts?: any) => {
console.log('>', cmd)
const [head, ...rest] = cmd.split(' ')
return new Promise((res, rej) => {
const child = spawn(head, rest, { stdio: ['inherit', 'pipe', 'pipe'], ...opts })
const outStr = []
const errStr = []
child.stdout.on('data', (data) => {
outStr.push(data.toString())
})
child.stderr.on('data', (data) => {
errStr.push(data.toString())
})
child.on('error', (err) => {
rej(err)
})
child.on('close', (code) => {
if (code === 0) {
res(outStr.join('\n'))
} else {
rej(errStr.join('\n'))
}
})
})
}
async function run() {
const workspaces = (await exec(`yarn workspaces list --json`)).stdout.trim().split('\n')
const packagePaths = workspaces.map((p) => JSON.parse(p)) as {
name: string
location: string
}[]
const packageJsons = (
await Promise.all(
packagePaths
.filter((i) => i.location !== '.' && !i.name.startsWith('@takeout'))
.map(async ({ name, location }) => {
const cwd = path.join(__dirname, location)
return {
name,
cwd,
json: await fs.readJSON(path.join(cwd, 'package.json')),
}
})
)
).filter((x) => {
return !x.json.private
})
async function checkDistDirs() {
await Promise.all(
packageJsons.map(async ({ cwd, json }) => {
const distDir = join(cwd, 'dist')
if (!json.scripts || json.scripts.build === 'true') {
return
}
if (!(await fs.pathExists(distDir))) {
console.warn('no dist dir!', distDir)
process.exit(1)
}
})
)
}
try {
if (tamaguiGitUser) {
await spawnify(`git config --global user.name 'Tamagui'`)
await spawnify(`git config --global user.email '[email protected]`)
}
let version = curVersion
if (!skipVersion) {
const answer = isCI
? { version: nextVersion }
: await prompts({
type: 'text',
name: 'version',
message: 'Version?',
initial: nextVersion,
})
version = answer.version
console.log('running checks')
await Promise.all([
//
spawnify(`yarn install`),
checkDistDirs(),
spawnify(`yarn build`),
spawnify(`yarn lint`),
spawnify(`yarn fix`),
])
if (!process.env.SKIP_GIT_CLEAN_CHECK) {
console.log('checking no git changes...')
const out = await exec(`git status --porcelain`)
if (out.stdout) {
throw new Error(`Has unsaved git changes: ${out.stdout}`)
}
}
await spawnify(
`yarn lerna version ${version} --ignore-changes --ignore-scripts --yes --no-push --no-git-tag-version`
)
}
console.log((await exec(`git diff`)).stdout)
if (!isCI) {
const { confirmed } = await prompts({
type: 'confirm',
name: 'confirmed',
message: 'Ready to publish?',
})
if (!confirmed) {
process.exit(0)
}
}
if (!skipPublish) {
// publish with tag
for (const chunk of _.chunk(packageJsons, 6)) {
await Promise.all(
chunk.map(async ({ cwd, name }) => {
console.log(`Publish ${name}`)
try {
await spawnify(`npm publish --tag prepub`, {
cwd,
})
} catch (err) {
// @ts-ignore
if (err.includes(`403`)) {
console.log('Already published, skipping')
return
}
throw err
}
})
)
}
console.log(`✅ Published under dist-tag "prepub"\n`)
// if all successful, re-tag as latest
for (const chunk of _.chunk(packageJsons, 20)) {
await Promise.all(
chunk.map(async ({ name, cwd }) => {
console.log(`Release ${name}`)
try {
await spawnify(`npm dist-tag remove ${name}@${version} prepub`, {
cwd,
})
await spawnify(`npm dist-tag add ${name}@${version} latest`, {
cwd,
})
} catch (err) {
// @ts-ignore
console.error(`Package ${name} failed with error:`, err.message, err.stack)
}
})
)
}
console.log(`✅ Published\n`)
// then git tag, commit, push
await spawnify(`yarn install`)
await spawnify(`git add -A`)
await spawnify(`git commit -m v${version}`)
await spawnify(`git tag v${version}`)
await spawnify(`git push origin head`)
await spawnify(`git push origin v${version}`)
console.log(`✅ Pushed and versioned\n`)
const seconds = 5
console.log(
`Update starters to v${version} in (${seconds}) seconds (give time to propogate)...`
)
await new Promise((res) => setTimeout(res, 5 * 1000))
await spawnify(`yarn upgrade:starters`)
await spawnify(`git commit -am update-starters-v${version}`)
await spawnify(`git push origin head`)
} else {
console.log(`Skipped publish`)
}
} catch (err) {
console.log('\nError:\n', err)
process.exit(1)
}
}
run()