|
5 | 5 | * (never a branch). Run via `pnpm sync` — also runs in `postinstall` so a |
6 | 6 | * fresh clone initializes the submodule automatically. |
7 | 7 | */ |
8 | | -import { existsSync } from 'node:fs' |
9 | | -import { readFile, writeFile } from 'node:fs/promises' |
| 8 | +import { existsSync, readdirSync } from 'node:fs' |
| 9 | +import { readFile, rm, writeFile } from 'node:fs/promises' |
10 | 10 | import process from 'node:process' |
11 | 11 | import { fileURLToPath } from 'node:url' |
12 | 12 | import { cac } from 'cac' |
@@ -63,8 +63,17 @@ async function tryGit(args: string[], opts: { cwd?: string } = {}): Promise<stri |
63 | 63 | } |
64 | 64 |
|
65 | 65 | async function ensureSubmoduleInit(): Promise<void> { |
66 | | - if (existsSync(resolve(ROOT, SUBMODULE, '.git'))) |
| 66 | + const submodulePath = resolve(ROOT, SUBMODULE) |
| 67 | + if (existsSync(resolve(submodulePath, '.git'))) |
67 | 68 | return |
| 69 | + // A non-empty directory without `.git` is leftover from a previous clone |
| 70 | + // whose git metadata was wiped (e.g. by `git clean -ffdx`). Submodule init |
| 71 | + // refuses to overwrite it, so clear it first — there's no git tracking, |
| 72 | + // hence nothing to lose beyond regeneratable build artifacts. |
| 73 | + if (existsSync(submodulePath) && readdirSync(submodulePath).length > 0) { |
| 74 | + console.log(`[sync] Clearing stale ${SUBMODULE}/ (missing .git link)…`) |
| 75 | + await rm(submodulePath, { recursive: true, force: true }) |
| 76 | + } |
68 | 77 | console.log(`[sync] Initializing ${SUBMODULE} submodule…`) |
69 | 78 | await git(['submodule', 'update', '--init', SUBMODULE]) |
70 | 79 | } |
|
0 commit comments