node-shntool
Version:
Node interface to shntool (http://www.etree.org/shnutils/shntool/)
84 lines • 2.6 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");
function parameters(src, opt) {
opt = opt || {};
const args = ['cue'];
const files = globby.sync(src) || [];
const destfile = opt.destfile ? path.normalize(opt.destfile) : undefined;
args.push(...files);
return {
args,
files,
destfile,
};
}
function cuefileService(src, opt, cb) {
const { args, files, destfile } = parameters(src, opt);
//console.log('CMD = shntool ' + args.join(' '));
if (destfile) {
try {
fs.accessSync(destfile, fs.constants.F_OK);
cb(new Error('destfile already exists'));
return;
}
catch (err) {
// ignore error because we were checking existence
}
}
let cuefile = '';
let err = null;
function accumulateFile(data, enc, cb) {
//console.log('accumulating');
cuefile += data;
cb(null, data);
}
if (files.length < 1) {
cb(new Error('no files match'));
return;
}
const proc = shntool_1.shntool(args), output = through(accumulateFile), 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) {
//console.log('emitting: end');
stream.emit('end');
}
else {
err = new Error(error.getBody().toString());
//console.log('emitting: error');
stream.emit('error');
}
});
function callback() {
//console.log('callback called');
const lines = cuefile
.split(/[\r\n]+/)
.filter(function (s) { return s; });
if (destfile && lines.length > 0) {
fs.writeFile(destfile, cuefile, (e) => {
cb(e, lines);
});
}
else {
cb(err, lines);
}
}
stream.on('end', callback);
stream.on('error', callback);
return;
}
const cuePromise = util_1.promisify(cuefileService);
async function cuefile(src, opt) {
return cuePromise(src, opt);
}
exports.cuefile = cuefile;
//# sourceMappingURL=cue.js.map