From 8d85464d3376bdc0258e24e56740fae93bd8a600 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 11:18:00 +0100 Subject: [PATCH 01/12] convert bundle from flow to typescript --- ...t.js => filterPlatformAssetScales-test.ts} | 0 ...est.js => getAssetDestPathAndroid-test.ts} | 0 ...OS-test.js => getAssetDestPathIOS-test.ts} | 0 .../{assetPathUtils.js => assetPathUtils.ts} | 22 +++++---- .../bundle/{buildBundle.js => buildBundle.ts} | 48 +++++++++++++------ .../commands/bundle/{bundle.js => bundle.ts} | 6 ++- ...ndLineArgs.js => bundleCommandLineArgs.ts} | 37 +++++++------- ...Scales.js => filterPlatformAssetScales.ts} | 0 ...hAndroid.js => getAssetDestPathAndroid.ts} | 2 +- ...tDestPathIOS.js => getAssetDestPathIOS.ts} | 2 +- .../bundle/{ramBundle.js => ramBundle.ts} | 7 ++- .../bundle/{saveAssets.js => saveAssets.ts} | 23 ++++++--- 12 files changed, 90 insertions(+), 57 deletions(-) rename packages/cli/src/commands/bundle/__tests__/{filterPlatformAssetScales-test.js => filterPlatformAssetScales-test.ts} (100%) rename packages/cli/src/commands/bundle/__tests__/{getAssetDestPathAndroid-test.js => getAssetDestPathAndroid-test.ts} (100%) rename packages/cli/src/commands/bundle/__tests__/{getAssetDestPathIOS-test.js => getAssetDestPathIOS-test.ts} (100%) rename packages/cli/src/commands/bundle/{assetPathUtils.js => assetPathUtils.ts} (82%) rename packages/cli/src/commands/bundle/{buildBundle.js => buildBundle.ts} (66%) rename packages/cli/src/commands/bundle/{bundle.js => bundle.ts} (76%) rename packages/cli/src/commands/bundle/{bundleCommandLineArgs.js => bundleCommandLineArgs.ts} (86%) rename packages/cli/src/commands/bundle/{filterPlatformAssetScales.js => filterPlatformAssetScales.ts} (100%) rename packages/cli/src/commands/bundle/{getAssetDestPathAndroid.js => getAssetDestPathAndroid.ts} (92%) rename packages/cli/src/commands/bundle/{getAssetDestPathIOS.js => getAssetDestPathIOS.ts} (91%) rename packages/cli/src/commands/bundle/{ramBundle.js => ramBundle.ts} (77%) rename packages/cli/src/commands/bundle/{saveAssets.js => saveAssets.ts} (77%) diff --git a/packages/cli/src/commands/bundle/__tests__/filterPlatformAssetScales-test.js b/packages/cli/src/commands/bundle/__tests__/filterPlatformAssetScales-test.ts similarity index 100% rename from packages/cli/src/commands/bundle/__tests__/filterPlatformAssetScales-test.js rename to packages/cli/src/commands/bundle/__tests__/filterPlatformAssetScales-test.ts diff --git a/packages/cli/src/commands/bundle/__tests__/getAssetDestPathAndroid-test.js b/packages/cli/src/commands/bundle/__tests__/getAssetDestPathAndroid-test.ts similarity index 100% rename from packages/cli/src/commands/bundle/__tests__/getAssetDestPathAndroid-test.js rename to packages/cli/src/commands/bundle/__tests__/getAssetDestPathAndroid-test.ts diff --git a/packages/cli/src/commands/bundle/__tests__/getAssetDestPathIOS-test.js b/packages/cli/src/commands/bundle/__tests__/getAssetDestPathIOS-test.ts similarity index 100% rename from packages/cli/src/commands/bundle/__tests__/getAssetDestPathIOS-test.js rename to packages/cli/src/commands/bundle/__tests__/getAssetDestPathIOS-test.ts diff --git a/packages/cli/src/commands/bundle/assetPathUtils.js b/packages/cli/src/commands/bundle/assetPathUtils.ts similarity index 82% rename from packages/cli/src/commands/bundle/assetPathUtils.js rename to packages/cli/src/commands/bundle/assetPathUtils.ts index 32334dd72..27337c49e 100644 --- a/packages/cli/src/commands/bundle/assetPathUtils.js +++ b/packages/cli/src/commands/bundle/assetPathUtils.ts @@ -5,14 +5,13 @@ * LICENSE file in the root directory of this source tree. * * @format - * @flow strict */ -export type PackagerAsset = { - +httpServerLocation: string, - +name: string, - +type: string, -}; +export interface PackagerAsset { + httpServerLocation: string; + name: string; + type: string; +} /** * FIXME: using number to represent discrete scale numbers is fragile in essence because of @@ -38,7 +37,7 @@ function getAndroidAssetSuffix(scale: number): string { } // See https://developer.android.com/guide/topics/resources/drawable-resource.html -const drawableFileTypes = new Set([ +const drawableFileTypes = new Set([ 'gif', 'jpeg', 'jpg', @@ -48,7 +47,10 @@ const drawableFileTypes = new Set([ 'xml', ]); -function getAndroidResourceFolderName(asset: PackagerAsset, scale: number) { +function getAndroidResourceFolderName( + asset: PackagerAsset, + scale: number, +): string { if (!drawableFileTypes.has(asset.type)) { return 'raw'; } @@ -64,7 +66,7 @@ function getAndroidResourceFolderName(asset: PackagerAsset, scale: number) { return androidFolder; } -function getAndroidResourceIdentifier(asset: PackagerAsset) { +function getAndroidResourceIdentifier(asset: PackagerAsset): string { const folderPath = getBasePath(asset); return `${folderPath}/${asset.name}` .toLowerCase() @@ -73,7 +75,7 @@ function getAndroidResourceIdentifier(asset: PackagerAsset) { .replace(/^assets_/, ''); // Remove "assets_" prefix } -function getBasePath(asset: PackagerAsset) { +function getBasePath(asset: PackagerAsset): string { let basePath = asset.httpServerLocation; if (basePath[0] === '/') { basePath = basePath.substr(1); diff --git a/packages/cli/src/commands/bundle/buildBundle.js b/packages/cli/src/commands/bundle/buildBundle.ts similarity index 66% rename from packages/cli/src/commands/bundle/buildBundle.js rename to packages/cli/src/commands/bundle/buildBundle.ts index 4d1fc0622..6f537c168 100644 --- a/packages/cli/src/commands/bundle/buildBundle.js +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -4,26 +4,44 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ +// @ts-ignore - no typed definition for the package import Server from 'metro/src/Server'; - +// @ts-ignore - no typed definition for the package import outputBundle from 'metro/src/shared/output/bundle'; import path from 'path'; import chalk from 'chalk'; -import type {CommandLineArgs} from './bundleCommandLineArgs'; -import type {ConfigT} from 'types'; +import {CommandLineArgs} from './bundleCommandLineArgs'; +// @ts-ignore FIXME after converting types +import {ConfigT} from 'types'; import saveAssets from './saveAssets'; -// $FlowFixMe - converted to typescript import loadMetroConfig from '../../tools/loadMetroConfig'; import {logger} from '@react-native-community/cli-tools'; -async function buildBundle( - args: CommandLineArgs, - ctx: ConfigT, - output: typeof outputBundle = outputBundle, -) { +interface RequestOptions { + entryFile: string; + sourceMapUrl: string; + dev: boolean; + minify: boolean; + platform: string; +} + +export interface AssetData { + __packager_asset: boolean; + fileSystemLocation: string; + hash: string; + height: number | null; + httpServerLocation: string; + name: string; + scales: number[]; + type: string; + width: number | null; + files: string[], +}; + +async function buildBundle(args: CommandLineArgs, ctx: ConfigT, output: outputBundle) { + const platform: string = args.platform || ''; const config = await loadMetroConfig(ctx, { maxWorkers: args.maxWorkers, resetCache: args.resetCache, @@ -39,7 +57,7 @@ async function buildBundle( logger.info( `Available platforms are: ${config.resolver.platforms - .map(x => `"${chalk.bold(x)}"`) + .map((x: string) => `"${chalk.bold(x)}"`) .join( ', ', )}. If you are trying to bundle for an out-of-tree platform, it may not be installed.`, @@ -52,7 +70,7 @@ async function buildBundle( // have other choice than defining it as an env variable here. process.env.NODE_ENV = args.dev ? 'development' : 'production'; - let sourceMapUrl = args.sourcemapOutput; + let sourceMapUrl: string = args.sourcemapOutput || ''; if (sourceMapUrl && !args.sourcemapUseAbsolutePath) { sourceMapUrl = path.basename(sourceMapUrl); } @@ -62,7 +80,7 @@ async function buildBundle( sourceMapUrl, dev: args.dev, minify: args.minify !== undefined ? args.minify : !args.dev, - platform: args.platform, + platform, }; const server = new Server(config); @@ -73,14 +91,14 @@ async function buildBundle( await output.save(bundle, args, logger.info); // Save the assets of the bundle - const outputAssets = await server.getAssets({ + const outputAssets: AssetData[] = await server.getAssets({ ...Server.DEFAULT_BUNDLE_OPTIONS, ...requestOpts, bundleType: 'todo', }); // When we're done saving bundle output and the assets, we're done. - return await saveAssets(outputAssets, args.platform, args.assetsDest); + return await saveAssets(outputAssets, platform, args.assetsDest); } finally { server.end(); } diff --git a/packages/cli/src/commands/bundle/bundle.js b/packages/cli/src/commands/bundle/bundle.ts similarity index 76% rename from packages/cli/src/commands/bundle/bundle.js rename to packages/cli/src/commands/bundle/bundle.ts index dc9790ec5..a355b12a3 100644 --- a/packages/cli/src/commands/bundle/bundle.js +++ b/packages/cli/src/commands/bundle/bundle.ts @@ -6,14 +6,16 @@ * * @format */ - +// @ts-ignore FIXME after converting types +import {ConfigT} from 'types'; +import {CommandLineArgs} from './bundleCommandLineArgs'; import buildBundle from './buildBundle'; import bundleCommandLineArgs from './bundleCommandLineArgs'; /** * Builds the bundle starting to look for dependencies at the given entry path. */ -function bundleWithOutput(_, config, args, output) { +function bundleWithOutput(_: any, config: ConfigT, args: CommandLineArgs, output: any) { return buildBundle(args, config, output); } diff --git a/packages/cli/src/commands/bundle/bundleCommandLineArgs.js b/packages/cli/src/commands/bundle/bundleCommandLineArgs.ts similarity index 86% rename from packages/cli/src/commands/bundle/bundleCommandLineArgs.js rename to packages/cli/src/commands/bundle/bundleCommandLineArgs.ts index 465e12b33..1c882d557 100644 --- a/packages/cli/src/commands/bundle/bundleCommandLineArgs.js +++ b/packages/cli/src/commands/bundle/bundleCommandLineArgs.ts @@ -4,29 +4,28 @@ * 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'; -export type CommandLineArgs = { - assetsDest?: string, - entryFile: string, - resetCache: boolean, - resetGlobalCache: boolean, - transformer?: string, - minify?: boolean, - config?: string, - platform?: string, - dev: boolean, - bundleOutput: string, - bundleEncoding?: string, - maxWorkers?: number, - sourcemapOutput?: string, - sourcemapSourcesRoot?: string, - sourcemapUseAbsolutePath: boolean, - verbose: boolean, -}; +export interface CommandLineArgs { + assetsDest?: string; + entryFile: string; + resetCache: boolean; + resetGlobalCache: boolean; + transformer?: string; + minify?: boolean; + config?: string; + platform?: string; + dev: boolean; + bundleOutput: string; + bundleEncoding?: string; + maxWorkers?: number; + sourcemapOutput?: string; + sourcemapSourcesRoot?: string; + sourcemapUseAbsolutePath: boolean; + verbose: boolean; +} export default [ { diff --git a/packages/cli/src/commands/bundle/filterPlatformAssetScales.js b/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts similarity index 100% rename from packages/cli/src/commands/bundle/filterPlatformAssetScales.js rename to packages/cli/src/commands/bundle/filterPlatformAssetScales.ts diff --git a/packages/cli/src/commands/bundle/getAssetDestPathAndroid.js b/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts similarity index 92% rename from packages/cli/src/commands/bundle/getAssetDestPathAndroid.js rename to packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts index 510d6ab48..5328dedeb 100644 --- a/packages/cli/src/commands/bundle/getAssetDestPathAndroid.js +++ b/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts @@ -9,7 +9,7 @@ */ import path from 'path'; -import type {PackagerAsset} from './assetPathUtils'; +import {PackagerAsset} from './assetPathUtils'; import assetPathUtils from './assetPathUtils'; diff --git a/packages/cli/src/commands/bundle/getAssetDestPathIOS.js b/packages/cli/src/commands/bundle/getAssetDestPathIOS.ts similarity index 91% rename from packages/cli/src/commands/bundle/getAssetDestPathIOS.js rename to packages/cli/src/commands/bundle/getAssetDestPathIOS.ts index cc50c2be1..814672668 100644 --- a/packages/cli/src/commands/bundle/getAssetDestPathIOS.js +++ b/packages/cli/src/commands/bundle/getAssetDestPathIOS.ts @@ -9,7 +9,7 @@ */ import path from 'path'; -import type {PackagerAsset} from './assetPathUtils'; +import {PackagerAsset} from './assetPathUtils'; function getAssetDestPathIOS(asset: PackagerAsset, scale: number): string { const suffix = scale === 1 ? '' : `@${scale}x`; diff --git a/packages/cli/src/commands/bundle/ramBundle.js b/packages/cli/src/commands/bundle/ramBundle.ts similarity index 77% rename from packages/cli/src/commands/bundle/ramBundle.js rename to packages/cli/src/commands/bundle/ramBundle.ts index c9420ad8b..7ff3ac084 100644 --- a/packages/cli/src/commands/bundle/ramBundle.js +++ b/packages/cli/src/commands/bundle/ramBundle.ts @@ -6,15 +6,18 @@ * * @format */ +// @ts-ignore - no typed definition for the package import outputUnbundle from 'metro/src/shared/output/RamBundle'; - +import {CommandLineArgs} from './bundleCommandLineArgs'; import {withOutput as bundleWithOutput} from './bundle'; import bundleCommandLineArgs from './bundleCommandLineArgs'; +// @ts-ignore FIXME after converting types +import {ConfigT} from 'types'; /** * Builds the bundle starting to look for dependencies at the given entry path. */ -function ramBundle(argv, config, args) { +function ramBundle(argv: CommandLineArgs, config: ConfigT, args: CommandLineArgs) { return bundleWithOutput(argv, config, args, outputUnbundle); } diff --git a/packages/cli/src/commands/bundle/saveAssets.js b/packages/cli/src/commands/bundle/saveAssets.ts similarity index 77% rename from packages/cli/src/commands/bundle/saveAssets.js rename to packages/cli/src/commands/bundle/saveAssets.ts index 34e7dcc08..d09351b2a 100644 --- a/packages/cli/src/commands/bundle/saveAssets.js +++ b/packages/cli/src/commands/bundle/saveAssets.ts @@ -15,8 +15,13 @@ import filterPlatformAssetScales from './filterPlatformAssetScales'; import getAssetDestPathAndroid from './getAssetDestPathAndroid'; import getAssetDestPathIOS from './getAssetDestPathIOS'; import {logger} from '@react-native-community/cli-tools'; +import { AssetData } from './buildBundle'; -function saveAssets(assets, platform, assetsDest) { +interface CopiedFiles { + [src: string]: string; +} + +function saveAssets(assets: AssetData[], platform: string, assetsDest: string | undefined) { if (!assetsDest) { logger.warn('Assets destination folder is not set, skipping...'); return Promise.resolve(); @@ -25,7 +30,7 @@ function saveAssets(assets, platform, assetsDest) { const getAssetDestPath = platform === 'android' ? getAssetDestPathAndroid : getAssetDestPathIOS; - const filesToCopy = Object.create(null); // Map src -> dest + const filesToCopy: CopiedFiles = Object.create(null); // Map src -> dest assets.forEach(asset => { const validScales = new Set( filterPlatformAssetScales(platform, asset.scales), @@ -43,7 +48,7 @@ function saveAssets(assets, platform, assetsDest) { return copyAll(filesToCopy); } -function copyAll(filesToCopy) { +function copyAll(filesToCopy: CopiedFiles) { const queue = Object.keys(filesToCopy); if (queue.length === 0) { return Promise.resolve(); @@ -51,7 +56,7 @@ function copyAll(filesToCopy) { logger.info(`Copying ${queue.length} asset files`); return new Promise((resolve, reject) => { - const copyNext = error => { + const copyNext = (error?: any) => { if (error) { reject(error); return; @@ -60,7 +65,7 @@ function copyAll(filesToCopy) { logger.info('Done copying assets'); resolve(); } else { - const src = queue.shift(); + const src = queue.shift() || ''; const dest = filesToCopy[src]; copy(src, dest, copyNext); } @@ -69,9 +74,13 @@ function copyAll(filesToCopy) { }); } -function copy(src, dest, callback) { +function copy( + src: string, + dest: string, + callback: (error: NodeJS.ErrnoException) => void, +): void { const destDir = path.dirname(dest); - mkdirp(destDir, err => { + mkdirp(destDir, (err?: NodeJS.ErrnoException) => { if (err) { callback(err); return; From e4762606091cdbb496662187ada603baa5ba3386 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 11:24:17 +0100 Subject: [PATCH 02/12] fix ReadonlyArray from filterPlatformAssetScales --- .../cli/src/commands/bundle/filterPlatformAssetScales.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts b/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts index 94c6bd9fa..f99d1367f 100644 --- a/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts +++ b/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts @@ -4,19 +4,18 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict * @format */ -const ALLOWED_SCALES = { +const ALLOWED_SCALES: {[key: string]: number[]} = { ios: [1, 2, 3], }; function filterPlatformAssetScales( platform: string, - scales: $ReadOnlyArray, -): $ReadOnlyArray { - const whitelist = ALLOWED_SCALES[platform]; + scales: ReadonlyArray, +): ReadonlyArray { + const whitelist: number[] = ALLOWED_SCALES[platform]; if (!whitelist) { return scales; } From 99da0071fe9c67abcf5eebc7a2165aa971911035 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 11:43:32 +0100 Subject: [PATCH 03/12] fix eslint and types --- .../cli/src/commands/bundle/assetPathUtils.ts | 1 - .../cli/src/commands/bundle/buildBundle.ts | 13 ++++++++----- packages/cli/src/commands/bundle/bundle.ts | 11 +++++++---- .../bundle/filterPlatformAssetScales.ts | 1 - .../bundle/getAssetDestPathAndroid.ts | 2 -- .../commands/bundle/getAssetDestPathIOS.ts | 2 -- packages/cli/src/commands/bundle/ramBundle.ts | 6 ++---- .../cli/src/commands/bundle/saveAssets.ts | 19 ++++++++++++------- 8 files changed, 29 insertions(+), 26 deletions(-) diff --git a/packages/cli/src/commands/bundle/assetPathUtils.ts b/packages/cli/src/commands/bundle/assetPathUtils.ts index 27337c49e..2d7e89713 100644 --- a/packages/cli/src/commands/bundle/assetPathUtils.ts +++ b/packages/cli/src/commands/bundle/assetPathUtils.ts @@ -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. * - * @format */ export interface PackagerAsset { diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index 6f537c168..95e671cab 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -13,8 +13,7 @@ import outputBundle from 'metro/src/shared/output/bundle'; import path from 'path'; import chalk from 'chalk'; import {CommandLineArgs} from './bundleCommandLineArgs'; -// @ts-ignore FIXME after converting types -import {ConfigT} from 'types'; +import {Config} from '@react-native-community/cli-types'; import saveAssets from './saveAssets'; import loadMetroConfig from '../../tools/loadMetroConfig'; import {logger} from '@react-native-community/cli-tools'; @@ -37,10 +36,14 @@ export interface AssetData { scales: number[]; type: string; width: number | null; - files: string[], -}; + files: string[]; +} -async function buildBundle(args: CommandLineArgs, ctx: ConfigT, output: outputBundle) { +async function buildBundle( + args: CommandLineArgs, + ctx: Config, + output: outputBundle, +) { const platform: string = args.platform || ''; const config = await loadMetroConfig(ctx, { maxWorkers: args.maxWorkers, diff --git a/packages/cli/src/commands/bundle/bundle.ts b/packages/cli/src/commands/bundle/bundle.ts index a355b12a3..e74c447f8 100644 --- a/packages/cli/src/commands/bundle/bundle.ts +++ b/packages/cli/src/commands/bundle/bundle.ts @@ -4,10 +4,8 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @format */ -// @ts-ignore FIXME after converting types -import {ConfigT} from 'types'; +import {Config} from '@react-native-community/cli-types'; import {CommandLineArgs} from './bundleCommandLineArgs'; import buildBundle from './buildBundle'; import bundleCommandLineArgs from './bundleCommandLineArgs'; @@ -15,7 +13,12 @@ import bundleCommandLineArgs from './bundleCommandLineArgs'; /** * Builds the bundle starting to look for dependencies at the given entry path. */ -function bundleWithOutput(_: any, config: ConfigT, args: CommandLineArgs, output: any) { +function bundleWithOutput( + _: Array, + config: Config, + args: CommandLineArgs, + output: any, +) { return buildBundle(args, config, output); } diff --git a/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts b/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts index f99d1367f..c294d318b 100644 --- a/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts +++ b/packages/cli/src/commands/bundle/filterPlatformAssetScales.ts @@ -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. * - * @format */ const ALLOWED_SCALES: {[key: string]: number[]} = { diff --git a/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts b/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts index 5328dedeb..adafaa01f 100644 --- a/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts +++ b/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts @@ -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 strict */ import path from 'path'; diff --git a/packages/cli/src/commands/bundle/getAssetDestPathIOS.ts b/packages/cli/src/commands/bundle/getAssetDestPathIOS.ts index 814672668..ca94f6d7e 100644 --- a/packages/cli/src/commands/bundle/getAssetDestPathIOS.ts +++ b/packages/cli/src/commands/bundle/getAssetDestPathIOS.ts @@ -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 strict */ import path from 'path'; diff --git a/packages/cli/src/commands/bundle/ramBundle.ts b/packages/cli/src/commands/bundle/ramBundle.ts index 7ff3ac084..31946a093 100644 --- a/packages/cli/src/commands/bundle/ramBundle.ts +++ b/packages/cli/src/commands/bundle/ramBundle.ts @@ -4,20 +4,18 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @format */ // @ts-ignore - no typed definition for the package import outputUnbundle from 'metro/src/shared/output/RamBundle'; import {CommandLineArgs} from './bundleCommandLineArgs'; import {withOutput as bundleWithOutput} from './bundle'; import bundleCommandLineArgs from './bundleCommandLineArgs'; -// @ts-ignore FIXME after converting types -import {ConfigT} from 'types'; +import {Config} from '@react-native-community/cli-types'; /** * Builds the bundle starting to look for dependencies at the given entry path. */ -function ramBundle(argv: CommandLineArgs, config: ConfigT, args: CommandLineArgs) { +function ramBundle(argv: Array, config: Config, args: CommandLineArgs) { return bundleWithOutput(argv, config, args, outputUnbundle); } diff --git a/packages/cli/src/commands/bundle/saveAssets.ts b/packages/cli/src/commands/bundle/saveAssets.ts index d09351b2a..56e79772a 100644 --- a/packages/cli/src/commands/bundle/saveAssets.ts +++ b/packages/cli/src/commands/bundle/saveAssets.ts @@ -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. * - * @format */ import mkdirp from 'mkdirp'; @@ -15,13 +14,17 @@ import filterPlatformAssetScales from './filterPlatformAssetScales'; import getAssetDestPathAndroid from './getAssetDestPathAndroid'; import getAssetDestPathIOS from './getAssetDestPathIOS'; import {logger} from '@react-native-community/cli-tools'; -import { AssetData } from './buildBundle'; +import {AssetData} from './buildBundle'; interface CopiedFiles { [src: string]: string; } -function saveAssets(assets: AssetData[], platform: string, assetsDest: string | undefined) { +function saveAssets( + assets: AssetData[], + platform: string, + assetsDest: string | undefined, +) { if (!assetsDest) { logger.warn('Assets destination folder is not set, skipping...'); return Promise.resolve(); @@ -56,7 +59,7 @@ function copyAll(filesToCopy: CopiedFiles) { logger.info(`Copying ${queue.length} asset files`); return new Promise((resolve, reject) => { - const copyNext = (error?: any) => { + const copyNext = (error?: NodeJS.ErrnoException) => { if (error) { reject(error); return; @@ -65,9 +68,11 @@ function copyAll(filesToCopy: CopiedFiles) { logger.info('Done copying assets'); resolve(); } else { - const src = queue.shift() || ''; - const dest = filesToCopy[src]; - copy(src, dest, copyNext); + const src = queue.shift(); + if (src) { + const dest = filesToCopy[src]; + copy(src, dest, copyNext); + } } }; copyNext(); From 464dcac4045622e566bce820d4076be5bbaa6f73 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 12:27:53 +0100 Subject: [PATCH 04/12] Supress eslint import/default error --- packages/cli/src/commands/bundle/buildBundle.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index 95e671cab..5ffbd86d2 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. * */ +/* eslint-disable import/namespace, import/default */ // @ts-ignore - no typed definition for the package import Server from 'metro/src/Server'; @@ -23,7 +24,7 @@ interface RequestOptions { sourceMapUrl: string; dev: boolean; minify: boolean; - platform: string; + platform: string | undefined; } export interface AssetData { @@ -44,7 +45,6 @@ async function buildBundle( ctx: Config, output: outputBundle, ) { - const platform: string = args.platform || ''; const config = await loadMetroConfig(ctx, { maxWorkers: args.maxWorkers, resetCache: args.resetCache, @@ -83,7 +83,7 @@ async function buildBundle( sourceMapUrl, dev: args.dev, minify: args.minify !== undefined ? args.minify : !args.dev, - platform, + platform: args.platform, }; const server = new Server(config); @@ -101,7 +101,8 @@ async function buildBundle( }); // When we're done saving bundle output and the assets, we're done. - return await saveAssets(outputAssets, platform, args.assetsDest); + // @ts-ignore + return await saveAssets(outputAssets, args.platform, args.assetsDest); } finally { server.end(); } From df18b51031dde032118a45c1911a377db58a4024 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 16:13:35 +0100 Subject: [PATCH 05/12] disable eslint for concerned line --- packages/cli/src/commands/bundle/buildBundle.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index 5ffbd86d2..a84a9838f 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. * */ -/* eslint-disable import/namespace, import/default */ // @ts-ignore - no typed definition for the package import Server from 'metro/src/Server'; From 74d0d16f9255473be3cf0e2d6af71dd3e3aa4037 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 16:25:31 +0100 Subject: [PATCH 06/12] merge imports to one line --- packages/cli/src/commands/bundle/buildBundle.ts | 1 - packages/cli/src/commands/bundle/bundle.ts | 3 +-- packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts | 4 +--- packages/cli/src/commands/bundle/ramBundle.ts | 3 +-- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index a84a9838f..28132d593 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -100,7 +100,6 @@ async function buildBundle( }); // When we're done saving bundle output and the assets, we're done. - // @ts-ignore return await saveAssets(outputAssets, args.platform, args.assetsDest); } finally { server.end(); diff --git a/packages/cli/src/commands/bundle/bundle.ts b/packages/cli/src/commands/bundle/bundle.ts index e74c447f8..e5b2a8864 100644 --- a/packages/cli/src/commands/bundle/bundle.ts +++ b/packages/cli/src/commands/bundle/bundle.ts @@ -6,9 +6,8 @@ * */ import {Config} from '@react-native-community/cli-types'; -import {CommandLineArgs} from './bundleCommandLineArgs'; import buildBundle from './buildBundle'; -import bundleCommandLineArgs from './bundleCommandLineArgs'; +import bundleCommandLineArgs, {CommandLineArgs} from './bundleCommandLineArgs'; /** * Builds the bundle starting to look for dependencies at the given entry path. diff --git a/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts b/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts index adafaa01f..1c3e01326 100644 --- a/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts +++ b/packages/cli/src/commands/bundle/getAssetDestPathAndroid.ts @@ -7,9 +7,7 @@ */ import path from 'path'; -import {PackagerAsset} from './assetPathUtils'; - -import assetPathUtils from './assetPathUtils'; +import assetPathUtils, {PackagerAsset} from './assetPathUtils'; function getAssetDestPathAndroid(asset: PackagerAsset, scale: number): string { const androidFolder = assetPathUtils.getAndroidResourceFolderName( diff --git a/packages/cli/src/commands/bundle/ramBundle.ts b/packages/cli/src/commands/bundle/ramBundle.ts index 31946a093..0f91f24d8 100644 --- a/packages/cli/src/commands/bundle/ramBundle.ts +++ b/packages/cli/src/commands/bundle/ramBundle.ts @@ -7,9 +7,8 @@ */ // @ts-ignore - no typed definition for the package import outputUnbundle from 'metro/src/shared/output/RamBundle'; -import {CommandLineArgs} from './bundleCommandLineArgs'; import {withOutput as bundleWithOutput} from './bundle'; -import bundleCommandLineArgs from './bundleCommandLineArgs'; +import bundleCommandLineArgs, {CommandLineArgs} from './bundleCommandLineArgs'; import {Config} from '@react-native-community/cli-types'; /** From 82d713638a2b5f39a706fc127a9ed5cea9d75d28 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 16:57:00 +0100 Subject: [PATCH 07/12] set ios as the default platform --- packages/cli/src/commands/bundle/buildBundle.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index 28132d593..4feaad227 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -100,7 +100,12 @@ async function buildBundle( }); // When we're done saving bundle output and the assets, we're done. - return await saveAssets(outputAssets, args.platform, args.assetsDest); + // ios is set as the default platform see bundleCommandLineArgs.ts + return await saveAssets( + outputAssets, + args.platform || 'ios', + args.assetsDest, + ); } finally { server.end(); } From c7a1fa164bb14567c585a1b80199e009ef86f154 Mon Sep 17 00:00:00 2001 From: quadriphobs1 Date: Tue, 10 Sep 2019 17:34:24 +0100 Subject: [PATCH 08/12] make platform required --- packages/cli/src/commands/bundle/buildBundle.ts | 7 +------ packages/cli/src/commands/bundle/bundleCommandLineArgs.ts | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index 4feaad227..28132d593 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -100,12 +100,7 @@ async function buildBundle( }); // When we're done saving bundle output and the assets, we're done. - // ios is set as the default platform see bundleCommandLineArgs.ts - return await saveAssets( - outputAssets, - args.platform || 'ios', - args.assetsDest, - ); + return await saveAssets(outputAssets, args.platform, args.assetsDest); } finally { server.end(); } diff --git a/packages/cli/src/commands/bundle/bundleCommandLineArgs.ts b/packages/cli/src/commands/bundle/bundleCommandLineArgs.ts index 1c882d557..38b80f91d 100644 --- a/packages/cli/src/commands/bundle/bundleCommandLineArgs.ts +++ b/packages/cli/src/commands/bundle/bundleCommandLineArgs.ts @@ -16,7 +16,7 @@ export interface CommandLineArgs { transformer?: string; minify?: boolean; config?: string; - platform?: string; + platform: string; dev: boolean; bundleOutput: string; bundleEncoding?: string; From 1f78eeba5b7abfc68c9e9af3352dbfa2e4108b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 11 Sep 2019 10:42:58 +0200 Subject: [PATCH 09/12] type adjustments --- packages/cli/src/commands/bundle/buildBundle.ts | 6 +++--- packages/cli/src/tools/loadMetroConfig.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index 28132d593..a2b472890 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -20,7 +20,7 @@ import {logger} from '@react-native-community/cli-tools'; interface RequestOptions { entryFile: string; - sourceMapUrl: string; + sourceMapUrl: string | undefined; dev: boolean; minify: boolean; platform: string | undefined; @@ -59,7 +59,7 @@ async function buildBundle( logger.info( `Available platforms are: ${config.resolver.platforms - .map((x: string) => `"${chalk.bold(x)}"`) + .map(x => `"${chalk.bold(x)}"`) .join( ', ', )}. If you are trying to bundle for an out-of-tree platform, it may not be installed.`, @@ -72,7 +72,7 @@ async function buildBundle( // have other choice than defining it as an env variable here. process.env.NODE_ENV = args.dev ? 'development' : 'production'; - let sourceMapUrl: string = args.sourcemapOutput || ''; + let sourceMapUrl = args.sourcemapOutput; if (sourceMapUrl && !args.sourcemapUseAbsolutePath) { sourceMapUrl = path.basename(sourceMapUrl); } diff --git a/packages/cli/src/tools/loadMetroConfig.ts b/packages/cli/src/tools/loadMetroConfig.ts index f13ac58c0..b9c3426c2 100644 --- a/packages/cli/src/tools/loadMetroConfig.ts +++ b/packages/cli/src/tools/loadMetroConfig.ts @@ -32,7 +32,7 @@ const INTERNAL_CALLSITES_REGEX = new RegExp( ].join('|'), ); -export interface DefaultConfigOption { +export interface MetroConfig { resolver: { resolverMainFields: string[]; blacklistRE: RegExp; @@ -64,7 +64,7 @@ export interface DefaultConfigOption { * @todo(grabbou): As a separate PR, haste.platforms should be added before "native". * Otherwise, a.native.js will not load on Windows or other platforms */ -export const getDefaultConfig = (ctx: Config): DefaultConfigOption => { +export const getDefaultConfig = (ctx: Config): MetroConfig => { const hasteImplPath = path.join(ctx.reactNativePath, 'jest/hasteImpl.js'); return { resolver: { @@ -125,7 +125,10 @@ export interface ConfigOptionsT { * * This allows the CLI to always overwrite the file settings. */ -export default function load(ctx: Config, options?: ConfigOptionsT) { +export default function load( + ctx: Config, + options?: ConfigOptionsT, +): Promise { const defaultConfig = getDefaultConfig(ctx); if (options && options.reporter) { /** From 9d2c052cddb2728c2f3bca0624342890f9722300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 11 Sep 2019 10:46:16 +0200 Subject: [PATCH 10/12] fix default outputBundle regression --- packages/cli/src/commands/bundle/buildBundle.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/bundle/buildBundle.ts b/packages/cli/src/commands/bundle/buildBundle.ts index a2b472890..72abc912e 100644 --- a/packages/cli/src/commands/bundle/buildBundle.ts +++ b/packages/cli/src/commands/bundle/buildBundle.ts @@ -42,7 +42,7 @@ export interface AssetData { async function buildBundle( args: CommandLineArgs, ctx: Config, - output: outputBundle, + output: typeof outputBundle = outputBundle, ) { const config = await loadMetroConfig(ctx, { maxWorkers: args.maxWorkers, From 30d8bc2aba05b84871711eb302812c794da6f959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 11 Sep 2019 10:52:52 +0200 Subject: [PATCH 11/12] moar comments --- packages/cli/src/commands/bundle/bundle.ts | 2 +- packages/cli/src/commands/bundle/saveAssets.ts | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/commands/bundle/bundle.ts b/packages/cli/src/commands/bundle/bundle.ts index e5b2a8864..95caa81bc 100644 --- a/packages/cli/src/commands/bundle/bundle.ts +++ b/packages/cli/src/commands/bundle/bundle.ts @@ -16,7 +16,7 @@ function bundleWithOutput( _: Array, config: Config, args: CommandLineArgs, - output: any, + output: any, // untyped metro/src/shared/output/bundle or metro/src/shared/output/RamBundle ) { return buildBundle(args, config, output); } diff --git a/packages/cli/src/commands/bundle/saveAssets.ts b/packages/cli/src/commands/bundle/saveAssets.ts index 56e79772a..d9549209b 100644 --- a/packages/cli/src/commands/bundle/saveAssets.ts +++ b/packages/cli/src/commands/bundle/saveAssets.ts @@ -68,11 +68,10 @@ function copyAll(filesToCopy: CopiedFiles) { logger.info('Done copying assets'); resolve(); } else { - const src = queue.shift(); - if (src) { - const dest = filesToCopy[src]; - copy(src, dest, copyNext); - } + // queue.length === 0 is checked in previous branch, so this is string + const src = queue.shift() as string; + const dest = filesToCopy[src]; + copy(src, dest, copyNext); } }; copyNext(); From cb110d7d1527ae3078d1d3a2aa0c16edc93f709f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 11 Sep 2019 11:12:11 +0200 Subject: [PATCH 12/12] restart CI