/
index.ts
47 lines (41 loc) · 1.37 KB
/
index.ts
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
import { promises as fs } from 'fs'
import path from 'path'
import { insertComments, CommentSpecifier } from '@pnpm/text.comments-parser'
import { ProjectManifest } from '@pnpm/types'
import JSON5 from 'json5'
import writeFileAtomic from 'write-file-atomic'
import writeYamlFile from 'write-yaml-file'
const YAML_FORMAT = {
noCompatMode: true,
noRefs: true,
}
export async function writeProjectManifest (
filePath: string,
manifest: ProjectManifest,
opts?: {
comments?: CommentSpecifier[]
indent?: string | number | undefined
insertFinalNewline?: boolean
}
): Promise<void> {
const fileType = filePath.slice(filePath.lastIndexOf('.') + 1).toLowerCase()
if (fileType === 'yaml') {
return writeYamlFile(filePath, manifest, YAML_FORMAT)
}
await fs.mkdir(path.dirname(filePath), { recursive: true })
const trailingNewline = opts?.insertFinalNewline === false ? '' : '\n'
const indent = opts?.indent ?? '\t'
const json = (
fileType === 'json5'
? stringifyJson5(manifest, indent, opts?.comments)
: JSON.stringify(manifest, undefined, indent)
)
return writeFileAtomic(filePath, `${json}${trailingNewline}`)
}
function stringifyJson5 (obj: object, indent: string | number, comments?: CommentSpecifier[]) {
const json5 = JSON5.stringify(obj, undefined, indent)
if (comments) {
return insertComments(json5, comments)
}
return json5
}