Skip to content

Commit

Permalink
feat(doctor): detect wrong dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunrajput committed Jun 23, 2023
1 parent 3569633 commit ce0ae4c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
95 changes: 95 additions & 0 deletions packages/cli-doctor/src/tools/healthchecks/dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import {HealthCheckInterface} from '../../types';
import {logManualInstallation} from './common';
import {findProjectRoot} from '@react-native-community/cli-tools';

const RNPackages = [
'@react-native/babel-plugin-codegen',
'@react-native/assets-registry',
'@react-native/eslint-config',
'@react-native/eslint-plugin-specs',
'@react-native/hermes-inspector-msggen',
'@react-native/metro-config',
'@react-native/normalize-colors',
'@react-native/js-polyfills',
'@react-native/bots',
'@react-native/codegen-typescript-test',
'@react-native/codegen',
'@react-native/gradle-plugin',
'@react-native/typescript-config',
'@react-native/virtualized-lists',
];

const getPackageJson = (root: string): Record<string, any> => {
try {
const packageJson = JSON.parse(
fs.readFileSync(path.join(root, 'package.json'), 'utf8'),
);
return packageJson;
} catch (e) {
return {};
}
};

const findDependencies = (root: string): Record<string, string> => {
const {devDependencies = {}, dependencies = {}} = getPackageJson(root);
return {
...devDependencies,
...dependencies,
};
};

export default {
label: 'Dependencies',
isRequired: false,
description: 'NPM dependencies needed for the project to work correctly',
getDiagnostics: async (_, config) => {
try {
const root = config?.root || findProjectRoot();
const dependencies = findDependencies(root);
const reactNativeVersion = dependencies['react-native'];
const reactNativeMinorVersion = semver.coerce(reactNativeVersion)?.minor;
const issues: string[] = [];

RNPackages.forEach((pkg) => {
if (dependencies[pkg]) {
issues.push(
` - ${pkg} is part of React Native and should not be a dependency in your package.json`,
);
const packageVersion = dependencies[pkg];
const packageMinorVersion = semver.coerce(packageVersion)?.minor;

if (reactNativeMinorVersion !== packageMinorVersion) {
issues.push(
` - ${pkg} "${packageVersion}" is not compatible with react-native: "${reactNativeVersion}"`,
);
}
}
});
if (issues.length) {
issues.unshift('There are some issues with your project dependencies');
return {
needsToBeFixed: true,
description: issues.join('\n'),
};
} else {
return {
needsToBeFixed: false,
};
}
} catch (e) {
return {
needsToBeFixed: true,
};
}
},
runAutomaticFix: async ({loader}) => {
loader.fail();
return logManualInstallation({
message:
'Please check your package.json and make sure the dependencies are correct',
});
},
} as HealthCheckInterface;
2 changes: 2 additions & 0 deletions packages/cli-doctor/src/tools/healthchecks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Healthchecks, HealthCheckCategory} from '../../types';
import loadConfig from '@react-native-community/cli-config';
import xcodeEnv from './xcodeEnv';
import packager from './packager';
import dependencies from './dependencies';

export const HEALTHCHECK_TYPES = {
ERROR: 'ERROR',
Expand All @@ -39,6 +40,7 @@ export const getHealthchecks = ({contributor}: Options): Healthchecks => {
common: {
label: 'Common',
healthchecks: [
dependencies,
nodeJS,
yarn,
npm,
Expand Down

0 comments on commit ce0ae4c

Please sign in to comment.