"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cpFile = require("cp-file");
const fg = require("fast-glob");
const fs = require("mz/fs");
const path = require("path");
const R = require("ramda");
const rimraf = require("rimraf-then");
const writeJsonFile = require("write-json-file");
const readImporterManifest_1 = require("../readImporterManifest");
const runNpm_1 = require("./runNpm");
async function default_1(args, opts, command) {
    if (args.length && args[0].endsWith('.tgz')) {
        await runNpm_1.default(['publish', ...args]);
        return;
    }
    const prefix = args.length && args[0] || process.cwd();
    let _status;
    await fakeRegularManifest({
        engineStrict: opts.engineStrict,
        prefix,
        workspacePrefix: opts.workspacePrefix || prefix,
    }, async () => {
        const { status } = await runNpm_1.default(['publish', ...opts.argv.original.slice(1)]);
        _status = status;
    });
    if (_status !== 0) {
        process.exit(_status);
    }
}
exports.default = default_1;
async function pack(args, opts, command) {
    let _status;
    await fakeRegularManifest({
        engineStrict: opts.engineStrict,
        prefix: opts.prefix,
        workspacePrefix: opts.workspacePrefix || opts.prefix,
    }, async () => {
        const { status } = await runNpm_1.default(['pack', ...opts.argv.original.slice(1)]);
        _status = status;
    });
    if (_status !== 0) {
        process.exit(_status);
    }
}
exports.pack = pack;
const LICENSE_GLOB = 'LICEN{S,C}E{,.*}';
const findLicenses = fg.bind(fg, [LICENSE_GLOB]);
async function fakeRegularManifest(opts, fn) {
    // If a workspace package has no License of its own,
    // license files from the root of the workspace are used
    const copiedLicenses = opts.prefix !== opts.workspacePrefix && (await findLicenses({ cwd: opts.prefix })).length === 0
        ? await copyLicenses(opts.workspacePrefix, opts.prefix) : [];
    const { fileName, manifest, writeImporterManifest } = await readImporterManifest_1.default(opts.prefix, opts);
    const publishManifest = makePublishManifest(manifest);
    const replaceManifest = fileName !== 'package.json' || !R.equals(manifest, publishManifest);
    if (replaceManifest) {
        await rimraf(path.join(opts.prefix, fileName));
        await writeJsonFile(path.join(opts.prefix, 'package.json'), publishManifest);
    }
    await fn();
    if (replaceManifest) {
        await rimraf(path.join(opts.prefix, 'package.json'));
        await writeImporterManifest(manifest, true);
    }
    await Promise.all(copiedLicenses.map((copiedLicense) => fs.unlink(copiedLicense)));
}
function makePublishManifest(originalManifest) {
    if (!originalManifest.publishConfig)
        return originalManifest;
    const publishManifest = Object.assign({}, originalManifest);
    if (originalManifest.publishConfig.main) {
        publishManifest.main = originalManifest.publishConfig.main;
    }
    if (originalManifest.publishConfig.module) {
        publishManifest.module = originalManifest.publishConfig.module;
    }
    if (originalManifest.publishConfig.typings) {
        publishManifest.typings = originalManifest.publishConfig.typings;
    }
    if (originalManifest.publishConfig.types) {
        publishManifest.types = originalManifest.publishConfig.types;
    }
    return publishManifest;
}
async function copyLicenses(sourceDir, destDir) {
    const licenses = await findLicenses({ cwd: sourceDir });
    if (licenses.length === 0)
        return [];
    const copiedLicenses = [];
    await Promise.all(licenses
        .map((licenseRelPath) => path.join(sourceDir, licenseRelPath))
        .map((licensePath) => {
        const licenseCopyDest = path.join(destDir, path.basename(licensePath));
        copiedLicenses.push(licenseCopyDest);
        return cpFile(licensePath, licenseCopyDest);
    }));
    return copiedLicenses;
}
