"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeNodes = exports.flatten = exports.getSourceOffset = exports.getAttribute = exports.hasAttribute = exports.findElementNodes = exports.parseFile = exports.FileChange = exports.tryUninstallPackage = exports.tryInstallPackage = exports.canResolvePackage = exports.getPackageManager = exports.supports = exports.escapeRegExp = exports.getProjects = exports.getWorkspace = exports.getWorkspacePath = exports.getProjectPaths = void 0;
const core_1 = require("@angular-devkit/core");
const path = require("path");
const workspace_models_1 = require("@schematics/angular/utility/workspace-models");
const child_process_1 = require("child_process");
const compiler_1 = require("@angular/compiler");
const tsUtils_1 = require("./tsUtils");
const configPaths = ['/.angular.json', '/angular.json'];
exports.getProjectPaths = (config, appendPrefix = true) => {
    const sourceDirs = [];
    const projects = exports.getProjects(config);
    for (const proj of projects) {
        let sourcePath = path.join('/', proj.sourceRoot);
        if (appendPrefix && (proj.prefix)) {
            sourcePath = path.join(sourcePath, proj.prefix);
        }
        sourceDirs.push(core_1.normalize(sourcePath));
    }
    return sourceDirs;
};
exports.getWorkspacePath = (host) => configPaths.find(x => host.exists(x));
exports.getWorkspace = (host) => {
    const configPath = exports.getWorkspacePath(host);
    if (configPath) {
        return JSON.parse(host.read(configPath).toString());
    }
    return null;
};
exports.getProjects = (config) => {
    const projects = [];
    for (const projName of Object.keys(config.projects)) {
        const proj = config.projects[projName];
        if ((proj.projectType && proj.projectType !== workspace_models_1.ProjectType.Application) ||
            (proj.architect && proj.architect.e2e && !proj.architect.build)) {
            continue;
        }
        projects.push(proj);
    }
    return projects;
};
exports.escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
exports.supports = (name) => {
    try {
        child_process_1.execSync(`${name} --version`, { stdio: 'ignore' });
        return true;
    }
    catch (_a) {
        return false;
    }
};
exports.getPackageManager = (host) => {
    const hasYarn = exports.supports('yarn');
    const hasYarnLock = host.exists('yarn.lock');
    if (hasYarn && hasYarnLock) {
        return 'yarn';
    }
    return 'npm';
};
exports.canResolvePackage = (pkg) => {
    let modulePath;
    try {
        modulePath = require.resolve(pkg);
    }
    finally {
        // eslint-disable-next-line no-unsafe-finally
        return !!modulePath;
    }
};
exports.tryInstallPackage = (context, packageManager, pkg) => {
    try {
        context.logger.debug(`Installing ${pkg} via ${packageManager}.`);
        switch (packageManager) {
            case 'yarn':
                child_process_1.execSync(`${packageManager} add ${pkg} --no-lock-file`, { stdio: 'ignore' });
                break;
            case 'npm':
                child_process_1.execSync(`${packageManager} i ${pkg} --no-save`, { stdio: 'ignore' });
                break;
        }
        context.logger.debug(`${pkg} installed successfully.`);
    }
    catch (e) {
        context.logger.warn(`Could not install ${pkg}.`, JSON.parse(e));
    }
};
exports.tryUninstallPackage = (context, packageManager, pkg) => {
    try {
        context.logger.debug(`Uninstalling ${pkg} via ${packageManager}`);
        switch (packageManager) {
            case 'yarn':
                child_process_1.execSync(`${packageManager} remove ${pkg}`, { stdio: 'ignore' });
                break;
            case 'npm':
                child_process_1.execSync(`${packageManager} uninstall ${pkg} --no-save`, { stdio: 'ignore' });
                break;
        }
        context.logger.debug(`${pkg} uninstalled successfully.`);
    }
    catch (e) {
        context.logger
            .warn(`Could not uninstall ${pkg}, you may want to uninstall it manually.`, JSON.parse(e));
    }
};
class FileChange {
    constructor(position = 0, text = '', replaceText = '', type = 'insert') {
        this.position = position;
        this.text = text;
        this.replaceText = replaceText;
        this.type = type;
    }
    apply(content) {
        if (this.type === 'insert') {
            return `${content.substring(0, this.position)}${this.text}${content.substring(this.position)}`;
        }
        return tsUtils_1.replaceMatch(content, this.replaceText, this.text, this.position);
    }
}
exports.FileChange = FileChange;
/**
 * Parses an Angular template file/content and returns an array of the root nodes of the file.
 *
 * @param host
 * @param filePath
 * @param encoding
 */
exports.parseFile = (host, filePath, encoding = 'utf8') => new compiler_1.HtmlParser().parse(host.read(filePath).toString(encoding), filePath).rootNodes;
exports.findElementNodes = (root, tag) => {
    const tags = new Set(Array.isArray(tag) ? tag : [tag]);
    return exports.flatten(Array.isArray(root) ? root : [root])
        .filter((node) => tags.has(node.name));
};
exports.hasAttribute = (root, attribute) => {
    const attrs = Array.isArray(attribute) ? attribute : [attribute];
    return !!root.attrs.find(a => attrs.includes(a.name));
};
exports.getAttribute = (root, attribute) => {
    const attrs = Array.isArray(attribute) ? attribute : [attribute];
    return root.attrs.filter(a => attrs.includes(a.name));
};
exports.getSourceOffset = (element) => {
    const { startSourceSpan, endSourceSpan } = element;
    return {
        startTag: { start: startSourceSpan.start.offset, end: startSourceSpan.end.offset },
        endTag: { start: endSourceSpan.start.offset, end: endSourceSpan.end.offset },
        file: {
            content: startSourceSpan.start.file.content,
            url: startSourceSpan.start.file.url
        },
        node: element
    };
};
const isElement = (node) => node.children !== undefined;
/**
 * Given an array of `Node` objects, flattens the ast tree to a single array.
 * De facto only `Element` type objects have children.
 *
 * @param list
 */
exports.flatten = (list) => {
    let r = [];
    for (const node of list) {
        r.push(node);
        if (isElement(node)) {
            r = r.concat(exports.flatten(node.children));
        }
    }
    return r;
};
/**
 * https://github.com/angular/angular/blob/master/packages/compiler/test/ml_parser/util/util.ts
 *
 * May be useful for validating the output of our own migrations,
 */
class SerializerVisitor {
    visitElement(element, context) {
        if (compiler_1.getHtmlTagDefinition(element.name).isVoid) {
            return `<${element.name}${this._visitAll(element.attrs, ' ')}/>`;
        }
        return `<${element.name}${this._visitAll(element.attrs, ' ')}>${this._visitAll(element.children)}</${element.name}>`;
    }
    visitAttribute(attribute, context) {
        return attribute.value === '' ? `${attribute.name}` : `${attribute.name}="${attribute.value}"`;
    }
    visitText(text, context) {
        return text.value;
    }
    visitComment(comment, context) {
        return `<!--${comment.value}-->`;
    }
    visitExpansion(expansion, context) {
        return `{${expansion.switchValue}, ${expansion.type},${this._visitAll(expansion.cases)}}`;
    }
    visitExpansionCase(expansionCase, context) {
        return ` ${expansionCase.value} {${this._visitAll(expansionCase.expression)}}`;
    }
    _visitAll(nodes, join = '') {
        if (nodes.length === 0) {
            return '';
        }
        return join + nodes.map(a => a.visit(this, null)).join(join);
    }
}
exports.serializeNodes = (nodes) => nodes.map(node => node.visit(new SerializerVisitor(), null));
