MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / detectInstallMethod

Function detectInstallMethod

src/upgrade/index.ts:109–179  ·  view source on GitHub ↗
(input: DetectInput)

Source from the content-addressed store, hash-verified

107}
108
109export function detectInstallMethod(input: DetectInput): InstallMethod {
110 const exists = input.exists ?? fs.existsSync;
111 const isWin = input.platform === 'win32';
112 // Path math keyed on the TARGET platform so detection is host-independent
113 // (a Windows layout resolves correctly even when unit-tested on macOS/Linux).
114 const P = isWin ? path.win32 : path.posix;
115 const binDir = P.dirname(input.filename); // <…>/bin
116
117 const norm = toPosix(input.filename);
118
119 // Path-based checks come FIRST. The npm thin-installer's per-platform
120 // package (@colbymchenry/codegraph-<platform>-<arch>) is itself a complete
121 // bundle — vendored node + bin/ launcher — living inside node_modules, so
122 // the layout sniff below would misread every npm install as a standalone
123 // bundle. `upgrade` would then curl install.sh into ~/.codegraph: a SECOND
124 // install that never wins the PATH race against npm's shim, leaving
125 // `codegraph -v` permanently on the old version (the #1071 shadow,
126 // self-inflicted). A path under node_modules is authoritative about HOW the
127 // user installed, whatever the artifact inside looks like.
128
129 // npx cache: <…>/_npx/<hash>/node_modules/@colbymchenry/codegraph/…
130 // (checked before npm — the npx cache path also contains /node_modules/).
131 if (norm.includes('/_npx/')) {
132 return { kind: 'npx' };
133 }
134
135 // npm install (global or local): lives under a node_modules tree. Which of
136 // the two is read from npm's own layout, never from cwd (#2477). A global
137 // prefix often sits inside the home folder — Windows `%APPDATA%\npm`, nvm,
138 // fnm, `~/.npm-global` — so "the running file is under cwd" took every such
139 // global install for a local one whenever `upgrade` ran from home: `npm
140 // install` without -g left the global copy stale and dropped a stray local
141 // one into the home folder.
142 const nodeModulesAt = norm.indexOf('/node_modules/');
143 if (nodeModulesAt !== -1) {
144 // The folder holding the outermost node_modules (the same offset in
145 // `filename`, which `toPosix` only changes slash by slash): the global
146 // prefix on Windows, `<prefix>/lib` on POSIX, or the project root.
147 let owner = input.filename.slice(0, nodeModulesAt);
148 if (owner === '' || /^[A-Za-z]:$/.test(owner)) owner += P.sep; // a tree at a filesystem/drive root
149 // pnpm, yarn and bun keep their global packages in a folder that looks
150 // like an npm project (a package.json beside node_modules); running npm
151 // there, or a second copy through npm, is never the upgrade.
152 const manager = otherPackageManager(norm, owner, P, exists);
153 if (manager) return manager;
154 if (isNpmGlobalTree(owner, P, isWin, exists)) return { kind: 'npm', scope: 'global' };
155 // A project's install is upgraded in that project, whatever folder
156 // `upgrade` runs from: npm run anywhere else — outside the project, or in
157 // another project inside it, as in a home folder that is itself one —
158 // installs a second copy there and leaves this one as it was.
159 return { kind: 'npm', scope: 'local', root: P.resolve(owner) };
160 }
161
162 // Bundle: <root>/lib/dist/bin/codegraph.js → <root> is up 3 from bin/.
163 // A bundle has a vendored node + a launcher script as siblings of lib/.
164 const bundleRoot = P.resolve(binDir, '..', '..', '..');
165 const vendoredNode = P.join(bundleRoot, isWin ? 'node.exe' : 'node');
166 const launcher = P.join(bundleRoot, 'bin', isWin ? 'codegraph.cmd' : 'codegraph');

Callers 3

installDirCandidatesFunction · 0.90
planBinaryRemovalFunction · 0.90
upgrade.test.tsFile · 0.90

Calls 7

otherPackageManagerFunction · 0.85
isNpmGlobalTreeFunction · 0.85
deriveInstallDirFunction · 0.85
toPosixFunction · 0.70
resolveMethod · 0.65
existsFunction · 0.50
joinMethod · 0.45

Tested by

no test coverage detected