MCPcopy Create free account
hub / github.com/npm/cli / getInstallScripts

Function getInstallScripts

workspaces/arborist/lib/install-scripts.js:44–111  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

42}
43
44const getInstallScripts = async (node) => {
45 /* istanbul ignore next: arborist Nodes always carry a `package` object;
46 defensive fallbacks for non-arborist callers. */
47 const pkg = node.package || {}
48 /* istanbul ignore next */
49 const scripts = pkg.scripts || {}
50 const collected = {}
51
52 if (scripts.preinstall) {
53 collected.preinstall = scripts.preinstall
54 }
55 if (scripts.install) {
56 collected.install = scripts.install
57 }
58 if (scripts.postinstall) {
59 collected.postinstall = scripts.postinstall
60 }
61 if (scripts.prepare && hasNonRegistryShape(node)) {
62 collected.prepare = scripts.prepare
63 }
64
65 const hasExplicitGypGate = !!(collected.preinstall || collected.install)
66 if (
67 !hasExplicitGypGate &&
68 pkg.gypfile !== false &&
69 await isNodeGypPackage(node.path).catch(() => false)
70 ) {
71 collected.install = 'node-gyp rebuild'
72 }
73
74 // Lockfile-only nodes carry `hasInstallScript: true` but no enumerated
75 // scripts: the lockfile records the presence flag, not the script bodies,
76 // so `node.package.scripts` is empty on a lockfile-driven install (`npm ci`,
77 // a repeat `npm install`). Before giving up, read the installed
78 // package.json from disk to recover the real script bodies. Builder#addToBuildSet
79 // does the same disk read to decide what to run, but unlike that path this
80 // one is read-only: we never mutate `node.package`.
81 if (Object.keys(collected).length === 0 && node.hasInstallScript === true) {
82 const { content } = await PackageJson.normalize(node.path)
83 .catch(() => ({ content: {} }))
84 /* istanbul ignore next: normalize resolves to an object with a scripts
85 object, or our catch fallback returns {}; defensive guard only. */
86 const diskScripts = content?.scripts || {}
87
88 if (diskScripts.preinstall) {
89 collected.preinstall = diskScripts.preinstall
90 }
91 if (diskScripts.install) {
92 collected.install = diskScripts.install
93 }
94 if (diskScripts.postinstall) {
95 collected.postinstall = diskScripts.postinstall
96 }
97 if (diskScripts.prepare && hasNonRegistryShape(node)) {
98 collected.prepare = diskScripts.prepare
99 }
100
101 // Still nothing. The package isn't on disk yet (e.g. `npm ci` before

Callers 3

runPruneMethod · 0.85
collectUnreviewedScriptsFunction · 0.85
install-scripts.jsFile · 0.85

Calls 1

hasNonRegistryShapeFunction · 0.85

Tested by

no test coverage detected