Skip to content

Commit

Permalink
add --dependency-check flag
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Aug 29, 2023
1 parent 8df7cca commit 708dde9
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/cli-config/src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import {
resolveNodeModuleDir,
UnknownProjectError,
} from '@react-native-community/cli-tools';
import findDependencies from './findDependencies';
import resolveReactNativePath from './resolveReactNativePath';
import {
readConfigFromDisk,
readDependencyConfigFromDisk,
} from './readConfigFromDisk';
import assign from './assign';
import merge from './merge';
import findDependencies from './findDependencies';

function getDependencyConfig(
root: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface BuildFlags {
tasks?: Array<string>;
extraParams?: Array<string>;
interactive?: boolean;
dependencyCheck?: boolean;
}

async function buildAndroid(
Expand Down Expand Up @@ -123,6 +124,11 @@ export const options = [
description:
'Explicitly select build type and flavour to use before running a build',
},
{
name: '--dependency-check',
description:
'Check if there are any transitive dependencies containing native code that are not declared as a direct dependency in your package.json.',
},
];

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
logAlreadyRunningBundler,
startServerInNewWindow,
handlePortUnavailable,
checkTransitiveDependencies,
} from '@react-native-community/cli-tools';
import {getAndroidProject} from '../../config/getAndroidProject';
import listAndroidDevices from './listAndroidDevices';
Expand Down Expand Up @@ -56,6 +57,10 @@ async function runAndroid(_argv: Array<string>, config: Config, args: Flags) {

let {packager, port} = args;

if (args.dependencyCheck) {
await checkTransitiveDependencies();
}

const packagerStatus = await isPackagerRunning(port);

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type BuildFlags = {
interactive?: boolean;
destination?: string;
extraParams?: string[];
dependencyCheck?: boolean;
};

export function buildProject(
Expand Down
15 changes: 14 additions & 1 deletion packages/cli-platform-ios/src/commands/buildIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import path from 'path';
import chalk from 'chalk';
import {Config} from '@react-native-community/cli-types';
import {logger, CLIError} from '@react-native-community/cli-tools';
import {
logger,
CLIError,
checkTransitiveDependencies,
} from '@react-native-community/cli-tools';
import {Device} from '../../types';
import {BuildFlags, buildProject} from './buildProject';
import {getDestinationSimulator} from '../../tools/getDestinationSimulator';
Expand Down Expand Up @@ -41,6 +45,10 @@ async function buildIOS(_: Array<string>, ctx: Config, args: FlagsT) {
);
}

if (args.dependencyCheck) {
await checkTransitiveDependencies();
}

process.chdir(sourceDir);

const projectInfo = getProjectInfo();
Expand Down Expand Up @@ -236,6 +244,11 @@ export const iosBuildOptions = [
name: '--target <string>',
description: 'Explicitly set Xcode target to use.',
},
{
name: '--dependency-check',
description:
'Check if there are any transitive dependencies containing native code that are not declared as a direct dependency in your package.json.',
},
];

export default {
Expand Down
5 changes: 5 additions & 0 deletions packages/cli-platform-ios/src/commands/runIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isPackagerRunning,
logAlreadyRunningBundler,
handlePortUnavailable,
checkTransitiveDependencies,
} from '@react-native-community/cli-tools';
import {BuildFlags, buildProject} from '../buildIOS/buildProject';
import {iosBuildOptions} from '../buildIOS';
Expand Down Expand Up @@ -88,6 +89,10 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
);
}

if (args.dependencyCheck) {
await checkTransitiveDependencies();
}

const {xcodeProject, sourceDir} = ctx.project.ios;

if (!xcodeProject) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export * as link from './doclink';
export {default as startServerInNewWindow} from './startServerInNewWindow';
export {default as handlePortUnavailable} from './handlePortUnavailable';
export * from './port';
export * as transitiveDeps from './resolveTransitiveDeps';
export {default as checkTransitiveDependencies} from './resolveTransitiveDeps';

export * from './errors';
12 changes: 6 additions & 6 deletions packages/cli-tools/src/resolveTransitiveDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface DependencyData {
duplicates?: DependencyData[];
}

export function isUsingYarn(root: string) {
function isUsingYarn(root: string) {
return fs.existsSync(path.join(root, 'yarn.lock'));
}

Expand Down Expand Up @@ -62,7 +62,7 @@ function findDependencyPath(
return dependencyPath;
}

export function collectDependencies(root: string): Map<string, DependencyData> {
function collectDependencies(root: string): Map<string, DependencyData> {
const dependencies = new Map<string, DependencyData>();

const checkDependency = (dependencyPath: string) => {
Expand Down Expand Up @@ -242,7 +242,7 @@ async function yarnSilentInstallPeerDeps(root: string) {
}
}

export default async function findPeerDepsForAutolinking(root: string) {
async function findPeerDepsForAutolinking(root: string) {
const deps = collectDependencies(root);
const nonEmptyPeers = filterNativeDependencies(root, deps);
const nonInstalledPeers = filterInstalledPeers(root, nonEmptyPeers);
Expand Down Expand Up @@ -340,7 +340,7 @@ function installMissingPackages(
}
}

export async function resolveTransitiveDeps() {
async function resolveTransitiveDeps() {
const root = process.cwd();
const isYarn = isUsingYarn(root);

Expand All @@ -367,7 +367,7 @@ export async function resolveTransitiveDeps() {
return false;
}

export async function resolvePodsInstallation() {
async function resolvePodsInstallation() {
const {install} = await prompt({
type: 'confirm',
name: 'install',
Expand All @@ -383,7 +383,7 @@ export async function resolvePodsInstallation() {
}
}

export async function checkTransitiveDeps() {
export default async function checkTransitiveDependencies() {
const packageJsonPath = path.join(process.cwd(), 'package.json');
const preInstallHash = generateFileHash(packageJsonPath);
const areTransitiveDepsInstalled = await resolveTransitiveDeps();
Expand Down
8 changes: 1 addition & 7 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import loadConfig from '@react-native-community/cli-config';
import {
CLIError,
logger,
transitiveDeps,
} from '@react-native-community/cli-tools';
import {CLIError, logger} from '@react-native-community/cli-tools';
import type {
Command,
Config,
Expand Down Expand Up @@ -175,8 +171,6 @@ async function setupAndRun() {
}
}

await transitiveDeps.checkTransitiveDeps();

let config: Config | undefined;

try {
Expand Down

0 comments on commit 708dde9

Please sign in to comment.