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