fix(dasel): update converter for v3 CLI (#591)
This commit is contained in:
parent
15e4046c19
commit
3602f12113
|
|
@ -11,6 +11,10 @@ export const properties = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function buildDaselArgs(filePath: string, fileType: string, convertTo: string): string[] {
|
||||||
|
return ["--var", `data=${fileType}:file:${filePath}`, "--out", convertTo, "$data"];
|
||||||
|
}
|
||||||
|
|
||||||
export async function convert(
|
export async function convert(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
fileType: string,
|
fileType: string,
|
||||||
|
|
@ -19,14 +23,10 @@ export async function convert(
|
||||||
options?: unknown,
|
options?: unknown,
|
||||||
execFile: ExecFileFn = execFileOriginal, // to make it mockable
|
execFile: ExecFileFn = execFileOriginal, // to make it mockable
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const args: string[] = [];
|
const args = buildDaselArgs(filePath, fileType, convertTo);
|
||||||
|
|
||||||
args.push("--file", filePath);
|
|
||||||
args.push("--read", fileType);
|
|
||||||
args.push("--write", convertTo);
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
execFile("dasel", args, (error, stdout, stderr) => {
|
const childProcess = execFile("dasel", args, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(`error: ${error}`);
|
reject(`error: ${error}`);
|
||||||
return;
|
return;
|
||||||
|
|
@ -44,5 +44,7 @@ export async function convert(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
childProcess?.stdin?.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
import { ExecFileOptions } from "child_process";
|
import type { ChildProcess, ExecFileOptions } from "child_process";
|
||||||
|
|
||||||
export type ExecFileFn = (
|
export type ExecFileFn = (
|
||||||
cmd: string,
|
cmd: string,
|
||||||
args: string[],
|
args: string[],
|
||||||
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||||
options?: ExecFileOptions,
|
options?: ExecFileOptions,
|
||||||
) => void;
|
) => ChildProcess | void;
|
||||||
|
|
||||||
export type ConvertFnWithExecFile = (
|
export type ConvertFnWithExecFile = (
|
||||||
filePath: string,
|
filePath: string,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { beforeEach, afterEach, expect, test, describe } from "bun:test";
|
import { beforeEach, afterEach, expect, test, describe } from "bun:test";
|
||||||
import { convert } from "../../src/converters/dasel";
|
import { buildDaselArgs, convert } from "../../src/converters/dasel";
|
||||||
import type { ExecFileFn } from "../../src/converters/types";
|
import type { ExecFileFn } from "../../src/converters/types";
|
||||||
|
|
||||||
const originalWriteFile = fs.writeFile;
|
const originalWriteFile = fs.writeFile;
|
||||||
|
|
@ -21,6 +21,16 @@ describe("convert", () => {
|
||||||
fs.writeFile = originalWriteFile;
|
fs.writeFile = originalWriteFile;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("should build dasel v3 arguments", () => {
|
||||||
|
expect(buildDaselArgs("input.yaml", "yaml", "json")).toEqual([
|
||||||
|
"--var",
|
||||||
|
"data=yaml:file:input.yaml",
|
||||||
|
"--out",
|
||||||
|
"json",
|
||||||
|
"$data",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
test("should call dasel with correct arguments and write output", async () => {
|
test("should call dasel with correct arguments and write output", async () => {
|
||||||
let calledArgs: Parameters<ExecFileFn> = ["", [], () => {}];
|
let calledArgs: Parameters<ExecFileFn> = ["", [], () => {}];
|
||||||
mockExecFile = (cmd, args, callback) => {
|
mockExecFile = (cmd, args, callback) => {
|
||||||
|
|
@ -48,11 +58,23 @@ describe("convert", () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(calledArgs[0]).toBe("dasel");
|
expect(calledArgs[0]).toBe("dasel");
|
||||||
expect(calledArgs[1]).toEqual(["--file", "input.yaml", "--read", "yaml", "--write", "json"]);
|
expect(calledArgs[1]).toEqual(["--var", "data=yaml:file:input.yaml", "--out", "json", "$data"]);
|
||||||
expect(writeFileCalled).toBe(true);
|
expect(writeFileCalled).toBe(true);
|
||||||
expect(result).toBe("Done");
|
expect(result).toBe("Done");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("should close dasel stdin so v3 does not wait for input", async () => {
|
||||||
|
let stdinEnded = false;
|
||||||
|
mockExecFile = (cmd, args, callback) => {
|
||||||
|
callback(null, "output-data", "");
|
||||||
|
return { stdin: { end: () => (stdinEnded = true) } } as ReturnType<ExecFileFn>;
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("input.yaml", "yaml", "json", "output.json", undefined, mockExecFile);
|
||||||
|
|
||||||
|
expect(stdinEnded).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
test("should reject if execFile returns an error", async () => {
|
test("should reject if execFile returns an error", async () => {
|
||||||
mockExecFile = (cmd, args, callback) => callback(new Error("fail"), "", "");
|
mockExecFile = (cmd, args, callback) => callback(new Error("fail"), "", "");
|
||||||
await expect(
|
await expect(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue