数通智联化工云平台
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.

56 lines
1.4 KiB

2 years ago
"use strict";
2 years ago
2 years ago
const path = require("path");
2 years ago
/**
* Gets the module version from package name
*/
module.exports = function getModuleVersion(...moduleNames) {
2 years ago
const packageName = moduleNames.pop();
2 years ago
2 years ago
let ownerModuleRootPath = process.cwd();
for (const ownerNames of moduleNames) {
ownerModuleRootPath =
getModuleRootPath(ownerNames, ownerModuleRootPath) || ownerModuleRootPath;
}
try {
const m = require("module");
const relativeTo = path.join(ownerModuleRootPath, "__placeholder__.js");
// eslint-disable-next-line node/no-unsupported-features/node-builtins -- ignore
return m.createRequire(relativeTo)(`${packageName}/package.json`).version;
} catch {
// ignore
}
try {
return require(`${packageName}/package.json`).version;
} catch {
// ignore
}
2 years ago
2 years ago
return null;
};
2 years ago
/**
* Get module root path
*/
function getModuleRootPath(packageName, ownerModuleRootPath) {
2 years ago
try {
const m = require("module");
const relativeTo = path.join(ownerModuleRootPath, "__placeholder__.js");
2 years ago
2 years ago
return path.dirname(
// eslint-disable-next-line node/no-unsupported-features/node-builtins -- ignore
m.createRequire(relativeTo).resolve(`${packageName}/package.json`)
);
} catch {
// ignore
}
try {
return path.dirname(require.resolve(`${packageName}/package.json`));
} catch {
// ignore
}
2 years ago
2 years ago
return null;
2 years ago
}