node.js - nodejs中shell命令的跟踪过程
问题描述
我想要:
- 下载一个大的 zip 文件 (
curl
), - 如果 #1 成功,将文件解压缩到目标目录 (
unzip
) - 如果 #2 成功,则删除文件 (
rm
)
我正在编写一个简单的nodejs
脚本来执行上述操作,并且正在child_process.execSync
为此使用。
const execSync = require('child_process').execSync
execSync(`curl --output ${source} ${target}`)
execSync(`unzip -q ${target} -d ${target-dir}`)
execSync(`rm ${target}`)
我正在使用的*sync
版本exec
来确保事情按顺序发生。由于这些是长时间运行的进程(包含大量文件的大型 zip 存档),我希望随着时间的推移看到进展。我可以使用类似的东西,npm progress
或者,因为curl
并且unzip
已经显示出进步,我不介意只使用它。我该如何实现这一目标?
更新:这是我迄今为止尝试过的。我可以获得一个下载进度条,但我一直盯着空白屏幕unzip
(这确实需要很长时间,因为存档真的很大)。我可以在unzip
没有 quiet 选项的情况下简单地执行,q
但随后我会得到每个解压缩文件的列表。我不想要那个。我只是想要一个进度条。我尝试使用节点模块unzip
和其他类似的模块,但它们不起作用。
const spawn = require('child_process').spawn
const ProgressBar = require('progress')
const http = require('http')
const fs = require('fs')
const target = fs.createWriteStream(target_file)
const req = http.request({ hostname: hostname, path: filename })
req.on('response', function(res) {
const len = parseInt(res.headers['content-length'], 10)
const bar = new ProgressBar(`downloading ${hostname}/${filename} [:bar] :rate/bps :percent :etas`, {
complete: '=',
incomplete: ' ',
width: 20,
total: len
})
res.on('data', function (chunk) {
bar.tick(chunk.length);
target.write(chunk);
})
res.on('end', function () {
target.end(function () {
console.log(`downloaded ${len} bytes to data/${filename}`)
const unzip = spawn('unzip', ['-q', filename, '-d', target_dir])
unzip.on('close', (code) => {
console.log(`unzip exited with code ${code}`)
const rm = spawn('rm', [new_filename])
rm.on('close', (code) => {
console.log(`rm exited with code ${code}`)
})
})
})
})
}).on('error', (err) => {
console.log('Error: ', err.message)
})
req.end()
解决方案
推荐阅读
- latex - 如何使用 biblatex 删除书目列表中的标签?
- javascript - window.matchMedia 在 Safari 中不起作用
- sql-server - 在两个日期时间之间分组
- python - Tradingview pinescript - 需要在前 2 天的高点和低点绘制水平虚线
- scala - Spark Json - 使用 nullable=false 应用架构
- pyspark - Pyspark - 固定列的汇总
- c# - 通过保存到 .txt 文件来持久化自定义类时的权限问题
- scala - Spark 转换生成未来日期
- python - Pymongo:有没有办法在获取数据时在同一个调用中使用查找和聚合
- javascript - 调整输入类型文件上传的图像大小时缺少图像经度和纬度