UNPKG

universal-webpack

Version:
70 lines (54 loc) 1.97 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = wait_for_file; var _fs = _interopRequireDefault(require("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). // 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["default"].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["default"].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