1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
"use strict";
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isWarningEnabled = exports.getSchematicDefaults = exports.migrateLegacyGlobalConfig = exports.getConfiguredPackageManager = exports.getProjectByCwd = exports.validateWorkspace = exports.getWorkspaceRaw = exports.createGlobalSettings = exports.getWorkspace = exports.AngularWorkspace = exports.workspaceSchemaPath = void 0;
const core_1 = require("@angular-devkit/core");
const fs_1 = require("fs");
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const find_up_1 = require("./find-up");
const json_file_1 = require("./json-file");
function isJsonObject(value) {
return value !== undefined && core_1.json.isJsonObject(value);
}
function createWorkspaceHost() {
return {
async readFile(path) {
return (0, fs_1.readFileSync)(path, 'utf-8');
},
async writeFile(path, data) {
(0, fs_1.writeFileSync)(path, data);
},
async isDirectory(path) {
try {
return (0, fs_1.statSync)(path).isDirectory();
}
catch {
return false;
}
},
async isFile(path) {
try {
return (0, fs_1.statSync)(path).isFile();
}
catch {
return false;
}
},
};
}
function getSchemaLocation() {
return path.join(__dirname, '../lib/config/schema.json');
}
exports.workspaceSchemaPath = getSchemaLocation();
const configNames = ['angular.json', '.angular.json'];
const globalFileName = '.angular-config.json';
function xdgConfigHome(home, configFile) {
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
const xdgConfigHome = process.env['XDG_CONFIG_HOME'] || path.join(home, '.config');
const xdgAngularHome = path.join(xdgConfigHome, 'angular');
return configFile ? path.join(xdgAngularHome, configFile) : xdgAngularHome;
}
function xdgConfigHomeOld(home) {
// Check the configuration files in the old location that should be:
// - $XDG_CONFIG_HOME/.angular-config.json (if XDG_CONFIG_HOME is set)
// - $HOME/.config/angular/.angular-config.json (otherwise)
const p = process.env['XDG_CONFIG_HOME'] || path.join(home, '.config', 'angular');
return path.join(p, '.angular-config.json');
}
function projectFilePath(projectPath) {
// Find the configuration, either where specified, in the Angular CLI project
// (if it's in node_modules) or from the current process.
return ((projectPath && (0, find_up_1.findUp)(configNames, projectPath)) ||
(0, find_up_1.findUp)(configNames, process.cwd()) ||
(0, find_up_1.findUp)(configNames, __dirname));
}
function globalFilePath() {
const home = os.homedir();
if (!home) {
return null;
}
// follow XDG Base Directory spec
// note that createGlobalSettings() will continue creating
// global file in home directory, with this user will have
// choice to move change its location to meet XDG convention
const xdgConfig = xdgConfigHome(home, 'config.json');
if ((0, fs_1.existsSync)(xdgConfig)) {
return xdgConfig;
}
// NOTE: This check is for the old configuration location, for more
// information see https://github.com/angular/angular-cli/pull/20556
const xdgConfigOld = xdgConfigHomeOld(home);
if ((0, fs_1.existsSync)(xdgConfigOld)) {
/* eslint-disable no-console */
console.warn(`Old configuration location detected: ${xdgConfigOld}\n` +
`Please move the file to the new location ~/.config/angular/config.json`);
return xdgConfigOld;
}
const p = path.join(home, globalFileName);
if ((0, fs_1.existsSync)(p)) {
return p;
}
return null;
}
class AngularWorkspace {
constructor(workspace, filePath) {
this.workspace = workspace;
this.filePath = filePath;
this.basePath = path.dirname(filePath);
}
get extensions() {
return this.workspace.extensions;
}
get projects() {
return this.workspace.projects;
}
// Temporary helper functions to support refactoring
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getCli() {
return this.workspace.extensions['cli'] || {};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getProjectCli(projectName) {
const project = this.workspace.projects.get(projectName);
return (project === null || project === void 0 ? void 0 : project.extensions['cli']) || {};
}
static async load(workspaceFilePath) {
const oldConfigFileNames = ['.angular-cli.json', 'angular-cli.json'];
if (oldConfigFileNames.includes(path.basename(workspaceFilePath))) {
// 1.x file format
// Create an empty workspace to allow update to be used
return new AngularWorkspace({ extensions: {}, projects: new core_1.workspaces.ProjectDefinitionCollection() }, workspaceFilePath);
}
const result = await core_1.workspaces.readWorkspace(workspaceFilePath, createWorkspaceHost(), core_1.workspaces.WorkspaceFormat.JSON);
return new AngularWorkspace(result.workspace, workspaceFilePath);
}
}
exports.AngularWorkspace = AngularWorkspace;
const cachedWorkspaces = new Map();
async function getWorkspace(level = 'local') {
const cached = cachedWorkspaces.get(level);
if (cached !== undefined) {
return cached;
}
const configPath = level === 'local' ? projectFilePath() : globalFilePath();
if (!configPath) {
cachedWorkspaces.set(level, null);
return null;
}
try {
const workspace = await AngularWorkspace.load(configPath);
cachedWorkspaces.set(level, workspace);
return workspace;
}
catch (error) {
throw new Error(`Workspace config file cannot be loaded: ${configPath}` +
`\n${error instanceof Error ? error.message : error}`);
}
}
exports.getWorkspace = getWorkspace;
function createGlobalSettings() {
const home = os.homedir();
if (!home) {
throw new Error('No home directory found.');
}
const globalPath = path.join(home, globalFileName);
(0, fs_1.writeFileSync)(globalPath, JSON.stringify({ version: 1 }));
return globalPath;
}
exports.createGlobalSettings = createGlobalSettings;
function getWorkspaceRaw(level = 'local') {
let configPath = level === 'local' ? projectFilePath() : globalFilePath();
if (!configPath) {
if (level === 'global') {
configPath = createGlobalSettings();
}
else {
return [null, null];
}
}
return [new json_file_1.JSONFile(configPath), configPath];
}
exports.getWorkspaceRaw = getWorkspaceRaw;
async function validateWorkspace(data) {
const schema = (0, json_file_1.readAndParseJson)(path.join(__dirname, '../lib/config/schema.json'));
const { formats } = await Promise.resolve().then(() => __importStar(require('@angular-devkit/schematics')));
const registry = new core_1.json.schema.CoreSchemaRegistry(formats.standardFormats);
const validator = await registry.compile(schema).toPromise();
const { success, errors } = await validator(data).toPromise();
if (!success) {
throw new core_1.json.schema.SchemaValidationException(errors);
}
}
exports.validateWorkspace = validateWorkspace;
function findProjectByPath(workspace, location) {
const isInside = (base, potential) => {
const absoluteBase = path.resolve(workspace.basePath, base);
const absolutePotential = path.resolve(workspace.basePath, potential);
const relativePotential = path.relative(absoluteBase, absolutePotential);
if (!relativePotential.startsWith('..') && !path.isAbsolute(relativePotential)) {
return true;
}
return false;
};
const projects = Array.from(workspace.projects)
.map(([name, project]) => [project.root, name])
.filter((tuple) => isInside(tuple[0], location))
// Sort tuples by depth, with the deeper ones first. Since the first member is a path and
// we filtered all invalid paths, the longest will be the deepest (and in case of equality
// the sort is stable and the first declared project will win).
.sort((a, b) => b[0].length - a[0].length);
if (projects.length === 0) {
return null;
}
else if (projects.length > 1) {
const found = new Set();
const sameRoots = projects.filter((v) => {
if (!found.has(v[0])) {
found.add(v[0]);
return false;
}
return true;
});
if (sameRoots.length > 0) {
// Ambiguous location - cannot determine a project
return null;
}
}
return projects[0][1];
}
function getProjectByCwd(workspace) {
if (workspace.projects.size === 1) {
// If there is only one project, return that one.
return Array.from(workspace.projects.keys())[0];
}
const project = findProjectByPath(workspace, process.cwd());
if (project) {
return project;
}
const defaultProject = workspace.extensions['defaultProject'];
if (defaultProject && typeof defaultProject === 'string') {
// If there is a default project name, return it.
return defaultProject;
}
return null;
}
exports.getProjectByCwd = getProjectByCwd;
async function getConfiguredPackageManager() {
var _a;
const getPackageManager = (source) => {
if (isJsonObject(source)) {
const value = source['packageManager'];
if (value && typeof value === 'string') {
return value;
}
}
};
let result;
const workspace = await getWorkspace('local');
if (workspace) {
const project = getProjectByCwd(workspace);
if (project) {
result = getPackageManager((_a = workspace.projects.get(project)) === null || _a === void 0 ? void 0 : _a.extensions['cli']);
}
result = result !== null && result !== void 0 ? result : getPackageManager(workspace.extensions['cli']);
}
if (result === undefined) {
const globalOptions = await getWorkspace('global');
result = getPackageManager(globalOptions === null || globalOptions === void 0 ? void 0 : globalOptions.extensions['cli']);
if (!workspace && !globalOptions) {
// Only check legacy if updated workspace is not found
result = getLegacyPackageManager();
}
}
// Default to null
return result !== null && result !== void 0 ? result : null;
}
exports.getConfiguredPackageManager = getConfiguredPackageManager;
function migrateLegacyGlobalConfig() {
const homeDir = os.homedir();
if (homeDir) {
const legacyGlobalConfigPath = path.join(homeDir, '.angular-cli.json');
if ((0, fs_1.existsSync)(legacyGlobalConfigPath)) {
const legacy = (0, json_file_1.readAndParseJson)(legacyGlobalConfigPath);
if (!isJsonObject(legacy)) {
return false;
}
const cli = {};
if (legacy.packageManager &&
typeof legacy.packageManager == 'string' &&
legacy.packageManager !== 'default') {
cli['packageManager'] = legacy.packageManager;
}
if (isJsonObject(legacy.defaults) &&
isJsonObject(legacy.defaults.schematics) &&
typeof legacy.defaults.schematics.collection == 'string') {
cli['defaultCollection'] = legacy.defaults.schematics.collection;
}
if (isJsonObject(legacy.warnings)) {
const warnings = {};
if (typeof legacy.warnings.versionMismatch == 'boolean') {
warnings['versionMismatch'] = legacy.warnings.versionMismatch;
}
if (Object.getOwnPropertyNames(warnings).length > 0) {
cli['warnings'] = warnings;
}
}
if (Object.getOwnPropertyNames(cli).length > 0) {
const globalPath = path.join(homeDir, globalFileName);
(0, fs_1.writeFileSync)(globalPath, JSON.stringify({ version: 1, cli }, null, 2));
return true;
}
}
}
return false;
}
exports.migrateLegacyGlobalConfig = migrateLegacyGlobalConfig;
// Fallback, check for packageManager in config file in v1.* global config.
function getLegacyPackageManager() {
const homeDir = os.homedir();
if (homeDir) {
const legacyGlobalConfigPath = path.join(homeDir, '.angular-cli.json');
if ((0, fs_1.existsSync)(legacyGlobalConfigPath)) {
const legacy = (0, json_file_1.readAndParseJson)(legacyGlobalConfigPath);
if (!isJsonObject(legacy)) {
return null;
}
if (legacy.packageManager &&
typeof legacy.packageManager === 'string' &&
legacy.packageManager !== 'default') {
return legacy.packageManager;
}
}
}
return null;
}
async function getSchematicDefaults(collection, schematic, project) {
var _a;
const result = {};
const mergeOptions = (source) => {
if (isJsonObject(source)) {
// Merge options from the qualified name
Object.assign(result, source[`${collection}:${schematic}`]);
// Merge options from nested collection schematics
const collectionOptions = source[collection];
if (isJsonObject(collectionOptions)) {
Object.assign(result, collectionOptions[schematic]);
}
}
};
// Global level schematic options
const globalOptions = await getWorkspace('global');
mergeOptions(globalOptions === null || globalOptions === void 0 ? void 0 : globalOptions.extensions['schematics']);
const workspace = await getWorkspace('local');
if (workspace) {
// Workspace level schematic options
mergeOptions(workspace.extensions['schematics']);
project = project || getProjectByCwd(workspace);
if (project) {
// Project level schematic options
mergeOptions((_a = workspace.projects.get(project)) === null || _a === void 0 ? void 0 : _a.extensions['schematics']);
}
}
return result;
}
exports.getSchematicDefaults = getSchematicDefaults;
async function isWarningEnabled(warning) {
var _a;
const getWarning = (source) => {
if (isJsonObject(source)) {
const warnings = source['warnings'];
if (isJsonObject(warnings)) {
const value = warnings[warning];
if (typeof value == 'boolean') {
return value;
}
}
}
};
let result;
const workspace = await getWorkspace('local');
if (workspace) {
const project = getProjectByCwd(workspace);
if (project) {
result = getWarning((_a = workspace.projects.get(project)) === null || _a === void 0 ? void 0 : _a.extensions['cli']);
}
result = result !== null && result !== void 0 ? result : getWarning(workspace.extensions['cli']);
}
if (result === undefined) {
const globalOptions = await getWorkspace('global');
result = getWarning(globalOptions === null || globalOptions === void 0 ? void 0 : globalOptions.extensions['cli']);
}
// All warnings are enabled by default
return result !== null && result !== void 0 ? result : true;
}
exports.isWarningEnabled = isWarningEnabled;
|