Skip to content

Commit acffd20

Browse files
Charles Lydingfilipesilva
authored andcommitted
refactor(@angular/cli): cleanup global config path discovery
1 parent db80f31 commit acffd20

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

packages/@angular/cli/models/config.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { oneLine } from 'common-tags';
44
import * as chalk from 'chalk';
55
import * as fs from 'fs';
66
import * as path from 'path';
7+
import { homedir } from 'os';
78

89
import {findUp} from '../utilities/find-up';
910

@@ -12,16 +13,6 @@ export const CLI_CONFIG_FILE_NAME = '.angular-cli.json';
1213
const CLI_CONFIG_FILE_NAME_ALT = 'angular-cli.json';
1314

1415

15-
function getUserHome() {
16-
const envHomeName = (process.platform.startsWith('win')) ? 'USERPROFILE' : 'HOME';
17-
const env = process.env[envHomeName];
18-
if (env == null) {
19-
throw new Error('Missing environment variable ' + envHomeName);
20-
}
21-
return env;
22-
}
23-
24-
2516
const configCacheMap = new Map<string, CliConfigBase<ConfigInterface>>();
2617

2718

@@ -52,11 +43,16 @@ export class CliConfig extends CliConfigBase<ConfigInterface> {
5243
}
5344

5445
static globalConfigFilePath(): string {
55-
let globalConfigPath = path.join(getUserHome(), CLI_CONFIG_FILE_NAME);
56-
const altGlobalConfigPath = path.join(getUserHome(), CLI_CONFIG_FILE_NAME_ALT);
57-
if (!fs.existsSync(globalConfigPath) && fs.existsSync(altGlobalConfigPath)) {
46+
const globalConfigPath = path.join(homedir(), CLI_CONFIG_FILE_NAME);
47+
if (fs.existsSync(globalConfigPath)) {
48+
return globalConfigPath;
49+
}
50+
51+
const altGlobalConfigPath = path.join(homedir(), CLI_CONFIG_FILE_NAME_ALT);
52+
if (fs.existsSync(altGlobalConfigPath)) {
5853
return altGlobalConfigPath;
5954
}
55+
6056
return globalConfigPath;
6157
}
6258

@@ -109,12 +105,7 @@ export class CliConfig extends CliConfigBase<ConfigInterface> {
109105
return configCacheMap.get(configPath);
110106
}
111107

112-
let globalConfigPath = path.join(getUserHome(), CLI_CONFIG_FILE_NAME);
113-
const altGlobalConfigPath = path.join(getUserHome(), CLI_CONFIG_FILE_NAME_ALT);
114-
if (!fs.existsSync(globalConfigPath) && fs.existsSync(altGlobalConfigPath)) {
115-
globalConfigPath = altGlobalConfigPath;
116-
}
117-
108+
const globalConfigPath = CliConfig.globalConfigFilePath();
118109
const cliConfig = CliConfigBase.fromConfigPath<ConfigInterface>(configPath, [globalConfigPath]);
119110

120111
const aliases = [

0 commit comments

Comments
 (0)