Skip to content

Commit ebaf82c

Browse files
committed
feat(migrate): add monorepo support for tsup migration script
Resolves #452
1 parent 8ed1b9f commit ebaf82c

File tree

13 files changed

+249
-5
lines changed

13 files changed

+249
-5
lines changed

packages/migrate/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"dependencies": {
5151
"@antfu/ni": "catalog:migrate",
5252
"@ast-grep/napi": "catalog:migrate",
53+
"@manypkg/get-packages": "catalog:migrate",
5354
"ansis": "catalog:prod",
5455
"cac": "catalog:prod",
5556
"consola": "catalog:migrate",

packages/migrate/src/index.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import path from 'node:path'
12
import process from 'node:process'
23
import { getCliCommand, parseNi, run } from '@antfu/ni'
3-
import { green, underline } from 'ansis'
4+
import { getPackagesSync } from '@manypkg/get-packages'
5+
import { green, greenBright, underline } from 'ansis'
46
import consola from 'consola'
57
import { migratePackageJson } from './helpers/package-json.ts'
68
import { migrateTsupConfig } from './helpers/tsup-config.ts'
@@ -29,11 +31,53 @@ export async function migrate({ cwd, dryRun }: MigrateOptions): Promise<void> {
2931

3032
if (cwd) process.chdir(cwd)
3133

32-
let migrated = await migratePackageJson(dryRun)
33-
if (await migrateTsupConfig(dryRun)) {
34-
migrated = true
34+
const traverseStartCwd = process.cwd()
35+
const { rootPackage, packages } = getPackagesSync(traverseStartCwd)
36+
const cwds = new Set([traverseStartCwd])
37+
// There are 3 possible cases here:
38+
// 1. The current directory is a package root without a monorepo configuration.
39+
// In this case, `packages` will contain only the current directory. We use a
40+
// Set to avoid running it twice.
41+
// 2. The current directory is not a package root, which means it is an internal
42+
// package within a monorepo. In this situation, we assume the user intentionally
43+
// specified the migration scope, so we operate only on the current directory.
44+
// 3. The current directory is a package root and has a monorepo configuration.
45+
// In this case, `packages` contains only the discovered subpackage directories,
46+
// and we are going to include them.
47+
if (rootPackage?.dir === traverseStartCwd) {
48+
for (const { dir } of packages) {
49+
cwds.add(dir)
50+
}
51+
}
52+
53+
let migratedAny = false
54+
55+
try {
56+
for (const dir of cwds) {
57+
process.chdir(dir)
58+
59+
const relativeDirLabel = greenBright(
60+
path.relative(traverseStartCwd, dir) || 'the current directory',
61+
)
62+
consola.info(`Processing ${relativeDirLabel}`)
63+
64+
let migrated = await migratePackageJson(dryRun)
65+
if (await migrateTsupConfig(dryRun)) {
66+
migrated = true
67+
}
68+
69+
if (!migrated) {
70+
consola.info(`No migrations to apply in ${relativeDirLabel}.`)
71+
continue
72+
}
73+
74+
migratedAny = true
75+
}
76+
} finally {
77+
process.chdir(traverseStartCwd)
3578
}
36-
if (!migrated) {
79+
80+
if (!migratedAny) {
3781
consola.error('No migration performed.')
3882
process.exitCode = 1
3983
return
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "issue-452",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"build": "pnpm --filter \"*\" build"
7+
}
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "pkg-app",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"main": "./src/index.ts",
7+
"dependencies": {
8+
"pkg2": "workspace:*"
9+
},
10+
"devDependencies": {
11+
"tsup": "catalog:"
12+
},
13+
"scripts": {
14+
"build": "tsup"
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { add } from 'pkg2'
2+
3+
export const answer: number = add(40, 2)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'tsup'
2+
3+
export default defineConfig({
4+
entry: ['src/index.ts'],
5+
format: ['esm'],
6+
dts: true,
7+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "pkg2",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"main": "./src/index.ts",
7+
"exports": {
8+
".": "./src/index.ts"
9+
},
10+
"devDependencies": {
11+
"tsup": "catalog:"
12+
},
13+
"scripts": {
14+
"build": "tsup"
15+
},
16+
"tsup": {
17+
"entry": [
18+
"src/index.ts"
19+
]
20+
}
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const add = (a: number, b: number) => a + b
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
packages:
2+
- packages/*
3+
4+
catalog:
5+
tsup: 8.5.1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "standalone-pkg",
3+
"version": "0.0.0",
4+
"type": "module",
5+
"packageManager": "[email protected]",
6+
"scripts": {
7+
"build": "tsup src/index.ts"
8+
},
9+
"dependencies": {
10+
"tsup": "^7.2.0"
11+
}
12+
}

0 commit comments

Comments
 (0)