"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageLatestVersion = exports.isNpmMaintainer = exports.npm = void 0;
/**
 * npm操作在这个文件维护
 */
const axios_1 = __importDefault(require("axios"));
const chalk_1 = __importDefault(require("chalk"));
const child_process_1 = __importDefault(require("child_process"));
const cli_1 = require("../constant/cli");
const BASE_URL = 'https://registry.npmjs.org'; // NPM 请求地址
/**
 * 执行npm命令(兼容mac和windows)。
 * @example npm(['install', 'vue','-D'])
 * @param {array} params
 * @param {object} options
 * @returns 子进程
 */
function npm(params, options = {}) {
    return new Promise((resolve) => {
        const res = child_process_1.default.spawnSync(cli_1.NPM, params, Object.assign({ stdio: 'inherit', cwd: process.cwd() }, options));
        resolve(res);
    });
}
exports.npm = npm;
// 判断传入的名字是否是npm包维护者的
function isNpmMaintainer(npm, name) {
    return __awaiter(this, void 0, void 0, function* () {
        const url = `${BASE_URL}/${npm}`;
        let result;
        try {
            result = yield axios_1.default.get(url);
        }
        catch (e) {
            console.log(chalk_1.default.red(`npm上无法查询到 ${npm} 这个包`));
            console.log('');
            return false;
        }
        if (result.status === 200) {
            const maintainers = result.data.maintainers;
            return maintainers.some((maintainer) => maintainer.name === name);
        }
        else {
            console.log(`请求npm接口出现${result.status}出错`, result);
            return false;
        }
    });
}
exports.isNpmMaintainer = isNpmMaintainer;
// 获取包的最新版本号
function getPackageLatestVersion(npm) {
    return __awaiter(this, void 0, void 0, function* () {
        const url = `${BASE_URL}/${npm}`;
        const result = yield axios_1.default.get(url);
        if (result.status === 200) {
            const latestVersion = result.data['dist-tags'].latest;
            return latestVersion;
        }
        else {
            console.log(`请求npm接口出现${result.status}出错`, result);
            return '';
        }
    });
}
exports.getPackageLatestVersion = getPackageLatestVersion;
exports.default = {
    isNpmMaintainer,
    npm,
    getPackageLatestVersion,
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vdXRpbHMvbnBtLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7OztBQUFBOztHQUVHO0FBQ0gsa0RBQXlCO0FBQ3pCLGtEQUF5QjtBQUN6QixrRUFBOEI7QUFDOUIseUNBQXFDO0FBRXJDLE1BQU0sUUFBUSxHQUFHLDRCQUE0QixDQUFBLENBQUMsV0FBVztBQUV6RDs7Ozs7O0dBTUc7QUFDSCxTQUFnQixHQUFHLENBQUMsTUFBcUIsRUFBRSxVQUFrQixFQUFFO0lBQzdELE9BQU8sSUFBSSxPQUFPLENBQUMsQ0FBQyxPQUFPLEVBQUUsRUFBRTtRQUM3QixNQUFNLEdBQUcsR0FBRyx1QkFBRSxDQUFDLFNBQVMsQ0FBQyxTQUFHLEVBQUUsTUFBTSxrQkFDbEMsS0FBSyxFQUFFLFNBQVMsRUFDaEIsR0FBRyxFQUFFLE9BQU8sQ0FBQyxHQUFHLEVBQUUsSUFDZixPQUFPLEVBQ1YsQ0FBQTtRQUNGLE9BQU8sQ0FBQyxHQUFHLENBQUMsQ0FBQTtJQUNkLENBQUMsQ0FBQyxDQUFBO0FBQ0osQ0FBQztBQVRELGtCQVNDO0FBRUQscUJBQXFCO0FBQ3JCLFNBQXNCLGVBQWUsQ0FBQyxHQUFXLEVBQUUsSUFBWTs7UUFDN0QsTUFBTSxHQUF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