@morfeo/compiler
Version:

35 lines • 1.01 kB
JavaScript
import * as fs from 'fs';
const DEFAULT_OPTIONS = {
delay: 10,
};
export function createWriter({ output, delay = DEFAULT_OPTIONS.delay, }) {
let timeout;
function safeWrite(path, content) {
const parts = path.split('/');
const checked = [];
parts.forEach((part, index) => {
checked.push(part);
const currentPath = checked.join('/');
if (fs.existsSync(currentPath)) {
return;
}
if (index === parts.length - 1) {
return;
}
fs.mkdirSync(currentPath);
});
return fs.promises.writeFile(path, content);
}
function writer(onWrite) {
if (timeout) {
clearTimeout(timeout);
}
return new Promise(resolve => {
timeout = setTimeout(() => {
safeWrite(output, onWrite()).then(resolve);
}, delay);
});
}
return writer;
}
//# sourceMappingURL=writer.js.map