@baseplate-dev/create-project
Version:
CLI starter kit for creating a new Baseplate project
24 lines (23 loc) • 836 B
JavaScript
import { findNearestPackageJson } from '@baseplate-dev/utils/node';
import { promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url';
let cachedVersion;
export async function getPackageVersion() {
if (cachedVersion === undefined) {
// Construct the path to the package.json file.
const packageJsonPath = await findNearestPackageJson({
cwd: fileURLToPath(import.meta.url),
});
if (packageJsonPath) {
// Read the package.json file.
const fileContent = await fs.readFile(packageJsonPath, 'utf8');
const packageJson = JSON.parse(fileContent);
// Return the version.
cachedVersion = packageJson.version || null;
}
else {
cachedVersion = null;
}
}
return cachedVersion;
}