-
-
Notifications
You must be signed in to change notification settings - Fork 955
/
index.ts
174 lines (166 loc) · 5.12 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { ImporterManifest } from '@pnpm/types'
import writeImporterManifest from '@pnpm/write-importer-manifest'
import detectIndent = require('detect-indent')
import fs = require('fs')
import { Stats } from 'fs'
import isWindows = require('is-windows')
import path = require('path')
import readYamlFile from 'read-yaml-file'
import { promisify } from 'util'
import {
readJson5File,
readJsonFile,
} from './readFile'
const stat = promisify(fs.stat)
type WriteImporterManifest = (manifest: ImporterManifest, force?: boolean) => Promise<void>
export default async function readImporterManifest (importerDir: string): Promise<{
fileName: string,
manifest: ImporterManifest
writeImporterManifest: WriteImporterManifest
}> {
const result = await tryReadImporterManifest(importerDir)
if (result.manifest !== null) {
return result as {
fileName: string,
manifest: ImporterManifest
writeImporterManifest: WriteImporterManifest
}
}
const err = new Error(`No package.json (or package.yaml, or package.json5) was found in "${importerDir}".`)
err['code'] = 'ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND'
throw err
}
export async function readImporterManifestOnly (importerDir: string): Promise<ImporterManifest> {
const { manifest } = await readImporterManifest(importerDir)
return manifest
}
export async function tryReadImporterManifest (importerDir: string): Promise<{
fileName: string,
manifest: ImporterManifest | null
writeImporterManifest: WriteImporterManifest
}> {
try {
const manifestPath = path.join(importerDir, 'package.json')
const { data, text } = await readJsonFile(manifestPath)
const { indent } = detectIndent(text)
return {
fileName: 'package.json',
manifest: data,
writeImporterManifest: createManifestWriter({
indent,
initialManifest: data,
manifestPath,
}),
}
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
try {
const manifestPath = path.join(importerDir, 'package.json5')
const { data, text } = await readJson5File(manifestPath)
const { indent } = detectIndent(text)
return {
fileName: 'package.json5',
manifest: data,
writeImporterManifest: createManifestWriter({
indent,
initialManifest: data,
manifestPath,
}),
}
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
try {
const manifestPath = path.join(importerDir, 'package.yaml')
const manifest = await readPackageYaml(manifestPath)
return {
fileName: 'package.yaml',
manifest,
writeImporterManifest: createManifestWriter({ initialManifest: manifest, manifestPath }),
}
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
if (isWindows()) {
// ENOTDIR isn't used on Windows, but pnpm expects it.
let s: Stats | undefined
try {
s = await stat(importerDir)
} catch (err) {
// Ignore
}
if (s && !s.isDirectory()) {
const err = new Error(`"${importerDir}" is not a directory`)
err['code'] = 'ENOTDIR' // tslint:disable-line
throw err
}
}
const filePath = path.join(importerDir, 'package.json')
return {
fileName: 'package.json',
manifest: null,
writeImporterManifest: writeImporterManifest.bind(null, filePath),
}
}
export async function readExactImporterManifest (manifestPath: string) {
const base = path.basename(manifestPath).toLowerCase()
switch (base) {
case 'package.json': {
const { data, text } = await readJsonFile(manifestPath)
const { indent } = detectIndent(text)
return {
manifest: data,
writeImporterManifest: createManifestWriter({
indent,
initialManifest: data,
manifestPath,
}),
}
}
case 'package.json5': {
const { data, text } = await readJson5File(manifestPath)
const { indent } = detectIndent(text)
return {
manifest: data,
writeImporterManifest: createManifestWriter({
indent,
initialManifest: data,
manifestPath,
}),
}
}
case 'package.yaml': {
const manifest = await readPackageYaml(manifestPath)
return {
manifest,
writeImporterManifest: createManifestWriter({ initialManifest: manifest, manifestPath }),
}
}
}
throw new Error(`Not supported manifest name "${base}"`)
}
async function readPackageYaml (filePath: string) {
try {
return await readYamlFile<ImporterManifest>(filePath)
} catch (err) {
if (err.name !== 'YAMLException') throw err
err.message += `\nin ${filePath}`
err['code'] = 'ERR_PNPM_YAML_PARSE'
throw err
}
}
function createManifestWriter (
opts: {
initialManifest: ImporterManifest,
indent?: string | number | undefined,
manifestPath: string,
},
): (WriteImporterManifest) {
const stringifiedInitialManifest = JSON.stringify(opts.initialManifest)
return async (updatedManifest: ImporterManifest, force?: boolean) => {
if (force === true || stringifiedInitialManifest !== JSON.stringify(updatedManifest)) {
return writeImporterManifest(opts.manifestPath, updatedManifest, { indent: opts.indent })
}
}
}