-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathutils.js
82 lines (66 loc) · 1.76 KB
/
utils.js
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
import { execa } from "execa";
import fs from "fs";
import yaml from "yaml";
async function cloneOrPullRepo({
repo,
path = "./repo",
branch = "main",
depth = 1,
}) {
if (!fs.existsSync(path)) {
console.log(`Cloning ${repo} into ${path}`);
await execa(
"git",
[
"clone",
["--depth", depth],
["--branch", branch],
"--single-branch",
repo,
path,
].flat()
);
} else {
console.log(`Pulling ${repo} into ${path}`);
await execa("git", ["pull"], { cwd: path });
}
}
async function removeContainerNames(path) {
console.log(`Removing container names from ${path}`);
const file = await fs.promises.readFile(path, "utf8");
const document = yaml.parseDocument(file);
document.get("services").items.forEach((item) => {
item.value.delete("container_name");
});
await fs.promises.writeFile(path, document.toString());
}
async function removePorts(path) {
console.log(`Removing ports from ${path}`);
const file = await fs.promises.readFile(path, "utf8");
const document = yaml.parseDocument(file);
document.get("services").items.forEach((item) => {
item.value.delete("ports");
});
await fs.promises.writeFile(path, document.toString());
}
async function copyDir(src, dest) {
console.log(`Copying ${src} to ${dest}`);
await execa("rm", ["-rf", dest]);
await execa("cp", ["-r", src, dest]);
}
async function downloadFile(url, dest) {
console.log(`Downloading ${url} to ${dest}`);
await execa("curl", ["-s", url, "-o", dest]);
}
async function renameFile(src, dest) {
console.log(`Renaming ${src} to ${dest}`);
await execa("mv", [src, dest]);
}
export default {
cloneOrPullRepo,
removeContainerNames,
removePorts,
copyDir,
downloadFile,
renameFile,
};