Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
[config] add getPublicExpoConfig method and tests (#2863)
Browse files Browse the repository at this point in the history
  • Loading branch information
esamelson authored Nov 9, 2020
1 parent 38de671 commit b718000
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/config/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export function getConfigWithMods(projectRoot: string, options?: GetConfigOption
* If a function is exported from the `app.config.js` then a partial config will be passed as an argument.
* The partial config is composed from any existing app.json, and certain fields from the `package.json` like name and description.
*
* If options.isPublicConfig is true, the Expo config will include only public-facing options (omitting private keys).
* The resulting config should be suitable for hosting or embedding in a publicly readable location.
*
* **Example**
* ```js
Expand Down Expand Up @@ -108,7 +110,7 @@ export function getConfig(projectRoot: string, options: GetConfigOptions = {}):
);

function fillAndReturnConfig(config: SplitConfigs, dynamicConfigObjectType: string | null) {
return {
const configWithDefaultValues = {
...ensureConfigHasDefaultValues(
projectRoot,
config.expo,
Expand All @@ -121,6 +123,20 @@ export function getConfig(projectRoot: string, options: GetConfigOptions = {}):
dynamicConfigPath: paths.dynamicConfigPath,
staticConfigPath: paths.staticConfigPath,
};

if (options.isPublicConfig) {
if (configWithDefaultValues.exp.hooks) {
delete configWithDefaultValues.exp.hooks;
}
if (configWithDefaultValues.exp.ios?.config) {
delete configWithDefaultValues.exp.ios.config;
}
if (configWithDefaultValues.exp.android?.config) {
delete configWithDefaultValues.exp.android.config;
}
}

return configWithDefaultValues;
}

// Fill in the static config
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/Config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export type ConfigContext = {
};

export type GetConfigOptions = {
isPublicConfig?: boolean;
skipSDKVersionRequirement?: boolean;
strict?: boolean;
};
Expand Down
77 changes: 77 additions & 0 deletions packages/config/src/__tests__/Config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,83 @@ describe(`getProjectConfigDescription`, () => {
});
});

describe('getConfig public config', () => {
const appJsonWithPrivateData = {
name: 'testing 123',
version: '0.1.0',
slug: 'testing-123',
sdkVersion: '100.0.0',
hooks: {
postPublish: [],
},
android: {
config: {
googleMaps: {
apiKey: 'test-key',
},
},
versionCode: 1,
},
ios: {
config: {
googleMapsApiKey: 'test-key',
},
buildNumber: '1.0',
},
};

const appJsonNoPrivateData = {
name: 'testing 123',
version: '0.1.0',
slug: 'testing-123',
sdkVersion: '100.0.0',
ios: {
buildNumber: '1.0',
},
};

beforeAll(() => {
const packageJson = JSON.stringify(
{
name: 'testing123',
version: '0.1.0',
description: 'fake description',
main: 'index.js',
},
null,
2
);

vol.fromJSON({
'/private-data/package.json': packageJson,
'/private-data/app.json': JSON.stringify({ expo: appJsonWithPrivateData }),
'/no-private-data/package.json': packageJson,
'/no-private-data/app.json': JSON.stringify({ expo: appJsonNoPrivateData }),
});
});

afterAll(() => vol.reset());

it('removes only private data from the config', () => {
const { exp } = getConfig('/private-data', { isPublicConfig: true });

expect(exp.hooks).toBeUndefined();

expect(exp.ios).toBeDefined();
expect(exp.ios.buildNumber).toEqual(appJsonWithPrivateData.ios.buildNumber);
expect(exp.ios.config).toBeUndefined();

expect(exp.android).toBeDefined();
expect(exp.android.versionCode).toEqual(appJsonWithPrivateData.android.versionCode);
expect(exp.android.config).toBeUndefined();
});

it('does not remove properties from a config with no private data', () => {
const { exp } = getConfig('/no-private-data', { isPublicConfig: true });
expect(exp).toMatchObject(appJsonNoPrivateData);
});
});

describe('readConfigJson', () => {
describe('sdkVersion', () => {
beforeAll(() => {
Expand Down

0 comments on commit b718000

Please sign in to comment.