Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(arborist)(reify): add an ability to add a hook #4260

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions workspaces/arborist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ arb.reify({
// write the lockfile(s) back to disk, and package.json with any updates
// defaults to 'true'
save: true,

// experimental hooks
hooks: {
// hook that runs after building the idealTree but before saving it
[Symbol.for('beforeReify')]: Arborist => ...
}
}).then(() => {
// node modules has been written to match the idealTree
})
Expand Down
7 changes: 7 additions & 0 deletions workspaces/arborist/lib/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ const _resolvedAdd = Symbol.for('resolvedAdd')
const _usePackageLock = Symbol.for('usePackageLock')
const _formatPackageLock = Symbol.for('formatPackageLock')

// hooks
const _beforeReify = Symbol.for('beforeReify')

module.exports = cls => class Reifier extends cls {
constructor (options) {
super(options)
Expand Down Expand Up @@ -147,6 +150,10 @@ module.exports = cls => class Reifier extends cls {
process.emit('time', 'reify')
await this[_validatePath]()
await this[_loadTrees](options)
if (options.hooks
&& typeof options.hooks[_beforeReify] === 'function') {
options.hooks[_beforeReify](this)
}
await this[_diffTrees]()
await this[_reifyPackages]()
await this[_saveIdealTree](options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32358,6 +32358,16 @@ ArboristNode {
}
`

exports[`test/arborist/reify.js TAP run hooks > must match snapshot 1`] = `
ArboristNode {
"isProjectRoot": true,
"location": "",
"name": "tap-testdir-reify-run-hooks",
"packageName": "hook-test-modified",
"path": "{CWD}/test/arborist/tap-testdir-reify-run-hooks",
}
`

exports[`test/arborist/reify.js TAP running lifecycle scripts of unchanged link nodes on reify > result 1`] = `
ArboristNode {
"children": Map {
Expand Down
13 changes: 13 additions & 0 deletions workspaces/arborist/test/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2432,3 +2432,16 @@ t.test('add local dep with existing dev + peer/optional', async t => {
t.equal(tree.children.get('abbrev').resolved, 'file:../../dep', 'resolved')
t.equal(tree.children.size, 1, 'children')
})

t.test('run hooks', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'hook-test',
}),
})
const tree = await reify(path, { hooks: {
[Symbol.for('beforeReify')]: (arborist) => {
arborist.idealTree.package.name = 'hook-test-modified'
} } })
t.matchSnapshot(printTree(tree))
})