UNPKG

moshai-cli

Version:

A modern, fast Node.js CLI powered by arasadrahman

42 lines (36 loc) 1.48 kB
const fs = require('fs'); const path = require('path'); const axios = require('axios'); const mkdirp = require('mkdirp'); const chalk = require('chalk').default; const os = require('os'); module.exports = class DownloadAssetsCommand { static signature = 'dwp:assets {json}'; static description = 'Download all assets in JSON to Desktop/{site}/'; async handle({ json }) { let parsed; try { parsed = JSON.parse(json); } catch (e) { console.error(chalk.red('❌ Invalid JSON input')); return; } const site = parsed.site || 'site'; const assets = parsed.assets || []; const outputDir = path.join(os.homedir(), 'Desktop', site); mkdirp.sync(outputDir); console.log(chalk.green(`📂 Downloading to ${outputDir}`)); for (const { url, path: relPath } of assets) { const fullPath = path.join(outputDir, relPath); try { const res = await axios.get(url, { responseType: 'arraybuffer' }); mkdirp.sync(path.dirname(fullPath)); fs.writeFileSync(fullPath, res.data); console.log(chalk.green(`✔ ${relPath}`)); } catch (err) { console.error(chalk.red(`✖ ${relPath} - ${err.message}`)); } } console.log(chalk.green(`✅ Download complete (${assets.length} files)`)); } };