Last active
April 30, 2019 16:15
-
-
Save loretoparisi/6e479d6b1bb4990af649cf3308b8b3c2 to your computer and use it in GitHub Desktop.
Find and Head with Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function scanDirStream(needle, params) { | |
var options = { | |
// find -type | |
type: 'f', | |
// find -name | |
name: '*', | |
limit: 100 | |
}; | |
for (var attrname in params) { options[attrname] = params[attrname]; } | |
return new Promise((resolve, reject) => { | |
var opt = [needle]; | |
for (var k in options) { | |
var v = options[k]; | |
if (!Util.empty(v) && k != 'limit') { | |
opt.push('-' + k); | |
opt.push(v); | |
} | |
}; | |
var res = ''; | |
console.log(opt) | |
var find = spawn('find', opt); | |
var head = spawn('head',[ '-n', options.limit]); | |
find.stdout.pipe(head.stdin); | |
head.stdout.on('data', _data => { | |
var buff = Buffer.from(_data, 'utf-8').toString(); | |
if (buff != '') res += buff; | |
}) | |
head.stderr.on('data', error => { | |
return reject(Buffer.from(error, 'utf-8').toString()); | |
}); | |
head.on('close', (_) => { | |
data = res.split('\n'); | |
data = data.slice(0, data.length - 1); | |
return resolve(data); | |
}); | |
}); | |
}//scanDirStream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment