Skip to content

Commit 19b8e73

Browse files
committed
Update EditablePackageJson to have better control of its inheritance and private properties
1 parent b14dca5 commit 19b8e73

File tree

1 file changed

+91
-14
lines changed

1 file changed

+91
-14
lines changed

registry/lib/packages.js

+91-14
Original file line numberDiff line numberDiff line change
@@ -171,34 +171,96 @@ let _EditablePackageJsonClass
171171
function getEditablePackageJsonClass() {
172172
if (_EditablePackageJsonClass === undefined) {
173173
const EditablePackageJsonBase = /*@__PURE__*/ require('@npmcli/package-json')
174+
const {
175+
read
176+
} = /*@__PURE__*/ require('@npmcli/package-json/lib/read-package')
174177
const {
175178
packageSort
176179
} = /*@__PURE__*/ require('@npmcli/package-json/lib/sort')
177180
_EditablePackageJsonClass = class EditablePackageJson extends (
178181
EditablePackageJsonBase
179182
) {
180-
#_canSave = true
181-
#_path
182-
#_readFileContent = ''
183+
static fixSteps = EditablePackageJsonBase.fixSteps
184+
static normalizeSteps = EditablePackageJsonBase.normalizeSteps
185+
static prepareSteps = EditablePackageJsonBase.prepareSteps
186+
187+
_canSave = true
188+
_path = undefined
189+
_readFileContent = ''
190+
191+
static async create(path, opts = {}) {
192+
const p = new _EditablePackageJsonClass()
193+
await p.create(path)
194+
return opts.data ? p.update(opts.data) : p
195+
}
196+
197+
static async fix(path, opts) {
198+
const p = new _EditablePackageJsonClass()
199+
await p.load(path, true)
200+
return p.fix(opts)
201+
}
202+
203+
static async load(path, opts = {}) {
204+
const p = new _EditablePackageJsonClass()
205+
// Avoid try/catch if we aren't going to create
206+
if (!opts.create) {
207+
return await p.load(path)
208+
}
209+
try {
210+
return await p.load(path)
211+
} catch (err) {
212+
if (!err.message.startsWith('Could not read package.json')) {
213+
throw err
214+
}
215+
return await p.create(path)
216+
}
217+
}
218+
219+
static async normalize(path, opts) {
220+
const p = new _EditablePackageJsonClass()
221+
await p.load(path)
222+
return await p.normalize(opts)
223+
}
224+
225+
static async prepare(path, opts) {
226+
const p = new _EditablePackageJsonClass()
227+
await p.load(path, true)
228+
return await p.prepare(opts)
229+
}
183230

184231
create(path) {
185232
super.create(path)
186-
this.#_path = path
233+
this._path = path
234+
return this
235+
}
236+
237+
async fix(opts = {}) {
238+
await super.fix(opts)
239+
return this
240+
}
241+
242+
fromComment(data) {
243+
super.fromComment(data)
187244
return this
188245
}
189246

190247
fromContent(data) {
191248
super.fromContent(data)
192-
this.#_canSave = false
249+
this._canSave = false
250+
return this
251+
}
252+
253+
fromJSON(data) {
254+
super.fromJSON(data)
193255
return this
194256
}
195257

196258
async load(path, parseIndex) {
197-
this.#_path = path
259+
this._path = path
198260
const { promises: fsPromises } = getFs()
199261
let parseErr
200262
try {
201-
this.#_readFileContent = await fsPromises.read(this.filename)
263+
this._readFileContent = await read(this.filename)
202264
} catch (err) {
203265
if (!parseIndex) {
204266
throw err
@@ -219,18 +281,28 @@ function getEditablePackageJsonClass() {
219281
throw parseErr
220282
}
221283
// This wasn't a package.json so prevent saving
222-
this.#_canSave = false
284+
this._canSave = false
223285
return this
224286
}
225-
return this.fromJSON(this.#_readFileContent)
287+
return this.fromJSON(this._readFileContent)
288+
}
289+
290+
async normalize(opts = {}) {
291+
await super.normalize(opts)
292+
return this
226293
}
227294

228295
get path() {
229-
return this.#_path
296+
return this._path
297+
}
298+
299+
async prepare(opts = {}) {
300+
await super.prepare(opts)
301+
return this
230302
}
231303

232304
async save({ sort } = {}) {
233-
if (!this.#_canSave) {
305+
if (!this._canSave) {
234306
throw new Error('No package.json to save to')
235307
}
236308
const {
@@ -247,17 +319,17 @@ function getEditablePackageJsonClass() {
247319
format
248320
)}\n`.replace(/\n/g, eol)
249321

250-
if (fileContent.trim() === this.#_readFileContent.trim()) {
322+
if (fileContent.trim() === this._readFileContent.trim()) {
251323
return false
252324
}
253325
const { promises: fsPromises } = getFs()
254326
await fsPromises.writeFile(this.filename, fileContent)
255-
this.#_readFileContent = fileContent
327+
this._readFileContent = fileContent
256328
return true
257329
}
258330

259331
async saveSync() {
260-
if (!this.#_canSave || this.content === undefined) {
332+
if (!this._canSave || this.content === undefined) {
261333
throw new Error('No package.json to save to')
262334
}
263335
const {
@@ -273,6 +345,11 @@ function getEditablePackageJsonClass() {
273345
const fs = getFs()
274346
fs.writeFileSync(this.filename, fileContent)
275347
}
348+
349+
update(content) {
350+
super.update(content)
351+
return this
352+
}
276353
}
277354
}
278355
return _EditablePackageJsonClass

0 commit comments

Comments
 (0)