// Imports
import * as fs from 'fs';
// Prepare arguments
let args = [];
process.argv.forEach(arg => {
let {0: key, 1: val = null} = arg.split('=', 2);
if (val) args[key] = val.replace(/^"(.*)"$/, '$1');
});
// Check required args
if (!args['path']) {
console.error('Use "path" argument!');
process.exit(0);
}
let filePath = args['path'];
// fs.stat promise wrapper
function fsStat(filePath) {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err, stats) => {
if (err) reject(stats);
else resolve(stats);
});
});
}
(async () => {
try {
// Check file exists
const stats = await fsStat(filePath);
if (!stats.isFile()) throw new Error('Is dir.');
console.log('File exists.');
} catch (e) {
console.log('File does not exists.');
}
console.log(fs.ReadStream(filePath));
})();