node-shntool
Version:
Node interface to shntool (http://www.etree.org/shnutils/shntool/)
76 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const concat = require("concat-stream");
const fs = require("fs");
const globby = require("globby");
const path = require("path");
const through = require("through2");
const util_1 = require("util");
const shntool_1 = require("../shntool");
const utils_1 = require("../utils");
function parameters(src, opt) {
opt = opt || {};
const joinfile = opt.dir ? path.join(opt.dir, 'joined.wav') : 'joined.wav';
//console.log('joinfile: ' + joinfile);
const destfile = opt.destfile ? opt.dir ? path.join(opt.dir, opt.destfile)
: opt.destfile
: joinfile;
const outputType = utils_1.safeString(opt.fmt) || 'wav';
const outputDirArgs = opt.dir ? ['-d', opt.dir] : [];
const args = ['join', '-O', 'always', '-P', 'none', '-o', outputType, '-q'];
args.push(...outputDirArgs);
const files = globby.sync(src) || [];
args.push(...files);
return {
args,
files,
joinfile,
destfile,
outputType,
};
}
function joinService(src, opt, cb) {
const { joinfile, destfile, args, files } = parameters(src, opt);
let err = null;
//console.log('destfile: ' + destfile);
function callback() {
const stats = fs.statSync(joinfile);
if (stats.isFile()) {
if (!opt.destfile) {
cb(err, path.normalize(joinfile));
}
else {
fs.renameSync(joinfile, destfile);
cb(err, path.normalize(destfile));
}
}
}
if (files.length < 1) {
cb(new Error('no files match'));
return;
}
else {
const proc = shntool_1.shntool(args), output = through(), stream = through(), error = concat();
proc.on('error', stream.emit.bind(stream, 'error'));
proc.stdout.pipe(output);
proc.stderr.pipe(error);
proc.on('close', function (code) {
if (code === 0) {
stream.emit('end');
}
else {
err = new Error(error.getBody().toString());
stream.emit('error');
}
});
stream.on('end', callback);
stream.on('error', callback);
}
return;
}
const joinPromise = util_1.promisify(joinService);
async function joined(src, opt) {
return joinPromise(src, opt);
}
exports.joined = joined;
//# sourceMappingURL=join.js.map