|
1 | | -import { execSync, spawn } from 'node:child_process'; |
2 | | -import { resolve } from 'node:path'; |
| 1 | +import childProcess from 'node:child_process'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
3 | 4 |
|
4 | | -const REGISTRY_URL = 'http://localhost:4873'; |
5 | | -const ROOT = resolve(import.meta.dirname, '..'); |
6 | | -const CONFIG = resolve(import.meta.dirname, 'config.yaml'); |
| 5 | +const [...publishCommandArgs] = process.argv.slice(2); |
| 6 | +const localRegistryConfig = `registry=http://localhost:4873 |
| 7 | +//localhost:4873/:_authToken="local-token" |
| 8 | +@lemoncode:registry=http://localhost:4873 |
| 9 | +`; |
| 10 | +const publishCommand = publishCommandArgs.join(' '); |
| 11 | +const NPMRC_FILE_PATH = path.resolve(process.cwd(), '.npmrc'); |
7 | 12 |
|
8 | | -const command = process.argv[2]; |
9 | | - |
10 | | -const startVerdaccio = () => |
11 | | - spawn('npx', ['verdaccio', '--config', CONFIG, '--listen', '4873'], { |
12 | | - cwd: import.meta.dirname, |
13 | | - stdio: 'inherit', |
14 | | - shell: true, |
15 | | - }); |
16 | | - |
17 | | -const waitForRegistry = async (url, retries = 30, delay = 1000) => { |
18 | | - for (let i = 0; i < retries; i++) { |
19 | | - try { |
20 | | - const res = await fetch(url); |
21 | | - if (res.ok) return; |
22 | | - } catch {} |
23 | | - await new Promise(r => setTimeout(r, delay)); |
24 | | - } |
25 | | - throw new Error(`Registry at ${url} did not start in time`); |
26 | | -}; |
27 | | - |
28 | | -const run = (cmd, opts = {}) => |
29 | | - execSync(cmd, { |
30 | | - stdio: 'inherit', |
31 | | - cwd: ROOT, |
32 | | - env: { ...process.env, npm_config_registry: REGISTRY_URL }, |
33 | | - ...opts, |
34 | | - }); |
35 | | - |
36 | | -if (command === 'start') { |
37 | | - // Just start Verdaccio (foreground) |
38 | | - const child = startVerdaccio(); |
39 | | - child.on('exit', code => process.exit(code ?? 0)); |
40 | | -} else if (command === 'publish') { |
41 | | - // Full flow: start Verdaccio → changeset version → changeset publish → stop |
42 | | - const child = startVerdaccio(); |
| 13 | +let originalContent = ''; |
| 14 | +try { |
| 15 | + originalContent = await fs.readFile(NPMRC_FILE_PATH, 'utf-8'); |
| 16 | +} catch { |
| 17 | + originalContent = ''; |
| 18 | +} |
43 | 19 |
|
44 | | - try { |
45 | | - console.log('Waiting for Verdaccio to start...'); |
46 | | - await waitForRegistry(REGISTRY_URL); |
47 | | - console.log('Verdaccio ready. Running changeset version...'); |
48 | | - run('npx changeset version'); |
49 | | - console.log('Publishing to local registry...'); |
50 | | - run('npx changeset publish'); |
51 | | - console.log('Done! Packages published to local Verdaccio.'); |
52 | | - } finally { |
53 | | - child.kill(); |
54 | | - } |
55 | | -} else { |
56 | | - console.error('Usage: node publish.js <start|publish>'); |
57 | | - process.exit(1); |
| 20 | +try { |
| 21 | + const newContent = originalContent.trimEnd() + '\n' + localRegistryConfig; |
| 22 | + await fs.writeFile(NPMRC_FILE_PATH, newContent); |
| 23 | + childProcess.execSync(`${publishCommand}`, { stdio: 'inherit', shell: true }); |
| 24 | +} finally { |
| 25 | + await fs.writeFile(NPMRC_FILE_PATH, originalContent); |
58 | 26 | } |
0 commit comments