Skip to content

Commit

Permalink
doctor: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Sep 3, 2019
1 parent f75f98d commit 370a264
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ esproposal.export_star_as=enable
esproposal.optional_chaining=enable
esproposal.nullish_coalescing=enable
module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'
module.name_mapper='types' -> '<PROJECT_ROOT>/types/index.js'
module.name_mapper='^types' -> '<PROJECT_ROOT>/types/index.js'

suppress_type=$FlowIssue
suppress_type=$FlowFixMe
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/commands/doctor/checkInstallation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
import semver from 'semver';
import commandExists from 'command-exists';

Expand All @@ -6,7 +7,7 @@ const PACKAGE_MANAGERS = {
NPM: 'NPM',
};

const isSoftwareInstalled = async command => {
const isSoftwareInstalled = async (command: string) => {
try {
await commandExists(command);

Expand All @@ -16,7 +17,12 @@ const isSoftwareInstalled = async command => {
}
};

const doesSoftwareNeedToBeFixed = ({version, versionRange}) =>
version === 'Not Found' || !semver.satisfies(version, versionRange);
const doesSoftwareNeedToBeFixed = ({
version,
versionRange,
}: {
version: string,
versionRange: string,
}) => version === 'Not Found' || !semver.satisfies(version, versionRange);

export {PACKAGE_MANAGERS, isSoftwareInstalled, doesSoftwareNeedToBeFixed};
44 changes: 41 additions & 3 deletions packages/cli/src/commands/doctor/doctor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// @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';

const printCategory = ({label, key}) => {
if (key > 0) {
Expand All @@ -14,7 +18,15 @@ const printCategory = ({label, key}) => {
logger.log(chalk.dim(label));
};

const printIssue = ({label, needsToBeFixed, isRequired}) => {
const printIssue = ({
label,
needsToBeFixed,
isRequired,
}: {
label: string,
needsToBeFixed: boolean,
isRequired: boolean,
}) => {
const symbol = needsToBeFixed
? isRequired
? chalk.red('✖')
Expand All @@ -29,7 +41,16 @@ const printOverallStats = ({errors, warnings}) => {
logger.log(`${chalk.bold('Warnings:')} ${warnings}`);
};

export default (async function runDoctor(argv, ctx, options) {
type FlagsT = {
fix: boolean | void,
contributor: boolean | void,
};

export default (async function runDoctor(
argv: Array<string>,
ctx: ConfigT,
options: FlagsT,
) {
const Loader = getLoader();
const loader = new Loader();

Expand All @@ -48,7 +69,24 @@ export default (async function runDoctor(argv, ctx, options) {
),
);

const iterateOverHealthchecks = async ({label, healthchecks}) => ({
const iterateOverHealthchecks = async ({
label,
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>,
}>,
}) => ({
label,
healthchecks: (await Promise.all(
healthchecks.map(async healthcheck => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @flow
import chalk from 'chalk';
import Ora from 'ora';
import {logManualInstallation} from './common';

// List of answers on how to set `ANDROID_HOME` for each platform
Expand All @@ -15,7 +17,7 @@ export default {
getDiagnostics: () => ({
needsToBeFixed: !process.env.ANDROID_HOME,
}),
runAutomaticFix: async ({loader}) => {
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
loader.info();

logManualInstallation({
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/src/commands/doctor/healthchecks/androidNDK.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// @flow
import chalk from 'chalk';
import Ora from 'ora';
import {logManualInstallation} from './common';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import type {EnvironmentInfo} from '../types';

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

Expand Down
13 changes: 11 additions & 2 deletions packages/cli/src/commands/doctor/healthchecks/androidSDK.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// @flow
import chalk from 'chalk';
import Ora from 'ora';
import {logManualInstallation} from './common';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import type {EnvironmentInfo} from '../types';

export default {
label: 'Android SDK',
getDiagnosticsAsync: async ({SDKs}) => ({
getDiagnosticsAsync: async ({SDKs}: EnvironmentInfo) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: SDKs['Android SDK']['Build Tools'][0],
versionRange: versionRanges.ANDROID_NDK,
}),
}),
runAutomaticFix: async ({loader, environmentInfo}) => {
runAutomaticFix: async ({
loader,
environmentInfo,
}: {
loader: typeof Ora,
environmentInfo: EnvironmentInfo,
}) => {
const version = environmentInfo.SDKs['Android SDK'][0];
const isNDKInstalled = version !== 'Not Found';

Expand Down
13 changes: 12 additions & 1 deletion packages/cli/src/commands/doctor/healthchecks/common.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
// @flow
import logger from '@react-native-community/cli-tools/build/logger';
import chalk from 'chalk';

// Space is necessary to keep correct ordering on screen
const logMessage = message => logger.log(` ${message}`);

const logManualInstallation = ({healthcheck, url, command, message}) => {
const logManualInstallation = ({
healthcheck = '',
url,
command,
message,
}: {
healthcheck?: string,
url?: string,
command?: string,
message?: string,
}) => {
if (message) {
return logMessage(message);
}
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/commands/doctor/healthchecks/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
import nodeJS from './nodeJS';
import {yarn, npm} from './packageManagers';
import watchman from './watchman';
Expand All @@ -13,14 +14,18 @@ export const HEALTHCHECK_TYPES = {
WARNING: 'WARNING',
};

export const getHealthchecks = ({contributor}) => ({
type Options = {
fix: boolean | void,
contributor: boolean | void,
};

export const getHealthchecks = ({contributor}: Options) => ({
common: {
label: 'Common',
healthchecks: [nodeJS, yarn, npm, watchman],
},
android: {
label: 'Android',
// TODO: Android NDK should be shown only with a special flag
healthchecks: [
androidHomeEnvVariable,
androidSDK,
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/commands/doctor/healthchecks/iosDeploy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @flow
import execa from 'execa';
import Ora from 'ora';
import {isSoftwareInstalled, PACKAGE_MANAGERS} from '../checkInstallation';
import {packageManager} from './packageManagers';
import {logManualInstallation} from './common';
Expand All @@ -21,7 +23,7 @@ export default {
getDiagnosticsAsync: async () => ({
needsToBeFixed: !(await isSoftwareInstalled('ios-deploy')),
}),
runAutomaticFix: async ({loader}) => {
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
const installationCommand = getInstallationCommand();

// This means that we couldn't "guess" the package manager
Expand All @@ -36,7 +38,7 @@ export default {
}

try {
const installationCommandArgs = installationCommand.split(' ');
const installationCommandArgs = (installationCommand || '').split(' ');

await execa(
installationCommandArgs[0],
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/commands/doctor/healthchecks/nodeJS.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// @flow
import Ora from 'ora';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import {logManualInstallation} from './common';
import type {EnvironmentInfo} from '../types';

export default {
label: 'Node.js',
getDiagnostics: ({Binaries}) => ({
getDiagnostics: ({Binaries}: EnvironmentInfo) => ({
version: Binaries.Node.version,
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.Node.version,
versionRange: versionRanges.NODE_JS,
}),
}),
runAutomaticFix: async ({loader}) => {
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
loader.fail();

logManualInstallation({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @flow
import fs from 'fs';
import versionRanges from '../versionRanges';
import {
PACKAGE_MANAGERS,
doesSoftwareNeedToBeFixed,
} from '../checkInstallation';
import type {EnvironmentInfo} from '../types';

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

const yarn = {
label: 'yarn',
getDiagnostics: ({Binaries}) => ({
getDiagnostics: ({Binaries}: EnvironmentInfo) => ({
version: Binaries.Node.version,
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.Yarn.version,
Expand All @@ -37,7 +39,7 @@ const yarn = {

const npm = {
label: 'npm',
getDiagnostics: ({Binaries}) => ({
getDiagnostics: ({Binaries}: EnvironmentInfo) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.npm.version,
versionRange: versionRanges.NPM,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/doctor/healthchecks/watchman.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @flow
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import type {EnvironmentInfo} from '../types';

export default {
label: 'Watchman',
getDiagnostics: ({Binaries}) => ({
getDiagnostics: ({Binaries}: EnvironmentInfo) => ({
needsToBeFixed: doesSoftwareNeedToBeFixed({
version: Binaries.Watchman.version,
versionRange: versionRanges.WATCHMAN,
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/commands/doctor/healthchecks/xcode.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// @flow
import Ora from 'ora';
import versionRanges from '../versionRanges';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import {logManualInstallation} from './common';
import type {EnvironmentInfo} from '../types';

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

logManualInstallation({
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/commands/doctor/printFixOptions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @flow
import chalk from 'chalk';
import {logger} from '@react-native-community/cli-tools';

const KEYS = {
FIX_ALL_ISSUES: 'f',
FIX_ERRORS: 'e',
Expand Down Expand Up @@ -28,9 +30,10 @@ const printOptions = () => {
};

export {KEYS};
export default ({onKeyPress}) => {
export default ({onKeyPress}: {onKeyPress: any}) => {
printOptions();

// $FlowFixMe
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/commands/doctor/runAutomaticFix.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import chalk from 'chalk';
import ora from 'ora';
import logger from '../../tools/logger';
import {HEALTHCHECK_TYPES} from './healthchecks';
import type {EnvironmentInfo} from './types';

const AUTOMATIC_FIX_LEVELS = {
ALL_ISSUES: 'ALL_ISSUES',
Expand All @@ -16,9 +18,17 @@ export default async ({
stats,
loader,
environmentInfo,
}:{
healthchecks: any,
automaticFixLevel: $Values<typeof AUTOMATIC_FIX_LEVELS>,
stats:{errors: any, warnings: any},
loader: typeof ora,
environmentInfo: EnvironmentInfo,
}) => {
// Remove the fix options from screen
// $FlowFixMe
process.stdout.moveCursor(0, -6);
// $FlowFixMe
process.stdout.clearScreenDown();

const totalIssuesBasedOnFixLevel = {
Expand Down
Loading

0 comments on commit 370a264

Please sign in to comment.