Skip to content

Commit

Permalink
imp: Migrate platform-ios to TypeScript (#620)
Browse files Browse the repository at this point in the history
* imp: move `platform-ios` to Typescript

* imp: Move tests and fixtures

* Add missing types

* Review fixes

* More review suggestions

* adjust tsdoc

* remove flowfixmes

* fix any from process
  • Loading branch information
Esemesek authored and thymikee committed Sep 9, 2019
1 parent f942c1b commit 4870ac5
Show file tree
Hide file tree
Showing 110 changed files with 540 additions and 369 deletions.
7 changes: 7 additions & 0 deletions packages/cli-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@react-native-community/cli-types",
"version": "2.8.3",
"main": "build",
"types": "build/index.d.ts",
"license": "MIT"
}
39 changes: 39 additions & 0 deletions packages/cli-types/src/android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export interface AndroidProjectConfig {
sourceDir: string;
isFlat: boolean;
folder: string;
stringsPath: string;
manifestPath: string;
buildGradlePath: string;
settingsGradlePath: string;
assetsPath: string;
mainFilePath: string;
packageName: string;
}

export interface AndroidProjectParams {
sourceDir?: string;
manifestPath?: string;
packageName?: string;
packageFolder?: string;
mainFilePath?: string;
stringsPath?: string;
settingsGradlePath?: string;
assetsPath?: string;
buildGradlePath?: string;
}

export interface AndroidDependencyConfig {
sourceDir: string;
folder: string;
packageImportPath: string;
packageInstance: string;
}

export interface AndroidDependencyParams {
packageName?: string;
sourceDir?: string;
manifestPath?: string;
packageImportPath?: string;
packageInstance?: string;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
export type InquirerPromptT = any;
import {
IOSProjectConfig,
IOSProjectParams,
IOSDependencyConfig,
IOSDependencyParams,
} from './ios';
import {
AndroidProjectConfig,
AndroidProjectParams,
AndroidDependencyConfig,
AndroidDependencyParams,
} from './android';

export type InquirerPrompt = any;

export interface Command {
name: string;
Expand All @@ -25,10 +38,10 @@ export interface Command {
}

interface PlatformConfig<
ProjectParams,
DependencyParams,
ProjectConfig,
DependencyConfig
ProjectParams,
DependencyConfig,
DependencyParams
> {
projectConfig: (
projectRoot: string,
Expand Down Expand Up @@ -60,6 +73,7 @@ interface PlatformConfig<
unlinkAssets: (assets: string[], projectConfig: ProjectConfig) => void;
};
}

/**
* @property root - Root where the configuration has been resolved from
* @property reactNativePath - Path to React Native source
Expand All @@ -74,7 +88,8 @@ export interface Config {
root: string;
reactNativePath: string;
project: {
android?: ProjectConfigAndroid;
android?: AndroidProjectConfig;
ios?: IOSProjectConfig;
[key: string]: any;
};
assets: string[];
Expand All @@ -83,23 +98,30 @@ export interface Config {
name: string;
root: string;
platforms: {
android?: DependencyConfigAndroid | null;
android?: AndroidDependencyConfig | null;
ios?: IOSDependencyConfig | null;
[key: string]: any;
};
assets: string[];
hooks: {
prelink?: string;
postlink?: string;
};
params: InquirerPromptT[];
params: InquirerPrompt[];
};
};
platforms: {
android: PlatformConfig<
ProjectParamsAndroid,
DependencyParamsAndroid,
ProjectConfigAndroid,
DependencyConfigAndroid
AndroidProjectConfig,
AndroidProjectParams,
AndroidDependencyConfig,
AndroidDependencyParams
>;
ios: PlatformConfig<
IOSProjectConfig,
IOSProjectParams,
IOSDependencyConfig,
IOSDependencyParams
>;
[name: string]: PlatformConfig<any, any, any, any>;
};
Expand All @@ -110,42 +132,33 @@ export interface Config {
};
}

export interface ProjectConfigAndroid {
sourceDir: string;
isFlat: boolean;
folder: string;
stringsPath: string;
manifestPath: string;
buildGradlePath: string;
settingsGradlePath: string;
assetsPath: string;
mainFilePath: string;
packageName: string;
}
type Diff<T, U> = T extends U ? never : T;

export interface ProjectParamsAndroid {
sourceDir?: string;
manifestPath?: string;
packageName?: string;
packageFolder?: string;
mainFilePath?: string;
stringsPath?: string;
settingsGradlePath?: string;
assetsPath?: string;
buildGradlePath?: string;
}
/**
* Shares some structure with ConfigT, except that haste and root
* are calculated and can't be defined
*/
export type UserConfig = Diff<Config, {root: any; haste: any}> & {
reactNativePath: string | void;

export interface DependencyConfigAndroid {
sourceDir: string;
folder: string;
packageImportPath: string;
packageInstance: string;
}
// Additional project settings
project: {
android?: AndroidProjectParams;
ios?: IOSProjectParams;
[key: string]: any;
};
};

export interface DependencyParamsAndroid {
packageName?: string;
sourceDir?: string;
manifestPath?: string;
packageImportPath?: string;
packageInstance?: string;
}
export {
IOSProjectConfig,
IOSProjectParams,
IOSDependencyConfig,
IOSDependencyParams,
};

export {
AndroidProjectConfig,
AndroidProjectParams,
AndroidDependencyConfig,
AndroidDependencyParams,
};
33 changes: 33 additions & 0 deletions packages/cli-types/src/ios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Settings that user can define in the project configuration for iOS.
* Same for dependency - we share the type.
*
* See UserDependencyConfigT and UserConfigT for details
*/
export interface IOSProjectParams {
project?: string;
podspecPath?: string;
sharedLibraries?: string[];
libraryFolder?: string;
plist: Array<any>;
scriptPhases: Array<any>;
}

export interface IOSDependencyParams extends IOSProjectParams {}

// The following types are used in untyped-parts of the codebase, so I am leaving them
// until we actually need them.
export interface IOSProjectConfig {
sourceDir: string;
folder: string;
pbxprojPath: string;
podfile: string;
podspecPath: string;
projectPath: string;
projectName: string;
libraryFolder: string;
sharedLibraries: Array<any>;
plist: Array<any>;
}

export interface IOSDependencyConfig extends IOSProjectConfig {}
7 changes: 7 additions & 0 deletions packages/cli-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
}
}
1 change: 1 addition & 0 deletions packages/platform-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"native_modules.gradle"
],
"devDependencies": {
"@react-native-community/cli-types": "^2.8.3",
"@types/execa": "^0.9.0",
"@types/fs-extra": "^8.0.0",
"@types/glob": "^7.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-android/src/commands/runAndroid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import path from 'path';
import execa from 'execa';
import chalk from 'chalk';
import fs from 'fs';
import {Config} from '../../types';
import {Config} from '@react-native-community/cli-types';
import adb from './adb';
import runOnAllDevices from './runOnAllDevices';
import tryRunAdbReverse from './tryRunAdbReverse';
Expand Down
9 changes: 6 additions & 3 deletions packages/platform-android/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import findAndroidAppFolder from './findAndroidAppFolder';
import findManifest from './findManifest';
import findPackageClassName from './findPackageClassName';
import readManifest from './readManifest';
import {ProjectParamsAndroid, DependencyParamsAndroid} from '../types';
import {
AndroidProjectParams,
AndroidDependencyParams,
} from '@react-native-community/cli-types';
import {XmlDocument} from 'xmldoc';

const getPackageName = (manifest: XmlDocument) => manifest.attr.package;
Expand All @@ -22,7 +25,7 @@ const getPackageName = (manifest: XmlDocument) => manifest.attr.package;
*/
export function projectConfig(
folder: string,
userConfig: ProjectParamsAndroid = {},
userConfig: AndroidProjectParams = {},
) {
const src = userConfig.sourceDir || findAndroidAppFolder(folder);

Expand Down Expand Up @@ -98,7 +101,7 @@ export function projectConfig(
*/
export function dependencyConfig(
folder: string,
userConfig: DependencyParamsAndroid = {},
userConfig: AndroidDependencyParams = {},
) {
const src = userConfig.sourceDir || findAndroidAppFolder(folder);

Expand Down
4 changes: 2 additions & 2 deletions packages/platform-android/src/link/patches/applyParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import {camelCase as toCamelCase} from 'lodash';
import {ProjectParamsAndroid} from '../../types';
import {AndroidProjectParams} from '@react-native-community/cli-types';

export default function applyParams(
str: string,
params: ProjectParamsAndroid,
params: AndroidProjectParams,
prefix: string,
) {
return str.replace(/\$\{(\w+)\}/g, (_pattern: string, param: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import applyParams from './applyParams';
import {ProjectParamsAndroid} from '../../types';
import {AndroidProjectParams} from '@react-native-community/cli-types';

export default function makePackagePatch(
packageInstance: string,
params: ProjectParamsAndroid,
params: AndroidProjectParams,
prefix: string,
) {
const processedInstance = applyParams(packageInstance, params, prefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/

import {camelCase as toCamelCase} from 'lodash';
import {ProjectParamsAndroid} from '../../types';
import {AndroidProjectParams} from '@react-native-community/cli-types';

export default function makeStringsPatch(
params: ProjectParamsAndroid,
params: AndroidProjectParams,
prefix: string,
) {
const values = Object.keys(params).map(param => {
Expand Down
14 changes: 7 additions & 7 deletions packages/platform-android/src/link/registerNativeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import makeBuildPatch from './patches/makeBuildPatch';
import makeImportPatch from './patches/makeImportPatch';
import makePackagePatch from './patches/makePackagePatch';
import {
ProjectConfigAndroid,
DependencyConfigAndroid,
ProjectParamsAndroid,
} from '../types';
AndroidProjectConfig,
AndroidDependencyConfig,
AndroidProjectParams,
} from '@react-native-community/cli-types';

export default function registerNativeAndroidModule(
name: string,
androidConfig: DependencyConfigAndroid,
params: ProjectParamsAndroid,
projectConfig: ProjectConfigAndroid,
androidConfig: AndroidDependencyConfig,
params: AndroidProjectParams,
projectConfig: AndroidProjectConfig,
) {
const buildPatch = makeBuildPatch(name);

Expand Down
9 changes: 6 additions & 3 deletions packages/platform-android/src/link/unregisterNativeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import makeBuildPatch from './patches/makeBuildPatch';
import makeStringsPatch from './patches/makeStringsPatch';
import makeImportPatch from './patches/makeImportPatch';
import makePackagePatch from './patches/makePackagePatch';
import {ProjectConfigAndroid, DependencyConfigAndroid} from '../types';
import {
AndroidProjectConfig,
AndroidDependencyConfig,
} from '@react-native-community/cli-types';

export default function unregisterNativeAndroidModule(
name: string,
androidConfig: DependencyConfigAndroid,
projectConfig: ProjectConfigAndroid,
androidConfig: AndroidDependencyConfig,
projectConfig: AndroidProjectConfig,
) {
const buildPatch = makeBuildPatch(name, projectConfig.buildGradlePath);
const strings = fs.readFileSync(projectConfig.stringsPath, 'utf8');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import {logger} from '@react-native-community/cli-tools';
import getLinkConfig from './index';
import {Config} from '../types';
import {Config} from '@react-native-community/cli-types';

// TODO: move to cli-tools once platform-ios and platform-android are migrated
// to TS and unify with iOS implementation
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-android/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"rootDir": "src",
"outDir": "build"
},
"references": [{"path": "../tools"}]
"references": [{"path": "../tools"}, {"path": "../cli-types"}]
}
Loading

0 comments on commit 4870ac5

Please sign in to comment.