Files
test-repo/scripts/utils/index.mjs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

42 lines
924 B
JavaScript
Raw Normal View History

2021-05-15 20:09:52 +08:00
import fs from "node:fs/promises";
2021-09-10 10:20:43 +08:00
import url from "node:url";
import path from "node:path";
2018-05-24 15:30:45 -03:00
async function readJson(file) {
2021-01-09 02:09:40 +09:00
const data = await fs.readFile(file);
2018-05-24 15:30:45 -03:00
return JSON.parse(data);
}
function writeJson(file, content) {
content = JSON.stringify(content, null, 2);
2021-09-10 10:20:43 +08:00
return writeFile(file, content);
2018-05-24 15:30:45 -03:00
}
2018-05-29 00:22:59 -06:00
async function copyFile(from, to) {
2021-01-09 02:09:40 +09:00
const data = await fs.readFile(from);
2021-09-10 10:20:43 +08:00
return writeFile(to, data);
2018-05-29 00:22:59 -06:00
}
2021-09-10 10:20:43 +08:00
async function writeFile(file, content) {
const directory = path.dirname(file);
try {
await fs.mkdir(directory, { recursive: true });
} catch {
// noop
}
return fs.writeFile(file, content);
}
const PROJECT_ROOT = url.fileURLToPath(new URL("../../", import.meta.url));
const DIST_DIR = path.join(PROJECT_ROOT, "dist");
2021-09-11 09:55:46 +08:00
const WEBSITE_DIR = path.join(PROJECT_ROOT, "website");
2021-09-10 10:20:43 +08:00
2021-09-11 09:55:46 +08:00
export {
PROJECT_ROOT,
DIST_DIR,
WEBSITE_DIR,
readJson,
writeJson,
writeFile,
copyFile,
};