UNPKG

msync

Version:

Easily manage building and syncing multiple node-modules in a flexibly defined workspace.

35 lines (34 loc) 1.12 kB
import { npm, defaultValue } from './libs'; export async function info(pkg, options = {}) { const modules = (Array.isArray(pkg) ? pkg : [pkg]).filter(pkg => pkg.json.private !== true); const batchSize = defaultValue(options.batchSize, 20); const getInfo = async (pkg) => { try { const name = pkg.name; const version = pkg.version; const latest = await npm.getVersion(pkg.name); const res = { name, version, latest }; return res; } catch (error) { const message = `Failed getting NPM info for '${pkg.name}'. ${error.message}`; throw new Error(message); } }; let res = []; for (const batch of chunk(batchSize, modules)) { const items = await Promise.all(batch.map(pkg => getInfo(pkg))); res = [...res, ...items]; } return res; } function chunk(size, items) { const chunks = Math.ceil(items.length / size); const result = []; let i = 0; while (i < chunks) { result[i] = items.splice(0, size); i++; } return result; }