@@ -171,34 +171,96 @@ let _EditablePackageJsonClass
171
171
function getEditablePackageJsonClass ( ) {
172
172
if ( _EditablePackageJsonClass === undefined ) {
173
173
const EditablePackageJsonBase = /*@__PURE__ */ require ( '@npmcli/package-json' )
174
+ const {
175
+ read
176
+ } = /*@__PURE__ */ require ( '@npmcli/package-json/lib/read-package' )
174
177
const {
175
178
packageSort
176
179
} = /*@__PURE__ */ require ( '@npmcli/package-json/lib/sort' )
177
180
_EditablePackageJsonClass = class EditablePackageJson extends (
178
181
EditablePackageJsonBase
179
182
) {
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
+ }
183
230
184
231
create ( path ) {
185
232
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 )
187
244
return this
188
245
}
189
246
190
247
fromContent ( data ) {
191
248
super . fromContent ( data )
192
- this . #_canSave = false
249
+ this . _canSave = false
250
+ return this
251
+ }
252
+
253
+ fromJSON ( data ) {
254
+ super . fromJSON ( data )
193
255
return this
194
256
}
195
257
196
258
async load ( path , parseIndex ) {
197
- this . # _path = path
259
+ this . _path = path
198
260
const { promises : fsPromises } = getFs ( )
199
261
let parseErr
200
262
try {
201
- this . # _readFileContent = await fsPromises . read ( this . filename )
263
+ this . _readFileContent = await read ( this . filename )
202
264
} catch ( err ) {
203
265
if ( ! parseIndex ) {
204
266
throw err
@@ -219,18 +281,28 @@ function getEditablePackageJsonClass() {
219
281
throw parseErr
220
282
}
221
283
// This wasn't a package.json so prevent saving
222
- this . # _canSave = false
284
+ this . _canSave = false
223
285
return this
224
286
}
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
226
293
}
227
294
228
295
get path ( ) {
229
- return this . #_path
296
+ return this . _path
297
+ }
298
+
299
+ async prepare ( opts = { } ) {
300
+ await super . prepare ( opts )
301
+ return this
230
302
}
231
303
232
304
async save ( { sort } = { } ) {
233
- if ( ! this . # _canSave) {
305
+ if ( ! this . _canSave ) {
234
306
throw new Error ( 'No package.json to save to' )
235
307
}
236
308
const {
@@ -247,17 +319,17 @@ function getEditablePackageJsonClass() {
247
319
format
248
320
) } \n`. replace ( / \n / g, eol )
249
321
250
- if ( fileContent . trim ( ) === this . # _readFileContent. trim ( ) ) {
322
+ if ( fileContent . trim ( ) === this . _readFileContent . trim ( ) ) {
251
323
return false
252
324
}
253
325
const { promises : fsPromises } = getFs ( )
254
326
await fsPromises . writeFile ( this . filename , fileContent )
255
- this . # _readFileContent = fileContent
327
+ this . _readFileContent = fileContent
256
328
return true
257
329
}
258
330
259
331
async saveSync ( ) {
260
- if ( ! this . # _canSave || this . content === undefined ) {
332
+ if ( ! this . _canSave || this . content === undefined ) {
261
333
throw new Error ( 'No package.json to save to' )
262
334
}
263
335
const {
@@ -273,6 +345,11 @@ function getEditablePackageJsonClass() {
273
345
const fs = getFs ( )
274
346
fs . writeFileSync ( this . filename , fileContent )
275
347
}
348
+
349
+ update ( content ) {
350
+ super . update ( content )
351
+ return this
352
+ }
276
353
}
277
354
}
278
355
return _EditablePackageJsonClass
0 commit comments