Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrated link command to TS #696

2 changes: 2 additions & 0 deletions packages/cli-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export interface Dependency {
hooks: {
prelink?: string;
postlink?: string;
preunlink?: string;
postunlink?: string;
};
params: InquirerPrompt[];
}
Expand Down
9 changes: 2 additions & 7 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ import {Command} from '@react-native-community/cli-types';

// @ts-ignore - JS file
import server from './server/server';
// @ts-ignore - JS file
import bundle from './bundle/bundle';
// @ts-ignore - JS file
import ramBundle from './bundle/ramBundle';
// @ts-ignore - JS file
import link from './link/link'; // eslint-disable-line import/namespace, import/default
// @ts-ignore - JS file
import unlink from './link/unlink'; // eslint-disable-line import/namespace, import/default
import link from './link/link';
import unlink from './link/unlink';
import install from './install/install';
import uninstall from './install/uninstall';
import upgrade from './upgrade/upgrade';
import info from './info/info';
import config from './config/config';
// @ts-ignore - JS file
import init from './init';
// @ts-ignore - JS file
import doctor from './doctor';
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/commands/install/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*/
import {logger} from '@react-native-community/cli-tools';
import * as PackageManager from '../../tools/packageManager';
// @ts-ignore FIXME after converting link/link
import link from '../link/link'; // eslint-disable-line import/namespace, import/default
import link from '../link/link';
// @ts-ignore FIXME after converting tools/config
import loadConfig from '../../tools/config'; // eslint-disable-line import/namespace, import/default

Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/commands/install/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import {Config} from '@react-native-community/cli-types';
import {logger} from '@react-native-community/cli-tools';
import * as PackageManager from '../../tools/packageManager';
// @ts-ignore FIXME after converting link/unlink
import unlink from '../link/unlink'; // eslint-disable-line import/namespace, import/default
import unlink from '../link/unlink';

async function uninstall(args: Array<string>, ctx: Config): Promise<void> {
const name = args[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {func as link} from '../link';
import loadConfig from '../../../tools/config';
import loadConfig from '../../../tools/config'; // eslint-disable-line import/namespace, import/default
import makeHook from '../makeHook';

jest.mock('chalk', () => ({grey: str => str, bold: str => str}));
jest.mock('../../../tools/config');
jest.mock('../makeHook', () => {
Expand Down Expand Up @@ -94,7 +93,7 @@ describe('link', () => {

await link(['react-native-blur'], config, {});
expect(registerNativeModule.mock.calls).toHaveLength(2);
expect(makeHook.mock.calls).toEqual([[prelink], [postlink]]);
expect((makeHook as jest.Mock).mock.calls).toEqual([[prelink], [postlink]]);
});

it('should copy assets only from the specific dependency that we are linking', done => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* @flow
*/
import makeHook from '../makeHook';

afterAll(() => {
Expand All @@ -10,14 +7,12 @@ afterAll(() => {
describe('makeHook', () => {
it('invokes the command', async () => {
const hook = makeHook('echo');
// $FlowFixMe - execa weird Promise-like return value
const result = await hook();
expect(result.cmd).toBe('echo');
});

it('invokes the command with multiple arguments', async () => {
const hook = makeHook('node -p "1;"');
// $FlowFixMe - execa weird Promise-like return value
const result = await hook();
expect(result.cmd).toBe('node -p "1;"');
});
Expand Down
12 changes: 0 additions & 12 deletions packages/cli/src/commands/link/getPlatformName.js

This file was deleted.

8 changes: 8 additions & 0 deletions packages/cli/src/commands/link/getPlatformName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const names: {[key: string]: string} = {
ios: 'iOS',
android: 'Android',
};

export default function getPlatformName(name: string): string {
return names[name] || name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import chalk from 'chalk';
import {pick} from 'lodash';
import {logger, CLIError} from '@react-native-community/cli-tools';
import {type ConfigT} from 'types';
import {Config} from '@react-native-community/cli-types';
import getPlatformName from './getPlatformName';
import linkDependency from './linkDependency';
import linkAssets from './linkAssets';
import linkAll from './linkAll';
import makeHook from './makeHook';

type FlagsType = {
platforms?: Array<string>,
all?: boolean,
platforms?: Array<string>;
all?: boolean;
};

/**
Expand All @@ -30,13 +29,14 @@ type FlagsType = {
*/
async function link(
[rawPackageName]: Array<string>,
ctx: ConfigT,
ctx: Config,
opts: FlagsType,
) {
let platforms = ctx.platforms;
let project = ctx.project;

if (opts.platforms) {
// @ts-ignore
platforms = pick(platforms, opts.platforms);
logger.debug('Skipping selected platforms');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/**
* @flow
*/

import {uniqBy} from 'lodash';
import path from 'path';
import * as path from 'path';
import chalk from 'chalk';
import {CLIError, logger} from '@react-native-community/cli-tools';
import type {ConfigT} from 'types';
import {Config} from '@react-native-community/cli-types';
import linkAssets from './linkAssets';
import linkDependency from './linkDependency';
import makeHook from './makeHook';
Expand All @@ -15,11 +11,11 @@ const dedupeAssets = (assets: Array<string>): Array<string> =>
uniqBy(assets, asset => path.basename(asset));

type Options = {
linkDeps?: boolean,
linkAssets?: boolean,
linkDeps?: boolean;
linkAssets?: boolean;
};

async function linkAll(config: ConfigT, options: Options) {
async function linkAll(config: Config, options: Options) {
if (options.linkDeps) {
logger.debug('Linking all dependencies');
logger.info(
Expand Down Expand Up @@ -60,7 +56,7 @@ async function linkAll(config: ConfigT, options: Options) {
),
);
try {
await linkAssets(config.platforms, config.project, assets);
linkAssets(config.platforms, config.project, assets);
} catch (error) {
throw new CLIError('Linking assets failed.', error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// @flow

import {isEmpty} from 'lodash';
import type {PlatformsT, ProjectConfigT} from 'types';

import {Config} from '@react-native-community/cli-types';
import {logger} from '@react-native-community/cli-tools';

const linkAssets = (
platforms: PlatformsT,
project: ProjectConfigT,
export default function linkAssets(
platforms: Config['platforms'],
project: Config['project'],
assets: Array<string>,
) => {
) {
if (isEmpty(assets)) {
return;
}
Expand All @@ -25,11 +22,9 @@ const linkAssets = (
}

logger.info(`Linking assets to ${platform} project`);
// $FlowFixMe: We check for existence of project[platform]

linkConfig.copyAssets(assets, project[platform]);
});

logger.success('Assets have been successfully linked to your project');
};

export default linkAssets;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// @flow
import chalk from 'chalk';
import type {DependencyConfigT, ProjectConfigT, PlatformsT} from 'types';
import {
Config,
Dependency,
AndroidDependencyConfig,
AndroidProjectConfig,
IOSDependencyConfig,
IOSProjectConfig,
} from '@react-native-community/cli-types';
import {logger} from '@react-native-community/cli-tools';
import pollParams from './pollParams';
import getPlatformName from './getPlatformName';

const linkDependency = async (
platforms: PlatformsT,
project: ProjectConfigT,
dependency: DependencyConfigT,
) => {
export default async function linkDependency(
platforms: Config['platforms'],
project: Config['project'],
dependency: Dependency,
) {
const params = await pollParams(dependency.params);

Object.keys(platforms || {}).forEach(platform => {
const projectConfig = project[platform];
const dependencyConfig = dependency.platforms[platform];
const projectConfig: AndroidProjectConfig | IOSProjectConfig =
project[platform];
const dependencyConfig: AndroidDependencyConfig | IOSDependencyConfig =
dependency.platforms[platform];

if (!projectConfig || !dependencyConfig) {
return;
Expand All @@ -30,10 +38,8 @@ const linkDependency = async (
}

const isInstalled = linkConfig.isInstalled(
// $FlowFixMe
projectConfig,
name,
// $FlowFixMe
dependencyConfig,
);

Expand All @@ -49,7 +55,7 @@ const linkDependency = async (
logger.info(
`Linking "${chalk.bold(name)}" ${getPlatformName(platform)} dependency`,
);
// $FlowFixMe

linkConfig.register(name, dependencyConfig, params, projectConfig);

logger.info(
Expand All @@ -58,6 +64,4 @@ const linkDependency = async (
)}" has been successfully linked`,
);
});
};

export default linkDependency;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/**
* @flow
*/

import execa from 'execa';

export default function makeHook(command: string) {
return () => {
const args = command.split(' ');
const cmd = args.shift();
const cmd = args.shift() as string;

return execa(cmd, args, {stdio: 'inherit'});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

import inquirer from 'inquirer';
import type {InquirerPromptT} from 'types';
// @ts-ignore untyped
import {prompt, QuestionCollection, Answers} from 'inquirer';

export default (questions: InquirerPromptT) =>
new Promise<any>((resolve, reject) => {
export default (questions: QuestionCollection) =>
new Promise<Answers>((resolve, reject) => {
if (!questions) {
resolve({});
return;
}

inquirer.prompt(questions).then(resolve, reject);
prompt(questions).then(resolve, reject);
});
Loading