UNPKG

universal-webpack

Version:
59 lines (47 loc) 1.72 kB
import fs from 'fs'; // Waits for a file to be created // (e.g. after Webpack build process finishes). // // The Promise is resolved when the file has been found // (for example, this is needed for development because // `webpack-dev-server` and Node.js application server // are run in parallel). // export default function wait_for_file(path) { // waits for condition to be met, then resolves the promise return new Promise(function (resolve) { var tick_interval = 300; // show the message not too often var message_timer = 0; var message_interval = 2000; // in milliseconds tick(function () { // Check if the file exists in the filesystem var exists = fs.existsSync(path); if (!exists) { return false; } // Check if the file contents have been written to disk // https://github.com/catamphetamine/universal-webpack/issues/24 var contents = fs.readFileSync(path, 'utf8'); // Check if the file contents is empty if (!contents) { return false; } return true; }, tick_interval, resolve, function () { message_timer += tick_interval; if (message_timer >= message_interval) { message_timer = 0; console.log("(\"".concat(path, "\" not found)")); console.log('(waiting for the file to be generated; e.g. as a result of a Webpack build)'); } }); }); } function tick(check_condition, interval, done, not_done_yet) { var condition_is_met = check_condition(); if (condition_is_met) { return done(); } not_done_yet(); setTimeout(function () { return tick(check_condition, interval, done, not_done_yet); }, interval); } //# sourceMappingURL=waitForFile.js.map