Skip to content

Commit

Permalink
imp: migrate platform-android to TypeScript (react-native-community…
Browse files Browse the repository at this point in the history
  • Loading branch information
Esemesek authored and thymikee committed Aug 1, 2019
1 parent e18c858 commit 5e5d50d
Show file tree
Hide file tree
Showing 58 changed files with 439 additions and 229 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const prettierConfig = require('./.prettierrc');

module.exports = {
extends: ['@react-native-community', 'plugin:import/errors'],
env: {
Expand Down Expand Up @@ -32,5 +34,11 @@ module.exports = {
jest: true,
},
},
{
files: ['*.ts', '**/*.ts'],
rules: {
'prettier/prettier': [2, {prettierConfig, parser: 'typescript'}],
},
},
],
};
1 change: 0 additions & 1 deletion docs/projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ type ProjectParamsAndroidT = {
settingsGradlePath?: string;
assetsPath?: string;
buildGradlePath?: string;
packageName?: string;
};

type ProjectParamsIOST = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@babel/preset-typescript": "^7.3.3",
"@react-native-community/eslint-config": "^0.0.5",
"@types/jest": "^24.0.11",
"@types/node": "^11.13.0",
"@types/node": "8",
"@types/node-fetch": "^2.3.7",
"babel-jest": "^24.6.0",
"babel-plugin-module-resolver": "^3.2.0",
Expand Down
12 changes: 9 additions & 3 deletions packages/platform-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
"execa": "^1.0.0",
"jetifier": "^1.6.2",
"logkitty": "^0.5.0",
"slash": "^2.0.0",
"xmldoc": "^0.4.0"
"slash": "^3.0.0",
"xmldoc": "^1.1.2"
},
"files": [
"build",
"native_modules.gradle"
]
],
"devDependencies": {
"@types/execa": "^0.9.0",
"@types/fs-extra": "^8.0.0",
"@types/glob": "^7.1.1",
"@types/xmldoc": "^1.1.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* @flow
*/
import logAndroid from './logAndroid';
import runAndroid from './runAndroid';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function logAndroid() {

const emitter = logkitty({
platform: 'android',
minPriority: AndroidPriority.VERBOSE,
priority: AndroidPriority.VERBOSE,
filter: makeTagsFilter('ReactNative', 'ReactNativeJS'),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('--appFolder', () => {
});

it('uses task "install[Variant]" as default task', () => {
// @ts-ignore
runOnAllDevices({
variant: 'debug',
});
Expand All @@ -30,6 +31,7 @@ describe('--appFolder', () => {
});

it('uses appFolder and default variant', () => {
// @ts-ignore
runOnAllDevices({
appFolder: 'someApp',
variant: 'debug',
Expand All @@ -39,6 +41,7 @@ describe('--appFolder', () => {
});

it('uses appFolder and custom variant', () => {
// @ts-ignore
runOnAllDevices({
appFolder: 'anotherApp',
variant: 'staging',
Expand All @@ -50,6 +53,7 @@ describe('--appFolder', () => {
});

it('uses only task argument', () => {
// @ts-ignore
runOnAllDevices({
tasks: ['someTask'],
variant: 'debug',
Expand All @@ -59,6 +63,7 @@ describe('--appFolder', () => {
});

it('uses appFolder and custom task argument', () => {
// @ts-ignore
runOnAllDevices({
appFolder: 'anotherApp',
tasks: ['someTask'],
Expand All @@ -69,13 +74,15 @@ describe('--appFolder', () => {
});

it('uses multiple tasks', () => {
// @ts-ignore
runOnAllDevices({
appFolder: 'app',
tasks: ['clean', 'someTask'],
});

expect(execFileSync.mock.calls[0][1]).toContain(
'app:clean',
// @ts-ignore
'app:someTask',
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* 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 {execSync, execFileSync} from 'child_process';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

function getAdbPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import path from 'path';
import execa from 'execa';
import chalk from 'chalk';
import fs from 'fs';
import type {ConfigT} from 'types';
import {Config} from '../../types';
import adb from './adb';
import runOnAllDevices from './runOnAllDevices';
import tryRunAdbReverse from './tryRunAdbReverse';
Expand All @@ -26,29 +25,29 @@ import {
import warnAboutManuallyLinkedLibs from '../../link/warnAboutManuallyLinkedLibs';

// Verifies this is an Android project
function checkAndroid(root) {
function checkAndroid(root: string) {
return fs.existsSync(path.join(root, 'android/gradlew'));
}

export type FlagsT = {|
tasks?: Array<string>,
root: string,
variant: string,
appFolder: string,
appId: string,
appIdSuffix: string,
mainActivity: string,
deviceId?: string,
packager: boolean,
port: number,
terminal: string,
jetifier: boolean,
|};
export interface Flags {
tasks?: Array<string>;
root: string;
variant: string;
appFolder: string;
appId: string;
appIdSuffix: string;
mainActivity: string;
deviceId?: string;
packager: boolean;
port: number;
terminal: string;
jetifier: boolean;
}

/**
* Starts the app on a connected Android emulator or device.
*/
async function runAndroid(argv: Array<string>, config: ConfigT, args: FlagsT) {
async function runAndroid(_argv: Array<string>, config: Config, args: Flags) {
if (!checkAndroid(args.root)) {
logger.error(
'Android project not found. Are you sure this is a React Native project?',
Expand Down Expand Up @@ -104,7 +103,11 @@ async function runAndroid(argv: Array<string>, config: ConfigT, args: FlagsT) {
});
}

function getPackageNameWithSuffix(appId, appIdSuffix, packageName) {
function getPackageNameWithSuffix(
appId: string,
appIdSuffix: string,
packageName: string,
) {
if (appId) {
return appId;
}
Expand All @@ -116,15 +119,15 @@ function getPackageNameWithSuffix(appId, appIdSuffix, packageName) {
}

// Builds the app and runs it on a connected emulator / device.
function buildAndRun(args) {
function buildAndRun(args: Flags) {
process.chdir(path.join(args.root, 'android'));
const cmd = process.platform.startsWith('win') ? 'gradlew.bat' : './gradlew';

// "app" is usually the default value for Android apps with only 1 app
const {appFolder} = args;
// @ts-ignore
const packageName = fs
.readFileSync(`${appFolder}/src/main/AndroidManifest.xml`, 'utf8')
// $FlowFixMe
.match(/package="(.+?)"/)[1];

const packageNameWithSuffix = getPackageNameWithSuffix(
Expand Down Expand Up @@ -154,11 +157,11 @@ function buildAndRun(args) {
}

function runOnSpecificDevice(
args,
gradlew,
packageNameWithSuffix,
packageName,
adbPath,
args: Flags,
gradlew: 'gradlew.bat' | './gradlew',
packageNameWithSuffix: string,
packageName: string,
adbPath: string,
) {
const devices = adb.getDevices(adbPath);
const {deviceId} = args;
Expand All @@ -183,7 +186,7 @@ function runOnSpecificDevice(
}
}

function buildApk(gradlew) {
function buildApk(gradlew: string) {
try {
// using '-x lint' in order to ignore linting errors while building the apk
const gradleArgs = ['build', '-x', 'lint'];
Expand All @@ -195,7 +198,7 @@ function buildApk(gradlew) {
}
}

function tryInstallAppOnDevice(args, adbPath, device) {
function tryInstallAppOnDevice(args: Flags, adbPath: string, device: string) {
try {
// "app" is usually the default value for Android apps with only 1 app
const {appFolder} = args;
Expand All @@ -222,11 +225,11 @@ function tryInstallAppOnDevice(args, adbPath, device) {
}

function getInstallApkName(
appFolder,
adbPath,
variant,
device,
buildDirectory,
appFolder: string,
adbPath: string,
variant: string,
device: string,
buildDirectory: string,
) {
const availableCPUs = adb.getAvailableCPUs(adbPath, device);

Expand All @@ -248,11 +251,11 @@ function getInstallApkName(
}

function installAndLaunchOnDevice(
args,
selectedDevice,
packageNameWithSuffix,
packageName,
adbPath,
args: Flags,
selectedDevice: string,
packageNameWithSuffix: string,
packageName: string,
adbPath: string,
) {
tryRunAdbReverse(args.port, selectedDevice);
tryInstallAppOnDevice(args, adbPath, selectedDevice);
Expand All @@ -265,7 +268,12 @@ function installAndLaunchOnDevice(
);
}

function startServerInNewWindow(port, terminal, reactNativePath) {
// @ts-ignore
function startServerInNewWindow(
port: number,
terminal: string,
reactNativePath: string,
) {
/**
* Set up OS-specific filenames and commands
*/
Expand Down Expand Up @@ -310,7 +318,7 @@ function startServerInNewWindow(port, terminal, reactNativePath) {
*/
const scriptsDir = path.dirname(launchPackagerScript);
const packagerEnvFile = path.join(scriptsDir, packagerEnvFilename);
const procConfig: Object = {cwd: scriptsDir};
const procConfig: execa.SyncOptions = {cwd: scriptsDir};

/**
* Ensure we overwrite file by passing the `w` flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* 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';
Expand All @@ -13,7 +12,7 @@ import {logger, CLIError} from '@react-native-community/cli-tools';
import adb from './adb';
import tryRunAdbReverse from './tryRunAdbReverse';
import tryLaunchAppOnDevice from './tryLaunchAppOnDevice';
import type {FlagsT} from '.';
import {Flags} from '.';

function getTaskNames(
appFolder: string,
Expand All @@ -29,7 +28,7 @@ function toPascalCase(value: string) {
}

function runOnAllDevices(
args: FlagsT,
args: Flags,
cmd: string,
packageNameWithSuffix: string,
packageName: string,
Expand All @@ -54,19 +53,21 @@ function runOnAllDevices(
}
const devices = adb.getDevices(adbPath);

(devices.length > 0 ? devices : [undefined]).forEach(device => {
tryRunAdbReverse(args.port, device);
tryLaunchAppOnDevice(
device,
packageNameWithSuffix,
packageName,
adbPath,
args.mainActivity,
);
});
(devices.length > 0 ? devices : [undefined]).forEach(
(device: string | void) => {
tryRunAdbReverse(args.port, device);
tryLaunchAppOnDevice(
device,
packageNameWithSuffix,
packageName,
adbPath,
args.mainActivity,
);
},
);
}

function createInstallError(error) {
function createInstallError(error: Error & {stderr: string}) {
const stderr = (error.stderr || '').toString();
const docs =
'https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment';
Expand Down
Loading

0 comments on commit 5e5d50d

Please sign in to comment.