Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions packages/cli/src/cliEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import childProcess from 'child_process';
import commander from 'commander';
import path from 'path';

import type {CommandT, ContextT} from './tools/types.flow';
import type {CommandT, ConfigT} from './tools/config/types.flow';

import {getCommands} from './commands';
import commands from './commands';
import init from './commands/init/initCompat';
import assertRequiredOptions from './tools/assertRequiredOptions';
import {logger} from '@react-native-community/cli-tools';
Expand Down Expand Up @@ -95,7 +95,7 @@ function printUnknownCommand(cmdName) {
}
}

const addCommand = (command: CommandT, ctx: ContextT) => {
const addCommand = (command: CommandT, ctx: ConfigT) => {
const options = command.options || [];

const cmd = commander
Expand Down Expand Up @@ -162,9 +162,7 @@ async function setupAndRun() {

setProjectDir(ctx.root);

const commands = getCommands(ctx);

commands.forEach(command => addCommand(command, ctx));
[...commands, ...ctx.commands].forEach(command => addCommand(command, ctx));

commander.parse(process.argv);

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/bundle/buildBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import outputBundle from 'metro/src/shared/output/bundle';
import path from 'path';
import chalk from 'chalk';
import type {CommandLineArgs} from './bundleCommandLineArgs';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import saveAssets from './saveAssets';
import loadMetroConfig from '../../tools/loadMetroConfig';
import {logger} from '@react-native-community/cli-tools';

async function buildBundle(
args: CommandLineArgs,
ctx: ContextT,
ctx: ConfigT,
output: typeof outputBundle = outputBundle,
) {
const config = await loadMetroConfig(ctx, {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/config/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @flow
*/
import {type ContextT} from '../../tools/types.flow';
import {type ConfigT} from '../../tools/config/types.flow';
export default {
name: 'config',
description: 'Print CLI configuration',
func: async (argv: string[], ctx: ContextT) => {
func: async (argv: string[], ctx: ConfigT) => {
console.log(JSON.stringify(ctx, null, 2));
},
};
70 changes: 3 additions & 67 deletions packages/cli/src/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
/**
* @flow
*/
import {type CommandT} from '../tools/config/types.flow';

import path from 'path';
import type {
CommandT,
ProjectCommandT,
LocalCommandT,
} from '../tools/types.flow';

import {type ContextT} from '../tools/types.flow';
import server from './server/server';
import runIOS from './runIOS/runIOS';
import runAndroid from './runAndroid/runAndroid';
Expand All @@ -27,11 +20,7 @@ import info from './info/info';
import config from './config/config';
import init from './init';

/**
* List of built-in commands
*/

const loadLocalCommands: Array<LocalCommandT> = [
export default ([
server,
runIOS,
runAndroid,
Expand All @@ -48,57 +37,4 @@ const loadLocalCommands: Array<LocalCommandT> = [
info,
config,
init,
];

/**
* Returns an array of commands that are defined in the project.
*
* This checks all CLI plugins for presence of 3rd party packages that define commands
* and loads them
*/
const loadProjectCommands = ({
root,
commands,
}: ContextT): Array<ProjectCommandT> => {
return commands.reduce((acc: Array<ProjectCommandT>, cmdPath: string) => {
/**
* `pathToCommand` is a path to a file where commands are defined, relative to `node_modules`
* folder.
*
* Following code gets the name of the package name out of the path, taking scope
* into consideration.
*/
const name =
cmdPath[0] === '@'
? cmdPath
.split(path.sep)
.slice(0, 2)
.join(path.sep)
: cmdPath.split(path.sep)[0];

const pkg = require(path.join(root, 'node_modules', name, 'package.json'));

const requiredCommands:
| ProjectCommandT
| Array<ProjectCommandT> = require(path.join(
root,
'node_modules',
cmdPath,
));

if (Array.isArray(requiredCommands)) {
return acc.concat(
requiredCommands.map(requiredCommand => ({...requiredCommand, pkg})),
);
}

return acc.concat({...requiredCommands, pkg});
}, []);
};

/**
* Loads all the commands inside a given `root` folder
*/
export function getCommands(ctx: ContextT): Array<CommandT> {
return [...loadLocalCommands, ...loadProjectCommands(ctx)];
}
]: CommandT[]);
4 changes: 2 additions & 2 deletions packages/cli/src/commands/info/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import envinfo from 'envinfo';
import {logger} from '@react-native-community/cli-tools';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';

const info = async function getInfo(
argv: Array<string>,
ctx: ContextT,
ctx: ConfigT,
options: {},
) {
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import fs from 'fs-extra';
import minimist from 'minimist';
import semver from 'semver';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import {validateProjectName} from './validate';
import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError';
import printRunInstructions from './printRunInstructions';
Expand Down Expand Up @@ -153,7 +153,7 @@ function createProject(projectName: string, options: Options, version: string) {

export default (async function initialize(
[projectName]: Array<string>,
_context: ContextT,
_context: ConfigT,
options: Options,
) {
validateProjectName(projectName);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/install/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* @flow
*/

import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import {logger} from '@react-native-community/cli-tools';
import * as PackageManager from '../../tools/packageManager';
import link from '../link/link';
import loadConfig from '../../tools/config';

async function install(args: Array<string>, ctx: ContextT) {
async function install(args: Array<string>, ctx: ConfigT) {
const name = args[0];

logger.info(`Installing "${name}"...`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/install/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
* @flow
*/

import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import {logger} from '@react-native-community/cli-tools';
import * as PackageManager from '../../tools/packageManager';
import link from '../link/unlink';

async function uninstall(args: Array<string>, ctx: ContextT) {
async function uninstall(args: Array<string>, ctx: ConfigT) {
const name = args[0];

logger.info(`Unlinking "${name}"...`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import {pick} from 'lodash';
import dedent from 'dedent';

import {type ContextT} from '../../tools/types.flow';
import {type ConfigT} from '../../tools/config/types.flow';

import {CLIError} from '../../tools/errors';

Expand All @@ -34,7 +34,7 @@ type FlagsType = {
* @param args If optional argument [packageName] is provided,
* only that package is processed.
*/
function link([rawPackageName]: Array<string>, ctx: ContextT, opts: FlagsType) {
function link([rawPackageName]: Array<string>, ctx: ConfigT, opts: FlagsType) {
let platforms = ctx.platforms;
let project = ctx.project;

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/link/unlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import {flatMap, values, difference} from 'lodash';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import dedent from 'dedent';
import {logger} from '@react-native-community/cli-tools';
import promiseWaterfall from './promiseWaterfall';
Expand Down Expand Up @@ -77,7 +77,7 @@ const unlinkDependency = (
* If optional argument [packageName] is provided, it's the only one
* that's checked
*/
function unlink(args: Array<string>, ctx: ContextT) {
function unlink(args: Array<string>, ctx: ConfigT) {
const packageName = args[0];

const {[packageName]: dependency, ...otherDependencies} = ctx.dependencies;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/runAndroid/runAndroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import fs from 'fs';
import isString from 'lodash/isString';

import isPackagerRunning from '../../tools/isPackagerRunning';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';

import adb from './adb';
import runOnAllDevices from './runOnAllDevices';
Expand All @@ -30,7 +30,7 @@ function checkAndroid(root) {
/**
* Starts the app on a connected Android emulator or device.
*/
function runAndroid(argv: Array<string>, ctx: ContextT, args: Object) {
function runAndroid(argv: Array<string>, ctx: ConfigT, args: Object) {
if (!checkAndroid(args.root)) {
logger.error(
'Android project not found. Are you sure this is a React Native project?',
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/runIOS/runIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import child_process from 'child_process';
import fs from 'fs';
import path from 'path';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import findXcodeProject from './findXcodeProject';
import parseIOSDevicesList from './parseIOSDevicesList';
import findMatchingSimulator from './findMatchingSimulator';
Expand All @@ -30,7 +30,7 @@ type FlagsT = {
port: number,
};

function runIOS(_: Array<string>, ctx: ContextT, args: FlagsT) {
function runIOS(_: Array<string>, ctx: ConfigT, args: FlagsT) {
if (!fs.existsSync(args.projectPath)) {
throw new Error(
'iOS project folder not found. Are you sure this is a React Native project?',
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {Terminal} from 'metro-core';

import morgan from 'morgan';
import path from 'path';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import messageSocket from './messageSocket';
import webSocketProxy from './webSocketProxy';
import MiddlewareManager from './middleware/MiddlewareManager';
Expand All @@ -40,7 +40,7 @@ export type Args = {|
config?: string,
|};

async function runServer(argv: Array<string>, ctx: ContextT, args: Args) {
async function runServer(argv: Array<string>, ctx: ConfigT, args: Args) {
const terminal = new Terminal(process.stdout);
const ReporterImpl = getReporterImpl(args.customLogReporterPath || null);
const reporter = new ReporterImpl(terminal);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/upgrade/legacyUpgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import {logger} from '@react-native-community/cli-tools';
import copyProjectTemplateAndReplace from '../../tools/generator/copyProjectTemplateAndReplace';

/**
* Migrate application to a new version of React Native.
* See http://facebook.github.io/react-native/docs/upgrading.html
*/
function validateAndUpgrade(argv: Array<string>, ctx: ContextT) {
function validateAndUpgrade(argv: Array<string>, ctx: ConfigT) {
const projectDir = ctx.root;

const packageJSON = JSON.parse(
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/upgrade/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs';
import chalk from 'chalk';
import semver from 'semver';
import execa from 'execa';
import type {ContextT} from '../../tools/types.flow';
import type {ConfigT} from '../../tools/config/types.flow';
import {logger} from '@react-native-community/cli-tools';
import * as PackageManager from '../../tools/packageManager';
import {fetch} from '../../tools/fetch';
Expand Down Expand Up @@ -176,7 +176,7 @@ const applyPatch = async (
/**
* Upgrade application to a new version of React Native.
*/
async function upgrade(argv: Array<string>, ctx: ContextT, args: FlagsT) {
async function upgrade(argv: Array<string>, ctx: ConfigT, args: FlagsT) {
if (args.legacy) {
return legacyUpgrade.func(argv, ctx);
}
Expand Down
Loading