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

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

51 lines
1.2 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 path from "node:path";
import url from "node:url";
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) {
2024-01-10 00:48:50 +08:00
await createDirectory(path.dirname(toPath(to)));
return fs.copyFile(from, to);
2018-05-29 00:22:59 -06:00
}
2024-01-10 00:48:50 +08:00
async function createDirectory(directory) {
2021-09-10 10:20:43 +08:00
try {
await fs.mkdir(directory, { recursive: true });
} catch {
// noop
}
2024-01-10 00:48:50 +08:00
}
async function writeFile(file, content) {
await createDirectory(path.dirname(toPath(file)));
2021-09-10 10:20:43 +08:00
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");
2025-05-27 09:35:31 +08:00
const PRODUCTION_MINIMAL_NODE_JS_VERSION = "14";
2021-09-10 10:20:43 +08:00
2021-09-11 09:55:46 +08:00
export {
copyFile,
DIST_DIR,
2025-05-27 09:35:31 +08:00
PRODUCTION_MINIMAL_NODE_JS_VERSION,
PROJECT_ROOT,
readJson,
SOURCE_DIR,
WEBSITE_DIR,
writeFile,
writeJson,
2021-09-11 09:55:46 +08:00
};