1import { RawExpoModuleConfig, SupportedPlatform } from './types';
2
3/**
4 * A class that wraps the raw config (`expo-module.json` or `unimodule.json`).
5 */
6export class ExpoModuleConfig {
7  constructor(readonly rawConfig: RawExpoModuleConfig) {}
8
9  /**
10   * Whether the module supports given platform.
11   */
12  supportsPlatform(platform: SupportedPlatform): boolean {
13    return this.rawConfig.platforms?.includes(platform) ?? false;
14  }
15
16  /**
17   * Returns a list of names of Swift native modules classes to put to the generated modules provider file.
18   */
19  iosModulesClassNames() {
20    return this.rawConfig.ios?.modulesClassNames ?? [];
21  }
22
23  /**
24   * Returns a list of names of Swift classes that receives AppDelegate life-cycle events.
25   */
26  iosAppDelegateSubscribers(): string[] {
27    return this.rawConfig.ios?.appDelegateSubscribers ?? [];
28  }
29
30  /**
31   * Returns a list of names of Swift classes that implement `ExpoReactDelegateHandler`.
32   */
33  iosReactDelegateHandlers(): string[] {
34    return this.rawConfig.ios?.reactDelegateHandlers ?? [];
35  }
36
37  /**
38   * Returns a podspec path defined by the module author.
39   */
40  iosPodspecPath(): string | undefined {
41    return this.rawConfig.ios?.podspecPath;
42  }
43
44  /**
45   * Returns a list of names of Kotlin native modules classes to put to the generated package provider file.
46   */
47  androidModulesClassNames() {
48    return this.rawConfig.android?.modulesClassNames ?? [];
49  }
50
51  /**
52   * Returns serializable raw config.
53   */
54  toJSON(): RawExpoModuleConfig {
55    return this.rawConfig;
56  }
57}
58
59/**
60 * Reads the config at given path and returns the config wrapped by `ExpoModuleConfig` class.
61 */
62export function requireAndResolveExpoModuleConfig(path: string): ExpoModuleConfig {
63  // TODO: Validate the raw config against a schema.
64  // TODO: Support for `*.js` files, not only static `*.json`.
65  return new ExpoModuleConfig(require(path) as RawExpoModuleConfig);
66}
67