1import Ajv, { JSONSchemaType } from 'ajv';
2
3/**
4 * Type representing base dev launcher configuration.
5 */
6export type PluginConfigType = PluginConfigOptionsByPlatform & PluginConfigOptions;
7
8/**
9 * Type representing available configuration for each platform.
10 */
11export type PluginConfigOptionsByPlatform = {
12  /**
13   * Type representing available configuration for Android dev launcher.
14   * @platform android
15   */
16  android?: PluginConfigOptions;
17  /**
18   * Type representing available configuration for iOS dev launcher.
19   * @platform ios
20   */
21  ios?: PluginConfigOptions;
22};
23
24/**
25 * Type representing available configuration for dev launcher.
26 */
27export type PluginConfigOptions = {
28  /**
29   * Attempts to launch directly into a previously opened project. If unable to connect,
30   * fall back to the launcher screen.
31   */
32  tryToLaunchLastOpenedBundle?: boolean;
33};
34
35const schema: JSONSchemaType<PluginConfigType> = {
36  type: 'object',
37  properties: {
38    tryToLaunchLastOpenedBundle: { type: 'boolean', nullable: true },
39    android: {
40      type: 'object',
41      properties: {
42        tryToLaunchLastOpenedBundle: { type: 'boolean', nullable: true },
43      },
44      nullable: true,
45    },
46    ios: {
47      type: 'object',
48      properties: {
49        tryToLaunchLastOpenedBundle: { type: 'boolean', nullable: true },
50      },
51      nullable: true,
52    },
53  },
54};
55
56/**
57 * @ignore
58 */
59export function validateConfig<T>(config: T): PluginConfigType {
60  const validate = new Ajv({ allowUnionTypes: true }).compile(schema);
61  if (!validate(config)) {
62    throw new Error('Invalid expo-dev-launcher config: ' + JSON.stringify(validate.errors));
63  }
64
65  return config;
66}
67