UNPKG

2.26 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.requireFreshPromise = exports.requireFreshSafe = exports.requireFreshCallback = exports.requireFresh = void 0;
4const path_1 = require("path");
5/**
6 * Require the path without any caching.
7 * @param path the path to require
8 * @returns the result of the `require` call
9 * @throws note that the `require` call may throw if something went wrong requiring the path
10 */
11function requireFresh(path) {
12 // Resolve the path to the one used for the cache
13 const resolvedPath = path_1.resolve(path);
14 // Attempt require with removals of the cache
15 delete require.cache[resolvedPath]; // clear require cache for the config file
16 const result = require(resolvedPath);
17 delete require.cache[resolvedPath]; // clear require cache for the config file
18 // Return result
19 return result;
20}
21exports.requireFresh = requireFresh;
22// alias
23exports.default = requireFresh;
24/**
25 * Require the path without any caching, but catch errors into the callback.
26 * Error cannot be returned because what if the module intended to RETURN (not throw) an error, hence why callback is used, as it can differentiate between returned and thrown errors.
27 * @param path the path to require
28 * @param next the callback
29 * @returns {void}
30 */
31function requireFreshCallback(path, next) {
32 let result, error;
33 try {
34 result = requireFresh(path);
35 }
36 catch (err) {
37 error = err;
38 }
39 next(error, result);
40}
41exports.requireFreshCallback = requireFreshCallback;
42// b/c alias
43exports.requireFreshSafe = requireFreshCallback;
44/**
45 * Require the path without any caching, but catch errors into the callback.
46 * Error cannot be returned because what if the module intended to RETURN (not throw) an error, hence why promise is used, as it can differentiate between returned and thrown errors.
47 * @param path the path to require
48 * @returns {void}
49 */
50function requireFreshPromise(path) {
51 return new Promise(function (resolve, reject) {
52 let result;
53 try {
54 result = requireFresh(path);
55 }
56 catch (err) {
57 return reject(err);
58 }
59 resolve(result);
60 });
61}
62exports.requireFreshPromise = requireFreshPromise;