25 lines
623 B
JavaScript
25 lines
623 B
JavaScript
'use strict';
|
|
|
|
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
|
|
function save(filepath, stream) {
|
|
return Promise.resolve().then(() => {
|
|
return fs.ensureDir(path.dirname(filepath));
|
|
}).then(() => {
|
|
const file = fs.createWriteStream(filepath);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
stream.pipe(file).on('error', error => {
|
|
reject(error);
|
|
}).on('finish', () => {
|
|
console.log('\x1b[32m%s\x1b[0m', `Saved '${filepath}'`);
|
|
|
|
resolve(filepath);
|
|
});
|
|
});
|
|
});
|
|
};
|
|
|
|
module.exports = save;
|