Подготовить HY2XS к production-сборке
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
export async function exists(path: string): Promise<boolean> {
|
||||
return await Bun.file(path).exists();
|
||||
}
|
||||
|
||||
export async function readText(path: string): Promise<string> {
|
||||
return await Bun.file(path).text();
|
||||
}
|
||||
|
||||
export async function writeText(path: string, data: string, mode?: number): Promise<void> {
|
||||
await Bun.write(path, data);
|
||||
if (mode !== undefined) {
|
||||
const result = Bun.spawnSync(["chmod", mode.toString(8), path], {
|
||||
stdout: "pipe",
|
||||
stderr: "pipe"
|
||||
});
|
||||
if (!result.success) {
|
||||
throw new Error(`chmod failed for ${path}: ${result.stderr.toString()}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function renderTemplate(template: string, values: Record<string, string | number>): string {
|
||||
let rendered = template;
|
||||
for (const [key, value] of Object.entries(values)) {
|
||||
rendered = rendered.replaceAll(`{{${key}}}`, String(value));
|
||||
}
|
||||
return rendered;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export function step(name: string): void {
|
||||
console.log(`\n[hy2xs] ==> ${name}`);
|
||||
}
|
||||
|
||||
export function info(message: string): void {
|
||||
console.log(`[hy2xs] ${message}`);
|
||||
}
|
||||
|
||||
export function fail(message: string): never {
|
||||
throw new Error(message);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { info } from "./log";
|
||||
|
||||
function shellQuote(value: unknown): string {
|
||||
const text = String(value);
|
||||
if (text.length === 0) {
|
||||
return "''";
|
||||
}
|
||||
return `'${text.replaceAll("'", "'\\''")}'`;
|
||||
}
|
||||
|
||||
function renderCommand(strings: TemplateStringsArray, values: unknown[]): string {
|
||||
let command = "";
|
||||
for (let i = 0; i < strings.length; i += 1) {
|
||||
command += strings[i];
|
||||
if (i < values.length) {
|
||||
command += shellQuote(values[i]);
|
||||
}
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
export async function run(command: TemplateStringsArray, ...args: unknown[]): Promise<string> {
|
||||
const rendered = renderCommand(command, args);
|
||||
const process = Bun.spawn(["sh", "-c", rendered], {
|
||||
stdout: "pipe",
|
||||
stderr: "pipe"
|
||||
});
|
||||
const [stdout, stderr, exitCode] = await Promise.all([
|
||||
new Response(process.stdout).text(),
|
||||
new Response(process.stderr).text(),
|
||||
process.exited
|
||||
]);
|
||||
if (exitCode !== 0) {
|
||||
throw new Error(`command failed (${exitCode}): ${rendered}\n${stderr.trim()}`);
|
||||
}
|
||||
return stdout.trim();
|
||||
}
|
||||
|
||||
export async function runVisible(command: TemplateStringsArray, ...args: unknown[]): Promise<void> {
|
||||
const rendered = renderCommand(command, args);
|
||||
info(`running: ${rendered}`);
|
||||
const process = Bun.spawn(["sh", "-c", rendered], {
|
||||
stdout: "inherit",
|
||||
stderr: "inherit"
|
||||
});
|
||||
const exitCode = await process.exited;
|
||||
if (exitCode !== 0) {
|
||||
throw new Error(`command failed (${exitCode}): ${rendered}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user