|
1 | 1 | import { readFileSync } from 'node:fs'; |
2 | 2 | import { homedir } from 'node:os'; |
3 | | -import { dirname, join } from 'node:path'; |
| 3 | +import { join } from 'node:path'; |
4 | 4 | import { fileURLToPath } from 'node:url'; |
5 | 5 |
|
6 | 6 | const getPackageJsonVersion = () => { |
7 | 7 | try { |
8 | | - // Use import.meta.resolve to get the URL of package.json |
9 | | - const packageJsonUrl = import.meta.resolve('../package.json'); |
10 | | - const packageJsonPath = fileURLToPath(packageJsonUrl); |
11 | | - const packageJson = readFileSync(packageJsonPath, 'utf-8'); |
12 | | - const packageJsonObject = JSON.parse(packageJson); |
13 | | - return packageJsonObject.version; |
| 8 | + // Try different possible locations for package.json |
| 9 | + const possibleLocations = ['../package.json', '../../package.json']; |
| 10 | + |
| 11 | + for (const location of possibleLocations) { |
| 12 | + try { |
| 13 | + const packageJsonUrl = import.meta.resolve(location); |
| 14 | + const packageJsonPath = fileURLToPath(packageJsonUrl); |
| 15 | + const packageJson = readFileSync(packageJsonPath, 'utf-8'); |
| 16 | + const packageJsonObject = JSON.parse(packageJson); |
| 17 | + if (packageJsonObject.version) { |
| 18 | + return packageJsonObject.version; |
| 19 | + } |
| 20 | + } catch (error) { |
| 21 | + // Continue to next location if this one fails |
| 22 | + continue; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // If we get here, we couldn't find a valid package.json in any location |
| 27 | + console.error('Could not find package.json in any of the expected locations'); |
| 28 | + return undefined; |
14 | 29 | } catch (error) { |
15 | 30 | console.error('Failed to load package.json:', error); |
16 | 31 | return undefined; |
|
0 commit comments