-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathpackages.mjs
59 lines (52 loc) · 1.42 KB
/
packages.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {existsSync, readFileSync} from 'node:fs';
import {resolve} from 'node:path';
import {fileURLToPath} from 'node:url';
export const workspacesRoot = resolve(
fileURLToPath(import.meta.url),
'..',
'..'
);
export const packageDirsNpmTag = /** @type {const} */ ([
'atomic',
'auth',
'bueno',
'headless',
'atomic-react',
'atomic-hosted-page',
'atomic-angular/projects/atomic-angular',
'quantic',
]);
/**
* @typedef {(typeof packageDirsNpmTag)[number]} PackageDir
*/
/**
* @typedef PackageDefinition
* @property {string} name
* @property {string} version
* @property {PackageDir} packageDir
*/
/**
* @param {PackageDir} packageDir
*/
export function getPackagePathFromPackageDir(packageDir) {
return resolve(workspacesRoot, 'packages', packageDir);
}
/**
* @param {string} fullPath
* @returns {import('@npmcli/package-json').PackageJson}
*/
export function getPackageManifestFromPackagePath(fullPath) {
return JSON.parse(readFileSync(resolve(fullPath, 'package.json')).toString());
}
/**
* @param {PackageDir} packageDir E.g.: `samples/some-package`
* @returns {PackageDefinition}
*/
export function getPackageDefinitionFromPackageDir(packageDir) {
const fullPath = getPackagePathFromPackageDir(packageDir);
if (!existsSync(fullPath)) {
throw `Could not find package at ${fullPath}.`;
}
const {name, version} = getPackageManifestFromPackagePath(fullPath);
return {name, version, packageDir};
}