Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
feat(reify): add an ability to add a hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ylemkimon committed Oct 23, 2021
1 parent d9c603e commit c8d00d2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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 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 @@ -148,6 +151,10 @@ module.exports = cls => class Reifier extends cls {
await this[_validatePath]()
await this[_loadTrees](options)
await this[_diffTrees]()
if (options.hooks
&& typeof options.hooks[_beforeReify] === 'function') {
options.hooks[_beforeReify](this)
}
await this[_reifyPackages]()
await this[_saveIdealTree](options)
await this[_copyIdealToActual]()
Expand Down
10 changes: 10 additions & 0 deletions tap-snapshots/test/arborist/reify.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32315,6 +32315,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 test/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2397,3 +2397,16 @@ t.test('no workspace', async t => {
t.equal(tree.inventory.query('name', 'abbrev').size, 0)
t.equal(tree.inventory.query('name', 'once').size, 1)
})

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))
})

0 comments on commit c8d00d2

Please sign in to comment.