"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.getSourceFile = getSourceFile;
exports.getPackageVersionFromPackageJson = getPackageVersionFromPackageJson;
exports.hasPackage = hasPackage;
exports.hasDevPackage = hasDevPackage;
exports.getSourceTreePath = getSourceTreePath;
exports.getDistPath = getDistPath;
exports.createHost = createHost;
exports.getWorkspaceProject = getWorkspaceProject;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const ts = require("typescript");
/** Gets the ts source file from a path */
function getSourceFile(host, path) {
    const buffer = host.read(path);
    if (!buffer) {
        throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`);
    }
    return ts.createSourceFile(path, buffer.toString(), ts.ScriptTarget.Latest, true);
}
/** Get the version of a package name */
function getPackageVersionFromPackageJson(tree, name) {
    if (!tree.exists('package.json')) {
        return null;
    }
    const packageJson = JSON.parse(tree.read('package.json').toString('utf-8'));
    if (packageJson.dependencies && packageJson.dependencies[name]) {
        return packageJson.dependencies[name];
    }
    return null;
}
/** Check if a package exists in the package.json */
function hasPackage(tree, name) {
    if (!tree.exists('package.json')) {
        return null;
    }
    const packageJson = JSON.parse(tree.read('package.json').toString('utf-8'));
    return packageJson.dependencies && packageJson.dependencies[name];
}
/** Check if a package exists in the package.json */
function hasDevPackage(tree, name) {
    if (!tree.exists('package.json')) {
        return null;
    }
    const packageJson = JSON.parse(tree.read('package.json').toString('utf-8'));
    return packageJson.devDependencies && packageJson.devDependencies[name];
}
/** Returns the source path for the application */
function getSourceTreePath(host, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const project = yield getWorkspaceProject(host, options);
        return project.sourceRoot ? project.sourceRoot : '';
    });
}
/** Returns the dist path for the application */
function getDistPath(host, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const project = yield getWorkspaceProject(host, options);
        const value = project.targets;
        const optionsRecord = value.get('build').options;
        const outputDistPath = optionsRecord['outputPath'];
        return outputDistPath.toString();
    });
}
/** factory function to create a workspaces.WorkspaceHost from a Tree */
function createHost(tree) {
    return {
        readFile(path) {
            return __awaiter(this, void 0, void 0, function* () {
                const data = tree.read(path);
                if (!data) {
                    throw new schematics_1.SchematicsException('File not found.');
                }
                return data.toString('utf-8');
            });
        },
        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);
            });
        }
    };
}
/** Returns the workspace project for the application */
function getWorkspaceProject(host, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const workspaceHost = createHost(host);
        const { workspace } = yield core_1.workspaces.readWorkspace('/', workspaceHost);
        if (!options.project) {
            options.project = workspace.extensions.defaultProject;
        }
        const project = workspace.projects.get(options.project);
        if (!project) {
            throw new schematics_1.SchematicsException(`Invalid project name: ${options.project}`);
        }
        return project;
    });
}
//# sourceMappingURL=package-utils.js.map