Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function outputCollectionOptionError(command: Command, code: string, message: st
}
const exitCode = code === "path_not_found" ? 4
: code === "missing_config" || code === "invalid_config" ? 3
: 1;
: 1;
process.exit(exitCode);
}

Expand Down
5 changes: 3 additions & 2 deletions src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { BaseFile } from "../base/parser.js";
import { executeBase, BaseExecutionError } from "../base/executor.js";
import { printResults } from "../base/formatter.js";
import type { OutputFormat } from "../base/formatter.js";
import { closeAndExit } from "../utils.js";

export function registerBase(program: Command): void {
const base = program
Expand Down Expand Up @@ -64,15 +65,15 @@ export function registerBase(program: Command): void {
});

printResults(results, opts.format as OutputFormat);
process.exit(0);
await closeAndExit(collection, 0);
} catch (err) {
if (err instanceof BaseExecutionError) {
if (opts.format === "json") {
console.log(JSON.stringify({ error: { code: err.code, message: err.message } }, null, 2));
} else {
console.error(chalk.red(`error: ${err.message}`));
}
process.exit(err.code === "view_not_found" ? 1 : 1);
await closeAndExit(collection, err.code === "view_not_found" ? 1 : 1);
}
throw err;
}
Expand Down
28 changes: 15 additions & 13 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import chalk from "chalk";
import { Collection } from "@callumalpass/mdbase";
import type { MdbaseError } from "@callumalpass/mdbase";
import yaml from "js-yaml";
import { parseFieldValue } from "../utils.js";
import { parseFieldValue, closeAndExit } from "../utils.js";

function parseFields(fieldArgs: string[]): Record<string, unknown> {
async function parseFields(fieldArgs: string[], collection: { close(): Promise<void> } | null): Promise<Record<string, unknown>> {
const frontmatter: Record<string, unknown> = {};
for (const f of fieldArgs) {
const eqIdx = f.indexOf("=");
if (eqIdx === -1) {
console.error(chalk.red(`error: invalid field format: ${f} (expected key=value)`));
process.exit(1);
await closeAndExit(collection, 1);
}
const key = f.slice(0, eqIdx);
const rawValue = f.slice(eqIdx + 1);
Expand Down Expand Up @@ -47,12 +47,12 @@ export function registerCreate(program: Command): void {
} else {
console.error(chalk.red(`error: ${openResult.error.message}`));
}
process.exit(3);
await closeAndExit(null, 3);
}
const collection = openResult.collection!;

// Parse field values
const frontmatter = opts.field ? parseFields(opts.field as string[]) : {};
const frontmatter = opts.field ? await parseFields(opts.field as string[], collection) : {};

// Read body from stdin if requested
let body: string | undefined = opts.body;
Expand Down Expand Up @@ -86,7 +86,7 @@ export function registerCreate(program: Command): void {
}

const result = await collection.create(input);
outputResult(result, relativePath, opts);
await outputResult(result, relativePath, opts, collection);
});
}

Expand All @@ -103,17 +103,18 @@ function formatIssue(issue: MdbaseError): string {
return ` ${tag}${field}: ${issue.message} ${chalk.dim(`[${issue.code}]`)}`;
}

function outputResult(
async function outputResult(
result: { valid?: boolean; frontmatter?: Record<string, unknown>; body?: string; path?: string; error?: { code: string; message: string }; issues?: MdbaseError[] },
requestedPath: string | undefined,
opts: { format: string },
): void {
collection: { close(): Promise<void> },
): Promise<void> {
if (result.error) {
const exitCode = result.error.code === "path_conflict" ? 1
: result.error.code === "unknown_type" ? 1
: result.error.code === "validation_failed" ? 2
: result.error.code === "permission_denied" ? 5
: 1;
: result.error.code === "validation_failed" ? 2
: result.error.code === "permission_denied" ? 5
: 1;

if (opts.format === "json") {
const output: Record<string, unknown> = { error: result.error };
Expand All @@ -129,7 +130,8 @@ function outputResult(
}
}
}
process.exit(exitCode);
await closeAndExit(collection, exitCode);
return;
}

const outputPath = result.path ?? requestedPath;
Expand Down Expand Up @@ -175,5 +177,5 @@ function outputResult(
}
}

process.exit(0);
await closeAndExit(collection, 0);
}
7 changes: 4 additions & 3 deletions src/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "node:path";
import chalk from "chalk";
import { Collection } from "@callumalpass/mdbase";
import yaml from "js-yaml";
import { closeAndExit } from "../utils.js";

export function registerDelete(program: Command): void {
program
Expand Down Expand Up @@ -35,14 +36,14 @@ export function registerDelete(program: Command): void {
if (result.error) {
const exitCode = result.error.code === "file_not_found" ? 4
: result.error.code === "permission_denied" ? 5
: 1;
: 1;

if (opts.format === "json") {
console.log(JSON.stringify({ error: result.error }, null, 2));
} else {
console.error(chalk.red(`error: ${result.error.message}`));
}
process.exit(exitCode);
await closeAndExit(collection, exitCode);
}

const brokenLinks = result.broken_links ?? [];
Expand Down Expand Up @@ -85,6 +86,6 @@ export function registerDelete(program: Command): void {
}
}

process.exit(0);
await closeAndExit(collection, 0);
});
}
9 changes: 5 additions & 4 deletions src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Command } from "commander";
import path from "node:path";
import chalk from "chalk";
import { Collection } from "@callumalpass/mdbase";
import { closeAndExit } from "../utils.js";

interface FieldDiff {
field: string;
Expand Down Expand Up @@ -78,15 +79,15 @@ export function registerDiff(program: Command): void {
} else {
console.error(chalk.red(`error: ${pathA}: ${readA.error.message}`));
}
process.exit(4);
await closeAndExit(collection, 4);
}
if (readB.error) {
if (opts.format === "json") {
console.log(JSON.stringify({ error: { ...readB.error, path: pathB } }, null, 2));
} else {
console.error(chalk.red(`error: ${pathB}: ${readB.error.message}`));
}
process.exit(4);
await closeAndExit(collection, 4);
}

const fmA = readA.frontmatter ?? {};
Expand Down Expand Up @@ -136,7 +137,7 @@ export function registerDiff(program: Command): void {
} else {
if (result.identical) {
console.log(chalk.green("Files are identical"));
process.exit(0);
await closeAndExit(collection, 0);
}

console.log(`${chalk.bold(pathA)} ${chalk.dim("vs")} ${chalk.bold(pathB)}`);
Expand Down Expand Up @@ -169,6 +170,6 @@ export function registerDiff(program: Command): void {
}
}

process.exit(0);
await closeAndExit(collection, 0);
});
}
6 changes: 3 additions & 3 deletions src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "node:fs";
import chalk from "chalk";
import { Collection } from "@callumalpass/mdbase";
import { stringify } from "csv-stringify/sync";
import { splitList } from "../utils.js";
import { splitList, closeAndExit } from "../utils.js";

function formatValue(value: unknown): string {
if (value === null || value === undefined) return "";
Expand Down Expand Up @@ -54,7 +54,7 @@ export function registerExport(program: Command): void {
} else {
console.error(chalk.red(`error: ${queryResult.error.message}`));
}
process.exit(1);
await closeAndExit(collection, 1);
}

const results = queryResult.results as Array<{
Expand Down Expand Up @@ -120,6 +120,6 @@ export function registerExport(program: Command): void {
console.log(output);
}

process.exit(0);
await closeAndExit(collection, 0);
});
}
7 changes: 4 additions & 3 deletions src/commands/fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Collection, loadConfig, loadTypes } from "@callumalpass/mdbase";
import type { TypeDefinition } from "@callumalpass/mdbase";
import yaml from "js-yaml";
import matter from "gray-matter";
import { closeAndExit } from "../utils.js";

interface FmtFileResult {
path: string;
Expand Down Expand Up @@ -59,7 +60,7 @@ export function registerFmt(program: Command): void {
} else {
console.error(chalk.red(`error: ${queryResult.error.message}`));
}
process.exit(1);
await closeAndExit(collection, 1);
}
filePaths = (queryResult.results ?? []).map((r: { path: string }) => r.path);
}
Expand Down Expand Up @@ -122,9 +123,9 @@ export function registerFmt(program: Command): void {

// In --check mode, exit 1 if any files need formatting
if (opts.check && changedCount > 0) {
process.exit(1);
await closeAndExit(collection, 1);
}
process.exit(0);
await closeAndExit(collection, 0);
});
}

Expand Down
11 changes: 6 additions & 5 deletions src/commands/graph.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from "commander";
import chalk from "chalk";
import { Collection } from "@callumalpass/mdbase";
import { closeAndExit } from "../utils.js";

const WIKILINK_RE = /\[\[([^\]|]+)(?:\|[^\]]+)?\]\]/g;

Expand Down Expand Up @@ -178,7 +179,7 @@ export function registerGraph(program: Command): void {
}
}
}
process.exit(0);
await closeAndExit(openResult.collection!, 0);
});

graph
Expand Down Expand Up @@ -216,7 +217,7 @@ export function registerGraph(program: Command): void {
}
}
}
process.exit(0);
await closeAndExit(openResult.collection!, 0);
});

graph
Expand Down Expand Up @@ -246,7 +247,7 @@ export function registerGraph(program: Command): void {
} else {
console.error(chalk.red(`error: ${readResult.error.message}`));
}
process.exit(4);
await closeAndExit(collection, 4);
}

const { incoming } = await buildGraph(collection);
Expand All @@ -264,7 +265,7 @@ export function registerGraph(program: Command): void {
}
}
}
process.exit(0);
await closeAndExit(collection, 0);
});

graph
Expand Down Expand Up @@ -317,6 +318,6 @@ export function registerGraph(program: Command): void {
console.log(` ${chalk.dim("Components:")} ${result.connected_components}`);
console.log(` ${chalk.dim("Density:")} ${result.density}`);
}
process.exit(0);
await closeAndExit(openResult.collection!, 0);
});
}
17 changes: 9 additions & 8 deletions src/commands/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "node:path";
import chalk from "chalk";
import { Collection } from "@callumalpass/mdbase";
import { parse } from "csv-parse/sync";
import { closeAndExit } from "../utils.js";

function coerceValue(value: string): unknown {
if (value === "true") return true;
Expand Down Expand Up @@ -54,7 +55,7 @@ export function registerImport(program: Command): void {
} else {
console.error(chalk.red(`error: file not found: ${file}`));
}
process.exit(4);
await closeAndExit(collection, 4);
}

const content = fs.readFileSync(csvPath, "utf-8");
Expand Down Expand Up @@ -102,7 +103,7 @@ export function registerImport(program: Command): void {
}
}

outputResult(result, opts.format);
outputResult(result, opts.format, collection);
});

imp
Expand Down Expand Up @@ -132,7 +133,7 @@ export function registerImport(program: Command): void {
} else {
console.error(chalk.red(`error: file not found: ${file}`));
}
process.exit(4);
await closeAndExit(collection, 4);
}

const content = fs.readFileSync(jsonPath, "utf-8");
Expand All @@ -145,7 +146,7 @@ export function registerImport(program: Command): void {
} else {
console.error(chalk.red("error: failed to parse JSON file"));
}
process.exit(1);
await closeAndExit(collection, 1);
return;
}

Expand All @@ -155,7 +156,7 @@ export function registerImport(program: Command): void {
} else {
console.error(chalk.red("error: JSON file must contain an array"));
}
process.exit(1);
await closeAndExit(collection, 1);
return;
}

Expand Down Expand Up @@ -204,11 +205,11 @@ export function registerImport(program: Command): void {
}
}

outputResult(result, opts.format);
outputResult(result, opts.format, collection);
});
}

function outputResult(result: ImportResult, format: string): void {
async function outputResult(result: ImportResult, format: string, collection: { close(): Promise<void> }): Promise<void> {
if (format === "json") {
console.log(JSON.stringify(result, null, 2));
} else {
Expand All @@ -228,5 +229,5 @@ function outputResult(result: ImportResult, format: string): void {
}
}

process.exit(result.failed > 0 ? 1 : 0);
await closeAndExit(collection, result.failed > 0 ? 1 : 0);
}
Loading