forked from opsfolio/resource-surveillance-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsctl.ts
executable file
·95 lines (88 loc) · 3.12 KB
/
rsctl.ts
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
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env --allow-run --allow-sys
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
/**
* Checks if a given file is executable.
*
* @param filePath - The path to the file that needs to be checked.
* @returns A promise that resolves to `true` if the file is executable, `false` otherwise.
*/
async function isExecutable(filePath: string): Promise<boolean> {
try {
const fileInfo = await Deno.lstat(filePath);
return fileInfo.mode !== null && (fileInfo.mode & 0o111) !== 0;
} catch (error) {
console.error(
colors.red(
` Error checking if file is executable: ${error.message}`,
),
);
return false;
}
}
/**
* Executes an executable file and stores its output in a new file.
* The new file's name is determined by the `getOutputFileName` function.
*
* @param filePath - The path to the file that needs to be executed.
* @param getOutputFileName - A function that takes the original file path and returns the new file name.
* @returns A promise that resolves when the file has been executed and the output has been stored.
*/
async function executeFile(
filePath: string,
getOutputFileName: (filePath: string) => string,
): Promise<void> {
try {
const outputFileName = getOutputFileName(filePath);
const command = new Deno.Command(filePath);
const output = await command.output();
if (output.success) {
await Deno.writeFile(outputFileName, output.stdout);
console.log("✅", colors.brightGreen(`${outputFileName}`));
} else {
console.error(
"❌",
colors.red(
`${filePath} (${new TextDecoder().decode(output.stderr)}`,
),
);
}
} catch (error) {
console.error("❌", colors.red(`${filePath} (${error.message})`));
}
}
/**
* Walks through the current directory, finds all files matching the specified pattern,
* checks if they are executable, and if so, executes them.
* The output of each executable file is stored in a corresponding output file whose name
* is determined by the `getOutputFileName` function.
*
* If a file matches the pattern but is not executable, an error message is emitted.
*
* @param match - A regular expression pattern to match file names.
* @param getOutputFileName - A function that takes the original file path and returns the new file name.
* @returns A promise that resolves when all matching files have been processed.
*/
async function generateSqlFromExecutable(
match: RegExp,
getOutputFileName: (filePath: string) => string,
): Promise<void> {
for await (
const entry of walk(Deno.cwd(), {
includeFiles: true,
match: [match],
})
) {
const filePath = entry.path;
console.log(colors.dim(`👀 ${filePath}`));
if (await isExecutable(filePath)) {
await executeFile(filePath, getOutputFileName);
} else {
console.error("⚠️ ", colors.yellow(`Not executable: ${filePath}`));
}
}
}
await generateSqlFromExecutable(
/.+\.sql\..*/,
(filePath) => filePath.replace(/\.sql\..*$/, ".auto.sql"),
);