1import { RawExpoModuleConfig, SupportedPlatform } from './types';
2
3function arrayize<T>(value: T[] | T | undefined): T[] {
4  if (Array.isArray(value)) {
5    return value;
6  }
7  return value != null ? [value] : [];
8}
9
10/**
11 * A class that wraps the raw config (`expo-module.json` or `unimodule.json`).
12 */
13export class ExpoModuleConfig {
14  constructor(readonly rawConfig: RawExpoModuleConfig) {}
15
16  /**
17   * Whether the module supports given platform.
18   */
19  supportsPlatform(platform: SupportedPlatform): boolean {
20    return this.rawConfig.platforms?.includes(platform) ?? false;
21  }
22
23  /**
24   * Returns a list of names of Swift native modules classes to put to the generated modules provider file.
25   */
26  iosModules() {
27    const iosConfig = this.rawConfig.ios;
28
29    // `modulesClassNames` is a legacy name for the same config.
30    return iosConfig?.modules ?? iosConfig?.modulesClassNames ?? [];
31  }
32
33  /**
34   * Returns a list of names of Swift classes that receives AppDelegate life-cycle events.
35   */
36  iosAppDelegateSubscribers(): string[] {
37    return this.rawConfig.ios?.appDelegateSubscribers ?? [];
38  }
39
40  /**
41   * Returns a list of names of Swift classes that implement `ExpoReactDelegateHandler`.
42   */
43  iosReactDelegateHandlers(): string[] {
44    return this.rawConfig.ios?.reactDelegateHandlers ?? [];
45  }
46
47  /**
48   * Returns podspec paths defined by the module author.
49   */
50  iosPodspecPaths(): string[] {
51    return arrayize(this.rawConfig.ios?.podspecPath);
52  }
53
54  /**
55   * Returns the product module names, if defined by the module author.
56   */
57  iosSwiftModuleNames(): string[] {
58    return arrayize(this.rawConfig.ios?.swiftModuleName);
59  }
60
61  /**
62   * Returns a list of names of Kotlin native modules classes to put to the generated package provider file.
63   */
64  androidModules() {
65    const androidConfig = this.rawConfig.android;
66
67    // `modulesClassNames` is a legacy name for the same config.
68    return androidConfig?.modules ?? androidConfig?.modulesClassNames ?? [];
69  }
70
71  /**
72   * Returns build.gradle file paths defined by the module author.
73   */
74  androidGradlePaths(): string[] {
75    return arrayize(this.rawConfig.android?.gradlePath ?? []);
76  }
77
78  /**
79   * Returns serializable raw config.
80   */
81  toJSON(): RawExpoModuleConfig {
82    return this.rawConfig;
83  }
84}
85
86/**
87 * Reads the config at given path and returns the config wrapped by `ExpoModuleConfig` class.
88 */
89export function requireAndResolveExpoModuleConfig(path: string): ExpoModuleConfig {
90  // TODO: Validate the raw config against a schema.
91  // TODO: Support for `*.js` files, not only static `*.json`.
92  return new ExpoModuleConfig(require(path) as RawExpoModuleConfig);
93}
94