"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTsConfigPaths = exports.getWorkspace = exports.createWorkspaceHost = void 0;
const core_1 = require("@angular-devkit/core");
const workspace_1 = require("@schematics/angular/utility/workspace");
/** Creates a file host to be used with Angular-Devkit's workspace utilities */
function createWorkspaceHost(tree) {
    return {
        readFile(path) {
            return __awaiter(this, void 0, void 0, function* () {
                const data = tree.read(path);
                if (!data) {
                    throw new Error(`File not found: ${path}`);
                }
                return core_1.virtualFs.fileBufferToString(data);
            });
        },
        writeFile(path, data) {
            return __awaiter(this, void 0, void 0, function* () {
                return tree.overwrite(path, data);
            });
        },
        isDirectory(path) {
            return __awaiter(this, void 0, void 0, function* () {
                return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
            });
        },
        isFile(path) {
            return __awaiter(this, void 0, void 0, function* () {
                return tree.exists(path);
            });
        }
    };
}
exports.createWorkspaceHost = createWorkspaceHost;
/** Returns the Angular workspace definition for the given path. The path must be a directory containing an angular.json or .angular.json file. */
function getWorkspace(path, tree) {
    return __awaiter(this, void 0, void 0, function* () {
        const host = createWorkspaceHost(tree);
        const { workspace } = yield core_1.workspaces.readWorkspace(path, host);
        return workspace;
    });
}
exports.getWorkspace = getWorkspace;
/** Returns all tsconfig paths from the angular workspace, including testing tsconfigs. */
function getTsConfigPaths(workspace, tree) {
    const buildPaths = new Set();
    const testPaths = new Set();
    for (const [name, target] of (0, workspace_1.allWorkspaceTargets)(workspace)) {
        if (name !== 'build' && name !== 'test') {
            continue;
        }
        for (const [, options] of (0, workspace_1.allTargetOptions)(target)) {
            if (options.tsConfig && typeof options.tsConfig === 'string' && tree.exists(options.tsConfig)) {
                const normalizedPath = (0, core_1.normalize)(options.tsConfig);
                name === 'build' ? buildPaths.add(normalizedPath) : testPaths.add(normalizedPath);
            }
        }
    }
    return [...buildPaths, ...testPaths];
}
exports.getTsConfigPaths = getTsConfigPaths;
//# sourceMappingURL=workspace.js.map