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  iosModules() {
20    const iosConfig = this.rawConfig.ios;
21
22    // `modulesClassNames` is a legacy name for the same config.
23    return iosConfig?.modules ?? iosConfig?.modulesClassNames ?? [];
24  }
25
26  /**
27   * Returns a list of names of Swift classes that receives AppDelegate life-cycle events.
28   */
29  iosAppDelegateSubscribers(): string[] {
30    return this.rawConfig.ios?.appDelegateSubscribers ?? [];
31  }
32
33  /**
34   * Returns a list of names of Swift classes that implement `ExpoReactDelegateHandler`.
35   */
36  iosReactDelegateHandlers(): string[] {
37    return this.rawConfig.ios?.reactDelegateHandlers ?? [];
38  }
39
40  /**
41   * Returns a podspec path defined by the module author.
42   */
43  iosPodspecPath(): string | undefined {
44    return this.rawConfig.ios?.podspecPath;
45  }
46
47  /**
48   * Returns a list of names of Kotlin native modules classes to put to the generated package provider file.
49   */
50  androidModules() {
51    const androidConfig = this.rawConfig.android;
52
53    // `modulesClassNames` is a legacy name for the same config.
54    return androidConfig?.modules ?? androidConfig?.modulesClassNames ?? [];
55  }
56
57  /**
58   * Returns serializable raw config.
59   */
60  toJSON(): RawExpoModuleConfig {
61    return this.rawConfig;
62  }
63}
64
65/**
66 * Reads the config at given path and returns the config wrapped by `ExpoModuleConfig` class.
67 */
68export function requireAndResolveExpoModuleConfig(path: string): ExpoModuleConfig {
69  // TODO: Validate the raw config against a schema.
70  // TODO: Support for `*.js` files, not only static `*.json`.
71  return new ExpoModuleConfig(require(path) as RawExpoModuleConfig);
72}
73