-
Notifications
You must be signed in to change notification settings - Fork 852
Expand file tree
/
Copy pathrelease-npm.ts
More file actions
497 lines (436 loc) · 15 KB
/
Copy pathrelease-npm.ts
File metadata and controls
497 lines (436 loc) · 15 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env tsx
/**
* Release script for EthereumJS monorepo packages
*
* Supports both regular releases and in-between releases (nightly, alpha, etc.)
* Optionally publishes under a different npm scope (e.g. for fork releases).
*
* Usage:
* tsx scripts/release-npm.ts [--bump-version=<version>] [--publish=<tag>] [--scope=<scope>] [--start-with=<package>]
*
* With no flags, publishes current package versions to npm under the `latest` tag.
*
* Examples:
* # Publish current versions (default tag: latest)
* tsx scripts/release-npm.ts
*
* # Bump versions only (no publish)
* tsx scripts/release-npm.ts --bump-version=10.1.0
*
* # Bump versions and publish
* tsx scripts/release-npm.ts --bump-version=10.1.1-nightly.1 --publish=nightly
*
* # Publish current versions explicitly
* tsx scripts/release-npm.ts --publish=latest
*
* # Resume an interrupted publish (dependency order in PUBLISH_ORDER)
* tsx scripts/release-npm.ts --publish=latest --start-with=util
*
* Maintainer default: `npm login` (2FA in browser; skip follow-up checks if offered),
* then `--publish=latest` with no --otp. See release-round skill Phase 4.
*
* # Fork release under a different npm scope
* tsx scripts/release-npm.ts --scope=feelyourprotocol --bump-version=8141.0.0 --publish=latest
*
* # Optional: per-command OTP (CI / Publish-token 2FA — not the usual maintainer flow)
* tsx scripts/release-npm.ts --publish=latest --otp=123456
*/
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { execSync } from 'child_process'
const DEFAULT_SCOPE = 'ethereumjs'
// Active packages from README.md (version + dependencies updated, published)
const ACTIVE_PACKAGES = [
'binarytree',
'block',
'blockchain',
'common',
'evm',
'genesis',
'mpt',
'rlp',
'statemanager',
'tx',
'util',
'vm',
]
// Dependency order for npm publish (deps must exist on the registry first)
const PUBLISH_ORDER = [
'rlp',
'util',
'common',
'binarytree',
'genesis',
'tx',
'mpt',
'block',
'statemanager',
'evm',
'blockchain',
'vm',
]
// Deprecated packages + testdata (only dependencies updated, not published)
// These packages keep their own version but need @ethereumjs/* deps updated
const DEPS_ONLY_PACKAGES = [
'client',
'devp2p',
'ethash',
'wallet',
'testdata',
]
interface PackageJson {
name: string
version: string
dependencies?: Record<string, string>
devDependencies?: Record<string, string>
[key: string]: unknown
}
interface PackageInfo {
name: string
path: string
oldVersion: string
packageJson: PackageJson
}
interface ParsedArgs {
version?: string
tag?: string
scope: string
otp?: string
startWith?: string
}
function parseArgs(): ParsedArgs {
const args = process.argv.slice(2)
// Extract named arguments
const versionArg = args.find((arg) => arg.startsWith('--bump-version='))
const publishArg = args.find((arg) => arg.startsWith('--publish='))
const scopeArg = args.find((arg) => arg.startsWith('--scope='))
const otpArg = args.find((arg) => arg.startsWith('--otp='))
const startWithArg = args.find((arg) => arg.startsWith('--start-with='))
const version = versionArg?.split('=')[1]
let tag = publishArg?.split('=')[1]
const scope = scopeArg?.split('=')[1] ?? DEFAULT_SCOPE
const otp = otpArg?.split('=')[1]
const startWith = startWithArg?.split('=')[1]
// No flags: publish current versions under `latest` (after a manual version bump)
if (!version && !tag) {
tag = 'latest'
}
if (startWith && !PUBLISH_ORDER.includes(startWith)) {
console.error(`Error: Unknown package "${startWith}" for --start-with`)
console.error(`Valid packages: ${PUBLISH_ORDER.join(', ')}`)
process.exit(1)
}
return { version, tag, scope, otp, startWith }
}
function readPackageJson(packagePath: string): PackageJson {
const filePath = join(packagePath, 'package.json')
const content = readFileSync(filePath, 'utf-8')
return JSON.parse(content) as PackageJson
}
function writePackageJson(packagePath: string, packageJson: PackageJson): void {
const filePath = join(packagePath, 'package.json')
writeFileSync(filePath, JSON.stringify(packageJson, null, 2) + '\n', 'utf-8')
}
function updateDependencies(
deps: Record<string, string> | undefined,
newVersion: string,
targetScope: string
): Record<string, string> | undefined {
if (!deps) return undefined
const updated: Record<string, string> = {}
for (const [depName, depVersion] of Object.entries(deps)) {
if (depName.startsWith(`@${DEFAULT_SCOPE}/`)) {
const packageName = depName.replace(`@${DEFAULT_SCOPE}/`, '')
if (!ACTIVE_PACKAGES.includes(packageName)) {
updated[depName] = depVersion
continue
}
const prefixMatch = depVersion.match(/^([\^~])?/)
const prefix = prefixMatch?.[1] || ''
const newDepName = `@${targetScope}/${packageName}`
updated[newDepName] = prefix ? `${prefix}${newVersion}` : newVersion
} else {
updated[depName] = depVersion
}
}
return updated
}
function updatePackages(
packages: PackageInfo[],
newVersion: string,
targetScope: string,
updateVersion: boolean = true
): void {
const isFork = targetScope !== DEFAULT_SCOPE
const mode = updateVersion ? 'version + deps' : 'deps only'
console.log(`\n📦 Updating packages (${mode}) to ${newVersion}...\n`)
for (const pkg of packages) {
if (updateVersion) {
console.log(` Updating ${pkg.name}...`)
pkg.packageJson.version = newVersion
} else {
console.log(` Updating deps in ${pkg.name} (version stays at ${pkg.oldVersion})...`)
}
if (isFork && updateVersion) {
pkg.packageJson.name = pkg.packageJson.name.replace(
`@${DEFAULT_SCOPE}/`,
`@${targetScope}/`
)
}
if (pkg.packageJson.dependencies) {
pkg.packageJson.dependencies =
updateDependencies(pkg.packageJson.dependencies, newVersion, targetScope) ||
pkg.packageJson.dependencies
}
if (pkg.packageJson.devDependencies) {
pkg.packageJson.devDependencies =
updateDependencies(pkg.packageJson.devDependencies, newVersion, targetScope) ||
pkg.packageJson.devDependencies
}
writePackageJson(pkg.path, pkg.packageJson)
}
console.log(`\n✅ All packages updated (${mode})\n`)
}
/**
* Rewrites all `@ethereumjs/` references to `@<targetScope>/` across source,
* compiled output, and type declarations for each active package.
*/
function rewriteImports(packages: PackageInfo[], targetScope: string): void {
console.log(`\n🔄 Rewriting imports: @${DEFAULT_SCOPE}/ → @${targetScope}/...\n`)
const dirs = ['src', 'dist/esm', 'dist/cjs']
const extensions = ['.ts', '.js', '.d.ts', '.d.ts.map', '.js.map']
let totalFiles = 0
for (const pkg of packages) {
let pkgCount = 0
for (const dir of dirs) {
const targetDir = join(pkg.path, dir)
if (!existsSync(targetDir)) continue
let files: string[]
try {
files = readdirSync(targetDir, { recursive: true, encoding: 'utf-8' })
.filter((f) => extensions.some((ext) => f.endsWith(ext)))
} catch {
continue
}
for (const file of files) {
const filePath = join(targetDir, file)
const content = readFileSync(filePath, 'utf-8')
const rewritten = content.split(`@${DEFAULT_SCOPE}/`).join(`@${targetScope}/`)
if (rewritten !== content) {
writeFileSync(filePath, rewritten, 'utf-8')
pkgCount++
}
}
}
if (pkgCount > 0) {
console.log(` ${pkg.name}: ${pkgCount} file(s) rewritten`)
totalFiles += pkgCount
}
}
console.log(`\n✅ Imports rewritten (${totalFiles} files total)\n`)
}
/**
* Builds all packages under the current (original) scope so that TypeScript
* can resolve all monorepo-internal imports. Must run BEFORE any rewriting.
*
* Clears dist/ first to remove stale tsbuildinfo from previous (possibly
* partially-rewritten) runs that would cause incremental compilation failures.
*/
function buildPackages(packages: PackageInfo[]): void {
console.log('\n🧹 Cleaning dist/ directories...')
for (const pkg of packages) {
const distDir = join(pkg.path, 'dist')
if (existsSync(distDir)) {
execSync(`rm -rf ${distDir}`)
}
}
console.log('\n🔨 Building all packages (pre-rewrite)...\n')
for (const pkg of packages) {
console.log(` Building ${pkg.name}...`)
try {
execSync('npm run build', { cwd: pkg.path, stdio: 'pipe' })
} catch (error: any) {
console.error(` ❌ Build failed for ${pkg.name}`)
if (error.stdout) console.error(error.stdout.toString())
throw error
}
}
console.log('\n✅ All packages built\n')
}
function packagesInPublishOrder(packages: PackageInfo[]): PackageInfo[] {
const byName = new Map(packages.map((pkg) => [pkg.name, pkg]))
return PUBLISH_ORDER.map((name) => {
const pkg = byName.get(name)
if (!pkg) {
throw new Error(`Publish order references unknown package: ${name}`)
}
return pkg
})
}
function verifyNpmAuth(): void {
try {
const user = execSync('npm whoami', { encoding: 'utf-8' }).trim()
console.log(`\n🔐 npm authenticated as ${user}\n`)
} catch {
console.error('\n❌ Not authenticated with npm.')
console.error(' Run `npm login` or configure a token in ~/.npmrc before publishing.')
console.error(' See DEVELOPER.md (Releases) for auth options.\n')
process.exit(1)
}
}
function publishPackages(
packages: PackageInfo[],
tag: string,
isFork: boolean,
otp?: string,
startWith?: string,
): void {
const ignoreScripts = isFork ? ' --ignore-scripts' : ''
const otpFlag = otp ? ` --otp=${otp}` : ''
let orderedPackages = packagesInPublishOrder(packages)
if (startWith) {
const startIndex = PUBLISH_ORDER.indexOf(startWith)
orderedPackages = orderedPackages.slice(startIndex)
console.log(
`\n📦 Resuming npm publish from "${startWith}" (${orderedPackages.length} packages remaining)...\n`,
)
}
console.log(`\n🚀 Publishing packages with tag "${tag}"${isFork ? ' (--ignore-scripts)' : ''}...\n`)
verifyNpmAuth()
for (const pkg of orderedPackages) {
const displayName = pkg.packageJson.name
console.log(` Publishing ${displayName}...`)
try {
execSync(`npm publish --tag=${tag} --access=public${ignoreScripts}${otpFlag}`, {
cwd: pkg.path,
stdio: 'inherit',
})
console.log(` ✅ ${displayName} published successfully\n`)
} catch (error) {
console.error(` ❌ Failed to publish ${displayName}`)
const nextIndex = orderedPackages.indexOf(pkg) + 1
if (nextIndex < orderedPackages.length) {
const otpSuffix = otp ? ` --otp=${otp}` : ''
console.error(
` To resume, run: tsx scripts/release-npm.ts --publish=${tag} --start-with=${orderedPackages[nextIndex].name}${otpSuffix}`,
)
}
throw error
}
}
console.log('\n✅ All packages published\n')
}
async function main(): Promise<void> {
const { version, tag, scope, otp, startWith } = parseArgs()
const isFork = scope !== DEFAULT_SCOPE
console.log('\n' + '='.repeat(60))
console.log('EthereumJS Release Script')
console.log('='.repeat(60))
console.log(`Bump version: ${version ?? 'no'}`)
console.log(`Publish: ${tag ? `yes (tag: ${tag})` : 'no'}`)
if (startWith) {
console.log(`Start with: ${startWith}`)
}
if (isFork) {
console.log(`Scope: @${scope} (fork release)`)
}
console.log('='.repeat(60) + '\n')
const rootPath = process.cwd()
const packagesPath = join(rootPath, 'packages')
// Read all package.json files for active packages
const packages: PackageInfo[] = []
for (const packageName of ACTIVE_PACKAGES) {
const packagePath = join(packagesPath, packageName)
const packageJson = readPackageJson(packagePath)
packages.push({
name: packageName,
path: packagePath,
oldVersion: packageJson.version,
packageJson,
})
}
// Read package.json files for deps-only packages (deprecated + testdata)
// Skipped entirely for fork releases (not published, rewriting would break local dev)
const depsOnlyPackages: PackageInfo[] = []
if (!isFork) {
for (const packageName of DEPS_ONLY_PACKAGES) {
const packagePath = join(packagesPath, packageName)
const packageJson = readPackageJson(packagePath)
depsOnlyPackages.push({
name: packageName,
path: packagePath,
oldVersion: packageJson.version,
packageJson,
})
}
}
// Display current versions
console.log('Active packages:')
for (const pkg of packages) {
console.log(` ${pkg.name}: ${pkg.oldVersion}`)
}
if (!isFork) {
console.log('\nDeps-only packages (deprecated + testdata):')
for (const pkg of depsOnlyPackages) {
console.log(` ${pkg.name}: ${pkg.oldVersion}`)
}
}
try {
// Step 0: For fork releases, restore all package files to their committed
// state. A previous failed run may have left rewritten source files and
// modified package.json files that would break the build.
if (isFork) {
console.log('\n🔄 Restoring packages/ to committed state...')
try {
execSync('git checkout -- packages/', { cwd: rootPath, stdio: 'pipe' })
for (const pkg of packages) {
pkg.packageJson = readPackageJson(pkg.path)
pkg.oldVersion = pkg.packageJson.version
}
console.log(' ✅ Restored\n')
} catch {
console.log(' ⚠️ git checkout failed (not a git repo?), continuing...\n')
}
}
// Step 1: For fork releases, build all packages FIRST under the original
// scope so TypeScript can resolve monorepo-internal dependencies.
// After this, dist/ contains compiled output with @ethereumjs/ imports.
if (isFork && tag) {
buildPackages(packages)
}
// Step 2: Bump versions (if --bump-version is set)
if (version) {
updatePackages(packages, version, scope, true)
if (!isFork) {
updatePackages(depsOnlyPackages, version, scope, false)
}
} else {
console.log('\n📋 Skipping version bump (use --bump-version to update versions)\n')
}
// Step 3: Rewrite imports in src/ AND dist/ (fork releases only)
if (isFork) {
rewriteImports(packages, scope)
}
// Step 4: Publish packages (if --publish is set)
// Fork releases use --ignore-scripts to skip prepublishOnly (which would
// clean + rebuild and fail since TS can't resolve the rewritten scope).
if (tag) {
publishPackages(packages, tag, isFork, otp, startWith)
} else {
console.log('\n📋 Skipping publish (use --publish=<tag> or run without flags after bumping)\n')
}
console.log('\n' + '='.repeat(60))
console.log('✅ Release completed successfully!')
console.log('='.repeat(60) + '\n')
} catch (error) {
console.error('\n' + '='.repeat(60))
console.error('❌ Release failed!')
console.error('='.repeat(60))
console.error(error)
process.exit(1)
}
}
main().catch((error) => {
console.error('Fatal error:', error)
process.exit(1)
})