get-registry
Version:
Get registry config for current package manager
47 lines (46 loc) • 2.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const child_process_1 = require("child_process");
const which_pm_runs_1 = __importDefault(require("which-pm-runs"));
function get({ cwd } = {}) {
const agent = (0, which_pm_runs_1.default)();
const key = (agent === null || agent === void 0 ? void 0 : agent.name) === 'yarn' && !(agent === null || agent === void 0 ? void 0 : agent.version.startsWith('1.')) ? 'npmRegistryServer' : 'registry';
let name = (agent === null || agent === void 0 ? void 0 : agent.name) || 'npm';
if (name === 'deno')
name = 'npm';
const child = (0, child_process_1.exec)([name, 'config', 'get', key].join(' '), { cwd });
return new Promise((resolve, reject) => {
let stdout = '';
child.on('exit', (code) => {
if (!code)
return resolve(stdout.trim());
if (name === 'npm' && (agent === null || agent === void 0 ? void 0 : agent.name) !== 'npm')
return resolve(undefined);
reject(new Error(`child process exited with code ${code}`));
});
child.stdout.on('data', (data) => {
stdout += data.toString();
});
});
}
(function (get) {
function sync({ cwd } = {}) {
const agent = (0, which_pm_runs_1.default)();
const key = (agent === null || agent === void 0 ? void 0 : agent.name) === 'yarn' && !(agent === null || agent === void 0 ? void 0 : agent.version.startsWith('1.')) ? 'npmRegistryServer' : 'registry';
let name = (agent === null || agent === void 0 ? void 0 : agent.name) || 'npm';
if (name === 'deno')
name = 'npm';
try {
return (0, child_process_1.execSync)([(agent === null || agent === void 0 ? void 0 : agent.name) || 'npm', 'config', 'get', key].join(' '), { cwd }).toString().trim();
}
catch (error) {
if (name === 'npm' && (agent === null || agent === void 0 ? void 0 : agent.name) !== 'npm')
return undefined;
throw error;
}
}
get.sync = sync;
})(get || (get = {}));
module.exports = get;