@morfeo/compiler
Version:

40 lines • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWriter = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const DEFAULT_OPTIONS = {
delay: 10,
};
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;
}
exports.createWriter = createWriter;
//# sourceMappingURL=writer.js.map