import { spawn } from 'node:child_process';
import { createRequire } from 'node:module';
import { dirname as pathDirname, resolve as pathResolve } from 'node:path';
import { isESModule } from '@intlayer/config';
type StartEditorOptions = {
env?: string;
envFile?: string;
};
export const startEditor = (options: StartEditorOptions): void => {
const args: string[] = ['start'];
if (options.env) args.push('--env', options.env);
if (options.envFile) args.push('--env-file', options.envFile);
const spawnNodeProcess = (binPath: string) =>
spawn(process.execPath, [binPath, ...args], {
stdio: 'inherit',
env: { ...process.env },
});
const spawnWith = (cmd: string, cmdArgs: string[]) =>
spawn(cmd, cmdArgs, {
stdio: 'inherit',
env: { ...process.env },
});
try {
const requireFunction = isESModule
? createRequire(import.meta.url)
: require;
const pkgJsonPath = requireFunction.resolve('intlayer-editor/package.json');
const pkgDir = pathDirname(pkgJsonPath);
const binPath = pathResolve(pkgDir, 'bin', 'intlayer-editor.mjs');
const child = spawnNodeProcess(binPath);
child.on('error', () => runFallback());
child.on('exit', (code) => {
if (code === 255) process.exit(code ?? 0);
});
} catch {
runFallback();
}
function runFallback() {
const bun = spawnWith('bun', ['dlx', 'intlayer-editor', ...args]);
bun.on('error', () => {
const npx = spawnWith('npx', ['-y', 'intlayer-editor', ...args]);
npx.on('error', (err) => {
console.error('Unable to execute intlayer-editor via bun or npx.');
console.error(String(err?.message ?? err));
process.exit(1);
});
});
}
};