Skip to content

Commit 07e8bf9

Browse files
joshuachpalan-agius4
authored andcommitted
fix(@angular/cli): Support XDG Base Directory Specfication
Reading throw #16034 the angular config should be placed in the `$XDG_CONFIG_HOME/angular/` directory, but if the environmental variable $XDG_CONFIG_HOME is set the first check of `xdgConfigHome` function will only put it in the `$XDG_CONFIG_HOME` directory. Also renamed the config file from `.angular-config.json` to `config.json` when it is in the `~/.config/angular` directory since it doesn't need to be hidden. (cherry picked from commit 094d36d)
1 parent dd1d4ef commit 07e8bf9

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

packages/angular/cli/utilities/config.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,24 @@ function getSchemaLocation(): string {
4848

4949
export const workspaceSchemaPath = getSchemaLocation();
5050

51-
const configNames = [ 'angular.json', '.angular.json' ];
51+
const configNames = ['angular.json', '.angular.json'];
5252
const globalFileName = '.angular-config.json';
5353

5454
function xdgConfigHome(home: string, configFile?: string): string {
5555
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
56+
const xdgConfigHome = process.env['XDG_CONFIG_HOME'] || path.join(home, '.config');
57+
const xdgAngularHome = path.join(xdgConfigHome, 'angular');
58+
59+
return configFile ? path.join(xdgAngularHome, configFile) : xdgAngularHome;
60+
}
61+
62+
function xdgConfigHomeOld(home: string): string {
63+
// Check the configuration files in the old location that should be:
64+
// - $XDG_CONFIG_HOME/.angular-config.json (if XDG_CONFIG_HOME is set)
65+
// - $HOME/.config/angular/.angular-config.json (otherwise)
5666
const p = process.env['XDG_CONFIG_HOME'] || path.join(home, '.config', 'angular');
5767

58-
return configFile ? path.join(p, configFile) : p;
68+
return path.join(p, '.angular-config.json');
5969
}
6070

6171
function projectFilePath(projectPath?: string): string | null {
@@ -78,10 +88,22 @@ function globalFilePath(): string | null {
7888
// note that createGlobalSettings() will continue creating
7989
// global file in home directory, with this user will have
8090
// choice to move change its location to meet XDG convention
81-
const xdgConfig = xdgConfigHome(home, globalFileName);
91+
const xdgConfig = xdgConfigHome(home, 'config.json');
8292
if (existsSync(xdgConfig)) {
8393
return xdgConfig;
8494
}
95+
// NOTE: This check is for the old configuration location, for more
96+
// information see https://github.com/angular/angular-cli/pull/20556
97+
const xdgConfigOld = xdgConfigHomeOld(home);
98+
if (existsSync(xdgConfigOld)) {
99+
// tslint:disable: no-console
100+
console.warn(
101+
`Old configuration location detected: ${xdgConfigOld}\n` +
102+
`Please move the file to the new location ~/.config/angular/config.json`,
103+
);
104+
105+
return xdgConfigOld;
106+
}
85107

86108
const p = path.join(home, globalFileName);
87109
if (existsSync(p)) {
@@ -201,7 +223,9 @@ export function getWorkspaceRaw(
201223
}
202224

203225
export async function validateWorkspace(data: json.JsonObject): Promise<void> {
204-
const schema = readAndParseJson(path.join(__dirname, '../lib/config/schema.json')) as json.schema.JsonSchema;
226+
const schema = readAndParseJson(
227+
path.join(__dirname, '../lib/config/schema.json'),
228+
) as json.schema.JsonSchema;
205229
const { formats } = await import('@angular-devkit/schematics');
206230
const registry = new json.schema.CoreSchemaRegistry(formats.standardFormats);
207231
const validator = await registry.compile(schema).toPromise();

0 commit comments

Comments
 (0)