Skip to content

Commit c02dc1b

Browse files
committed
fix: graceful issue handling
1 parent 40e11ba commit c02dc1b

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

src/cli.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { defineCommand, runMain } from "citty";
55
import consola from "consola";
66
import { getColor } from "consola/utils";
77
import { name, description, version } from "../package.json";
8-
import { automd } from "./automd";
8+
import { AutomdResult, automd } from "./automd";
99

1010
const main = defineCommand({
1111
meta: {
@@ -48,20 +48,41 @@ const main = defineCommand({
4848
updated: { label: "updated", color: getColor("blue") },
4949
noChanges: { label: "no changes", color: getColor("green") },
5050
alreadyUpdate: { label: "already up-to-date", color: getColor("gray") },
51+
issues: { label: "with issues", color: getColor("yellow") },
52+
};
53+
const getChangeType = (res: AutomdResult) => {
54+
if (res.updates.length === 0) {
55+
return changeTypes.alreadyUpdate;
56+
}
57+
if (res.hasIssues) {
58+
return changeTypes.issues;
59+
}
60+
return res.hasChanged ? changeTypes.updated : changeTypes.noChanges;
5161
};
5262

53-
for (const f of fileUpdates) {
54-
const [input, output] = [f.input, f.output].map((i) =>
55-
relative(f._config.dir, i),
63+
for (const res of fileUpdates) {
64+
const [input, output] = [res.input, res.output].map((i) =>
65+
relative(res._config.dir, i),
5666
);
57-
const fileStr =
58-
input === output ? ` ${input}` : ` ${input} ~> ${output}`;
67+
const t = getChangeType(res);
68+
69+
const f = `${input === output ? ` ${input}` : ` ${input} ~> ${output}`}`;
5970

60-
const t =
61-
// prettier-ignore
62-
f.updates.length === 0 ? changeTypes.alreadyUpdate : (f.hasChanged ? changeTypes.updated : changeTypes.noChanges);
71+
consola.log(t.color(` ─ ${f} ${t.label}`));
72+
}
73+
74+
const issues = fileUpdates
75+
.filter((f) => f.hasIssues)
76+
.map(
77+
(f) =>
78+
`${changeTypes.issues.color(relative(f._config.dir, f.input))} \n\n ${f.updates.flatMap((u) => u.result.issues).join("\n")}`,
79+
);
6380

64-
consola.log(t.color(` ─ ${fileStr} ${t.label}`));
81+
if (issues.length > 0) {
82+
consola.warn(`Some issues happened during update!`);
83+
for (const issue of issues) {
84+
consola.error(issue);
85+
}
6586
}
6687
},
6788
});

src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface GenerateContext {
99

1010
export interface GenerateResult {
1111
contents: string;
12-
warnings?: string[];
12+
issues?: string[];
1313
}
1414

1515
export interface Generator {

src/transform.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Config, ResolvedConfig, resolveConfig } from "./config";
66

77
export interface TransformResult {
88
hasChanged: boolean;
9+
hasIssues: boolean;
910
contents: string;
1011
updates: { block: Block; result: GenerateResult }[];
1112
}
@@ -39,6 +40,7 @@ export async function transform(
3940

4041
return {
4142
hasChanged: editor.hasChanged(),
43+
hasIssues: updates.some((u) => u.result.issues?.length),
4244
contents: editor.toString(),
4345
updates,
4446
};
@@ -55,10 +57,10 @@ async function _transformBlock(
5557
if (!generator) {
5658
const didYouMean = await import("didyoumean2").then((r) => r.default || r);
5759
const suggestions = didYouMean(block.generator, Object.keys(generators));
58-
const warn = `[automd] Unknown generator:\`${block.generator}\`.${suggestions ? ` Did you mean "generator:\`${suggestions}\`"?` : ""}`;
60+
const error = `Unknown generator:\`${block.generator}\`.${suggestions ? ` Did you mean "generator:\`${suggestions}\`"?` : ""}`;
5961
return {
60-
contents: `/* ${warn} */`,
61-
warnings: [warn],
62+
contents: `<!-- ⚠️ ${error} -->`,
63+
issues: [error],
6264
};
6365
}
6466

@@ -68,7 +70,14 @@ async function _transformBlock(
6870
block,
6971
};
7072

71-
const result = (await generator.generate(context)) as GenerateResult;
72-
73-
return result;
73+
try {
74+
const result = (await generator.generate(context)) as GenerateResult;
75+
return result;
76+
} catch (_error: any) {
77+
const error = `(${block.generator}) ${_error.message || _error}`;
78+
return {
79+
contents: `<!-- ⚠️ ${error} -->`,
80+
issues: [error],
81+
};
82+
}
7483
}

0 commit comments

Comments
 (0)