-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackageJson.ts
More file actions
77 lines (59 loc) · 1.68 KB
/
packageJson.ts
File metadata and controls
77 lines (59 loc) · 1.68 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fs from 'fs/promises'
import {
type ProjectManifestVersionUpdate,
ProjectManifest
} from './manifest.js'
import type { PackageJsonProps } from './packageJson.types.js'
export * from './packageJson.types.js'
/**
* A class that represents a package.json manifest.
*/
export class PackageJsonManifest extends ProjectManifest<PackageJsonProps> {
static override Filename = 'package.json'
contents: string | undefined
manifest: PackageJsonProps | undefined
private contentsMutex: Promise<string> | undefined
async readManifest() {
if (!this.contentsMutex) {
this.contentsMutex = fs.readFile(this.manifestPath, 'utf-8')
this.contents = await this.contentsMutex
this.manifest = JSON.parse(this.contents) as PackageJsonProps
return this.manifest
}
await this.contentsMutex
return this.manifest!
}
async getName() {
const manifest = await this.readManifest()
return manifest.name
}
async getVersion() {
const manifest = await this.readManifest()
return manifest.version
}
async isPrivate() {
const manifest = await this.readManifest()
return Boolean(manifest.private)
}
async writeVersion(version: string, dryRun?: boolean): Promise<ProjectManifestVersionUpdate> {
await this.readManifest()
const {
name,
version: from
} = this.manifest!
this.manifest!.version = version
this.contents = this.contents!.replace(
/"version":(\s*)"[^"]+"/,
`"version":$1"${version}"`
)
if (!dryRun) {
await fs.writeFile(this.manifestPath, this.contents, 'utf-8')
}
return {
name,
from,
to: version,
files: [this.manifestPath]
}
}
}