"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.devComponentAction = void 0;
const content_checker_1 = require("@ewizardjs/content-checker");
const fs_1 = require("fs");
const fs_extra_1 = require("fs-extra");
const os = require("os");
const path = require("path");
const util_1 = require("util");
const nodeWindowsPromise_1 = require("../../bin/utils/nodeWindowsPromise");
const action_1 = require("./action");
const lstatP = (0, util_1.promisify)(fs_1.lstat);
function devComponentAction(dir, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const demoDir = path.resolve(dir, 'demo');
        const symlinkDest = path.resolve(demoDir, 'node_modules');
        yield createComponentSymlink(dir, symlinkDest);
        yield (0, content_checker_1.validateInstalledDependencies)(demoDir);
        return (0, action_1.runDevBuild)(demoDir, options);
    });
}
exports.devComponentAction = devComponentAction;
function createComponentSymlink(componentSource, symlinkDest) {
    return __awaiter(this, void 0, void 0, function* () {
        const componentName = yield getComponentName(componentSource);
        const componentSymlinkDest = path.resolve(symlinkDest, componentName);
        try {
            const exists = yield (0, fs_extra_1.pathExists)(componentSymlinkDest);
            if (exists) {
                const stats = yield lstatP(componentSymlinkDest);
                if (!stats.isSymbolicLink()) {
                    yield (0, fs_extra_1.remove)(componentSymlinkDest);
                    yield (0, fs_extra_1.symlink)(componentSource, componentSymlinkDest, 'dir');
                }
            }
            else {
                yield (0, fs_extra_1.symlink)(componentSource, componentSymlinkDest, 'dir');
            }
        }
        catch (err) {
            if (err.code === 'EPERM' && os.platform() === 'win32') {
                return createComponentSymlinkAsAdmin(componentSource, componentSymlinkDest);
            }
            throw err;
        }
    });
}
function createComponentSymlinkAsAdmin(componentSource, symlinkDest) {
    return (0, nodeWindowsPromise_1.elevateAsync)(`cmd /c mklink /d ${symlinkDest} ${componentSource}`);
}
function getComponentName(componentSource) {
    const componentPackage = path.resolve(componentSource, 'package.json');
    return (0, fs_extra_1.readJSON)(componentPackage).then(({ name }) => name);
}
//# sourceMappingURL=component.action.js.map