Skip to content

Commit af2f088

Browse files
committed
Fix whoami returning root
1 parent be8d300 commit af2f088

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/mac.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"getPATHCommand": "",
1111
"getPrefixPathCommand": "npm config get prefix",
1212
"getCachePathCommand": "npm config get cache",
13-
"fixNPMPermissionsCommand": "chown -R $(whoami) %{0}"
13+
"getCurrentUserCommand": "whoami",
14+
"fixNPMPermissionsCommand": "chown -R %{0} %{1}"
1415
}

lib/permissions-service.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = (helpers, config) => {
66
const npmPrefixPathSuffixes = ["/lib/node_modules", "/lib/bin", "/lib/share"];
77

88
const { logMessage, execSync, sudoExec, stringFormat } = helpers;
9-
const { getPrefixPathCommand, getCachePathCommand, fixNPMPermissionsCommand } = config;
9+
const { getPrefixPathCommand, getCachePathCommand, getCurrentUserCommand, fixNPMPermissionsCommand } = config;
1010

1111
const getNPMFoldersWithMissingPermissions = (stderr) => {
1212
if (_.isEmpty(getPrefixPathCommand) || _.isEmpty(getCachePathCommand) || !eaccessCodeString.test(stderr)) {
@@ -47,6 +47,24 @@ module.exports = (helpers, config) => {
4747
return folders;
4848
};
4949

50+
const getCurrentUser = () => {
51+
if (_.isEmpty(getCurrentUserCommand)) {
52+
return "";
53+
}
54+
55+
let currentUser = "";
56+
57+
try {
58+
currentUser = execSync(getCurrentUserCommand);
59+
} catch (error) {
60+
logMessage(`Get current user failed. Error:\n${error}`);
61+
62+
currentUser = "";
63+
}
64+
65+
return currentUser;
66+
};
67+
5068
const fixMissingNPMPermissions = (folderPaths) => {
5169
if (_.isEmpty(folderPaths)) {
5270
logMessage("No folders with incorrect permissions.");
@@ -60,11 +78,19 @@ module.exports = (helpers, config) => {
6078
return Promise.resolve();
6179
}
6280

81+
const currentUser = getCurrentUser();
82+
83+
if (_.isEmpty(currentUser)) {
84+
logMessage("No current user.");
85+
86+
return Promise.resolve();
87+
}
88+
6389
const folderExpression = _.chain(folderPaths)
6490
.map(folderPath => `'${folderPath}'`)
6591
.join(" ")
6692
.value();
67-
const command = stringFormat(fixNPMPermissionsCommand, folderExpression);
93+
const command = stringFormat(fixNPMPermissionsCommand, currentUser, folderExpression);
6894

6995
return sudoExec(command);
7096
};

lib/windows.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"getPATHCommand": "echo %PATH%",
1111
"getPrefixPathCommand": "",
1212
"getCachePathCommand": "",
13+
"getCurrentUser": "",
1314
"fixNPMPermissionsCommand": ""
1415
}

0 commit comments

Comments
 (0)