Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/api/generateOverrides.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { BuildFlags } from "./BuildFlags";
import { readConfig } from "./readConfig";

export const resolveEnabledFlagNames = async ({
flagsToEnable,
flagsToDisable,
}: {
flagsToEnable?: Set<string>;
flagsToDisable?: Set<string>;
}): Promise<string[]> => {
const { flags: defaultFlags } = await readConfig();
const flags = new BuildFlags(defaultFlags);
if (flagsToEnable) {
flags.enable(flagsToEnable);
}
if (flagsToDisable) {
flags.disable(flagsToDisable);
}
return Object.entries(flags.flags)
.filter(([_, config]) => config.value)
.map(([name]) => name);
};

export const generateOverrides = async ({
flagsToEnable,
flagsToDisable,
Expand Down
12 changes: 10 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { generateOverrides } from "./generateOverrides";
import {
generateOverrides,
resolveEnabledFlagNames,
} from "./generateOverrides";
import { resolveFlagsToInvert } from "./resolveFlagsToInvert";
import { readConfig } from "./readConfig";

export { generateOverrides, resolveFlagsToInvert, readConfig };
export {
generateOverrides,
resolveEnabledFlagNames,
resolveFlagsToInvert,
readConfig,
};
61 changes: 46 additions & 15 deletions src/config-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,42 @@ import {
withInfoPlist,
} from "@expo/config-plugins";
import { ExpoConfig } from "@expo/config-types";
import { generateOverrides, resolveFlagsToInvert } from "../api";
import {
generateOverrides,
resolveEnabledFlagNames,
resolveFlagsToInvert,
} from "../api";
import pkg from "../../package.json";
import { withFlaggedAutolinking } from "./withFlaggedAutolinking";
import { mergeSets } from "../api/mergeSets";

const withAndroidBuildFlags: ConfigPlugin<{ flags: string[] }> = (
type NativeFlagPluginProps = { envFlags: string[]; expoConfig: ExpoConfig };

let cachedResolvedFlags: string[] | null = null;
const resolveAllEnabledFlags = async (
envFlags: string[],
expoConfig: ExpoConfig
): Promise<string[]> => {
if (cachedResolvedFlags) {
return cachedResolvedFlags;
}
let flagsToEnable = new Set(envFlags);
const invertable = await resolveFlagsToInvert(expoConfig);
if (invertable.flagsToEnable.size > 0) {
flagsToEnable = mergeSets(flagsToEnable, invertable.flagsToEnable);
}
cachedResolvedFlags = await resolveEnabledFlagNames({
flagsToEnable,
flagsToDisable: invertable.flagsToDisable,
});
return cachedResolvedFlags;
};

const withAndroidBuildFlags: ConfigPlugin<NativeFlagPluginProps> = (
config,
props
) => {
return withAndroidManifest(config, (config) => {
return withAndroidManifest(config, async (config) => {
if (!config.modResults) {
throw new Error("AndroidManifest.xml not found in the project");
}
Expand All @@ -26,13 +52,18 @@ const withAndroidBuildFlags: ConfigPlugin<{ flags: string[] }> = (
throw new Error("Application node not found in AndroidManifest.xml");
}

const resolvedFlags = await resolveAllEnabledFlags(
props.envFlags,
props.expoConfig
);

const meta = mainApplication["meta-data"];
mainApplication["meta-data"] = [
...(meta ?? []),
{
$: {
"android:name": "EXBuildFlags",
"android:value": props.flags.join(","),
"android:value": resolvedFlags.join(","),
},
},
];
Expand All @@ -41,12 +72,16 @@ const withAndroidBuildFlags: ConfigPlugin<{ flags: string[] }> = (
});
};

const withAppleBuildFlags: ConfigPlugin<{ flags: string[] }> = (
const withAppleBuildFlags: ConfigPlugin<NativeFlagPluginProps> = (
config,
props
) => {
return withInfoPlist(config, (config) => {
config.modResults.EXBuildFlags = props.flags;
return withInfoPlist(config, async (config) => {
const resolvedFlags = await resolveAllEnabledFlags(
props.envFlags,
props.expoConfig
);
config.modResults.EXBuildFlags = resolvedFlags;
return config;
});
};
Expand Down Expand Up @@ -124,15 +159,11 @@ type ConfigPluginProps =
type WithBuildFlagsProps = { skipBundleOverride?: boolean; flags: string[] };

const withBuildFlags: ConfigPlugin<WithBuildFlagsProps> = (config, props) => {
const flags = props.flags;
if (!flags.length) {
return props?.skipBundleOverride
? config
: withBundleFlags(config, { flags });
}
const { flags } = props;
const nativeProps = { envFlags: flags, expoConfig: config };

const nativeConfig = withAndroidBuildFlags(config, { flags });
const mergedNativeConfig = withAppleBuildFlags(nativeConfig, { flags });
const nativeConfig = withAndroidBuildFlags(config, nativeProps);
const mergedNativeConfig = withAppleBuildFlags(nativeConfig, nativeProps);
if (props?.skipBundleOverride) {
return mergedNativeConfig;
}
Expand Down
4 changes: 3 additions & 1 deletion test/test-config-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export const BuildFlags = {
`;

const expectedManifestTag =
'<meta-data android:name="EXBuildFlags" android:value="secretFeature,newFeature"/>';
'<meta-data android:name="EXBuildFlags" android:value="secretFeature,publishedFeatured,newFeature,bundleIdScopedFeature"/>';

const expectedPlistFlagArray = `
<key>EXBuildFlags</key>
<array>
<string>secretFeature</string>
<string>publishedFeatured</string>
<string>newFeature</string>
<string>bundleIdScopedFeature</string>
</array>
`;

Expand Down