From 2bd4513ddead8b39b53a9eded238634cb2d639d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3n=20Molleda?= Date: Thu, 27 Feb 2020 21:21:21 -0800 Subject: [PATCH] chore: refactor envinfo --- packages/cli/src/commands/info/info.ts | 13 ++----- packages/cli/src/tools/envinfo.ts | 49 +++++++++++++++++--------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/packages/cli/src/commands/info/info.ts b/packages/cli/src/commands/info/info.ts index eb5fca11c..fedc47863 100644 --- a/packages/cli/src/commands/info/info.ts +++ b/packages/cli/src/commands/info/info.ts @@ -6,7 +6,7 @@ */ // @ts-ignore untyped -import envinfo from 'envinfo'; +import getEnvironmentInfo from '../../tools/envinfo'; import {logger} from '@react-native-community/cli-tools'; import {Config} from '@react-native-community/cli-types'; import releaseChecker from '../../tools/releaseChecker'; @@ -14,15 +14,8 @@ import releaseChecker from '../../tools/releaseChecker'; const info = async function getInfo(_argv: Array, ctx: Config) { try { logger.info('Fetching system and libraries information...'); - const output = await envinfo.run({ - System: ['OS', 'CPU', 'Memory', 'Shell'], - Binaries: ['Node', 'Yarn', 'npm', 'Watchman'], - IDEs: ['Xcode', 'Android Studio'], - SDKs: ['iOS SDK', 'Android SDK'], - npmPackages: ['react', 'react-native', '@react-native-community/cli'], - npmGlobalPackages: '*react-native*', - }); - logger.log(output.trim()); + const output = await getEnvironmentInfo(false); + logger.log(output); } catch (err) { logger.error(`Unable to print environment info.\n${err}`); } finally { diff --git a/packages/cli/src/tools/envinfo.ts b/packages/cli/src/tools/envinfo.ts index f6f696a90..c27230425 100644 --- a/packages/cli/src/tools/envinfo.ts +++ b/packages/cli/src/tools/envinfo.ts @@ -2,20 +2,37 @@ import envinfo from 'envinfo'; import {EnvironmentInfo} from '../commands/doctor/types'; -export default async function getEnvironmentInfo() { - return JSON.parse( - await envinfo.run( - { - Binaries: ['Node', 'Yarn', 'npm', 'Watchman'], - IDEs: ['Xcode', 'Android Studio'], - SDKs: ['iOS SDK', 'Android SDK'], - npmPackages: ['react', 'react-native', '@react-native-community/cli'], - npmGlobalPackages: ['*react-native*'], - }, - { - json: true, - showNotFound: true, - }, - ), - ) as EnvironmentInfo; +/** + * Returns information about the running system. + * If `json === true`, or no options are passed, + * the return type will be an `EnvironmentInfo`. + * If set to `false`, it will be a `string`. + */ +async function getEnvironmentInfo(): Promise; +async function getEnvironmentInfo(json: true): Promise; +async function getEnvironmentInfo(json: false): Promise; +async function getEnvironmentInfo( + json = true, +): Promise { + const options = {json, showNotFound: true}; + + const info = (await envinfo.run( + { + System: ['OS', 'CPU', 'Memory', 'Shell'], + Binaries: ['Node', 'Yarn', 'npm', 'Watchman'], + IDEs: ['Xcode', 'Android Studio'], + SDKs: ['iOS SDK', 'Android SDK'], + npmPackages: ['react', 'react-native', '@react-native-community/cli'], + npmGlobalPackages: ['*react-native*'], + }, + options, + )) as string; + + if (options.json) { + return JSON.parse(info); + } + + return info.trim(); } + +export default getEnvironmentInfo;