Skip to content

Commit d093c6c

Browse files
connescbestander
authored andcommitted
Move ROOT_USER to a separate file, fixes #2807 (#2855)
1 parent 52da2c8 commit d093c6c

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

__tests__/constants.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import {getPathKey, isRootUser} from '../src/constants.js';
3+
import {getPathKey} from '../src/constants.js';
44

55
test('getPathKey', () => {
66
expect(getPathKey('win32', {PATH: 'foobar'})).toBe('PATH');
@@ -10,9 +10,3 @@ test('getPathKey', () => {
1010
expect(getPathKey('linux', {})).toBe('PATH');
1111
expect(getPathKey('darwin', {})).toBe('PATH');
1212
});
13-
14-
test('isRootUser', () => {
15-
expect(isRootUser(null)).toBe(false);
16-
expect(isRootUser(1001)).toBe(false);
17-
expect(isRootUser(0)).toBe(true);
18-
});

__tests__/util/root-user.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* @flow */
2+
3+
import {isRootUser} from '../../src/util/root-user.js';
4+
5+
test('isRootUser', () => {
6+
expect(isRootUser(null)).toBe(false);
7+
expect(isRootUser(1001)).toBe(false);
8+
expect(isRootUser(0)).toBe(true);
9+
});

src/constants.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,3 @@ export function getPathKey(platform: string, env: Env): string {
9090

9191
return pathKey;
9292
}
93-
94-
function getUid(): ?number {
95-
if (process.platform !== 'win32' && process.getuid) {
96-
return process.getuid();
97-
}
98-
return null;
99-
}
100-
101-
export const ROOT_USER = isRootUser(getUid());
102-
103-
export function isRootUser(uid: ?number): boolean {
104-
return uid === 0;
105-
}

src/fetchers/tarball-fetcher.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as constants from '../constants.js';
88
import * as crypto from '../util/crypto.js';
99
import BaseFetcher from './base-fetcher.js';
1010
import * as fsUtil from '../util/fs.js';
11+
import ROOT_USER from '../util/root-user.js';
1112

1213
const invariant = require('invariant');
1314
const path = require('path');
@@ -83,7 +84,7 @@ export default class TarballFetcher extends BaseFetcher {
8384
.pipe(untarStream)
8485
.on('error', reject)
8586
.on('entry', (entry: Object) => {
86-
if (constants.ROOT_USER) {
87+
if (ROOT_USER) {
8788
entry.props.uid = entry.uid = 0;
8889
entry.props.gid = entry.gid = 0;
8990
}

src/util/root-user.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* @flow */
2+
3+
function getUid(): ?number {
4+
if (process.platform !== 'win32' && process.getuid) {
5+
return process.getuid();
6+
}
7+
return null;
8+
}
9+
10+
export default isRootUser(getUid());
11+
12+
export function isRootUser(uid: ?number): boolean {
13+
return uid === 0;
14+
}

src/util/user-home-dir.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* @flow */
2+
3+
import ROOT_USER from './root-user.js';
4+
25
const path = require('path');
3-
const {ROOT_USER} = require('../constants');
46

57
const userHomeDir = (process.platform === 'linux' && ROOT_USER) ?
68
path.resolve('/usr/local/share') :

0 commit comments

Comments
 (0)