@jsbits/get-package-version
Version:
Get the version of the package.json file found in the given directory or in one of its parents.
49 lines (45 loc) • 1.42 kB
JavaScript
/*
@jsbits/get-package-version
@author aMarCruz
@version 1.0.3 ESM+ES6
@license MIT
*/
/* eslint-disable */
import { resolve, join } from 'path';
import { existsSync } from 'fs';
const extractVersion = (pkgPath) => {
const pkgname = join(pkgPath, 'package.json');
let version;
if (existsSync(pkgname)) {
try {
version = require(pkgname).version;
}
catch ( _a) { }
}
return version;
};
/**
* Returns the version of the first package.json file found in the given
* directory or in one of its parents.
*
* - If you don't provide a path, the search starts in the current one.
* - The provided path can be relative to the current working directory.
* - Packages with a missing or empty `version` property are ignored.
*
* @param {string} [pkgPath=.] Initial directory to search, defaults to `process.cwd()`.
* @returns {string} The package version, or an empty string if it could not be found.
* @since 1.0.0
*/
const getPackageVersion = function _getPackageVersion(pkgPath) {
pkgPath = (pkgPath ? resolve(pkgPath) : process.cwd()).replace(/\\/g, '/');
while (~pkgPath.indexOf('/')) {
const version = extractVersion(pkgPath);
if (version) {
return version;
}
pkgPath = pkgPath.replace(/\/[^/]*$/, '');
}
return '';
};
export default getPackageVersion;
//# sourceMappingURL=index.mjs.map