1import { getConfig, Platform, ProjectTarget } from '@expo/config';
2
3import * as Log from '../log';
4import { resolveEntryPoint } from '../start/server/middleware/resolveEntryPoint';
5import { bundleAsync, BundleOutput } from './fork-bundleAsync';
6
7export type PublishOptions = {
8  releaseChannel?: string;
9  target?: ProjectTarget;
10  resetCache?: boolean;
11  maxWorkers?: number;
12};
13
14// TODO: Reduce layers of indirection
15export async function createBundlesAsync(
16  projectRoot: string,
17  publishOptions: PublishOptions = {},
18  bundleOptions: { platforms: Platform[]; dev?: boolean; useDevServer: boolean }
19): Promise<Partial<Record<Platform, BundleOutput>>> {
20  const { exp } = getConfig(projectRoot, { skipSDKVersionRequirement: true });
21
22  const bundles = await bundleAsync(
23    projectRoot,
24    exp,
25    {
26      // If not legacy, ignore the target option to prevent warnings from being thrown.
27      resetCache: publishOptions.resetCache,
28      maxWorkers: publishOptions.maxWorkers,
29      logger: {
30        info(tag: unknown, message: string) {
31          Log.log(message);
32        },
33        error(tag: unknown, message: string) {
34          Log.error(message);
35        },
36      } as any,
37      quiet: false,
38    },
39    bundleOptions.platforms.map((platform: Platform) => ({
40      platform,
41      entryPoint: resolveEntryPoint(projectRoot, platform),
42      dev: bundleOptions.dev,
43    }))
44  );
45
46  // { ios: bundle, android: bundle }
47  return bundleOptions.platforms.reduce<Partial<Record<Platform, BundleOutput>>>(
48    (prev, platform, index) => ({
49      ...prev,
50      [platform]: bundles[index],
51    }),
52    {}
53  );
54}
55