1+ import path from 'path' ;
2+ import type { PackageInfo as WSPackageInfo } from 'workspace-tools' ;
13import type { PackageInfo , PackageInfos } from '../types/PackageInfo' ;
24import { getPackageInfosWithOptions } from '../options/getPackageInfosWithOptions' ;
35import type { CliOptions , RepoOptions } from '../types/BeachballOptions' ;
46import { defaultRemoteBranchName } from './gitDefaults' ;
57
8+ export type PartialPackageInfo = Omit < Partial < PackageInfo > , 'combinedOptions' | 'packageOptions' > & {
9+ beachball ?: PackageInfo [ 'packageOptions' ] ;
10+ } ;
11+
612export type PartialPackageInfos = {
7- [ name : string ] : Omit < Partial < PackageInfo > , 'combinedOptions' | 'packageOptions' > & {
8- beachball ?: PackageInfo [ 'packageOptions' ] ;
9- } ;
13+ [ name : string ] : PartialPackageInfo ;
1014} ;
1115
1216/**
@@ -16,31 +20,70 @@ export type PartialPackageInfos = {
1620 * name: '<key>',
1721 * version: '1.0.0',
1822 * private: false,
19- * packageJsonPath: ''
23+ * packageJsonPath: `${cliOptions.path || ''}/packages/<basename>/package.json`,
2024 * }
2125 * ```
2226 * Other defaults and values are filled by the actual logic in `getPackageInfosWithOptions`,
2327 * including the overrides in `repoOptions` merged in realistic order.
28+ * @param repoOptions Extra repo options. A `branch` option is included automatically (to prevent lookup).
29+ * @param cliOptions CLI options. Use `path` to specify the CWD.
2430 */
2531export function makePackageInfos (
2632 packageInfos : PartialPackageInfos ,
2733 repoOptions ?: Partial < RepoOptions > ,
2834 cliOptions ?: Partial < CliOptions >
2935) : PackageInfos {
36+ const cwd = cliOptions ?. path || '' ;
3037 return getPackageInfosWithOptions (
31- Object . entries ( packageInfos ) . map ( ( [ name , info ] ) => {
32- return {
38+ Object . entries ( packageInfos ) . map (
39+ ( [ name , info ] ) : WSPackageInfo => ( {
3340 name,
3441 version : '1.0.0' ,
3542 private : false ,
36- packageOptions : { } ,
37- packageJsonPath : '' ,
43+ packageJsonPath : path . join ( cwd , 'packages' , path . basename ( name ) , 'package.json' ) ,
3844 ...info ,
39- } ;
40- } ) ,
45+ } )
46+ ) ,
4147 {
4248 repoOptions : { branch : defaultRemoteBranchName , ...repoOptions } ,
43- cliOptions : { path : '' , command : '' , ...cliOptions } ,
49+ cliOptions : { path : cwd , command : '' , ...cliOptions } ,
4450 }
4551 ) ;
4652}
53+
54+ /**
55+ * Makes a properly typed PackageInfos object from a partial object, filling in defaults:
56+ * ```js
57+ * {
58+ * name: '<folder basename>',
59+ * version: '1.0.0',
60+ * private: false,
61+ * packageJsonPath: `${cwd}/${key}/package.json`,
62+ * }
63+ * ```
64+ * Other defaults and values are filled by the actual logic in `getPackageInfosWithOptions`,
65+ * including the overrides in `repoOptions` merged in realistic order.
66+ */
67+ export function makePackageInfosByFolder ( params : {
68+ packages : { [ folder : string ] : PartialPackageInfo } ;
69+ cwd : string ;
70+ /** Extra repo options. A `branch` option is included automatically (to prevent lookup). */
71+ repoOptions ?: Partial < RepoOptions > ;
72+ /** Extra CLI options */
73+ cliOptions ?: Partial < Omit < CliOptions , 'path' > > ;
74+ } ) : PackageInfos {
75+ const { packages, cwd, repoOptions, cliOptions } = params ;
76+ return makePackageInfos (
77+ Object . fromEntries (
78+ Object . entries ( packages ) . map ( ( [ folder , info ] ) => [
79+ path . basename ( folder ) ,
80+ {
81+ packageJsonPath : path . join ( cwd , folder , 'package.json' ) ,
82+ ...info ,
83+ } ,
84+ ] )
85+ ) ,
86+ repoOptions ,
87+ { ...cliOptions , path : cwd }
88+ ) ;
89+ }
0 commit comments