bend-lint 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -10
- package/package.json +2 -2
- package/src/linter/cli.bend +261 -185
- package/src/linter/{directives.bend → directives/impl.bend} +193 -153
- package/src/linter/directives/spec.bend +82 -0
- package/src/linter/seam.ts +387 -54
- package/src/{files → linter/shared/files}/effs/files.c +7 -0
- package/src/{files/main.bend → linter/shared/files/impl.bend} +24 -32
- package/src/linter/shared/files/spec.bend +39 -0
- package/src/linter/{fixes.bend → shared/fixes/impl.bend} +41 -45
- package/src/linter/shared/fixes/spec.bend +28 -0
- package/src/{json/main.bend → linter/shared/json/impl.bend} +39 -46
- package/src/linter/shared/json/spec.bend +24 -0
- package/src/{text/main.bend → linter/shared/text/impl.bend} +82 -73
- package/src/linter/shared/text/spec.bend +82 -0
- package/src/shared/bend2.ts +0 -171
- package/src/shared/result.ts +0 -57
- package/src/shared/utils.ts +0 -107
- /package/src/{files → linter/shared/files}/effs/files.js +0 -0
package/src/shared/utils.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// Imports
|
|
2
|
-
// =======
|
|
3
|
-
|
|
4
|
-
import { createHash } from "node:crypto";
|
|
5
|
-
import * as nodeFs from "node:fs";
|
|
6
|
-
import * as nodePath from "node:path";
|
|
7
|
-
import * as url from "node:url";
|
|
8
|
-
|
|
9
|
-
import { error, ok } from "./result.ts";
|
|
10
|
-
import type { Result } from "./result.ts";
|
|
11
|
-
|
|
12
|
-
// Types
|
|
13
|
-
// =====
|
|
14
|
-
|
|
15
|
-
// How a command ended: its exit code, and what it printed.
|
|
16
|
-
export type Outcome = { code: number; out: string; err: string };
|
|
17
|
-
|
|
18
|
-
// Functions
|
|
19
|
-
// =========
|
|
20
|
-
|
|
21
|
-
// How `cmd` ends, run in `cwd`, with `env` added to this process's environment.
|
|
22
|
-
export const getCommandOutcome = async (
|
|
23
|
-
cmd: string[],
|
|
24
|
-
cwd: string,
|
|
25
|
-
env: Record<string, string> = {},
|
|
26
|
-
input?: string,
|
|
27
|
-
): Promise<Outcome> => {
|
|
28
|
-
const child = Bun.spawn(cmd, {
|
|
29
|
-
cwd,
|
|
30
|
-
env: { ...process.env, ...env },
|
|
31
|
-
stdin: input === undefined ? "ignore" : new TextEncoder().encode(input),
|
|
32
|
-
stdout: "pipe",
|
|
33
|
-
stderr: "pipe",
|
|
34
|
-
});
|
|
35
|
-
const [code, out, err] = await Promise.all([
|
|
36
|
-
child.exited,
|
|
37
|
-
new Response(child.stdout).text(),
|
|
38
|
-
new Response(child.stderr).text(),
|
|
39
|
-
]);
|
|
40
|
-
return { code, out, err };
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const getErrorMessageFromUnknown = (e: unknown): string =>
|
|
44
|
-
e instanceof Error ? e.message : String(e);
|
|
45
|
-
|
|
46
|
-
export const getPathWithForwardSlashes = (p: string): string => p.split(nodePath.sep).join("/");
|
|
47
|
-
|
|
48
|
-
// f's value, or undefined if it throws.
|
|
49
|
-
export const tryGet = <T>(f: () => T): T | undefined => {
|
|
50
|
-
try {
|
|
51
|
-
return f();
|
|
52
|
-
} catch {
|
|
53
|
-
return undefined;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export const getSha256Hex = (text: string): string =>
|
|
58
|
-
createHash("sha256").update(text).digest("hex");
|
|
59
|
-
|
|
60
|
-
// Writes `text` to `file` whole or not at all: to a file beside it, in the
|
|
61
|
-
// file's folder, made if need be, then renamed over it.
|
|
62
|
-
export const writeFileAtomically = (file: string, text: string): void => {
|
|
63
|
-
nodeFs.mkdirSync(nodePath.dirname(file), { recursive: true });
|
|
64
|
-
const temp = `${file}.${process.pid}.${Date.now()}`;
|
|
65
|
-
nodeFs.writeFileSync(temp, text);
|
|
66
|
-
try {
|
|
67
|
-
nodeFs.renameSync(temp, file);
|
|
68
|
-
} catch (e) {
|
|
69
|
-
nodeFs.rmSync(temp, { force: true });
|
|
70
|
-
throw e;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
// Writes `text` to `file` whole, unless `file` is there: a file that runs at once may each write,
|
|
75
|
-
// with the same text. On Windows, a rename over a file that another process has open fails; a run
|
|
76
|
-
// that loses that race keeps the file it found.
|
|
77
|
-
export const writeNewFileAtomically = (file: string, text: string): void => {
|
|
78
|
-
try {
|
|
79
|
-
if (!nodeFs.existsSync(file)) {
|
|
80
|
-
writeFileAtomically(file, text);
|
|
81
|
-
}
|
|
82
|
-
} catch (e) {
|
|
83
|
-
if (!nodeFs.existsSync(file)) {
|
|
84
|
-
throw e;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
// The JSON value in `file`, or undefined if it cannot be read.
|
|
90
|
-
export const tryGetJson = (file: string): unknown =>
|
|
91
|
-
tryGet(() => JSON.parse(nodeFs.readFileSync(file, "utf8")) as unknown);
|
|
92
|
-
|
|
93
|
-
// Writes `value` to `file` as JSON, whole or not at all; a write that fails is skipped.
|
|
94
|
-
export const tryWriteJsonAtomically = (file: string, value: unknown): void => {
|
|
95
|
-
tryGet(() => writeFileAtomically(file, JSON.stringify(value)));
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// The folder at its real path, as a file: URL that ends in `/`, so
|
|
99
|
-
// `new URL("file", folder)` finds a file in it. An error if `dir` cannot be
|
|
100
|
-
// resolved.
|
|
101
|
-
export const tryGetUrlFromFolderPathOrUrl = (dir: string | URL): Result<URL, string> => {
|
|
102
|
-
try {
|
|
103
|
-
return ok(url.pathToFileURL(nodeFs.realpathSync(dir) + nodePath.sep));
|
|
104
|
-
} catch (e) {
|
|
105
|
-
return error(getErrorMessageFromUnknown(e));
|
|
106
|
-
}
|
|
107
|
-
};
|
|
File without changes
|