"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const parse_name_1 = require("@schematics/angular/utility/parse-name");
const project_1 = require("@schematics/angular/utility/project");
const fs_1 = require("fs");
const project_2 = require("./project");
const tap_config_parser_1 = require("./tap-config-parser");
const utility_1 = require("./utility");
class MissingAppConfigKey extends Error {
    constructor(key) {
        super(`Missing key "${key}" in file ${project_2.APP_CONFIG_FILEPATH}`);
    }
}
exports.MissingAppConfigKey = MissingAppConfigKey;
// var _projectCache: Project | undefined = undefined;
function loadProject(tree, _context) {
    // if (!_projectCache){
    //     _projectCache = new Project(
    //         tree,
    //         _context
    //     );
    // }
    // return _projectCache;
    return new project_2.Project(tree, _context);
}
exports.loadProject = loadProject;
function updateTapConfig(tree, cb) {
    utility_1.updateJsonFile(tree, project_2.TAP_CONFIG_FILEPATH, cb);
}
exports.updateTapConfig = updateTapConfig;
function updateAppConfig(tree, cb) {
    utility_1.updateJsonFile(tree, project_2.APP_CONFIG_FILEPATH, cb);
}
exports.updateAppConfig = updateAppConfig;
function getTsPathConfigFromProject(tree, projectName) {
    const project = getAngularProject(tree, {
        project: projectName
    });
    const tsConfigPath = project['architect']['build']['options']['tsConfig'];
    if (!tsConfigPath) {
        throw new schematics_1.SchematicsException(`Cannot find tsConfig path`);
    }
    return tsConfigPath;
}
exports.getTsPathConfigFromProject = getTsPathConfigFromProject;
function addRouteInConfig(route, tree) {
    updateAppConfig(tree, (appConfig) => {
        // TODO 
        // if (!appConfig.app.routes.find(r => route.path === r.path)){
        //     appConfig.app.routes.push(route);
        // }
        return appConfig;
    });
}
exports.addRouteInConfig = addRouteInConfig;
function writeJsonFile(tree, filepath, content) {
    return tree.create(filepath, `${JSON.stringify(content, null, 4)}`);
}
exports.writeJsonFile = writeJsonFile;
// function getJsonFileContent(content: Buffer){
//     try {
//         return JSON.parse(content.toString());
//     }
//     catch (err){
//         throw new SchematicsException(`Cannot parse file ${filename} as JSON. ${err.message}`);
//     }
// }
function loadTapConfigFromFilePath(tapConfigFile) {
    let fileContent = fs_1.readFileSync(tapConfigFile).toString();
    return tap_config_parser_1.parseTapConfig(fileContent);
}
exports.loadTapConfigFromFilePath = loadTapConfigFromFilePath;
function hasAngularProject(tree, _options) {
    const workspaceConfig = getAngularConfig(tree);
    const projectName = _options.project || workspaceConfig.defaultProject;
    return projectName in workspaceConfig.projects;
}
exports.hasAngularProject = hasAngularProject;
function getDefaultAngularProject(tree) {
    let project = getAngularConfig(tree);
    if (!('defaultProject' in project)) {
        throw new schematics_1.SchematicsException(`Missing key ${'defaultProject'}`);
    }
    return project['defaultProject'];
}
exports.getDefaultAngularProject = getDefaultAngularProject;
function getAngularConfig(tree) {
    const workspaceConfigBuffer = tree.read('angular.json');
    if (!workspaceConfigBuffer) {
        throw new schematics_1.SchematicsException('Not an Angular CLI workspace');
    }
    const workspaceConfig = JSON.parse(workspaceConfigBuffer.toString());
    return workspaceConfig;
}
exports.getAngularConfig = getAngularConfig;
function getAngularProject(tree, _options) {
    const workspaceConfig = getAngularConfig(tree);
    const projectName = _options.project || workspaceConfig.defaultProject;
    const project = workspaceConfig.projects[projectName];
    if (!project) {
        throw new schematics_1.SchematicsException(`Project ${_options.project} does not exist.`);
    }
    return project;
}
exports.getAngularProject = getAngularProject;
function getAngularProjectPath(name, tree, _options) {
    const project = getAngularProject(tree, _options);
    const defaultProjectPath = project_1.buildDefaultPath(project);
    const parsedPath = parse_name_1.parseName(defaultProjectPath, name);
    return parsedPath;
}
exports.getAngularProjectPath = getAngularProjectPath;
//# sourceMappingURL=projects-utility.js.map