Skip to content

Commit

Permalink
chore: refactor envinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
molant committed Feb 28, 2020
1 parent 109ebf2 commit b1b2623
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
13 changes: 3 additions & 10 deletions packages/cli/src/commands/info/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@
*/

// @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';

const info = async function getInfo(_argv: Array<string>, 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 {
Expand Down
51 changes: 35 additions & 16 deletions packages/cli/src/tools/envinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,39 @@
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<EnvironmentInfo>;
async function getEnvironmentInfo(json: true): Promise<EnvironmentInfo>;
async function getEnvironmentInfo(json: false): Promise<string>;
async function getEnvironmentInfo(
json = true,
): Promise<string | EnvironmentInfo> {
const options = json
? {json: true, showNotFound: true}
: {json: false, 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;

0 comments on commit b1b2623

Please sign in to comment.