1import { ConfigPlugin } from '../Plugin.types';
2import { addHistoryItem, getHistoryItem, PluginHistoryItem } from '../utils/history';
3
4/**
5 * Prevents the same plugin from being run twice.
6 * Used for migrating from unversioned expo config plugins to versioned plugins.
7 *
8 * @param config
9 * @param name
10 */
11export const withRunOnce: ConfigPlugin<{
12  plugin: ConfigPlugin<void>;
13  name: PluginHistoryItem['name'];
14  version?: PluginHistoryItem['version'];
15}> = (config, { plugin, name, version }) => {
16  // Detect if a plugin has already been run on this config.
17  if (getHistoryItem(config, name)) {
18    return config;
19  }
20
21  // Push the history item so duplicates cannot be run.
22  config = addHistoryItem(config, { name, version });
23
24  return plugin(config);
25};
26
27/**
28 * Helper method for creating mods from existing config functions.
29 *
30 * @param action
31 */
32export function createRunOncePlugin<T>(
33  plugin: ConfigPlugin<T>,
34  name: string,
35  version?: string
36): ConfigPlugin<T> {
37  return (config, props) => {
38    return withRunOnce(config, { plugin: (config) => plugin(config, props), name, version });
39  };
40}
41