|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import {execSync} from 'node:child_process'; |
8 | | -import {existsSync, readFileSync, writeFileSync} from 'node:fs'; |
| 8 | +import {existsSync, readFileSync, writeFileSync, rmSync} from 'node:fs'; |
9 | 9 | import {resolve} from 'node:path'; |
10 | 10 |
|
11 | 11 | const projectRoot = process.cwd(); |
@@ -51,29 +51,60 @@ function ensureSubmodule(): void { |
51 | 51 | return; |
52 | 52 | } |
53 | 53 |
|
54 | | - console.log('Initializing devtools-frontend submodule...'); |
| 54 | + console.log( |
| 55 | + 'Initializing devtools-frontend submodule with sparse checkout...', |
| 56 | + ); |
55 | 57 | try { |
56 | | - execSync('git submodule update --init --depth 1', { |
| 58 | + const gitModuleDir = resolve( |
| 59 | + projectRoot, |
| 60 | + '.git', |
| 61 | + 'modules', |
| 62 | + 'devtools-frontend', |
| 63 | + ); |
| 64 | + // If the submodule was previously initialized (e.g. as a full checkout), |
| 65 | + // the existing directories will cause `git clone` or `absorbgitdirs` to fail. |
| 66 | + // We clean them up here to ensure the sparse checkout initialization succeeds. |
| 67 | + if (existsSync(devtoolsFrontendDir)) { |
| 68 | + rmSync(devtoolsFrontendDir, {recursive: true, force: true}); |
| 69 | + } |
| 70 | + if (existsSync(gitModuleDir)) { |
| 71 | + rmSync(gitModuleDir, {recursive: true, force: true}); |
| 72 | + } |
| 73 | + |
| 74 | + // 1. Clone only the tree structure of the default branch (no blobs) |
| 75 | + execSync( |
| 76 | + 'git clone --no-checkout --depth 1 --filter=blob:none https://github.com/ChromeDevTools/devtools-frontend.git third_party/devtools-frontend', |
| 77 | + { |
| 78 | + cwd: projectRoot, |
| 79 | + stdio: 'inherit', |
| 80 | + }, |
| 81 | + ); |
| 82 | + |
| 83 | + // 2. Set the sparse checkout configuration |
| 84 | + execSync( |
| 85 | + 'git sparse-checkout set --no-cone "/*" "!/*/" "/front_end/" "!/front_end/panels/" "!/front_end/ui/" "/mcp/" "/extension-api/"', |
| 86 | + { |
| 87 | + cwd: devtoolsFrontendDir, |
| 88 | + stdio: 'inherit', |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + // 3. Move the submodule's .git directory to the superproject's .git/modules |
| 93 | + execSync('git submodule absorbgitdirs third_party/devtools-frontend', { |
57 | 94 | cwd: projectRoot, |
58 | 95 | stdio: 'inherit', |
59 | 96 | }); |
60 | | - } catch (submoduleError) { |
61 | | - console.warn('git submodule update failed:', submoduleError); |
62 | | - } |
63 | 97 |
|
64 | | - if (!existsSync(mcpEntry)) { |
65 | | - console.log('Fetching devtools-frontend...'); |
66 | | - try { |
67 | | - execSync( |
68 | | - 'git clone --depth 1 https://github.com/ChromeDevTools/devtools-frontend.git third_party/devtools-frontend', |
69 | | - { |
70 | | - cwd: projectRoot, |
71 | | - stdio: 'inherit', |
72 | | - }, |
73 | | - ); |
74 | | - } catch (cloneError) { |
75 | | - console.error('Failed to clone devtools-frontend:', cloneError); |
76 | | - } |
| 98 | + // 4. Update the submodule to the correct commit (this fetches the commit and only downloads the blobs for the sparse checkout) |
| 99 | + execSync( |
| 100 | + 'git submodule update --force --checkout third_party/devtools-frontend', |
| 101 | + { |
| 102 | + cwd: projectRoot, |
| 103 | + stdio: 'inherit', |
| 104 | + }, |
| 105 | + ); |
| 106 | + } catch (error) { |
| 107 | + console.error('Failed to initialize devtools-frontend:', error); |
77 | 108 | } |
78 | 109 | } |
79 | 110 |
|
|
0 commit comments