UNPKG

@morfeo/compiler

Version:

![Morfeo logo](https://morfeo.dev/img/morfeo.png)

35 lines 1.01 kB
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