UNPKG

11.3 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google Inc. All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10const core_1 = require("@angular-devkit/core");
11const node_1 = require("@angular-devkit/core/node");
12const fs_1 = require("fs");
13const os = require("os");
14const path = require("path");
15const find_up_1 = require("./find-up");
16function getSchemaLocation() {
17 return path.join(__dirname, '../lib/config/schema.json');
18}
19exports.workspaceSchemaPath = getSchemaLocation();
20const configNames = ['angular.json', '.angular.json'];
21const globalFileName = '.angular-config.json';
22function projectFilePath(projectPath) {
23 // Find the configuration, either where specified, in the Angular CLI project
24 // (if it's in node_modules) or from the current process.
25 return (projectPath && find_up_1.findUp(configNames, projectPath))
26 || find_up_1.findUp(configNames, process.cwd())
27 || find_up_1.findUp(configNames, __dirname);
28}
29function globalFilePath() {
30 const home = os.homedir();
31 if (!home) {
32 return null;
33 }
34 const p = path.join(home, globalFileName);
35 if (fs_1.existsSync(p)) {
36 return p;
37 }
38 return null;
39}
40const cachedWorkspaces = new Map();
41function getWorkspace(level = 'local') {
42 const cached = cachedWorkspaces.get(level);
43 if (cached != undefined) {
44 return cached;
45 }
46 const configPath = level === 'local' ? projectFilePath() : globalFilePath();
47 if (!configPath) {
48 cachedWorkspaces.set(level, null);
49 return null;
50 }
51 const root = core_1.normalize(path.dirname(configPath));
52 const file = core_1.normalize(path.basename(configPath));
53 const workspace = new core_1.experimental.workspace.Workspace(root, new node_1.NodeJsSyncHost());
54 let error;
55 workspace.loadWorkspaceFromHost(file).subscribe({
56 error: e => error = e,
57 });
58 if (error) {
59 throw new Error(`Workspace config file cannot be loaded: ${configPath}`
60 + `\n${error instanceof Error ? error.message : error}`);
61 }
62 cachedWorkspaces.set(level, workspace);
63 return workspace;
64}
65exports.getWorkspace = getWorkspace;
66function createGlobalSettings() {
67 const home = os.homedir();
68 if (!home) {
69 throw new Error('No home directory found.');
70 }
71 const globalPath = path.join(home, globalFileName);
72 fs_1.writeFileSync(globalPath, JSON.stringify({ version: 1 }));
73 return globalPath;
74}
75exports.createGlobalSettings = createGlobalSettings;
76function getWorkspaceRaw(level = 'local') {
77 let configPath = level === 'local' ? projectFilePath() : globalFilePath();
78 if (!configPath) {
79 if (level === 'global') {
80 configPath = createGlobalSettings();
81 }
82 else {
83 return [null, null];
84 }
85 }
86 let content = '';
87 new node_1.NodeJsSyncHost().read(core_1.normalize(configPath))
88 .subscribe(data => content = core_1.virtualFs.fileBufferToString(data));
89 const ast = core_1.parseJsonAst(content, core_1.JsonParseMode.Loose);
90 if (ast.kind != 'object') {
91 throw new Error(`Invalid JSON file: ${configPath}`);
92 }
93 return [ast, configPath];
94}
95exports.getWorkspaceRaw = getWorkspaceRaw;
96function validateWorkspace(json) {
97 const workspace = new core_1.experimental.workspace.Workspace(core_1.normalize('.'), new node_1.NodeJsSyncHost());
98 let error;
99 workspace.loadWorkspaceFromJson(json).subscribe({
100 error: e => error = e,
101 });
102 if (error) {
103 throw error;
104 }
105 return true;
106}
107exports.validateWorkspace = validateWorkspace;
108function getProjectByCwd(workspace) {
109 try {
110 return workspace.getProjectByPath(core_1.normalize(process.cwd()));
111 }
112 catch (e) {
113 if (e instanceof core_1.experimental.workspace.AmbiguousProjectPathException) {
114 return workspace.getDefaultProjectName();
115 }
116 throw e;
117 }
118}
119exports.getProjectByCwd = getProjectByCwd;
120function getConfiguredPackageManager() {
121 let workspace = getWorkspace('local');
122 if (workspace) {
123 const project = getProjectByCwd(workspace);
124 if (project && workspace.getProjectCli(project)) {
125 const value = workspace.getProjectCli(project)['packageManager'];
126 if (typeof value == 'string') {
127 return value;
128 }
129 }
130 if (workspace.getCli()) {
131 const value = workspace.getCli()['packageManager'];
132 if (typeof value == 'string') {
133 return value;
134 }
135 }
136 }
137 workspace = getWorkspace('global');
138 if (workspace && workspace.getCli()) {
139 const value = workspace.getCli()['packageManager'];
140 if (typeof value == 'string') {
141 return value;
142 }
143 }
144 // Only check legacy if updated workspace is not found.
145 if (!workspace) {
146 const legacyPackageManager = getLegacyPackageManager();
147 if (legacyPackageManager !== null) {
148 return legacyPackageManager;
149 }
150 }
151 return null;
152}
153exports.getConfiguredPackageManager = getConfiguredPackageManager;
154function migrateLegacyGlobalConfig() {
155 const homeDir = os.homedir();
156 if (homeDir) {
157 const legacyGlobalConfigPath = path.join(homeDir, '.angular-cli.json');
158 if (fs_1.existsSync(legacyGlobalConfigPath)) {
159 const content = fs_1.readFileSync(legacyGlobalConfigPath, 'utf-8');
160 const legacy = core_1.parseJson(content, core_1.JsonParseMode.Loose);
161 if (!legacy || typeof legacy != 'object' || Array.isArray(legacy)) {
162 return false;
163 }
164 const cli = {};
165 if (legacy.packageManager && typeof legacy.packageManager == 'string'
166 && legacy.packageManager !== 'default') {
167 cli['packageManager'] = legacy.packageManager;
168 }
169 if (legacy.defaults && typeof legacy.defaults == 'object' && !Array.isArray(legacy.defaults)
170 && legacy.defaults.schematics && typeof legacy.defaults.schematics == 'object'
171 && !Array.isArray(legacy.defaults.schematics)
172 && typeof legacy.defaults.schematics.collection == 'string') {
173 cli['defaultCollection'] = legacy.defaults.schematics.collection;
174 }
175 if (legacy.warnings && typeof legacy.warnings == 'object'
176 && !Array.isArray(legacy.warnings)) {
177 const warnings = {};
178 if (typeof legacy.warnings.versionMismatch == 'boolean') {
179 warnings['versionMismatch'] = legacy.warnings.versionMismatch;
180 }
181 if (Object.getOwnPropertyNames(warnings).length > 0) {
182 cli['warnings'] = warnings;
183 }
184 }
185 if (Object.getOwnPropertyNames(cli).length > 0) {
186 const globalPath = path.join(homeDir, globalFileName);
187 fs_1.writeFileSync(globalPath, JSON.stringify({ version: 1, cli }, null, 2));
188 return true;
189 }
190 }
191 }
192 return false;
193}
194exports.migrateLegacyGlobalConfig = migrateLegacyGlobalConfig;
195// Fallback, check for packageManager in config file in v1.* global config.
196function getLegacyPackageManager() {
197 const homeDir = os.homedir();
198 if (homeDir) {
199 const legacyGlobalConfigPath = path.join(homeDir, '.angular-cli.json');
200 if (fs_1.existsSync(legacyGlobalConfigPath)) {
201 const content = fs_1.readFileSync(legacyGlobalConfigPath, 'utf-8');
202 const legacy = core_1.parseJson(content, core_1.JsonParseMode.Loose);
203 if (!legacy || typeof legacy != 'object' || Array.isArray(legacy)) {
204 return null;
205 }
206 if (legacy.packageManager && typeof legacy.packageManager === 'string'
207 && legacy.packageManager !== 'default') {
208 return legacy.packageManager;
209 }
210 }
211 }
212 return null;
213}
214function getSchematicDefaults(collection, schematic, project) {
215 let result = {};
216 const fullName = `${collection}:${schematic}`;
217 let workspace = getWorkspace('global');
218 if (workspace && workspace.getSchematics()) {
219 const schematicObject = workspace.getSchematics()[fullName];
220 if (schematicObject) {
221 result = { ...result, ...schematicObject };
222 }
223 const collectionObject = workspace.getSchematics()[collection];
224 if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
225 result = { ...result, ...collectionObject[schematic] };
226 }
227 }
228 workspace = getWorkspace('local');
229 if (workspace) {
230 if (workspace.getSchematics()) {
231 const schematicObject = workspace.getSchematics()[fullName];
232 if (schematicObject) {
233 result = { ...result, ...schematicObject };
234 }
235 const collectionObject = workspace.getSchematics()[collection];
236 if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
237 result = { ...result, ...collectionObject[schematic] };
238 }
239 }
240 project = project || getProjectByCwd(workspace);
241 if (project && workspace.getProjectSchematics(project)) {
242 const schematicObject = workspace.getProjectSchematics(project)[fullName];
243 if (schematicObject) {
244 result = { ...result, ...schematicObject };
245 }
246 const collectionObject = workspace.getProjectSchematics(project)[collection];
247 if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
248 result = { ...result, ...collectionObject[schematic] };
249 }
250 }
251 }
252 return result;
253}
254exports.getSchematicDefaults = getSchematicDefaults;
255function isWarningEnabled(warning) {
256 let workspace = getWorkspace('local');
257 if (workspace) {
258 const project = getProjectByCwd(workspace);
259 if (project && workspace.getProjectCli(project)) {
260 const warnings = workspace.getProjectCli(project)['warnings'];
261 if (typeof warnings == 'object' && !Array.isArray(warnings)) {
262 const value = warnings[warning];
263 if (typeof value == 'boolean') {
264 return value;
265 }
266 }
267 }
268 if (workspace.getCli()) {
269 const warnings = workspace.getCli()['warnings'];
270 if (typeof warnings == 'object' && !Array.isArray(warnings)) {
271 const value = warnings[warning];
272 if (typeof value == 'boolean') {
273 return value;
274 }
275 }
276 }
277 }
278 workspace = getWorkspace('global');
279 if (workspace && workspace.getCli()) {
280 const warnings = workspace.getCli()['warnings'];
281 if (typeof warnings == 'object' && !Array.isArray(warnings)) {
282 const value = warnings[warning];
283 if (typeof value == 'boolean') {
284 return value;
285 }
286 }
287 }
288 return true;
289}
290exports.isWarningEnabled = isWarningEnabled;