2024-08-13 04:53:07 +03:00
|
|
|
import { readFile, readdir } from 'node:fs/promises'
|
|
|
|
import { dirname, join } from 'node:path'
|
|
|
|
import { fileURLToPath } from 'node:url'
|
2023-06-04 20:00:33 +03:00
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
export const rootDir = join(dirname(fileURLToPath(import.meta.url)), '../')
|
2023-06-04 20:00:33 +03:00
|
|
|
|
|
|
|
export async function tryParsePackageJson(packageName) {
|
|
|
|
try {
|
|
|
|
const path = packageName === '$root' ? rootDir : join(rootDir, 'packages', packageName)
|
|
|
|
|
|
|
|
return JSON.parse(
|
2023-06-05 03:30:48 +03:00
|
|
|
await readFile(join(path, 'package.json'), 'utf-8'),
|
2023-06-04 20:00:33 +03:00
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'ENOENT') throw e
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2023-06-04 20:00:33 +03:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPackageJsons(includeRoot = false) {
|
|
|
|
const packages = (await readdir(join(rootDir, 'packages')))
|
2024-08-13 04:53:07 +03:00
|
|
|
.filter(s => !s.startsWith('.'))
|
2023-06-04 20:00:33 +03:00
|
|
|
|
|
|
|
if (includeRoot) packages.push('$root')
|
|
|
|
|
2024-08-13 04:53:07 +03:00
|
|
|
return Promise.all(packages.map(tryParsePackageJson)).then(f => f.filter(Boolean))
|
2023-06-04 20:00:33 +03:00
|
|
|
}
|