1*fe0fd9e3STomasz Sapeta// NOTE: Keep these types in-sync with expo-modules-autolinking
2*fe0fd9e3STomasz Sapeta
3*fe0fd9e3STomasz Sapetaexport type SearchResults = {
4*fe0fd9e3STomasz Sapeta  [moduleName: string]: object;
5*fe0fd9e3STomasz Sapeta};
6*fe0fd9e3STomasz Sapeta
7*fe0fd9e3STomasz Sapetaexport type SearchOptions = {
8*fe0fd9e3STomasz Sapeta  searchPaths: string[];
9*fe0fd9e3STomasz Sapeta  platform: 'ios' | 'android' | 'web';
10*fe0fd9e3STomasz Sapeta  silent?: boolean;
11*fe0fd9e3STomasz Sapeta};
12*fe0fd9e3STomasz Sapeta
13*fe0fd9e3STomasz Sapetaexport type AutolinkingModule = {
14*fe0fd9e3STomasz Sapeta  resolveSearchPathsAsync(searchPaths: string[] | null, cwd: string): Promise<string[]>;
15*fe0fd9e3STomasz Sapeta  findModulesAsync(providedOptions: SearchOptions): Promise<SearchResults>;
16*fe0fd9e3STomasz Sapeta};
17*fe0fd9e3STomasz Sapeta
18*fe0fd9e3STomasz Sapeta/**
19*fe0fd9e3STomasz Sapeta * Imports the `expo-modules-autolinking` package installed in the project at the given path.
20*fe0fd9e3STomasz Sapeta */
21*fe0fd9e3STomasz Sapetaexport function importExpoModulesAutolinking(projectRoot: string): AutolinkingModule {
22*fe0fd9e3STomasz Sapeta  const autolinking = tryRequireExpoModulesAutolinking(projectRoot);
23*fe0fd9e3STomasz Sapeta  assertAutolinkingCompatibility(autolinking);
24*fe0fd9e3STomasz Sapeta  return autolinking;
25*fe0fd9e3STomasz Sapeta}
26*fe0fd9e3STomasz Sapeta
27*fe0fd9e3STomasz Sapetafunction tryRequireExpoModulesAutolinking(projectRoot: string): AutolinkingModule {
28*fe0fd9e3STomasz Sapeta  try {
29*fe0fd9e3STomasz Sapeta    const resolvedAutolinkingPath = require.resolve('expo-modules-autolinking/build/autolinking', {
30*fe0fd9e3STomasz Sapeta      paths: [projectRoot],
31*fe0fd9e3STomasz Sapeta    });
32*fe0fd9e3STomasz Sapeta    return require(resolvedAutolinkingPath);
33*fe0fd9e3STomasz Sapeta  } catch {
34*fe0fd9e3STomasz Sapeta    throw new Error(
35*fe0fd9e3STomasz Sapeta      "Cannot find 'expo-modules-autolinking' package in your project, make sure that you have 'expo' package installed"
36*fe0fd9e3STomasz Sapeta    );
37*fe0fd9e3STomasz Sapeta  }
38*fe0fd9e3STomasz Sapeta}
39*fe0fd9e3STomasz Sapeta
40*fe0fd9e3STomasz Sapetafunction assertAutolinkingCompatibility(autolinking: AutolinkingModule): void {
41*fe0fd9e3STomasz Sapeta  if ('resolveSearchPathsAsync' in autolinking && 'findModulesAsync' in autolinking) {
42*fe0fd9e3STomasz Sapeta    return;
43*fe0fd9e3STomasz Sapeta  }
44*fe0fd9e3STomasz Sapeta  throw new Error(
45*fe0fd9e3STomasz Sapeta    "The 'expo-modules-autolinking' package has been found, but it seems to be incompatible with '@expo/prebuild-config'"
46*fe0fd9e3STomasz Sapeta  );
47*fe0fd9e3STomasz Sapeta}
48