|
1 | | -import fs from 'fs'; |
2 | | -import { execFileSync } from 'child_process'; |
| 1 | +import { readFile, access } from 'node:fs/promises'; |
| 2 | +import { execFileSync } from 'node:child_process'; |
3 | 3 |
|
4 | | -const webpackDir = './webpack'; |
5 | | -const headCommitFile = './HEAD_COMMIT'; |
| 4 | +const WEBPACK_DIR = 'webpack'; |
| 5 | +const REF = (await readFile('HEAD_COMMIT', 'utf-8')).trim(); |
6 | 6 |
|
7 | | -// Allow an env var override (used by CI) |
8 | | -let ref = process.env.WEBPACK_REF; |
9 | | - |
10 | | -if (!ref && fs.existsSync(headCommitFile)) { |
11 | | - ref = fs.readFileSync(headCommitFile, 'utf-8').trim(); |
12 | | -} |
13 | | - |
14 | | -if (!ref) { |
15 | | - // Default fallback if running locally before any syncing has happened |
16 | | - console.log( |
17 | | - `Warning: WEBPACK_REF not set and ${headCommitFile} not found. Defaulting to 'main'.` |
18 | | - ); |
19 | | - ref = 'main'; |
20 | | -} |
21 | | - |
22 | | -console.log(`Cloning webpack repository at ref: ${ref}...`); |
23 | | - |
24 | | -if (fs.existsSync(webpackDir)) { |
25 | | - console.log( |
26 | | - `Repository already exists at ${webpackDir}. Fetching latest updates...` |
27 | | - ); |
| 7 | +try { |
| 8 | + await access(WEBPACK_DIR); |
28 | 9 | execFileSync('git', ['fetch', '--all'], { |
29 | | - cwd: webpackDir, |
| 10 | + cwd: WEBPACK_DIR, |
30 | 11 | stdio: 'inherit', |
31 | 12 | }); |
32 | | -} else { |
33 | | - // Clone the repository |
34 | | - console.log('Running git clone https://github.com/webpack/webpack.git...'); |
| 13 | +} catch { |
35 | 14 | execFileSync( |
36 | 15 | 'git', |
37 | | - ['clone', 'https://github.com/webpack/webpack.git', webpackDir], |
| 16 | + ['clone', 'https://github.com/webpack/webpack.git', WEBPACK_DIR], |
38 | 17 | { |
39 | 18 | stdio: 'inherit', |
40 | 19 | } |
41 | 20 | ); |
42 | 21 | } |
43 | 22 |
|
44 | | -// Checkout the target commit |
45 | | -console.log(`Checking out commit/branch: ${ref}...`); |
46 | | -execFileSync('git', ['checkout', ref], { cwd: webpackDir, stdio: 'inherit' }); |
47 | | - |
48 | | -console.log('Successfully completed clone-webpack script.'); |
| 23 | +execFileSync('git', ['checkout', REF], { cwd: WEBPACK_DIR, stdio: 'inherit' }); |
0 commit comments