-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgetPackagesToInstall.ts
86 lines (73 loc) · 2.77 KB
/
getPackagesToInstall.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* eslint-disable no-console */
import uniq from 'lodash/uniq';
import { ALL_PACKAGES } from './ALL_PACKAGES';
import { BASIC_PACKAGES } from './BASIC_PACKAGES';
import { ESSENTIAL_PACKAGES } from './ESSENTIAL_PACKAGES';
import type { InstallCommandOptions } from './types';
/**
* Returns a list of packages to install based on the provided options.
* If specific packages are requested, those will be used,
* Otherwise, we filter packages based on the provided flags.
*/
export function getPackagesToInstall(
explicitPackages: Array<string>,
options: InstallCommandOptions,
): Array<string> {
const { verbose, essentials, basic, ui, charts, chat } = options;
const availablePackages = ALL_PACKAGES;
verbose &&
console.log(`Found ${availablePackages.length} packages in static file`);
// If specific packages are requested, use those
if (explicitPackages.length > 0) {
return availablePackages
.filter(fullName => {
const pkgName = fullName.split('/')[1]; // Get name without scope
return explicitPackages.includes(pkgName);
})
.map(pkg => `${pkg}@latest`);
}
const scopeFilters: Record<string, boolean> = {
'@leafygreen-ui/': ui ?? false,
'@lg-charts/': charts ?? false,
'@lg-chat/': chat ?? false,
};
const hasScopeFilters = Object.values(scopeFilters).some(Boolean);
const hasFilterFlags = hasScopeFilters || essentials || basic;
const packagesToInstall: Array<string> = [];
// If any filter flags are set, we need to filter the packages based on the provided flags
// otherwise, we install all packages
if (hasFilterFlags) {
verbose && console.log('Filtering packages based on flags');
// Filter by specific package sets based on flags
if (essentials) {
packagesToInstall.push(...ESSENTIAL_PACKAGES);
verbose && console.log('Installing essential packages');
}
if (basic) {
packagesToInstall.push(...BASIC_PACKAGES);
verbose && console.log('Installing basic packages');
}
if (hasScopeFilters) {
// Filter packages based on any scope flags
const scopedPackages = availablePackages.filter(pkg => {
for (const [scope, include] of Object.entries(scopeFilters)) {
if (include && pkg.startsWith(scope)) {
return true;
}
}
return false;
});
packagesToInstall.push(...scopedPackages);
verbose &&
console.log(
`Filtered to ${scopedPackages.length} packages based on scope flags`,
);
}
} else {
// If no specific flags are set, install all packages
packagesToInstall.push(...availablePackages);
verbose && console.log('Installing all packages');
}
// Remove any duplicates and add @latest tag
return uniq(packagesToInstall).map(pkg => `${pkg}@latest`);
}