-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
format.mts
96 lines (87 loc) · 2.14 KB
/
format.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { sync } from "cross-spawn";
import { IndentationText, NewLineKind, Project, QuoteKind } from "ts-morph";
import type { LimitedUserConfig } from "./cli.mjs";
export const formatOutput = async (outputPath: string) => {
const project = new Project({
skipAddingFilesFromTsConfig: true,
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
newLineKind: NewLineKind.LineFeed,
quoteKind: QuoteKind.Double,
usePrefixAndSuffixTextForRename: false,
useTrailingCommas: true,
},
});
const sourceFiles = project.addSourceFilesAtPaths(`${outputPath}/**/*`);
const tasks = sourceFiles.map((sourceFile) => {
sourceFile.formatText();
sourceFile.fixMissingImports();
sourceFile.organizeImports();
return sourceFile.save();
});
await Promise.all(tasks);
};
type OutputProcesser = {
args: (path: string) => ReadonlyArray<string>;
command: string;
name: string;
};
const formatters: Record<
Extract<LimitedUserConfig["format"], string>,
OutputProcesser
> = {
biome: {
args: (path) => ["format", "--write", path],
command: "biome",
name: "Biome (Format)",
},
prettier: {
args: (path) => [
"--ignore-unknown",
path,
"--write",
"--ignore-path",
"./.prettierignore",
],
command: "prettier",
name: "Prettier",
},
};
/**
* Map of supported linters
*/
const linters: Record<
Extract<LimitedUserConfig["lint"], string>,
OutputProcesser
> = {
biome: {
args: (path) => ["lint", "--write", path],
command: "biome",
name: "Biome (Lint)",
},
eslint: {
args: (path) => [path, "--fix"],
command: "eslint",
name: "ESLint",
},
};
export const processOutput = async ({
output,
format,
lint,
}: {
output: string;
format?: "prettier" | "biome";
lint?: "biome" | "eslint";
}) => {
if (format) {
const module = formatters[format];
console.log(`✨ Running ${module.name} on queries`);
sync(module.command, module.args(output));
}
if (lint) {
const module = linters[lint];
console.log(`✨ Running ${module.name} on queries`);
sync(module.command, module.args(output));
}
};