2026-05-10 23:12:26 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
import { constants } from 'node:fs';
|
|
|
|
|
import { access, chmod } from 'node:fs/promises';
|
|
|
|
|
import { dirname, resolve } from 'node:path';
|
|
|
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
export function ktxRootDir() {
|
2026-05-10 23:12:26 +02:00
|
|
|
return resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
export function cliBinPath(rootDir = ktxRootDir()) {
|
2026-05-10 23:12:26 +02:00
|
|
|
return resolve(rootDir, 'packages', 'cli', 'dist', 'bin.js');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function canExecute(path) {
|
|
|
|
|
try {
|
|
|
|
|
await access(path, constants.X_OK);
|
|
|
|
|
return true;
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
export async function ensureCliBinExecutable(rootDir = ktxRootDir()) {
|
2026-05-10 23:12:26 +02:00
|
|
|
const binPath = cliBinPath(rootDir);
|
|
|
|
|
await access(binPath, constants.R_OK);
|
|
|
|
|
|
|
|
|
|
if (process.platform !== 'win32' && !(await canExecute(binPath))) {
|
|
|
|
|
await chmod(binPath, 0o755);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return binPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
|
|
|
try {
|
|
|
|
|
const binPath = await ensureCliBinExecutable();
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write(`Prepared KTX CLI bin: ${binPath}\n`);
|
2026-05-10 23:12:26 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|