Skip to content

Commit

Permalink
[et] Resolve all TypeScript issues in expotools (#7786)
Browse files Browse the repository at this point in the history
# Why

Running `yarn tsc` in expotools has been outputing with ~30 issues. Most of them were because of using `fs.exists` which is deprecated in favor of `fs.pathExists` or wrong formatting.

# How

According to the rule: __*Gotta fix 'em all*__

# Test Plan

I've just changed method names, typings and let prettier do its work.
  • Loading branch information
tsapeta committed Apr 10, 2020
1 parent 6a84da9 commit a9160d7
Show file tree
Hide file tree
Showing 16 changed files with 398 additions and 291 deletions.
3 changes: 2 additions & 1 deletion tools/expotools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"express": "^4.16.0",
"folder-hash": "^3.0.0",
"freeport-async": "^1.1.1",
"fs-extra": "^7.0.1",
"fs-extra": "^9.0.0",
"glob": "^7.1.2",
"glob-promise": "^3.4.0",
"globby": "^7.1.1",
Expand Down Expand Up @@ -77,6 +77,7 @@
"xcode": "^2.0.0"
},
"devDependencies": {
"@types/fs-extra": "^8.1.0",
"typescript": "^3.8.3"
}
}
4 changes: 2 additions & 2 deletions tools/expotools/src/Packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Package {
const { podspecName } = this;
return (
podspecName != null &&
fs.existsSync(path.join(IOS_DIR, 'Pods', 'Headers', 'Public', podspecName))
fs.pathExistsSync(path.join(IOS_DIR, 'Pods', 'Headers', 'Public', podspecName))
);
} else if (platform === 'android') {
// On Android we need to read expoview's build.gradle file
Expand Down Expand Up @@ -147,7 +147,7 @@ async function getListOfPackagesAsync(
if (!(await fs.lstat(packagePath)).isDirectory()) {
continue;
}
if (await fs.exists(packageJsonPath)) {
if (await fs.pathExists(packageJsonPath)) {
const packageJson = require(packageJsonPath);
packages.push(new Package(packagePath, packageJson));
} else {
Expand Down
10 changes: 5 additions & 5 deletions tools/expotools/src/ProjectTemplates.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import path from 'path';
import fs from 'fs-extra';
import JsonFile from '@expo/json-file';
import fs from 'fs-extra';
import path from 'path';

import { TEMPLATES_DIR } from './Constants';

export interface Template {
export type Template = {
name: string;
version: string;
path: string;
}
};

export async function getAvailableProjectTemplatesAsync(): Promise<Template[]> {
const templates = await fs.readdir(TEMPLATES_DIR);

return Promise.all<Template>(
templates.map(async template => {
const packageJson = await JsonFile.readAsync(
const packageJson = await JsonFile.readAsync<Template>(
path.join(TEMPLATES_DIR, template, 'package.json')
);

Expand Down
2 changes: 1 addition & 1 deletion tools/expotools/src/ProjectVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function getSDKVersionsAsync(platform: Platform): Promise<string[]>
'sdkVersions.json'
);

if (!(await fs.exists(sdkVersionsPath))) {
if (!(await fs.pathExists(sdkVersionsPath))) {
throw new Error(`File at path "${sdkVersionsPath}" not found.`);
}
const { sdkVersions } = (await JsonFile.readAsync(sdkVersionsPath)) as SDKVersionsObject;
Expand Down
10 changes: 5 additions & 5 deletions tools/expotools/src/commands/AndroidBuildPackages.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import spawnAsync from '@expo/spawn-async';
import chalk from 'chalk';
import fs from 'fs-extra';
import inquirer from 'inquirer';
import path from 'path';
import readline from 'readline';
import spawnAsync from '@expo/spawn-async';

import * as Directories from '../Directories';
import * as Packages from '../Packages';
Expand Down Expand Up @@ -244,9 +244,9 @@ async function _updateExpoViewAsync(packages: Package[], sdkVersion: string): Pr

console.log(' 🚚 Copying newly built packages...');

await fs.mkdir(path.join(ANDROID_DIR, 'maven/com/facebook'), { recursive: true });
await fs.mkdir(path.join(ANDROID_DIR, 'maven/host/exp/exponent'), { recursive: true });
await fs.mkdir(path.join(ANDROID_DIR, 'maven/org/unimodules'), { recursive: true });
await fs.mkdirs(path.join(ANDROID_DIR, 'maven/com/facebook'));
await fs.mkdirs(path.join(ANDROID_DIR, 'maven/host/exp/exponent'));
await fs.mkdirs(path.join(ANDROID_DIR, 'maven/org/unimodules'));

for (const pkg of packages) {
if (failedPackages.includes(pkg.name)) {
Expand Down Expand Up @@ -295,7 +295,7 @@ async function action(options: ActionOptions) {
const match = expoviewBuildGradle
.toString()
.match(/api 'com.facebook.react:react-native:([\d.]+)'/);
if (!match[1]) {
if (!match || !match[1]) {
throw new Error(
'Could not find SDK version in android/expoview/build.gradle: unexpected format'
);
Expand Down
52 changes: 39 additions & 13 deletions tools/expotools/src/commands/CheckAndroidPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ const EXPO_ROOT_DIR = Directories.getExpoRepositoryRootDir();
const ANDROID_DIR = Directories.getAndroidDir();

async function _getOutdatedUnimodules(packages: Packages.Package[]): Promise<string[]> {
let outdatedPackages: string[] = [];
const outdatedPackages: string[] = [];
for (const pkg of packages) {
if (!pkg.isSupportedOnPlatform('android') || !pkg.androidPackageName || pkg.packageSlug === 'expo-module-template') continue;
const buildDir = `${pkg.androidPackageName.replace(/\./g, '/')}/${pkg.packageSlug}`
if (
!pkg.isSupportedOnPlatform('android') ||
!pkg.androidPackageName ||
pkg.packageSlug === 'expo-module-template'
)
continue;
const buildDir = `${pkg.androidPackageName.replace(/\./g, '/')}/${pkg.packageSlug}`;
const version = pkg.packageVersion;
if (!(await fs.exists(path.join(EXPO_ROOT_DIR, 'expokit-npm-package', 'maven', buildDir, version)))) {
outdatedPackages.push(pkg.packageSlug);
}
if (
!(await fs.pathExists(
path.join(EXPO_ROOT_DIR, 'expokit-npm-package', 'maven', buildDir, version)
))
) {
outdatedPackages.push(pkg.packageSlug);
}
}
return outdatedPackages;
}
Expand All @@ -27,21 +36,39 @@ async function action() {
const match = expoviewBuildGradle
.toString()
.match(/api 'com.facebook.react:react-native:([\d.]+)'/);
if (!match[1]) {
if (!match || !match[1]) {
throw new Error(
'Could not find SDK version in android/expoview/build.gradle: unexpected format'
);
}
const sdkVersion = match[1];

let outdatedPackages = await _getOutdatedUnimodules(unimodules)
const outdatedPackages = await _getOutdatedUnimodules(unimodules);

const reactNativePath = path.join(EXPO_ROOT_DIR, 'expokit-npm-package', 'maven', 'com', 'facebook', 'react', 'react-native', sdkVersion);
const expoviewPath = path.join(EXPO_ROOT_DIR, 'expokit-npm-package', 'maven', 'host', 'exp', 'exponent', 'expoview', sdkVersion);
if (!(await fs.exists(reactNativePath))) {
const reactNativePath = path.join(
EXPO_ROOT_DIR,
'expokit-npm-package',
'maven',
'com',
'facebook',
'react',
'react-native',
sdkVersion
);
const expoviewPath = path.join(
EXPO_ROOT_DIR,
'expokit-npm-package',
'maven',
'host',
'exp',
'exponent',
'expoview',
sdkVersion
);
if (!(await fs.pathExists(reactNativePath))) {
outdatedPackages.push('ReactAndroid');
}
if (!(await fs.exists(expoviewPath))) {
if (!(await fs.pathExists(expoviewPath))) {
outdatedPackages.push('expoview');
}

Expand All @@ -52,7 +79,6 @@ async function action() {
);
} else {
console.log('All packages are up-to-date!');
return;
}
}

Expand Down
71 changes: 45 additions & 26 deletions tools/expotools/src/commands/ClientBuild.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import path from 'path';
import fs from 'fs-extra';
import chalk from 'chalk';
import aws from 'aws-sdk';
import inquirer from 'inquirer';
import { Command } from '@expo/commander';
import spawnAsync from '@expo/spawn-async';
import { Config, UpdateVersions } from '@expo/xdl';
import aws from 'aws-sdk';
import chalk from 'chalk';
import fs from 'fs-extra';
import inquirer from 'inquirer';
import path from 'path';

import { Platform, iosAppVersionAsync, androidAppVersionAsync } from '../ProjectVersions';
import { STAGING_API_HOST, EXPO_DIR, IOS_DIR, ANDROID_DIR } from '../Constants';
import { Platform, iosAppVersionAsync, androidAppVersionAsync } from '../ProjectVersions';
import askForPlatformAsync from '../utils/askForPlatformAsync';
import getSDKVersionFromBranchNameAsync from '../utils/getSDKVersionFromBranchNameAsync';

Expand All @@ -25,7 +25,9 @@ async function askToRecreateSimulatorBuildAsync(archivePath: string) {
{
type: 'confirm',
name: 'createNew',
message: `Simulator archive already exists at ${chalk.magenta(path.relative(EXPO_DIR, archivePath))}. Do you want to create a fresh one?`,
message: `Simulator archive already exists at ${chalk.magenta(
path.relative(EXPO_DIR, archivePath)
)}. Do you want to create a fresh one?`,
default: true,
},
]);
Expand All @@ -41,31 +43,30 @@ function getApplicationPathForPlatform(platform: Platform) {
'Build',
'Products',
'Release-iphonesimulator',
'Exponent.app',
'Exponent.app'
);
}
case 'android': {
return path.join(
ANDROID_DIR,
'app',
'build',
'outputs',
'apk',
'release',
'app-release.apk',
);
return path.join(ANDROID_DIR, 'app', 'build', 'outputs', 'apk', 'release', 'app-release.apk');
}
default: {
throw new Error(`Platform "${platform}" is not supported yet!`);
}
}
}

async function buildAndReleaseClientAsync(platform: Platform, sdkVersion: string | undefined, release: boolean) {
async function buildAndReleaseClientAsync(
platform: Platform,
sdkVersion: string | undefined,
release: boolean
) {
const appPath = getApplicationPathForPlatform(platform);

if (!await fs.exists(appPath) || await askToRecreateSimulatorBuildAsync(appPath)) {
const args = platform === 'ios' ? ['ios', 'create_simulator_build'] : ['android', 'build', 'build_type:Release'];
if (!(await fs.pathExists(appPath)) || (await askToRecreateSimulatorBuildAsync(appPath))) {
const args =
platform === 'ios'
? ['ios', 'create_simulator_build']
: ['android', 'build', 'build_type:Release'];

await spawnAsync('fastlane', args, {
cwd: EXPO_DIR,
Expand All @@ -87,12 +88,24 @@ async function releaseBuildAsync(platform: Platform, appPath: string, sdkVersion
switch (platform) {
case 'ios': {
const appVersion = await iosAppVersionAsync();
console.log(`Uploading iOS ${chalk.cyan(appVersion)} build and saving its url to staging versions endpoint for SDK ${chalk.cyan(sdkVersion)}...`);
console.log(
`Uploading iOS ${chalk.cyan(
appVersion
)} build and saving its url to staging versions endpoint for SDK ${chalk.cyan(
sdkVersion
)}...`
);
return await UpdateVersions.updateIOSSimulatorBuild(s3, appPath, appVersion, sdkVersion);
}
case 'android': {
const appVersion = await androidAppVersionAsync();
console.log(`Uploading Android ${chalk.cyan(appVersion)} build and saving its url to staging versions endpoint for SDK ${chalk.cyan(sdkVersion)}...`);
console.log(
`Uploading Android ${chalk.cyan(
appVersion
)} build and saving its url to staging versions endpoint for SDK ${chalk.cyan(
sdkVersion
)}...`
);
return await UpdateVersions.updateAndroidApk(s3, appPath, appVersion, sdkVersion);
}
default: {
Expand All @@ -104,8 +117,8 @@ async function releaseBuildAsync(platform: Platform, appPath: string, sdkVersion
async function action(options: ActionOptions) {
Config.api.host = STAGING_API_HOST;

const platform = options.platform || await askForPlatformAsync();
const sdkVersion = await getSDKVersionFromBranchNameAsync() || '20.0.0';
const platform = options.platform || (await askForPlatformAsync());
const sdkVersion = (await getSDKVersionFromBranchNameAsync()) || '20.0.0';

if (options.release && !sdkVersion) {
throw new Error(`Client builds can be released only from the release branch!`);
Expand All @@ -118,8 +131,14 @@ export default (program: Command) => {
program
.command('client-build')
.alias('cb')
.description('Builds Expo client for iOS simulator or APK for Android, uploads the archive to S3 and saves its url to versions endpoint.')
.description(
'Builds Expo client for iOS simulator or APK for Android, uploads the archive to S3 and saves its url to versions endpoint.'
)
.option('-p, --platform [string]', 'Platform for which the client will be built.')
.option('-r, --release', 'Whether to upload and release the client build to staging versions endpoint.', false)
.option(
'-r, --release',
'Whether to upload and release the client build to staging versions endpoint.',
false
)
.asyncAction(action);
};
21 changes: 13 additions & 8 deletions tools/expotools/src/commands/GenerateSDKDocs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path';
import fs from 'fs-extra';
import chalk from 'chalk';
import JsonFile from '@expo/json-file';
import spawnAsync from '@expo/spawn-async';
import chalk from 'chalk';
import fs from 'fs-extra';
import path from 'path';

import { Directories } from '../expotools';

Expand Down Expand Up @@ -59,21 +59,26 @@ async function action(options) {

await JsonFile.setAsync(path.join(DOCS_DIR, 'package.json'), 'version', sdk);

if (await fs.exists(targetSdkDirectory)) {
if (await fs.pathExists(targetSdkDirectory)) {
console.log(chalk.magenta(`v${sdk}`), 'directory already exists. Skipping copy operation.');
} else {
console.log(
`Copying ${chalk.yellow('unversioned')} docs to ${chalk.yellow(`v${sdk}`)} directory...`
);

await fs.copy(path.join(SDK_DOCS_DIR, 'unversioned'), targetSdkDirectory);
};
}

if (await fs.exists(targetExampleDirectory)) {
console.log(chalk.magenta(`v${sdk}`), 'examples directory already exists. Skipping copy operation.');
if (await fs.pathExists(targetExampleDirectory)) {
console.log(
chalk.magenta(`v${sdk}`),
'examples directory already exists. Skipping copy operation.'
);
} else {
console.log(
`Copying ${chalk.yellow('unversioned')} static examples to ${chalk.yellow(`v${sdk}`)} directory...`
`Copying ${chalk.yellow('unversioned')} static examples to ${chalk.yellow(
`v${sdk}`
)} directory...`
);

await fs.copy(path.join(STATIC_EXAMPLES_DIR, 'unversioned'), targetExampleDirectory);
Expand Down

0 comments on commit a9160d7

Please sign in to comment.