Skip to content

Commit

Permalink
feat: add prepare function
Browse files Browse the repository at this point in the history
This implements most of the logic found in `read-package-json`, except
for the index.js/comments parsing that we don't currently do from inside
npm.

It also shares all the normalization logic into one method, that
`normalize` and `prepare` then pick and choose which ones to use.

This will allow us to maintain the functionality that
`read-package-json` has of skipping certain steps, but doing so in a
more config-based way instead of passing in functions to run.

All the existing `read-package-json` tests were ported over, and new
ones added to get to 100% coverage.

For now the 'steps' will be undocumented.
  • Loading branch information
wraithgar committed May 12, 2023
1 parent a11ffa7 commit e2d289d
Show file tree
Hide file tree
Showing 5 changed files with 808 additions and 54 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ Convenience static method like `load` but for calling `normalize`

---

---

### `async PackageJson.prepare()`

Like `load` but intended for reading package.json files before publish.

---

### **static** `async PackageJson.prepare(path)`

Convenience static method like `load` but for calling `prepare`

---

### `PackageJson.update(content)`

Updates the contents of the `package.json` with the `content` provided.
Expand Down
59 changes: 52 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,45 @@ const knownKeys = new Set([
])

class PackageJson {
static normalizeSteps = Object.freeze([
'_id',
'_attributes',
'bundleDependencies',
'optionalDedupe',
'scripts',
'funding',
'bin',
])

static prepareSteps = Object.freeze([
'_attributes',
'bundleDependencies',
'gypfile',
'serverjs',
'scriptpath',
'authors',
'readme',
'mans',
'binDir',
'gitHead',
'fillTypes',
'normalizeData',
'binRefs',
])

// default behavior, just loads and parses
static async load (path) {
return await new PackageJson(path).load()
}

// read-package-json compatible behavior
static async prepare (path, opts) {
return await new PackageJson(path).prepare(opts)
}

// read-package-json-fast compatible behavior
static async normalize (path) {
return await new PackageJson(path).normalize()
static async normalize (path, opts) {
return await new PackageJson(path).normalize(opts)
}

#filename
Expand All @@ -53,20 +84,22 @@ class PackageJson {
}

try {
this.#manifest =
parseJSON(this.#readFileContent)
this.#manifest = parseJSON(this.#readFileContent)
} catch (err) {
err.message = `Invalid package.json: ${err}`
throw err
}

return this
}

get content () {
return this.#manifest
}

get path () {
return this.#path
}

update (content) {
// validates both current manifest and content param
const invalidContent =
Expand Down Expand Up @@ -111,9 +144,21 @@ class PackageJson {
}
}

async normalize () {
async normalize (opts = {}) {
if (!opts.steps) {
opts.steps = this.constructor.normalizeSteps
}
await this.load()
await normalize(this)
await normalize(this, opts)
return this
}

async prepare (opts = {}) {
if (!opts.steps) {
opts.steps = this.constructor.prepareSteps
}
await this.load(true)
await normalize(this, opts)
return this
}
}
Expand Down
Loading

0 comments on commit e2d289d

Please sign in to comment.