'use strict'; const fs = require('node:fs'); const path = require('node:path'); const node_stream = require('node:stream'); const mkdirp = require('mkdirp'); const axios = require('axios'); const minimist = require('minimist'); const chalk = require('chalk'); const consola = require('consola'); const lockfile = require('@yarnpkg/lockfile'); const yaml = require('yaml'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const fs__default = /*#__PURE__*/_interopDefaultCompat(fs); const path__default = /*#__PURE__*/_interopDefaultCompat(path); const axios__default = /*#__PURE__*/_interopDefaultCompat(axios); const minimist__default = /*#__PURE__*/_interopDefaultCompat(minimist); const chalk__default = /*#__PURE__*/_interopDefaultCompat(chalk); const lockfile__default = /*#__PURE__*/_interopDefaultCompat(lockfile); var LockfileEnum = /* @__PURE__ */ ((LockfileEnum2) => { LockfileEnum2["YARN"] = "yarn.lock"; LockfileEnum2["PNPM"] = "pnpm-lock.yaml"; LockfileEnum2["NPM"] = "package-lock.json"; return LockfileEnum2; })(LockfileEnum || {}); const defaultConfig = { registry: "https://registry.npm.taobao.org/", directory: "./tgz-packages" }; const fileTypeList = Object.values(LockfileEnum); async function getFilePathByYarn(file) { const { object } = lockfile__default.parse(file); const yarnpkgInfo = Object.values(JSON.parse(JSON.stringify(object))); return Promise.resolve([...new Set(yarnpkgInfo.map((item) => item.resolved))]); } async function getFilePathByNpm(file) { const npmpkgInfo = Object.values(JSON.parse(file).dependencies); return Promise.resolve([...new Set(npmpkgInfo.map((item) => item.resolved))]); } async function getFilePathByPnpm(file) { const { packages } = yaml.parse(file); const pnpmPkgInfo = Object.values(packages); return Promise.resolve([...new Set(pnpmPkgInfo.map((item) => item.resolution.tarball))].filter((item) => item)); } (async () => { const args = minimist__default(process.argv.slice(2)); if (!args.lockfilePath) throw consola.consola.error(new Error(chalk__default.red("Please provide the `lockfilePath` parameter."))); const lockfileName = path__default.posix.basename(args.lockfilePath); if (!fileTypeList.includes(lockfileName)) throw new Error(chalk__default.red("Please provide the correct file type.")); let tgzUrlList = []; const file = fs__default.readFileSync(args.lockfilePath, "utf8"); if (lockfileName === LockfileEnum.NPM) tgzUrlList = await getFilePathByNpm(file); if (lockfileName === LockfileEnum.PNPM) tgzUrlList = await getFilePathByPnpm(file); if (lockfileName === LockfileEnum.YARN) tgzUrlList = await getFilePathByYarn(file); const dir = args.outputDir ?? defaultConfig.directory; await mkdirp.mkdirp(dir); consola.consola.start("Starting......."); for (const [index, item] of tgzUrlList.entries()) { if (item.includes("/@types/")) await mkdirp.mkdirp(`${dir}/types`); const directory = path__default.join(args.outputDir ?? defaultConfig.directory, item.includes("/@types/") ? "types" : "", path__default.posix.basename(new URL(item).pathname)); const response = await axios__default.get(item, { responseType: "stream" }); const output = fs__default.createWriteStream(directory); await new Promise((_resolve, reject) => { node_stream.pipeline(response.data, output, (err) => { if (!err) _resolve(true); else reject(err); }); }); process.stdout.write(chalk__default.blue(`schedule\uFF1A${index + 1} / ${tgzUrlList.length} \r`)); } consola.consola.success(chalk__default.green("done")); })();