|
| 1 | +import { join } from 'path'; |
| 2 | +import { spawn } from 'child_process'; |
| 3 | +import { input } from '@inquirer/prompts'; |
| 4 | +import { executeCommand } from '../runner/utils/exec.js'; |
| 5 | +import { readFile, writeFile } from 'fs/promises'; |
| 6 | + |
| 7 | +const root = join(import.meta.dirname, '..'); |
| 8 | +const distDirectory = join(root, 'dist'); |
| 9 | +const packageJsonPath = join(root, 'package.json'); |
| 10 | +const registry = 'https://wombat-dressing-room.appspot.com'; |
| 11 | + |
| 12 | +(async () => { |
| 13 | + try { |
| 14 | + const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8')) as { |
| 15 | + version: string; |
| 16 | + }; |
| 17 | + |
| 18 | + const version = await input({ |
| 19 | + message: `Which version would you like to publish? Current version is ${packageJson.version}`, |
| 20 | + required: true, |
| 21 | + }); |
| 22 | + |
| 23 | + // Build the project. |
| 24 | + await executeCommand( |
| 25 | + `pnpm release-build --version=${version}`, |
| 26 | + root, |
| 27 | + undefined, |
| 28 | + { |
| 29 | + forwardStdoutToParent: true, |
| 30 | + forwardStderrToParent: true, |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + // Log into our registry. |
| 35 | + await spawnInteractive('npm', ['login', '--registry', registry]); |
| 36 | + |
| 37 | + // Publish to npm. |
| 38 | + await executeCommand( |
| 39 | + `npm --registry ${registry} publish --access public --tag latest`, |
| 40 | + distDirectory, |
| 41 | + undefined, |
| 42 | + { |
| 43 | + forwardStderrToParent: true, |
| 44 | + forwardStdoutToParent: true, |
| 45 | + } |
| 46 | + ); |
| 47 | + |
| 48 | + // Write the package.json back to disk so the version is in sync. |
| 49 | + packageJson.version = version; |
| 50 | + await writeFile(packageJsonPath, JSON.stringify(packageJson, undefined, 2)); |
| 51 | + |
| 52 | + console.log('Done! 🎉'); |
| 53 | + console.log('Remember to push the changed package.json!'); |
| 54 | + } catch (e: unknown) { |
| 55 | + // If the user presses ctrl + c, Inquirer will throw `ExitPromptError`. Ignore it. |
| 56 | + if (!(e instanceof Error) || e.name !== 'ExitPromptError') { |
| 57 | + throw e; |
| 58 | + } |
| 59 | + } |
| 60 | +})(); |
| 61 | + |
| 62 | +function spawnInteractive(command: string, args: string[]) { |
| 63 | + return new Promise<void>((resolve, reject) => { |
| 64 | + const childProcess = spawn(command, args, { |
| 65 | + shell: true, |
| 66 | + stdio: 'inherit', |
| 67 | + }); |
| 68 | + |
| 69 | + childProcess.on('close', (status) => |
| 70 | + status === 0 ? resolve() : reject(status) |
| 71 | + ); |
| 72 | + }); |
| 73 | +} |
0 commit comments