Skip to content

Commit 1b4f532

Browse files
committed
simplify deps installation
1 parent 5a07556 commit 1b4f532

File tree

1 file changed

+6
-71
lines changed

1 file changed

+6
-71
lines changed

packages/cli/src/tools/resolveTransitiveDeps.ts

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {getLoader, logger} from '@react-native-community/cli-tools';
44
import chalk from 'chalk';
55
import {prompt} from 'prompts';
66
import execa from 'execa';
7-
import semver from 'semver';
87

98
interface DependencyInfo {
109
path: string;
@@ -92,55 +91,6 @@ function excludeInstalledPeerDependencies(
9291
return missingPeerDependencies;
9392
}
9493

95-
function getMatchingPackageVersion(
96-
packageName: string,
97-
range: string,
98-
yarn = true,
99-
) {
100-
if (yarn) {
101-
const {stdout} = execa.sync('yarn', [
102-
'info',
103-
packageName,
104-
'versions',
105-
'--json',
106-
]);
107-
const versions = JSON.parse(stdout).data as string[];
108-
const satisfying = versions.filter((version) =>
109-
semver.satisfies(version, range),
110-
);
111-
const maxSatisfying = semver.maxSatisfying(satisfying, range);
112-
113-
return maxSatisfying;
114-
} else {
115-
const {stdout} = execa.sync('npm', [
116-
'view',
117-
`${packageName}@${range}`,
118-
'version',
119-
'--json',
120-
]);
121-
const versions = JSON.parse(stdout);
122-
const maxSatisfying = semver.maxSatisfying(versions, range);
123-
return maxSatisfying;
124-
}
125-
}
126-
127-
function flattenSemver(input) {
128-
const result = {};
129-
input.forEach((item) => {
130-
Object.entries(item).forEach(([key, value]) => {
131-
if (result[key]) {
132-
if (value !== '*' && result[key] !== '*') {
133-
result[key] = `${result[key]} && ${value}`;
134-
}
135-
} else {
136-
result[key] = value;
137-
}
138-
});
139-
});
140-
141-
return result;
142-
}
143-
14494
export default async function installTransitiveDeps() {
14595
const root = process.cwd();
14696
const packageJsonPath = path.join(root, 'package.json');
@@ -178,34 +128,19 @@ export default async function installTransitiveDeps() {
178128
const loader = getLoader({text: 'Installing peer dependencies...'});
179129

180130
if (install) {
181-
let deps = {} as Record<string, string>;
182-
const semverRanges = flattenSemver(Object.values(depsToInstall));
183-
131+
let deps = new Set();
184132
dependenciesWithMissingDeps.map((dep) => {
185133
const missingDeps = depsToInstall[dep];
186-
Object.entries(missingDeps).map(([name]) => {
187-
const version = getMatchingPackageVersion(
188-
name,
189-
semverRanges[name],
190-
isYarn,
191-
);
192-
if (version) {
193-
deps[name] = version;
194-
}
195-
});
134+
deps.add(Object.keys(missingDeps));
196135
});
136+
const arr = Array.from(deps) as string[];
137+
const flat = [].concat(...arr);
197138
loader.start();
198139

199140
if (isYarn) {
200-
execa.sync('yarn', [
201-
'add',
202-
...Object.entries(deps).map(([k, v]) => `${k}@^${v}`),
203-
]);
141+
execa.sync('yarn', ['add', ...flat.map((dep) => dep)]);
204142
} else {
205-
execa.sync('npm', [
206-
'install',
207-
...Object.entries(deps).map(([k, v]) => `${k}@^${v}`),
208-
]);
143+
execa.sync('npm', ['install', ...flat.map((dep) => dep)]);
209144
}
210145
loader.succeed();
211146
}

0 commit comments

Comments
 (0)