1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const core_1 = require("@angular-devkit/core");
|
11 | const node_1 = require("@angular-devkit/core/node");
|
12 | const fs_1 = require("fs");
|
13 | const os = require("os");
|
14 | const path = require("path");
|
15 | const find_up_1 = require("./find-up");
|
16 | function getSchemaLocation() {
|
17 | return path.join(__dirname, '../lib/config/schema.json');
|
18 | }
|
19 | exports.workspaceSchemaPath = getSchemaLocation();
|
20 | const configNames = ['angular.json', '.angular.json'];
|
21 | const globalFileName = '.angular-config.json';
|
22 | function projectFilePath(projectPath) {
|
23 |
|
24 |
|
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 | }
|
29 | function 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 | }
|
40 | const cachedWorkspaces = new Map();
|
41 | function 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 | }
|
65 | exports.getWorkspace = getWorkspace;
|
66 | function 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 | }
|
75 | exports.createGlobalSettings = createGlobalSettings;
|
76 | function 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 | }
|
95 | exports.getWorkspaceRaw = getWorkspaceRaw;
|
96 | function 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 | }
|
107 | exports.validateWorkspace = validateWorkspace;
|
108 | function 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 | }
|
119 | exports.getProjectByCwd = getProjectByCwd;
|
120 | function 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 |
|
145 | if (!workspace) {
|
146 | const legacyPackageManager = getLegacyPackageManager();
|
147 | if (legacyPackageManager !== null) {
|
148 | return legacyPackageManager;
|
149 | }
|
150 | }
|
151 | return null;
|
152 | }
|
153 | exports.getConfiguredPackageManager = getConfiguredPackageManager;
|
154 | function 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 | }
|
194 | exports.migrateLegacyGlobalConfig = migrateLegacyGlobalConfig;
|
195 |
|
196 | function 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 | }
|
214 | function 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 | }
|
254 | exports.getSchematicDefaults = getSchematicDefaults;
|
255 | function 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 | }
|
290 | exports.isWarningEnabled = isWarningEnabled;
|