UNPKG

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 });
10exports.verifyBaseTsConfigExists = exports.addTsConfigProjectReferences = void 0;
11const core_1 = require("@angular-devkit/core");
12const schematics_1 = require("@angular-devkit/schematics");
13const json_utils_1 = require("./json-utils");
14const SOLUTION_TSCONFIG_PATH = 'tsconfig.json';
15/**
16 * Add project references in "Solution Style" tsconfig.
17 */
18function addTsConfigProjectReferences(paths) {
19 return (host, context) => {
20 const logger = context.logger;
21 // We need to read after each write to avoid missing `,` when appending multiple items.
22 for (const path of paths) {
23 const source = host.read(SOLUTION_TSCONFIG_PATH);
24 if (!source) {
25 // Solution tsconfig doesn't exist.
26 logger.warn(`Cannot add reference '${path}' in '${SOLUTION_TSCONFIG_PATH}'. File doesn't exists.`);
27 return;
28 }
29 const jsonAst = core_1.parseJsonAst(source.toString(), core_1.JsonParseMode.Loose);
30 if ((jsonAst === null || jsonAst === void 0 ? void 0 : jsonAst.kind) !== 'object') {
31 // Invalid JSON
32 throw new schematics_1.SchematicsException(`Invalid JSON AST Object '${SOLUTION_TSCONFIG_PATH}'.`);
33 }
34 // Solutions style tsconfig can contain 2 properties:
35 // - 'files' with a value of empty array
36 // - 'references'
37 const filesAst = json_utils_1.findPropertyInAstObject(jsonAst, 'files');
38 const referencesAst = json_utils_1.findPropertyInAstObject(jsonAst, 'references');
39 if ((filesAst === null || filesAst === void 0 ? void 0 : filesAst.kind) !== 'array' ||
40 filesAst.elements.length !== 0 ||
41 (referencesAst === null || referencesAst === void 0 ? void 0 : referencesAst.kind) !== 'array') {
42 logger.warn(`Cannot add reference '${path}' in '${SOLUTION_TSCONFIG_PATH}'. It appears to be an invalid solution style tsconfig.`);
43 return;
44 }
45 // Append new paths
46 const recorder = host.beginUpdate(SOLUTION_TSCONFIG_PATH);
47 json_utils_1.appendValueInAstArray(recorder, referencesAst, { 'path': `./${path}` }, 4);
48 host.commitUpdate(recorder);
49 }
50 };
51}
52exports.addTsConfigProjectReferences = addTsConfigProjectReferences;
53/**
54 * Throws an exception when the base tsconfig doesn't exists.
55 */
56function verifyBaseTsConfigExists(host) {
57 if (host.exists('tsconfig.base.json')) {
58 return;
59 }
60 throw new schematics_1.SchematicsException(`Cannot find base TypeScript configuration file 'tsconfig.base.json'.`);
61}
62exports.verifyBaseTsConfigExists = verifyBaseTsConfigExists;