Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate doctor command to TypeScript (3) #719

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/src/commands/doctor/doctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import chalk from 'chalk';
import envinfo from 'envinfo';
import {logger} from '@react-native-community/cli-tools';
// $FlowFixMe - converted to TS
import {getHealthchecks, HEALTHCHECK_TYPES} from './healthchecks';
// $FlowFixMe - converted to TS
import {getLoader} from '../../tools/loader';
Expand Down
21 changes: 13 additions & 8 deletions packages/cli/src/commands/doctor/healthchecks/androidNDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ import {EnvironmentInfo, HealthCheckInterface} from '../types';

export default {
label: 'Android NDK',
getDiagnostics: async ({SDKs}: EnvironmentInfo) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: SDKs['Android SDK']['Android NDK'],
versionRange: versionRanges.ANDROID_NDK,
}),
}),
getDiagnostics: async ({SDKs}: EnvironmentInfo) => {
const androidSdk = SDKs['Android SDK'];
return {
needsToBeFixed: doesSoftwareNeedToBeFixed({
version:
androidSdk === 'Not Found' ? 'Not Found' : androidSdk['Android NDK'],
versionRange: versionRanges.ANDROID_NDK,
}),
};
},
runAutomaticFix: async ({
loader,
environmentInfo,
}: {
loader: Ora;
environmentInfo: EnvironmentInfo;
}) => {
const version = environmentInfo.SDKs['Android SDK']['Android NDK'];
const isNDKInstalled = version !== 'Not Found';
const androidSdk = environmentInfo.SDKs['Android SDK'];
const isNDKInstalled =
androidSdk !== 'Not Found' && androidSdk['Android NDK'] !== 'Not Found';

loader.fail();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
// @flow
import chalk from 'chalk';
import Ora from 'ora';
// $FlowFixMe - converted to TS
import {logManualInstallation} from './common';
// $FlowFixMe - converted to TS
import versionRanges from '../versionRanges';
// $FlowFixMe - converted to TS
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import execa from 'execa';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';
import {HealthCheckInterface} from '../types';

const installMessage = `Read more about how to update Android SDK at ${chalk.dim(
'https://developer.android.com/studio',
)}`;

export default ({
export default {
label: 'Android SDK',
getDiagnostics: async ({SDKs}: EnvironmentInfo) => {
getDiagnostics: async ({SDKs}) => {
let sdks = SDKs['Android SDK'];

// This is a workaround for envinfo's Android SDK check not working on
Expand All @@ -43,6 +38,9 @@ export default ({
if (matches.length > 0) {
sdks = {
'Build Tools': matches,
'API Levels': 'Not Found',
'Android NDK': 'Not Found',
'System Images': 'Not Found',
};
}
} catch {}
Expand All @@ -58,14 +56,8 @@ export default ({
})),
};
},
runAutomaticFix: async ({
loader,
environmentInfo,
}: {
loader: typeof Ora,
environmentInfo: EnvironmentInfo,
}) => {
const version = environmentInfo.SDKs['Android SDK'][0];
runAutomaticFix: async ({loader, environmentInfo}) => {
Esemesek marked this conversation as resolved.
Show resolved Hide resolved
const version = environmentInfo.SDKs['Android SDK'];
const isSDKInstalled = version !== 'Not Found';

loader.fail();
Expand All @@ -81,4 +73,4 @@ export default ({
url: 'https://facebook.github.io/react-native/docs/getting-started',
});
},
}: HealthCheckInterface);
} as HealthCheckInterface;
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// @flow
import nodeJS from './nodeJS';
import {yarn, npm} from './packageManagers';
import watchman from './watchman';
// $FlowFixMe - converted to TS
import androidHomeEnvVariable from './androidHomeEnvVariable';
import androidSDK from './androidSDK';
// $FlowFixMe - converted to TS
import androidNDK from './androidNDK';
import xcode from './xcode';
// $FlowFixMe - converted to TS
import cocoaPods from './cocoaPods';
import iosDeploy from './iosDeploy';

Expand All @@ -18,8 +14,8 @@ export const HEALTHCHECK_TYPES = {
};

type Options = {
fix: boolean | void,
contributor: boolean | void,
fix: boolean | void;
contributor: boolean | void;
};

export const getHealthchecks = ({contributor}: Options) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// @flow
import execa from 'execa';
import Ora from 'ora';
// $FlowFixMe - converted to TS
import {checkSoftwareInstalled, PACKAGE_MANAGERS} from '../checkInstallation';
import {packageManager} from './packageManagers';
// $FlowFixMe - converted to TS
import {logManualInstallation} from './common';
import type {HealthCheckInterface} from '../types';
import {HealthCheckInterface} from '../types';

const getInstallationCommand = () => {
if (packageManager === PACKAGE_MANAGERS.YARN) {
Expand All @@ -20,13 +16,13 @@ const getInstallationCommand = () => {
return undefined;
};

export default ({
export default {
label: 'ios-deploy',
isRequired: false,
getDiagnostics: async () => ({
needsToBeFixed: await checkSoftwareInstalled('ios-deploy'),
}),
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
runAutomaticFix: async ({loader}) => {
Esemesek marked this conversation as resolved.
Show resolved Hide resolved
const installationCommand = getInstallationCommand();

// This means that we couldn't "guess" the package manager
Expand Down Expand Up @@ -59,4 +55,4 @@ export default ({
});
}
},
}: HealthCheckInterface);
} as HealthCheckInterface;
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
// @flow
import Ora from 'ora';
// $FlowFixMe - converted to TS
import versionRanges from '../versionRanges';
// $FlowFixMe - converted to TS
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
// $FlowFixMe - converted to TS
import {logManualInstallation} from './common';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';
import {HealthCheckInterface} from '../types';

export default ({
export default {
label: 'Node.js',
getDiagnostics: async ({Binaries}: EnvironmentInfo) => ({
getDiagnostics: async ({Binaries}) => ({
Esemesek marked this conversation as resolved.
Show resolved Hide resolved
version: Binaries.Node.version,
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.Node.version,
versionRange: versionRanges.NODE_JS,
}),
}),
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
runAutomaticFix: async ({loader}) => {
Esemesek marked this conversation as resolved.
Show resolved Hide resolved
loader.fail();

logManualInstallation({
healthcheck: 'Node.js',
url: 'https://nodejs.org/en/download/',
});
},
}: HealthCheckInterface);
} as HealthCheckInterface;
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
// @flow
import fs from 'fs';
import Ora from 'ora';
// $FlowFixMe - converted to TS
import versionRanges from '../versionRanges';
import {
PACKAGE_MANAGERS,
doesSoftwareNeedToBeFixed,
// $FlowFixMe - converted to TS
} from '../checkInstallation';
// $FlowFixMe - converted to TS
import {install} from '../../../tools/install';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';
import {HealthCheckInterface} from '../types';

const packageManager = (() => {
if (fs.existsSync('yarn.lock')) {
Expand All @@ -26,7 +21,7 @@ const packageManager = (() => {

const yarn: HealthCheckInterface = {
label: 'yarn',
getDiagnostics: async ({Binaries}: EnvironmentInfo) => ({
getDiagnostics: async ({Binaries}) => ({
version: Binaries.Node.version,
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.Yarn.version,
Expand All @@ -37,13 +32,18 @@ const yarn: HealthCheckInterface = {
// or if we can't identify that the user uses yarn or npm
visible:
packageManager === PACKAGE_MANAGERS.YARN || packageManager === undefined,
runAutomaticFix: async ({loader}: typeof Ora) =>
await install('yarn', 'https://yarnpkg.com/docs/install', loader),
runAutomaticFix: async ({loader}) =>
await install({
pkg: 'yarn',
label: 'yarn',
source: 'https://yarnpkg.com/docs/install',
loader,
}),
};

const npm: HealthCheckInterface = {
label: 'npm',
getDiagnostics: async ({Binaries}: EnvironmentInfo) => ({
getDiagnostics: async ({Binaries}) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.npm.version,
versionRange: versionRanges.NPM,
Expand All @@ -53,8 +53,13 @@ const npm: HealthCheckInterface = {
// or if we can't identify that the user uses yarn or npm
visible:
packageManager === PACKAGE_MANAGERS.NPM || packageManager === undefined,
runAutomaticFix: async ({loader}: typeof Ora) =>
await install('node', 'https://nodejs.org/', loader),
runAutomaticFix: async ({loader}) =>
await install({
pkg: 'node',
label: 'node',
source: 'https://nodejs.org/',
loader,
}),
};

export {packageManager, yarn, npm};
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
// @flow
import Ora from 'ora';
// $FlowFixMe - converted to TS
import versionRanges from '../versionRanges';
// $FlowFixMe - converted to TS
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
// $FlowFixMe - converted to TS
import {install} from '../../../tools/install';
import type {EnvironmentInfo} from '../types';
import {HealthCheckInterface} from '../types';

const label = 'Watchman';

export default {
label,
getDiagnostics: ({Binaries}: EnvironmentInfo) => ({
getDiagnostics: async ({Binaries}) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.Watchman.version,
versionRange: versionRanges.WATCHMAN,
}),
}),
runAutomaticFix: async ({loader}: typeof Ora) =>
runAutomaticFix: async ({loader}) =>
await install({
pkg: 'watchman',
label,
source: 'https://facebook.github.io/watchman/docs/install.html',
loader,
}),
};
} as HealthCheckInterface;
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
// @flow
import Ora from 'ora';
// $FlowFixMe - converted to TS
import versionRanges from '../versionRanges';
// $FlowFixMe - converted to TS
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
// $FlowFixMe - converted to TS
import {logManualInstallation} from './common';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';
import {HealthCheckInterface} from '../types';

export default ({
export default {
label: 'Xcode',
getDiagnostics: async ({IDEs}: EnvironmentInfo) => ({
getDiagnostics: async ({IDEs}) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: IDEs.Xcode.version.split('/')[0],
versionRange: versionRanges.XCODE,
}),
}),
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
runAutomaticFix: async ({loader}) => {
loader.info();

logManualInstallation({
healthcheck: 'Xcode',
url: 'https://developer.apple.com/xcode/',
});
},
}: HealthCheckInterface);
} as HealthCheckInterface;
1 change: 1 addition & 0 deletions packages/cli/src/commands/doctor/runAutomaticFix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import chalk from 'chalk';
import ora from 'ora';
import {logger} from '@react-native-community/cli-tools';
// $FlowFixMe - converted to TS
import {HEALTHCHECK_TYPES} from './healthchecks';
import type {EnvironmentInfo} from './types';

Expand Down
14 changes: 8 additions & 6 deletions packages/cli/src/commands/doctor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ export type EnvironmentInfo = {
'iOS SDK': {
Platforms: string[];
};
'Android SDK': {
'API Levels': string[];
'Build Tools': string[];
'System Images': string[];
'Android NDK': string;
};
'Android SDK':
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we should contribute these typings to the envinfo repo. There are no typings yet in there. There is an ongoing PR, but the typings are quite incomplete.

| {
'API Levels': string[] | 'Not Found';
'Build Tools': string[] | 'Not Found';
'System Images': string[] | 'Not Found';
'Android NDK': string | 'Not Found';
}
| 'Not Found';
};
IDEs: {
'Android Studio': string;
Expand Down