forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (41 loc) · 1.12 KB
/
index.js
File metadata and controls
49 lines (41 loc) · 1.12 KB
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
import fs from "node:fs/promises";
import path from "node:path";
import url from "node:url";
const toPath = (path) => (path instanceof URL ? url.fileURLToPath(path) : path);
async function readJson(file) {
const data = await fs.readFile(file);
return JSON.parse(data);
}
function writeJson(file, content) {
content = JSON.stringify(content, null, 2) + "\n";
return writeFile(file, content);
}
async function copyFile(from, to) {
await createDirectory(path.dirname(toPath(to)));
return fs.copyFile(from, to);
}
async function createDirectory(directory) {
try {
await fs.mkdir(directory, { recursive: true });
} catch {
// noop
}
}
async function writeFile(file, content) {
await createDirectory(path.dirname(toPath(file)));
return fs.writeFile(file, content);
}
const PROJECT_ROOT = url.fileURLToPath(new URL("../../", import.meta.url));
const DIST_DIR = path.join(PROJECT_ROOT, "dist");
const WEBSITE_DIR = path.join(PROJECT_ROOT, "website");
const SOURCE_DIR = path.join(PROJECT_ROOT, "src");
export {
copyFile,
DIST_DIR,
PROJECT_ROOT,
readJson,
SOURCE_DIR,
WEBSITE_DIR,
writeFile,
writeJson,
};