Skip to content

Commit f942c1b

Browse files
authored
doctor: add types (#664)
* doctor: add types * --amend * --amend * fix install cocoapods not awaiting results
1 parent 7de69ee commit f942c1b

19 files changed

+242
-55
lines changed

.flowconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ esproposal.export_star_as=enable
2727
esproposal.optional_chaining=enable
2828
esproposal.nullish_coalescing=enable
2929
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'
30-
module.name_mapper='types' -> '<PROJECT_ROOT>/types/index.js'
30+
module.name_mapper='^types' -> '<PROJECT_ROOT>/types/index.js'
3131

3232
suppress_type=$FlowIssue
3333
suppress_type=$FlowFixMe

packages/cli/src/commands/doctor/checkInstallation.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @flow
12
import semver from 'semver';
23
import commandExists from 'command-exists';
34

@@ -6,7 +7,7 @@ const PACKAGE_MANAGERS = {
67
NPM: 'NPM',
78
};
89

9-
const isSoftwareInstalled = async command => {
10+
const isSoftwareInstalled = async (command: string) => {
1011
try {
1112
await commandExists(command);
1213

@@ -16,7 +17,13 @@ const isSoftwareInstalled = async command => {
1617
}
1718
};
1819

19-
const doesSoftwareNeedToBeFixed = ({version, versionRange}) =>
20+
const doesSoftwareNeedToBeFixed = ({
21+
version,
22+
versionRange,
23+
}: {
24+
version: string,
25+
versionRange: string,
26+
}) =>
2027
version === 'Not Found' ||
2128
!semver.satisfies(semver.coerce(version), versionRange);
2229

packages/cli/src/commands/doctor/doctor.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
// @flow
12
import chalk from 'chalk';
23
import envinfo from 'envinfo';
34
import {logger} from '@react-native-community/cli-tools';
45
import {getHealthchecks, HEALTHCHECK_TYPES} from './healthchecks';
56
import {getLoader} from '../../tools/loader';
67
import printFixOptions, {KEYS} from './printFixOptions';
78
import runAutomaticFix, {AUTOMATIC_FIX_LEVELS} from './runAutomaticFix';
9+
import type {ConfigT} from 'types';
10+
import type {HealthCheckInterface} from './types';
811

912
const printCategory = ({label, key}) => {
1013
if (key > 0) {
@@ -14,7 +17,15 @@ const printCategory = ({label, key}) => {
1417
logger.log(chalk.dim(label));
1518
};
1619

17-
const printIssue = ({label, needsToBeFixed, isRequired}) => {
20+
const printIssue = ({
21+
label,
22+
needsToBeFixed,
23+
isRequired,
24+
}: {
25+
label: string,
26+
needsToBeFixed: boolean,
27+
isRequired: boolean,
28+
}) => {
1829
const symbol = needsToBeFixed
1930
? isRequired
2031
? chalk.red('✖')
@@ -29,7 +40,16 @@ const printOverallStats = ({errors, warnings}) => {
2940
logger.log(`${chalk.bold('Warnings:')} ${warnings}`);
3041
};
3142

32-
export default (async function runDoctor(argv, ctx, options) {
43+
type FlagsT = {
44+
fix: boolean | void,
45+
contributor: boolean | void,
46+
};
47+
48+
export default (async function runDoctor(
49+
argv: Array<string>,
50+
ctx: ConfigT,
51+
options: FlagsT,
52+
) {
3353
const Loader = getLoader();
3454
const loader = new Loader();
3555

@@ -48,25 +68,31 @@ export default (async function runDoctor(argv, ctx, options) {
4868
),
4969
);
5070

51-
const iterateOverHealthChecks = async ({label, healthchecks}) => ({
71+
const iterateOverHealthChecks = async ({
72+
label,
73+
healthchecks,
74+
}: {
75+
label: string,
76+
healthchecks: Array<HealthCheckInterface>,
77+
}) => ({
5278
label,
5379
healthchecks: (await Promise.all(
5480
healthchecks.map(async healthcheck => {
5581
if (healthcheck.visible === false) {
5682
return;
5783
}
5884

59-
const {needsToBeFixed} = healthcheck.getDiagnostics
60-
? healthcheck.getDiagnostics(environmentInfo)
61-
: await healthcheck.getDiagnosticsAsync(environmentInfo);
85+
const {needsToBeFixed} = await healthcheck.getDiagnostics(
86+
environmentInfo,
87+
);
6288

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

6793
return {
6894
label: healthcheck.label,
69-
needsToBeFixed,
95+
needsToBeFixed: Boolean(needsToBeFixed),
7096
runAutomaticFix: healthcheck.runAutomaticFix,
7197
isRequired,
7298
type: needsToBeFixed
@@ -87,6 +113,7 @@ export default (async function runDoctor(argv, ctx, options) {
87113
);
88114

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

92119
const healthchecksPerCategory = await iterateOverCategories(
@@ -129,6 +156,7 @@ export default (async function runDoctor(argv, ctx, options) {
129156
}
130157

131158
const onKeyPress = async key => {
159+
// $FlowFixMe
132160
process.stdin.setRawMode(false);
133161
process.stdin.removeAllListeners('data');
134162

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
// @flow
12
import chalk from 'chalk';
3+
import Ora from 'ora';
24
import {logManualInstallation} from './common';
5+
import type {HealthCheckInterface} from '../types';
36

47
// List of answers on how to set `ANDROID_HOME` for each platform
58
const URLS = {
@@ -10,12 +13,12 @@ const URLS = {
1013

1114
const label = 'ANDROID_HOME';
1215

13-
export default {
16+
export default ({
1417
label,
15-
getDiagnostics: () => ({
18+
getDiagnostics: async () => ({
1619
needsToBeFixed: !process.env.ANDROID_HOME,
1720
}),
18-
runAutomaticFix: async ({loader}) => {
21+
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
1922
loader.info();
2023

2124
logManualInstallation({
@@ -24,4 +27,4 @@ export default {
2427
)}.`,
2528
});
2629
},
27-
};
30+
}: HealthCheckInterface);

packages/cli/src/commands/doctor/healthchecks/androidNDK.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
// @flow
12
import chalk from 'chalk';
3+
import Ora from 'ora';
24
import {logManualInstallation} from './common';
35
import versionRanges from '../versionRanges';
46
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
7+
import type {EnvironmentInfo, HealthCheckInterface} from '../types';
58

6-
export default {
9+
export default ({
710
label: 'Android NDK',
8-
getDiagnosticsAsync: async ({SDKs}) => ({
11+
getDiagnostics: async ({SDKs}: EnvironmentInfo) => ({
912
needsToBeFixed: doesSoftwareNeedToBeFixed({
1013
version: SDKs['Android SDK']['Android NDK'],
1114
versionRange: versionRanges.ANDROID_NDK,
1215
}),
1316
}),
14-
runAutomaticFix: async ({loader, environmentInfo}) => {
17+
runAutomaticFix: async ({
18+
loader,
19+
environmentInfo,
20+
}: {
21+
loader: typeof Ora,
22+
environmentInfo: EnvironmentInfo,
23+
}) => {
1524
const version = environmentInfo.SDKs['Android SDK']['Android NDK'];
1625
const isNDKInstalled = version !== 'Not Found';
1726

@@ -30,4 +39,4 @@ export default {
3039
url: 'https://developer.android.com/ndk/downloads',
3140
});
3241
},
33-
};
42+
}: HealthCheckInterface);

packages/cli/src/commands/doctor/healthchecks/androidSDK.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1+
// @flow
12
import chalk from 'chalk';
3+
import Ora from 'ora';
24
import {logManualInstallation} from './common';
35
import versionRanges from '../versionRanges';
46
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
57
import execa from 'execa';
8+
import type {EnvironmentInfo, HealthCheckInterface} from '../types';
69

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

11-
export default {
14+
export default ({
1215
label: 'Android SDK',
13-
getDiagnosticsAsync: async ({SDKs}) => {
16+
getDiagnostics: async ({SDKs}: EnvironmentInfo) => {
1417
let sdks = SDKs['Android SDK'];
1518

1619
// This is a workaround for envinfo's Android SDK check not working on
1720
// Windows. This can be removed when envinfo fixes it.
1821
// See the PR: https://github.com/tabrindle/envinfo/pull/119
1922
if (sdks === 'Not Found' && process.platform !== 'darwin') {
2023
try {
24+
// $FlowFixMe bad execa types
2125
const {stdout} = await execa(
2226
process.env.ANDROID_HOME
2327
? `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`
@@ -29,7 +33,9 @@ export default {
2933
const regex = /build-tools;([\d|.]+)[\S\s]/g;
3034
let match = null;
3135
while ((match = regex.exec(stdout)) !== null) {
32-
matches.push(match[1]);
36+
if (match) {
37+
matches.push(match[1]);
38+
}
3339
}
3440
if (matches.length > 0) {
3541
sdks = {
@@ -42,13 +48,20 @@ export default {
4248
return {
4349
needsToBeFixed:
4450
(sdks === 'Not Found' && installMessage) ||
45-
doesSoftwareNeedToBeFixed({
46-
version: sdks['Build Tools'][0],
47-
versionRange: versionRanges.ANDROID_NDK,
48-
}),
51+
(sdks !== 'Not Found' &&
52+
doesSoftwareNeedToBeFixed({
53+
version: sdks['Build Tools'][0],
54+
versionRange: versionRanges.ANDROID_NDK,
55+
})),
4956
};
5057
},
51-
runAutomaticFix: async ({loader, environmentInfo}) => {
58+
runAutomaticFix: async ({
59+
loader,
60+
environmentInfo,
61+
}: {
62+
loader: typeof Ora,
63+
environmentInfo: EnvironmentInfo,
64+
}) => {
5265
const version = environmentInfo.SDKs['Android SDK'][0];
5366
const isNDKInstalled = version !== 'Not Found';
5467

@@ -65,4 +78,4 @@ export default {
6578
url: 'https://facebook.github.io/react-native/docs/getting-started',
6679
});
6780
},
68-
};
81+
}: HealthCheckInterface);
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// @flow
12
import {isSoftwareInstalled} from '../checkInstallation';
23
import {installCocoaPods} from '../../../tools/installPods';
4+
import {type HealthCheckInterface} from '../types';
35

4-
export default {
6+
export default ({
57
label: 'CocoaPods',
6-
getDiagnosticsAsync: async () => ({
8+
getDiagnostics: async () => ({
79
needsToBeFixed: !(await isSoftwareInstalled('pod')),
810
}),
911
runAutomaticFix: async ({loader}) => await installCocoaPods(loader),
10-
};
12+
}: HealthCheckInterface);

packages/cli/src/commands/doctor/healthchecks/common.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
// @flow
12
import logger from '@react-native-community/cli-tools/build/logger';
23
import chalk from 'chalk';
34

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

7-
const logManualInstallation = ({healthcheck, url, command, message}) => {
8+
const logManualInstallation = ({
9+
healthcheck = '',
10+
url,
11+
command,
12+
message,
13+
}: {
14+
healthcheck?: string,
15+
url?: string,
16+
command?: string,
17+
message?: string,
18+
}) => {
819
if (message) {
920
return logMessage(message);
1021
}

packages/cli/src/commands/doctor/healthchecks/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @flow
12
import nodeJS from './nodeJS';
23
import {yarn, npm} from './packageManagers';
34
import watchman from './watchman';
@@ -13,7 +14,12 @@ export const HEALTHCHECK_TYPES = {
1314
WARNING: 'WARNING',
1415
};
1516

16-
export const getHealthchecks = ({contributor}) => ({
17+
type Options = {
18+
fix: boolean | void,
19+
contributor: boolean | void,
20+
};
21+
22+
export const getHealthchecks = ({contributor}: Options) => ({
1723
common: {
1824
label: 'Common',
1925
healthchecks: [
@@ -25,7 +31,6 @@ export const getHealthchecks = ({contributor}) => ({
2531
},
2632
android: {
2733
label: 'Android',
28-
// TODO: Android NDK should be shown only with a special flag
2934
healthchecks: [
3035
androidHomeEnvVariable,
3136
androidSDK,

packages/cli/src/commands/doctor/healthchecks/iosDeploy.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
// @flow
12
import execa from 'execa';
3+
import Ora from 'ora';
24
import {isSoftwareInstalled, PACKAGE_MANAGERS} from '../checkInstallation';
35
import {packageManager} from './packageManagers';
46
import {logManualInstallation} from './common';
7+
import type {HealthCheckInterface} from '../types';
58

69
const getInstallationCommand = () => {
710
if (packageManager === PACKAGE_MANAGERS.YARN) {
@@ -15,13 +18,13 @@ const getInstallationCommand = () => {
1518
return undefined;
1619
};
1720

18-
export default {
21+
export default ({
1922
label: 'ios-deploy',
2023
isRequired: false,
21-
getDiagnosticsAsync: async () => ({
24+
getDiagnostics: async () => ({
2225
needsToBeFixed: !(await isSoftwareInstalled('ios-deploy')),
2326
}),
24-
runAutomaticFix: async ({loader}) => {
27+
runAutomaticFix: async ({loader}: {loader: typeof Ora}) => {
2528
const installationCommand = getInstallationCommand();
2629

2730
// This means that we couldn't "guess" the package manager
@@ -33,6 +36,7 @@ export default {
3336
healthcheck: 'ios-deploy',
3437
url: 'https://github.com/ios-control/ios-deploy#readme',
3538
});
39+
return;
3640
}
3741

3842
try {
@@ -51,4 +55,4 @@ export default {
5155
});
5256
}
5357
},
54-
};
58+
}: HealthCheckInterface);

0 commit comments

Comments
 (0)