You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.4 KiB

'use strict';
const path = require('path');
const resolveFrom = require('resolve-from');
const parentModule = require('parent-module');
const resolve = moduleId => {
try {
return resolveFrom(path.dirname(parentModule(__filename)), moduleId);
} catch (error) {
return undefined;
}
};
const clear = moduleId => {
if (typeof moduleId !== 'string') {
throw new TypeError(`Expected a \`string\`, got \`${typeof moduleId}\``);
}
const filePath = resolve(moduleId);
if (!filePath) {
return;
}
// Delete itself from module parent
if (require.cache[filePath] && require.cache[filePath].parent) {
let i = require.cache[filePath].parent.children.length;
while (i--) {
if (require.cache[filePath].parent.children[i].id === filePath) {
require.cache[filePath].parent.children.splice(i, 1);
}
}
}
// Remove all descendants from cache as well
if (require.cache[filePath]) {
for (const {id} of require.cache[filePath].children) {
clear(id);
}
}
// Delete module from cache
delete require.cache[filePath];
};
clear.all = () => {
const directory = path.dirname(parentModule(__filename));
for (const moduleId of Object.keys(require.cache)) {
delete require.cache[resolveFrom(directory, moduleId)];
}
};
clear.match = regex => {
for (const moduleId of Object.keys(require.cache)) {
if (regex.test(moduleId)) {
clear(moduleId);
}
}
};
module.exports = clear;