-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
39 lines (30 loc) · 1.13 KB
/
build.js
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
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises"
const sourceDirectory = "src"
const destinationDirectory = "dist"
const reservedFilenames = ["biome.json", "package.json"]
await mkdir(destinationDirectory, { recursive: true })
const filenames = await readdir("src")
await Promise.all(filenames.filter(isJsonWithComments).map(buildFile))
function isJsonWithComments(filename) {
return filename.endsWith(".jsonc")
}
async function buildFile(filename) {
if (reservedFilenames.includes(filename)) {
throw new Error(`${filename}: Reserved filename.`)
}
const sourcePath = `${sourceDirectory}/${filename}`
const destinationPath = `${destinationDirectory}/${filename.slice(0, -1)}`
try {
const content = await readFile(sourcePath, "utf8")
const output = minifyJson(removeJsonLineComments(content))
await writeFile(destinationPath, output, "utf8")
} catch (error) {
throw new Error(`${filename}: ${error.message}.`)
}
}
function removeJsonLineComments(jsonContent) {
return jsonContent.replace(/(?<=["}\]0-9e],?\s|\t)\/\/.*$/gm, "")
}
function minifyJson(jsonContent) {
return JSON.stringify(JSON.parse(jsonContent))
}