-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate-server-loc.ts
More file actions
139 lines (117 loc) · 3.68 KB
/
validate-server-loc.ts
File metadata and controls
139 lines (117 loc) · 3.68 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* Server LOC ceiling validator.
*
* Fails if any non-test file in a directory exceeds the configured line limit.
*
* @module
*/
import * as primitives from "@eserstack/primitives";
import * as cliParseArgs from "@std/cli/parse-args";
import type * as shellArgs from "@eserstack/shell/args";
import * as span from "@eserstack/streams/span";
import { runtime } from "@eserstack/standards/cross-runtime";
import { createCliContext, runCliMain } from "./cli-support.ts";
export type CheckServerLocOptions = {
readonly root?: string;
readonly directory: string;
readonly maxLines?: number;
readonly excludeSuffix?: string;
readonly extension?: string;
};
export type CheckServerLocResult = {
readonly violations: readonly { path: string; lines: number }[];
readonly checked: number;
readonly passed: boolean;
};
export const checkServerLoc = async (
options: CheckServerLocOptions,
): Promise<CheckServerLocResult> => {
const {
root = ".",
directory,
maxLines = 500,
excludeSuffix = "_test.go",
extension = "go",
} = options;
const ext = extension.startsWith(".") ? extension.slice(1) : extension;
const dirPath = runtime.path.join(root, directory);
const violations: { path: string; lines: number }[] = [];
let checked = 0;
for await (
const entry of runtime.fs.walk(dirPath, {
exts: [ext],
includeDirs: false,
})
) {
if (entry.path.endsWith(excludeSuffix)) {
continue;
}
const content = await runtime.fs.readTextFile(entry.path);
const lines = content.split("\n").length;
checked++;
if (lines > maxLines) {
violations.push({ path: entry.path, lines });
}
}
return { violations, checked, passed: violations.length === 0 };
};
export const main = async (
cliArgs?: readonly string[],
): Promise<shellArgs.CliResult<void>> => {
const { output: out } = createCliContext();
const parsed = cliParseArgs.parseArgs((cliArgs ?? []) as string[], {
string: ["directory", "exclude-suffix", "extension"],
default: {},
});
const directory = parsed["directory"] as string | undefined;
if (!directory) {
out.writeln(
span.red("✗"),
span.text(" validate-server-loc requires a --directory argument"),
);
return primitives.results.fail({ exitCode: 1 });
}
const maxLinesRaw = parsed["max-lines"];
const maxLines = maxLinesRaw !== undefined ? Number(maxLinesRaw) : undefined;
try {
const result = await checkServerLoc({
root: ".",
directory,
maxLines,
excludeSuffix: parsed["exclude-suffix"] as string | undefined,
extension: parsed["extension"] as string | undefined,
});
out.writeln(
span.blue("ℹ"),
span.text(` Checked ${result.checked} files in ${directory}.`),
);
if (!result.passed) {
out.writeln(
span.red("✗"),
span.text(
` Found ${result.violations.length} file(s) exceeding the line ceiling:`,
),
);
for (const v of result.violations) {
out.writeln(
span.yellow("⚠"),
span.text(` ${v.path}: ${v.lines} lines (limit ${maxLines ?? 500})`),
);
}
return primitives.results.fail({ exitCode: 1 });
}
out.writeln(
span.green("✓"),
span.text(" All files within the line ceiling."),
);
return primitives.results.ok(undefined);
} catch (err) {
out.writeln(span.red("✗"), span.text(` ${String(err)}`));
return primitives.results.fail({ exitCode: 1 });
}
};
if (import.meta.main) {
const { output: out } = createCliContext();
runCliMain(await main(Deno.args), out);
}