@jsbits/get-package-version
Version:
Get the version of the package.json file found in the given directory or in one of its parents.
1 lines • 2.62 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["index.ts"],"sourcesContent":["import * as path from 'path'\nimport * as fs from 'fs'\n\n/**\n * Returns the version on package.json file, if it exists.\n *\n * @param {string} pkgPath Path to check out.\n * @returns {string|undefined} Version\n * @private\n */\nconst extractVersion = (pkgPath: string) => {\n\n const pkgname = path.join(pkgPath, 'package.json')\n let version: string | undefined\n\n // Try to get the version if package.json exists.\n if (fs.existsSync(pkgname)) {\n try {\n version = require(pkgname).version\n } catch { /**/ }\n }\n\n return version\n}\n\n/**\n * Returns the version of the first package.json file found in the given\n * directory or in one of its parents.\n *\n * - If you don't provide a path, the search starts in the current one.\n * - The provided path can be relative to the current working directory.\n * - Packages with a missing or empty `version` property are ignored.\n *\n * @param {string} [pkgPath=.] Initial directory to search, defaults to `process.cwd()`.\n * @returns {string} The package version, or an empty string if it could not be found.\n * @since 1.0.0\n */\nconst getPackageVersion = function _getPackageVersion (pkgPath?: string) {\n\n // Start with the current working directory, with normalized slashes\n pkgPath = (pkgPath ? path.resolve(pkgPath) : process.cwd()).replace(/\\\\/g, '/')\n\n while (~pkgPath.indexOf('/')) {\n\n // Try to get the version, the package may not contain one.\n const version = extractVersion(pkgPath)\n if (version) {\n return version\n }\n\n // package.json not found or does not contains version, move up\n pkgPath = pkgPath.replace(/\\/[^/]*$/, '')\n }\n\n return ''\n}\n\n// Export here so that TS places the JSDdoc in the correct position.\nexport = getPackageVersion\n"],"names":["path.join","fs.existsSync","path.resolve"],"mappings":";;;;;;;;;;;AAUA,MAAM,cAAc,GAAG,CAAC,OAAe;IAErC,MAAM,OAAO,GAAGA,IAAS,CAAC,OAAO,EAAE,cAAc,CAAC,CAAA;IAClD,IAAI,OAA2B,CAAA;IAG/B,IAAIC,UAAa,CAAC,OAAO,CAAC,EAAE;QAC1B,IAAI;YACF,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAA;SACnC;QAAC,QAAQ,IAAF,IAAQ;KACjB;IAED,OAAO,OAAO,CAAA;CACf,CAAA;;;;;;;;;;;;;AAcD,MAAM,iBAAiB,GAAG,SAAS,kBAAkB,CAAE,OAAgB;IAGrE,OAAO,GAAG,CAAC,OAAO,GAAGC,OAAY,CAAC,OAAO,CAAC,GAAI,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IAEhF,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAG5B,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC,CAAA;QACvC,IAAI,OAAO,EAAE;YACX,OAAO,OAAO,CAAA;SACf;QAGD,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAA;KAC1C;IAED,OAAO,EAAE,CAAA;CACV,CAAA;;;;"}