-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.ts
More file actions
46 lines (35 loc) · 1.26 KB
/
filesystem.ts
File metadata and controls
46 lines (35 loc) · 1.26 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
import * as fs from "node:fs";
import * as yaml from "js-yaml";
import { deepmerge } from "deepmerge-ts";
import { exec as nodeExec } from "node:child_process";
import { promisify } from "node:util";
import { type Config, defaultConfig } from "../types/config";
export const cwd = (): string => {
const path = process.env.GITHUB_WORKSPACE;
if (path === undefined) {
throw new Error("GitHub Actions has not set the working directory");
}
return path;
};
const filePath = (config: Config, filename: string): string =>
`${config.directory}/${filename}`;
export const fileExists = (config: Config, filename: string): boolean =>
fs.existsSync(filePath(config, filename));
export const readFile = (config: Config, filename: string): string => {
if (!fs.existsSync(filePath(config, filename))) {
return "";
}
return fs.readFileSync(filePath(config, filename), "utf-8");
};
export const writeFile = (
config: Config,
filename: string,
content: string,
): void => {
fs.writeFileSync(filePath(config, filename), content);
};
export const exec = async (command: string): Promise<string> => {
const execAsync = promisify(nodeExec);
const { stdout } = await execAsync(command);
return stdout.toString().trim();
};