node/test/fixtures/module-hooks/get-stats.js
Joyee Cheung e85964610c module: implement module.registerHooks()
PR-URL: https://github.com/nodejs/node/pull/55698
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
2024-12-09 23:27:08 +00:00

21 lines
774 B
JavaScript

'use strict';
const path = require('path');
// Adapted from https://github.com/watson/module-details-from-path/blob/master/index.js
// used by require-in-the-middle to check the logic is still compatible with our new hooks.
exports.getStats = function getStats(filepath) {
const segments = filepath.split(path.sep);
const index = segments.lastIndexOf('node_modules');
if (index === -1) return {};
if (!segments[index + 1]) return {};
const scoped = segments[index + 1][0] === '@';
const name = scoped ? segments[index + 1] + '/' + segments[index + 2] : segments[index + 1];
const offset = scoped ? 3 : 2;
return {
name: name,
basedir: segments.slice(0, index + offset).join(path.sep),
path: segments.slice(index + offset).join(path.sep)
}
};