'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var rc = require('rc'); var async = require('async'); var semver = require('semver'); var request = require('request'); var once = require('once'); var npa = require('npm-package-arg'); var Cacheman = require('cacheman'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var rc__default = /*#__PURE__*/_interopDefaultLegacy(rc); var semver__default = /*#__PURE__*/_interopDefaultLegacy(semver); var request__default = /*#__PURE__*/_interopDefaultLegacy(request); var once__default = /*#__PURE__*/_interopDefaultLegacy(once); var npa__default = /*#__PURE__*/_interopDefaultLegacy(npa); var Cacheman__default = /*#__PURE__*/_interopDefaultLegacy(Cacheman); let config = null; function config$1 (opts) { if (opts || !config) config = opts; return config } function getRegistry(scope) { const result = rc__default["default"]('npm', { registry: 'https://registry.npmjs.org/' }); const url = scope && result[`${scope}:registry`] || result.config_registry || result.registry; return url.endsWith('/') ? url : `${url}/` } function merge (result, ...objects) { for (const obj of objects) { for (const key in obj) { const source = obj[key]; if (source !== undefined) result[key] = source; } } return result; } const { get } = request__default["default"]; const { maxSatisfying, SemVer } = semver__default["default"]; const cache = new Cacheman__default["default"]('package', { ttl: 1000 }); function clearCache() { return cache.clear() } const defaultLog = msg => process.stderr.write(`${msg}\n`); function RemoteLS (opts) { merge(this, { development: true, optional: true, peer: false, license: false, verbose: false, queue: async.queue((task, done) => this._loadPackageJson(task, done), 8), tree: {}, flat: {}, errors: [] }, config$1(), opts); if (!this.registry) this.registry = getRegistry(); if (!this.logger) { const log = this.silent ? () => {} : defaultLog; this.logger = { debug: log, error: log }; } else { if (!this.logger.debug) this.logger.debug = this.logger.log; if (!this.logger.error) this.logger.error = this.logger.log; } this.queue.pause(); this.queue.error((err, task) => { const { name, parent, version } = task; err.module = { name, parent, version }; this.errors.push(err); this.logger.error(err.message); }); } RemoteLS.prototype._request = function (packageName, scope, name, task, callback) { const registryUrl = scope ? getRegistry(scope) : this.registry; const packageUrl = `${registryUrl.replace(/\/$/, '')}/${packageName}`; cache.get(packageName, (error, value) => { if (error || typeof value === 'undefined') { get(packageUrl, { json: true }, (err, res, pkg) => { if (err) { callback(err); } else if (res.statusCode < 200 || res.statusCode >= 400) { callback(new Error(`could not load ${name}@${task.version} - status ${res.statusCode}`)); } else { cache.set(packageName, pkg); callback(null, pkg); } }); } else { callback(null, value); } }); }; RemoteLS.prototype._loadPackageJson = function (task, done) { const { name } = task; const packateInfos = npa__default["default"](name); const couchPackageName = name && packateInfos.escapedName; const scope = name && packateInfos.scope; done = once__default["default"](done); this._request(couchPackageName, scope, name, task, (err, pkg) => { if (err) return done(err) try { if (this.verbose) { const license = this.license && pkg.license ? ` - ${pkg.license && pkg.license.type || pkg.license}` : ''; this.logger.debug(`loading: ${name}@${task.version}${license}`); } this._walkDependencies(task, pkg, done); } catch (e) { done(e); } }); }; RemoteLS.prototype._walkDependencies = function (task, packageJson, done) { const version = this._guessVersion(task.version, packageJson); const mandatoryDependencies = packageJson.versions[version].dependencies; const optionalDependencies = packageJson.versions[version].optionalDependencies; if (optionalDependencies) { for (const optionalDep in optionalDependencies) { delete mandatoryDependencies[optionalDep]; } } const dependencies = merge( {}, mandatoryDependencies, this.optional ? packageJson.versions[version].optionalDependencies : {}, this.peer ? packageJson.versions[version].peerDependencies : {}, (task.parent === this.tree && this.development) ? packageJson.versions[version].devDependencies : {} ); let license = packageJson.versions[version].license; if (license && license.type) license = license.type; const fullName = `${packageJson.name}@${version}${license ? ' - ' + license : ''}`; const parent = task.parent[fullName] = {}; if (this.flat[fullName]) return done() else this.flat[fullName] = true; for (const name in dependencies) { this.queue.push({ name, version: dependencies[name], parent }); } done(); }; RemoteLS.prototype._guessVersion = function (versionString, packageJson) { if (versionString === 'latest') versionString = '*'; const availableVersions = Object.keys(packageJson.versions); let version = maxSatisfying(availableVersions, versionString, true); if (!version && versionString === '*' && availableVersions.every(av => new SemVer(av, true).prerelease.length)) { version = packageJson['dist-tags'] && packageJson['dist-tags'].latest; } if (!version) throw Error(`could not find a satisfactory version for string ${versionString}`) else return version }; RemoteLS.prototype.ls = function (name, version, callback) { this.errors = []; this.queue.push({ name, version, parent: this.tree }); let promise; if (!callback) promise = new Promise(resolve => callback = resolve); this.queue.drain(() => callback(this.tree)); this.queue.resume(); return promise }; function ls(name, version, flatten, cb) { const ls = new RemoteLS(); if (typeof version === 'function') { cb = version; version = 'latest'; } if (typeof flatten === 'function') { cb = flatten; flatten = false; } const packages = () => flatten ? Object.keys(ls.flat) : ls.tree; if (!cb) { return ls.ls(name, version).then(() => ({ packages: packages(), errors: ls.errors })) } ls.ls(name, version, () => cb(packages(), ls.errors)); } exports.RemoteLS = RemoteLS; exports.clearCache = clearCache; exports.config = config$1; exports.ls = ls; //# sourceMappingURL=index.cjs.map