Skip to content

Commit 9c6e747

Browse files
committed
refactor: improve cli impl
1 parent e4ac409 commit 9c6e747

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

src/cli.ts

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,63 @@ const main = defineCommand({
2929
},
3030
},
3131
async setup({ args }) {
32-
const fileUpdates = await automd({
32+
const results = await automd({
3333
dir: args.dir,
3434
input: args.input,
3535
output: args.output,
3636
});
3737

38-
if (fileUpdates.length === 0) {
38+
if (results.length === 0) {
3939
consola.warn(`No files processed!`);
4040
process.exit(1);
4141
}
42-
4342
consola.success(
44-
`Automd updated in \`${relative(process.cwd(), fileUpdates[0]._config.dir)}\``,
43+
`Automd updated in \`${relative(process.cwd(), results[0]._config.dir)}\``,
4544
);
45+
_printResults(results);
46+
},
47+
});
4648

47-
const changeTypes = {
48-
updated: { label: "updated", color: getColor("blue") },
49-
noChanges: { label: "no changes", color: getColor("green") },
50-
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;
61-
};
49+
runMain(main);
6250

63-
for (const res of fileUpdates) {
64-
const [input, output] = [res.input, res.output].map((i) =>
65-
relative(res._config.dir, i),
66-
);
67-
const t = getChangeType(res);
51+
// --- internal utils ---
6852

69-
const f = `${input === output ? ` ${input}` : ` ${input} ~> ${output}`}`;
53+
const _types = {
54+
updated: { label: "updated", color: getColor("blue") },
55+
noChanges: { label: "no changes", color: getColor("green") },
56+
alreadyUpdate: { label: "already up-to-date", color: getColor("gray") },
57+
issues: { label: "with issues", color: getColor("yellow") },
58+
};
7059

71-
consola.log(t.color(` ─ ${f} ${t.label}`));
60+
function _printResults(results: AutomdResult[]) {
61+
for (const res of results) {
62+
const type = _getChangeType(res);
63+
const input = relative(res._config.dir, res.input);
64+
const output = relative(res._config.dir, res.output);
65+
const name = `${input === output ? ` ${input}` : ` ${input} ~> ${output}`}`;
66+
consola.log(type.color(` ─ ${name} ${type.label}`));
67+
}
68+
const issues = results
69+
.filter((res) => res.hasIssues)
70+
.map((res) => _formatIssues(res));
71+
if (issues.length > 0) {
72+
consola.warn(`Some issues happened during update:`);
73+
for (const issue of issues) {
74+
consola.error(issue);
7275
}
76+
}
77+
}
7378

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-
);
79+
function _getChangeType(res: AutomdResult) {
80+
if (res.updates.length === 0) {
81+
return _types.alreadyUpdate;
82+
}
83+
if (res.hasIssues) {
84+
return _types.issues;
85+
}
86+
return res.hasChanged ? _types.updated : _types.noChanges;
87+
}
8088

81-
if (issues.length > 0) {
82-
consola.warn(`Some issues happened during update!`);
83-
for (const issue of issues) {
84-
consola.error(issue);
85-
}
86-
}
87-
},
88-
});
89-
90-
runMain(main);
89+
function _formatIssues(res: AutomdResult) {
90+
return `${_types.issues.color(relative(res._config.dir, res.input))} \n\n ${res.updates.flatMap((u) => u.result.issues).join("\n")}`;
91+
}

0 commit comments

Comments
 (0)