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

fix(global-prefix): remove global-prefix package #11408

Merged
merged 1 commit into from
Jan 11, 2023
Merged
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
2 changes: 0 additions & 2 deletions packages/amplify-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"folder-hash": "^4.0.2",
"fs-extra": "^8.1.0",
"glob": "^7.2.0",
"global-prefix": "^3.0.0",
"graphql": "^15.5.0",
"graphql-transformer-core": "^7.6.7",
"gunzip-maybe": "^1.4.2",
Expand All @@ -114,7 +113,6 @@
"@types/folder-hash": "^4.0.1",
"@types/fs-extra": "^8.0.1",
"@types/glob": "^7.1.1",
"@types/global-prefix": "^3.0.0",
"@types/gunzip-maybe": "^1.4.0",
"@types/node": "^12.12.6",
"@types/node-fetch": "^2.6.1",
Expand Down
65 changes: 64 additions & 1 deletion packages/amplify-cli/src/utils/global-prefix.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as os from 'os';
import * as path from 'path';
import globalNpmPrefix from 'global-prefix';
import * as fs from 'fs-extra';
import * as ini from 'ini';
import * as which from 'which';

export function getGlobalNodeModuleDirPath() {
const yarnPrefix = getYarnPrefix();
if (__dirname.includes(yarnPrefix)) {
return path.join(yarnPrefix, 'node_modules');
}

const globalNpmPrefix = getGlobalNpmPrefix();
if (process.platform === 'win32') {
return path.join(globalNpmPrefix, 'node_modules');
}
Expand All @@ -21,3 +25,62 @@ function getYarnPrefix() {
}
return yarnPrefix;
}

// NOTE: This issue: https://github.com/jonschlinkert/global-prefix/issues/30 prevents us from using the global-prefix package
function getGlobalNpmPrefix() {
// System-wide prefix
if (process.env.PREFIX) return process.env.PREFIX;

// $HOME variable
const homeDirPath = os.homedir();
if (homeDirPath) {
const prefix = tryConfigPath(path.resolve(homeDirPath, '.npmrc'));
if (prefix) return prefix;
}

// Find global NPM path
const npmPath = tryWhich('npm');
if (npmPath) {
// Check the built-in npm config file
const prefix = tryConfigPath(path.resolve(npmPath, '..', '..', 'npmrc'));
if (prefix) {
// Custom npm config ?
const globalPrefix = tryConfigPath(path.resolve(prefix, 'etc', 'npmrc')) || prefix;
if (globalPrefix) return globalPrefix;
}
}

const nodePath = tryWhich('node') // Search for Node's global executable
if (nodePath) {
const { APPDATA, DESTDIR, OSTYPE } = process.env;

// Windows
// c:\node\node.exe --> prefix=c:\node\
if (process.platform === 'win32' || OSTYPE === 'msys' || OSTYPE === 'cygwin') {
return APPDATA ? path.join(APPDATA, 'npm') : path.dirname(nodePath);
}

// UNIX
// /usr/local/bin/node --> prefix=/usr/local
const prefix = path.dirname(path.dirname(nodePath));
if (DESTDIR) {
return path.join(DESTDIR, prefix);
}
MorCohenAres marked this conversation as resolved.
Show resolved Hide resolved

return prefix;
}

return '';
}

function tryWhich(exec) {
try {
return fs.realpathSync(which.sync(exec));
} catch (err) { /* do nothing */ }
}

function tryConfigPath(configPath) {
try {
return ini.parse(fs.readFileSync(configPath, 'utf-8')).prefix;
} catch (err) { /* do nothing */ }
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8607,11 +8607,6 @@
"@types/minimatch" "*"
"@types/node" "*"

"@types/global-prefix@^3.0.0":
version "3.0.0"
resolved "https://registry.npmjs.org/@types/global-prefix/-/global-prefix-3.0.0.tgz#153c4534331e0dcb73a78e83b77668af1c274a9d"
integrity sha512-qkgDK8cbId7LPdNchNaBhTduJehCUwEFlKhORd3yLE+RPyKeJXtZu8doBTEOCEFkchyfAb6cbL/li486pCGY4w==

"@types/graceful-fs@^4.1.2":
version "4.1.5"
resolved "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
Expand Down