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

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

46 lines
1.1 KiB
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
const toPath = (path) => (path instanceof URL ? url.fileURLToPath(path) : 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) + "\n";
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(toPath(file));
2021-09-10 10:20:43 +08:00
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");
2022-09-18 17:50:14 +08:00
const SOURCE_DIR = path.join(PROJECT_ROOT, "src");
2021-09-10 10:20:43 +08:00
2021-09-11 09:55:46 +08:00
export {
PROJECT_ROOT,
DIST_DIR,
WEBSITE_DIR,
2022-09-18 17:50:14 +08:00
SOURCE_DIR,
2021-09-11 09:55:46 +08:00
readJson,
writeJson,
writeFile,
copyFile,
};