|
| 1 | +import { Flags, ux } from '@oclif/core'; |
| 2 | +import { SelfHostedInstanceCommand } from '@powersync/cli-core'; |
| 3 | +import { spawn } from 'node:child_process'; |
| 4 | +import { createRequire } from 'node:module'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import open from 'open'; |
| 8 | + |
| 9 | +const require = createRequire(import.meta.url); |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | + |
| 12 | +export default class EditConfig extends SelfHostedInstanceCommand { |
| 13 | + static summary = 'Open the PowerSync configuration editor (Nitro preview).'; |
| 14 | + static description = |
| 15 | + 'Sets POWERSYNC_DIRECTORY for the current project and runs the editor Vite preview to edit config files.'; |
| 16 | + |
| 17 | + static examples = ['<%= config.bin %> edit config', '<%= config.bin %> edit config --directory ./powersync']; |
| 18 | + |
| 19 | + static flags = { |
| 20 | + ...SelfHostedInstanceCommand.flags, |
| 21 | + host: Flags.string({ |
| 22 | + description: 'Host to bind the editor preview server.', |
| 23 | + required: false, |
| 24 | + default: '0.0.0.0' |
| 25 | + }), |
| 26 | + port: Flags.integer({ |
| 27 | + description: 'Port for the editor preview server.', |
| 28 | + required: false, |
| 29 | + default: 3000 |
| 30 | + }) |
| 31 | + }; |
| 32 | + |
| 33 | + async run(): Promise<void> { |
| 34 | + const { flags } = await this.parse(EditConfig); |
| 35 | + const projectDir = this.ensureProjectDirExists(flags); |
| 36 | + |
| 37 | + const editorDir = path.resolve(__dirname, '../../..'); |
| 38 | + const targetDist = path.join(editorDir, 'editor-dist'); |
| 39 | + |
| 40 | + const env = { |
| 41 | + ...process.env, |
| 42 | + POWERSYNC_DIRECTORY: projectDir |
| 43 | + }; |
| 44 | + |
| 45 | + this.log('Launching PowerSync configuration editor...'); |
| 46 | + this.log(`Project directory: ${projectDir}`); |
| 47 | + this.log(`Editor path: ${editorDir}`); |
| 48 | + this.log(`Serving built editor from: ${targetDist}`); |
| 49 | + |
| 50 | + const vitePkg = require.resolve('vite/package.json'); |
| 51 | + const viteBin = path.join(path.dirname(vitePkg), 'bin', 'vite.js'); |
| 52 | + |
| 53 | + const child = spawn( |
| 54 | + process.execPath, |
| 55 | + [viteBin, 'preview', '--host', flags.host, '--port', `${flags.port}`, '--outDir', targetDist], |
| 56 | + { |
| 57 | + cwd: editorDir, |
| 58 | + env, |
| 59 | + stdio: 'inherit' |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + const urlHost = flags.host === '0.0.0.0' ? 'localhost' : flags.host; |
| 64 | + const previewUrl = `http://${urlHost}:${flags.port}`; |
| 65 | + |
| 66 | + child.on('spawn', () => { |
| 67 | + this.log(`Opening ${previewUrl} in your browser...`); |
| 68 | + void open(previewUrl).catch((err) => |
| 69 | + this.warn(`Could not open browser automatically: ${err instanceof Error ? err.message : String(err)}`) |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + child.on('exit', (code) => { |
| 74 | + if (code === 0) return; |
| 75 | + this.error(ux.colorize('red', `Editor exited with code ${code ?? 'unknown'}`)); |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
0 commit comments