Skip to content

Commit

Permalink
use config param to autolink peer deps
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Sep 22, 2023
1 parent 4a5b4b2 commit 26f262e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 14 deletions.
21 changes: 16 additions & 5 deletions packages/cli-config/src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
version,
resolveNodeModuleDir,
UnknownProjectError,
resolveTransitiveDeps,
} from '@react-native-community/cli-tools';
import findDependencies from './findDependencies';
import resolveReactNativePath from './resolveReactNativePath';
Expand Down Expand Up @@ -75,7 +76,9 @@ function getReactNativeVersion(reactNativePath: string) {
/**
* Loads CLI configuration
*/
function loadConfig(projectRoot: string = findProjectRoot()): Config {
async function loadConfig(
projectRoot: string = findProjectRoot(),
): Promise<Config> {
let lazyProject: ProjectConfig;
const userConfig = readConfigFromDisk(projectRoot);

Expand Down Expand Up @@ -113,11 +116,19 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
},
};

const dependencyMap = findDependencies(projectRoot);
let dependencies = Array.from(dependencyMap.keys());

if (userConfig.unstable_autolinkPeerDependencies) {
const installedDependencies = await resolveTransitiveDeps(
projectRoot,
dependencyMap,
);
dependencies = [...dependencies, ...installedDependencies];
}

const finalConfig = Array.from(
new Set([
...Object.keys(userConfig.dependencies),
...findDependencies(projectRoot),
]),
new Set([...Object.keys(userConfig.dependencies), ...dependencies]),
).reduce((acc: Config, dependencyName) => {
const localDependencyRoot =
userConfig.dependencies[dependencyName] &&
Expand Down
1 change: 1 addition & 0 deletions packages/cli-config/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export const projectConfig = t
linkConfig: t.func(),
}),
).default({}),
unstable_autolinkPeerDependencies: t.bool(),
})
.unknown(true)
.default();
2 changes: 1 addition & 1 deletion packages/cli-doctor/src/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const doctorCommand = (async (_, options, config) => {
Promise.all(categories.map(iterateOverHealthChecks));

const healthchecksPerCategory = await iterateOverCategories(
Object.values(getHealthchecks(options)).filter(
Object.values(await getHealthchecks(options)).filter(
(category) => category !== undefined,
) as HealthCheckCategory[],
);
Expand Down
10 changes: 6 additions & 4 deletions packages/cli-doctor/src/tools/healthchecks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import loadConfig from '@react-native-community/cli-config';
import {logger} from '@react-native-community/cli-tools';
import nodeJS from './nodeJS';
import {yarn, npm} from './packageManagers';
import adb from './adb';
Expand All @@ -12,11 +14,9 @@ import xcode from './xcode';
import cocoaPods from './cocoaPods';
import iosDeploy from './iosDeploy';
import {Healthchecks, HealthCheckCategory} from '../../types';
import loadConfig from '@react-native-community/cli-config';
import xcodeEnv from './xcodeEnv';
import packager from './packager';
import deepmerge from 'deepmerge';
import {logger} from '@react-native-community/cli-tools';

export const HEALTHCHECK_TYPES = {
ERROR: 'ERROR',
Expand All @@ -28,14 +28,16 @@ type Options = {
contributor: boolean | void;
};

export const getHealthchecks = ({contributor}: Options): Healthchecks => {
export const getHealthchecks = async ({
contributor,
}: Options): Promise<Healthchecks> => {
let additionalChecks: HealthCheckCategory[] = [];
let projectSpecificHealthchecks = {};
let config;

// Doctor can run in a detached mode, where there isn't a config so this can fail
try {
config = loadConfig();
config = await loadConfig();
additionalChecks = config.healthChecks;

if (config.reactNativePath) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-tools/src/bun.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {logger} from '@react-native-community/cli-tools';
import {execSync} from 'child_process';
import findUp from 'find-up';
import semver from 'semver';
import logger from './logger';

export function getBunVersionIfAvailable() {
let bunVersion;
Expand Down
3 changes: 2 additions & 1 deletion packages/cli-tools/src/resolveTransitiveDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import chalk from 'chalk';
import {prompt} from 'prompts';
import execa from 'execa';
import semver from 'semver';
import {getLoader, logger} from '@react-native-community/cli-tools';
import {DependencyMap} from '@react-native-community/cli-types';
import {isProjectUsingYarn} from './yarn';
import {getLoader} from './loader';
import logger from './logger';

export async function fetchAvailableVersions(
packageName: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-tools/src/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import {execSync} from 'child_process';
import semver from 'semver';
import {logger} from '@react-native-community/cli-tools';
import findUp from 'find-up';
import logger from './logger';

/**
* Use Yarn if available, it's much faster than the npm client.
Expand Down
1 change: 1 addition & 0 deletions packages/cli-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export type UserConfig = Omit<Config, 'root'> & {
ios?: IOSProjectParams;
[key: string]: any;
};
unstable_autolinkPeerDependencies?: boolean;
};

export type UserDependencyConfig = {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async function setupAndRun() {

let config: Config | undefined;
try {
config = loadConfig();
config = await loadConfig();

logger.enable();

Expand Down

0 comments on commit 26f262e

Please sign in to comment.