Skip to content

Instantly share code, notes, and snippets.

@cellvia
Last active December 13, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cellvia/2c5d60461139cdb96cc9f907abd99833 to your computer and use it in GitHub Desktop.
Save cellvia/2c5d60461139cdb96cc9f907abd99833 to your computer and use it in GitHub Desktop.
retry loop grabbing a folder of files via adb
var Promise = require('bluebird')
var fs = require('fs')
var adb = require('adbkit')
var client = adb.createClient()
const pathToFiles = '/sdcard/Download';
const loop = () => {
client.listDevices()
.then(function(devices) {
const device = devices[0];
return client.readdir(device.id, pathToFiles)
.then(function(files) {
if(!files.length) throw new Error("no files")
console.log("THIS MANY LEFT: "+files.length)
const allFiles = files.filter( file => file.isFile() )
const thresholds = [];
for(var x=0, len = allFiles.length; x< len; x+=5 ){
thresholds.push(x);
}
return Promise.mapSeries( thresholds, x => Promise.map( allFiles.slice(x,x+5), file => {
console.log("pulling file: "+file.name)
console.log(file)
return client.pull(device.id, pathToFiles+'/'+file.name)
.then(function(transfer) {
return (new Promise(function(resolve, reject) {
var fn = __dirname + '/tmp/download/' + file.name;
transfer.on('end', function() {
console.log('[%s] Pull complete', device.id)
resolve(true)
})
transfer.on('error', reject)
transfer.pipe(fs.createWriteStream(fn))
})).then( () => {
console.log("stat: "+file.name)
return client.shell(device.id, "rm \""+pathToFiles+"/"+file.name+"\"")
.then(adb.util.readAll)
.then(function(output) {
console.log('[%s] %s', device.id, output.toString().trim())
})
})
})
}))
})
})
.then(function(res) {
console.log('Done pulling all files!')
})
.catch(function(err) {
console.error('Something went wrong:', err.stack)
setTimeout( loop, 2000 )
})
}
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment