107 lines
2.3 KiB
JavaScript
107 lines
2.3 KiB
JavaScript
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]);
|
|
}
|
|
|
|
async function searchReplace(path, search, replace) {
|
|
console.log(`Searching and replacing ${search} with ${replace} in ${path}`);
|
|
|
|
const file = await fs.promises.readFile(path, "utf8");
|
|
const newFile = file.replaceAll(search, replace);
|
|
|
|
await fs.promises.writeFile(path, newFile);
|
|
}
|
|
|
|
async function removeDir(path) {
|
|
console.log(`Removing directory ${path}`);
|
|
|
|
await execa("rm", ["-rf", path]);
|
|
}
|
|
|
|
async function renameDir(src, dest) {
|
|
console.log(`Renaming directory ${src} to ${dest}`);
|
|
|
|
await execa("mv", [src, dest]);
|
|
}
|
|
|
|
export default {
|
|
cloneOrPullRepo,
|
|
removeContainerNames,
|
|
removePorts,
|
|
copyDir,
|
|
downloadFile,
|
|
renameFile,
|
|
searchReplace,
|
|
removeDir,
|
|
renameDir,
|
|
};
|