You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
410 lines
16 KiB
410 lines
16 KiB
"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 fs_1.readFileSync(path, 'utf-8');
|
|
},
|
|
async writeFile(path, data) {
|
|
fs_1.writeFileSync(path, data);
|
|
},
|
|
async isDirectory(path) {
|
|
try {
|
|
return fs_1.statSync(path).isDirectory();
|
|
}
|
|
catch {
|
|
return false;
|
|
}
|
|
},
|
|
async isFile(path) {
|
|
try {
|
|
return 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 && find_up_1.findUp(configNames, projectPath)) ||
|
|
find_up_1.findUp(configNames, process.cwd()) ||
|
|
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 (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 (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 (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);
|
|
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 = 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 (fs_1.existsSync(legacyGlobalConfigPath)) {
|
|
const legacy = 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);
|
|
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 (fs_1.existsSync(legacyGlobalConfigPath)) {
|
|
const legacy = 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;
|