| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- "use strict";
- /**
- * @license
- * Copyright Google Inc. 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
- */
- Object.defineProperty(exports, "__esModule", { value: true });
- const core_1 = require("@angular-devkit/core");
- const node_1 = require("@angular-devkit/core/node");
- const fs_1 = require("fs");
- const os = require("os");
- const path = require("path");
- const find_up_1 = require("./find-up");
- 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 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;
- }
- const p = path.join(home, globalFileName);
- if (fs_1.existsSync(p)) {
- return p;
- }
- return null;
- }
- const cachedWorkspaces = new Map();
- 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;
- }
- const root = core_1.normalize(path.dirname(configPath));
- const file = core_1.normalize(path.basename(configPath));
- const workspace = new core_1.experimental.workspace.Workspace(root, new node_1.NodeJsSyncHost());
- let error;
- workspace.loadWorkspaceFromHost(file).subscribe({
- error: e => error = e,
- });
- if (error) {
- throw new Error(`Workspace config file cannot be loaded: ${configPath}`
- + `\n${error instanceof Error ? error.message : error}`);
- }
- cachedWorkspaces.set(level, workspace);
- return workspace;
- }
- 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];
- }
- }
- let content = '';
- new node_1.NodeJsSyncHost().read(core_1.normalize(configPath))
- .subscribe(data => content = core_1.virtualFs.fileBufferToString(data));
- const ast = core_1.parseJsonAst(content, core_1.JsonParseMode.Loose);
- if (ast.kind != 'object') {
- throw new Error(`Invalid JSON file: ${configPath}`);
- }
- return [ast, configPath];
- }
- exports.getWorkspaceRaw = getWorkspaceRaw;
- function validateWorkspace(json) {
- const workspace = new core_1.experimental.workspace.Workspace(core_1.normalize('.'), new node_1.NodeJsSyncHost());
- let error;
- workspace.loadWorkspaceFromJson(json).subscribe({
- error: e => error = e,
- });
- if (error) {
- throw error;
- }
- return true;
- }
- exports.validateWorkspace = validateWorkspace;
- function getProjectByCwd(workspace) {
- try {
- return workspace.getProjectByPath(core_1.normalize(process.cwd()));
- }
- catch (e) {
- if (e instanceof core_1.experimental.workspace.AmbiguousProjectPathException) {
- return workspace.getDefaultProjectName();
- }
- throw e;
- }
- }
- exports.getProjectByCwd = getProjectByCwd;
- function getConfiguredPackageManager() {
- let workspace = getWorkspace('local');
- if (workspace) {
- const project = getProjectByCwd(workspace);
- if (project && workspace.getProjectCli(project)) {
- const value = workspace.getProjectCli(project)['packageManager'];
- if (typeof value == 'string') {
- return value;
- }
- }
- if (workspace.getCli()) {
- const value = workspace.getCli()['packageManager'];
- if (typeof value == 'string') {
- return value;
- }
- }
- }
- workspace = getWorkspace('global');
- if (workspace && workspace.getCli()) {
- const value = workspace.getCli()['packageManager'];
- if (typeof value == 'string') {
- return value;
- }
- }
- // Only check legacy if updated workspace is not found.
- if (!workspace) {
- const legacyPackageManager = getLegacyPackageManager();
- if (legacyPackageManager !== null) {
- return legacyPackageManager;
- }
- }
- return 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 content = fs_1.readFileSync(legacyGlobalConfigPath, 'utf-8');
- const legacy = core_1.parseJson(content, core_1.JsonParseMode.Loose);
- if (!legacy || typeof legacy != 'object' || Array.isArray(legacy)) {
- return false;
- }
- const cli = {};
- if (legacy.packageManager && typeof legacy.packageManager == 'string'
- && legacy.packageManager !== 'default') {
- cli['packageManager'] = legacy.packageManager;
- }
- if (legacy.defaults && typeof legacy.defaults == 'object' && !Array.isArray(legacy.defaults)
- && legacy.defaults.schematics && typeof legacy.defaults.schematics == 'object'
- && !Array.isArray(legacy.defaults.schematics)
- && typeof legacy.defaults.schematics.collection == 'string') {
- cli['defaultCollection'] = legacy.defaults.schematics.collection;
- }
- if (legacy.warnings && typeof legacy.warnings == 'object'
- && !Array.isArray(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 content = fs_1.readFileSync(legacyGlobalConfigPath, 'utf-8');
- const legacy = core_1.parseJson(content, core_1.JsonParseMode.Loose);
- if (!legacy || typeof legacy != 'object' || Array.isArray(legacy)) {
- return null;
- }
- if (legacy.packageManager && typeof legacy.packageManager === 'string'
- && legacy.packageManager !== 'default') {
- return legacy.packageManager;
- }
- }
- }
- return null;
- }
- function getSchematicDefaults(collection, schematic, project) {
- let result = {};
- const fullName = `${collection}:${schematic}`;
- let workspace = getWorkspace('global');
- if (workspace && workspace.getSchematics()) {
- const schematicObject = workspace.getSchematics()[fullName];
- if (schematicObject) {
- result = { ...result, ...schematicObject };
- }
- const collectionObject = workspace.getSchematics()[collection];
- if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
- result = { ...result, ...collectionObject[schematic] };
- }
- }
- workspace = getWorkspace('local');
- if (workspace) {
- if (workspace.getSchematics()) {
- const schematicObject = workspace.getSchematics()[fullName];
- if (schematicObject) {
- result = { ...result, ...schematicObject };
- }
- const collectionObject = workspace.getSchematics()[collection];
- if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
- result = { ...result, ...collectionObject[schematic] };
- }
- }
- project = project || getProjectByCwd(workspace);
- if (project && workspace.getProjectSchematics(project)) {
- const schematicObject = workspace.getProjectSchematics(project)[fullName];
- if (schematicObject) {
- result = { ...result, ...schematicObject };
- }
- const collectionObject = workspace.getProjectSchematics(project)[collection];
- if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
- result = { ...result, ...collectionObject[schematic] };
- }
- }
- }
- return result;
- }
- exports.getSchematicDefaults = getSchematicDefaults;
- function isWarningEnabled(warning) {
- let workspace = getWorkspace('local');
- if (workspace) {
- const project = getProjectByCwd(workspace);
- if (project && workspace.getProjectCli(project)) {
- const warnings = workspace.getProjectCli(project)['warnings'];
- if (typeof warnings == 'object' && !Array.isArray(warnings)) {
- const value = warnings[warning];
- if (typeof value == 'boolean') {
- return value;
- }
- }
- }
- if (workspace.getCli()) {
- const warnings = workspace.getCli()['warnings'];
- if (typeof warnings == 'object' && !Array.isArray(warnings)) {
- const value = warnings[warning];
- if (typeof value == 'boolean') {
- return value;
- }
- }
- }
- }
- workspace = getWorkspace('global');
- if (workspace && workspace.getCli()) {
- const warnings = workspace.getCli()['warnings'];
- if (typeof warnings == 'object' && !Array.isArray(warnings)) {
- const value = warnings[warning];
- if (typeof value == 'boolean') {
- return value;
- }
- }
- }
- return true;
- }
- exports.isWarningEnabled = isWarningEnabled;
|