Skip to content

Commit

Permalink
--amend
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Sep 9, 2019
1 parent 45c0074 commit 809bdfb
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 58 deletions.
26 changes: 8 additions & 18 deletions packages/cli/src/commands/doctor/doctor.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// @flow
import chalk from 'chalk';
import envinfo from 'envinfo';
import Ora from 'ora';
import {logger} from '@react-native-community/cli-tools';
import {getHealthchecks, HEALTHCHECK_TYPES} from './healthchecks';
import {getLoader} from '../../tools/loader';
import printFixOptions, {KEYS} from './printFixOptions';
import runAutomaticFix, {AUTOMATIC_FIX_LEVELS} from './runAutomaticFix';
import type {ConfigT} from 'types';
import type {EnvironmentInfo} from './types';
import type {HealthCheckInterface} from './types';

const printCategory = ({label, key}) => {
if (key > 0) {
Expand Down Expand Up @@ -74,18 +73,7 @@ export default (async function runDoctor(
healthchecks,
}: {
label: string,
healthchecks: Array<{
label: string,
visible: boolean,
isRequired: boolean,
getDiagnostics: (
envInfor: EnvironmentInfo,
) => {version: string, needsToBeFixed: boolean},
getDiagnosticsAsync: (
envInfor: EnvironmentInfo,
) => Promise<{version: string, needsToBeFixed: boolean}>,
runAutomaticFix: (args: {loader: typeof Ora}) => Promise<void>,
}>,
healthchecks: Array<HealthCheckInterface>,
}) => ({
label,
healthchecks: (await Promise.all(
Expand All @@ -94,17 +82,17 @@ export default (async function runDoctor(
return;
}

const {needsToBeFixed} = healthcheck.getDiagnostics
? healthcheck.getDiagnostics(environmentInfo)
: await healthcheck.getDiagnosticsAsync(environmentInfo);
const {needsToBeFixed} = await healthcheck.getDiagnostics(
environmentInfo,
);

// Assume that it's required unless specified otherwise
const isRequired = healthcheck.isRequired !== false;
const isWarning = needsToBeFixed && !isRequired;

return {
label: healthcheck.label,
needsToBeFixed,
needsToBeFixed: Boolean(needsToBeFixed),
runAutomaticFix: healthcheck.runAutomaticFix,
isRequired,
type: needsToBeFixed
Expand All @@ -125,6 +113,7 @@ export default (async function runDoctor(
);

const iterateOverCategories = categories =>
// $FlowFixMe - bad Object.values typings
Promise.all(categories.map(iterateOverHealthChecks));

const healthchecksPerCategory = await iterateOverCategories(
Expand Down Expand Up @@ -167,6 +156,7 @@ export default (async function runDoctor(
}

const onKeyPress = async key => {
// $FlowFixMe
process.stdin.setRawMode(false);
process.stdin.removeAllListeners('data');

Expand Down
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 {logManualInstallation} from './common';
import type {HealthCheckInterface} from '../types';

// List of answers on how to set `ANDROID_HOME` for each platform
const URLS = {
Expand All @@ -12,9 +13,9 @@ const URLS = {

const label = 'ANDROID_HOME';

export default {
export default ({
label,
getDiagnostics: () => ({
getDiagnostics: async () => ({
needsToBeFixed: !process.env.ANDROID_HOME,
}),
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
Expand All @@ -26,4 +27,4 @@ export default {
)}.`,
});
},
};
}: HealthCheckInterface);
8 changes: 4 additions & 4 deletions packages/cli/src/commands/doctor/healthchecks/androidNDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import Ora from 'ora';
import {logManualInstallation} from './common';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import type {EnvironmentInfo} from '../types';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';

export default {
export default ({
label: 'Android NDK',
getDiagnosticsAsync: async ({SDKs}: EnvironmentInfo) => ({
getDiagnostics: async ({SDKs}: EnvironmentInfo) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: SDKs['Android SDK']['Android NDK'],
versionRange: versionRanges.ANDROID_NDK,
Expand Down Expand Up @@ -39,4 +39,4 @@ export default {
url: 'https://developer.android.com/ndk/downloads',
});
},
};
}: HealthCheckInterface);
21 changes: 13 additions & 8 deletions packages/cli/src/commands/doctor/healthchecks/androidSDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import {logManualInstallation} from './common';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import execa from 'execa';
import type {EnvironmentInfo, 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',
getDiagnosticsAsync: async ({SDKs}: EnvironmentInfo) => {
getDiagnostics: async ({SDKs}: EnvironmentInfo) => {
let sdks = SDKs['Android SDK'];

// This is a workaround for envinfo's Android SDK check not working on
// Windows. This can be removed when envinfo fixes it.
// See the PR: https://github.com/tabrindle/envinfo/pull/119
if (sdks === 'Not Found' && process.platform !== 'darwin') {
try {
// $FlowFixMe bad execa types
const {stdout} = await execa(
process.env.ANDROID_HOME
? `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`
Expand All @@ -31,7 +33,9 @@ export default {
const regex = /build-tools;([\d|.]+)[\S\s]/g;
let match = null;
while ((match = regex.exec(stdout)) !== null) {
matches.push(match[1]);
if (match) {
matches.push(match[1]);
}
}
if (matches.length > 0) {
sdks = {
Expand All @@ -44,10 +48,11 @@ export default {
return {
needsToBeFixed:
(sdks === 'Not Found' && installMessage) ||
doesSoftwareNeedToBeFixed({
version: sdks['Build Tools'][0],
versionRange: versionRanges.ANDROID_NDK,
}),
(sdks !== 'Not Found' &&
doesSoftwareNeedToBeFixed({
version: sdks['Build Tools'][0],
versionRange: versionRanges.ANDROID_NDK,
})),
};
},
runAutomaticFix: async ({
Expand All @@ -73,4 +78,4 @@ export default {
url: 'https://facebook.github.io/react-native/docs/getting-started',
});
},
};
}: HealthCheckInterface);
8 changes: 5 additions & 3 deletions packages/cli/src/commands/doctor/healthchecks/cocoaPods.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// @flow
import {isSoftwareInstalled} from '../checkInstallation';
import {installCocoaPods} from '../../../tools/installPods';
import {type HealthCheckInterface} from '../types';

export default {
export default ({
label: 'CocoaPods',
getDiagnosticsAsync: async () => ({
getDiagnostics: async () => ({
needsToBeFixed: !(await isSoftwareInstalled('pod')),
}),
runAutomaticFix: async ({loader}) => await installCocoaPods(loader),
};
}: HealthCheckInterface);
7 changes: 4 additions & 3 deletions packages/cli/src/commands/doctor/healthchecks/iosDeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Ora from 'ora';
import {isSoftwareInstalled, PACKAGE_MANAGERS} from '../checkInstallation';
import {packageManager} from './packageManagers';
import {logManualInstallation} from './common';
import type {HealthCheckInterface} from '../types';

const getInstallationCommand = () => {
if (packageManager === PACKAGE_MANAGERS.YARN) {
Expand All @@ -17,10 +18,10 @@ const getInstallationCommand = () => {
return undefined;
};

export default {
export default ({
label: 'ios-deploy',
isRequired: false,
getDiagnosticsAsync: async () => ({
getDiagnostics: async () => ({
needsToBeFixed: !(await isSoftwareInstalled('ios-deploy')),
}),
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
Expand Down Expand Up @@ -53,4 +54,4 @@ export default {
});
}
},
};
}: HealthCheckInterface);
8 changes: 4 additions & 4 deletions packages/cli/src/commands/doctor/healthchecks/nodeJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Ora from 'ora';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import {logManualInstallation} from './common';
import type {EnvironmentInfo} from '../types';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';

export default {
export default ({
label: 'Node.js',
getDiagnostics: ({Binaries}: EnvironmentInfo) => ({
getDiagnostics: async ({Binaries}: EnvironmentInfo) => ({
version: Binaries.Node.version,
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.Node.version,
Expand All @@ -22,4 +22,4 @@ export default {
url: 'https://nodejs.org/en/download/',
});
},
};
}: HealthCheckInterface);
15 changes: 8 additions & 7 deletions packages/cli/src/commands/doctor/healthchecks/packageManagers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// @flow
import fs from 'fs';
import Ora from 'ora';
import versionRanges from '../versionRanges';
import {
PACKAGE_MANAGERS,
doesSoftwareNeedToBeFixed,
} from '../checkInstallation';
import {install} from '../../../tools/install';
import type {EnvironmentInfo} from '../types';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';

const packageManager = (() => {
if (fs.existsSync('yarn.lock')) {
Expand All @@ -20,9 +21,9 @@ const packageManager = (() => {
return undefined;
})();

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

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

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/doctor/healthchecks/watchman.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
import Ora from 'ora';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import {install} from '../../../tools/install';
Expand All @@ -12,7 +13,7 @@ export default {
versionRange: versionRanges.WATCHMAN,
}),
}),
runAutomaticFix: async ({loader}) =>
runAutomaticFix: async ({loader}: typeof Ora) =>
await install(
'watchman',
'https://facebook.github.io/watchman/docs/install.html',
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/commands/doctor/healthchecks/xcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import Ora from 'ora';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import {logManualInstallation} from './common';
import type {EnvironmentInfo} from '../types';
import type {EnvironmentInfo, HealthCheckInterface} from '../types';

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

logManualInstallation({
healthcheck: 'Xcode',
url: 'https://developer.apple.com/xcode/',
});
},
};
}: HealthCheckInterface);
4 changes: 2 additions & 2 deletions packages/cli/src/commands/doctor/runAutomaticFix.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export default async ({
stats,
loader,
environmentInfo,
}:{
}: {
healthchecks: any,
automaticFixLevel: $Values<typeof AUTOMATIC_FIX_LEVELS>,
stats:{errors: any, warnings: any},
stats: {errors: any, warnings: any},
loader: typeof ora,
environmentInfo: EnvironmentInfo,
}) => {
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/src/commands/doctor/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @flow
import Ora from 'ora';

export type EnvironmentInfo = {
System: {
OS: string,
Expand Down Expand Up @@ -62,3 +64,16 @@ export type EnvironmentInfo = {
},
},
};

export type HealthCheckInterface = {
label: string,
visible?: boolean | void,
isRequired?: boolean,
getDiagnostics: (
environmentInfo: EnvironmentInfo,
) => Promise<{version?: string, needsToBeFixed: boolean | string}>,
runAutomaticFix: (args: {
loader: typeof Ora,
environmentInfo: EnvironmentInfo,
}) => Promise<void> | void,
};

0 comments on commit 809bdfb

Please sign in to comment.