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

[config] add getPublicExpoConfig method and tests #2863

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions packages/config/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Platform,
ProjectConfig,
ProjectTarget,
PublicExpoConfig,
WriteConfigOptions,
} from './Config.types';
import { ConfigError } from './Errors';
Expand Down Expand Up @@ -149,6 +150,28 @@ export function getConfig(projectRoot: string, options: GetConfigOptions = {}):
return fillAndReturnConfig(staticConfig || {}, null);
}

/**
* Evaluate and return the public-facing portions of the Expo config for a project.
* The resulting config should be suitable for hosting or embedding in a publicly readable location.
*
* @param projectRoot the root folder containing all of your application code
* @param options enforce criteria for a project config
*/
export function getPublicExpoConfig(
projectRoot: string,
options: GetConfigOptions = {}
EvanBacon marked this conversation as resolved.
Show resolved Hide resolved
): PublicExpoConfig {
const { exp } = getConfig(projectRoot, options);
delete exp.hooks;
if (exp.ios?.config) {
delete exp.ios.config;
}
if (exp.android?.config) {
delete exp.android.config;
}
return exp;
}

export function getPackageJson(
projectRoot: string,
config: Partial<Pick<ExpoConfig, 'nodeModulesPath'>> = {}
Expand Down
5 changes: 5 additions & 0 deletions packages/config/src/Config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export type HookArguments = {
log: (msg: any) => void;
};

export type PublicExpoConfig = Omit<ExpoConfig, 'hooks' | 'ios' | 'android'> & {
ios?: Omit<ExpoConfig['ios'], 'config'>;
android?: Omit<ExpoConfig['android'], 'config'>;
};

export type ExpoAppManifest = ExpoConfig & {
sdkVersion: string;
bundledAssets?: string[];
Expand Down
84 changes: 83 additions & 1 deletion packages/config/src/__tests__/Config-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { vol } from 'memfs';

import { getConfig, getProjectConfigDescription, readConfigJson } from '../Config';
import {
getConfig,
getProjectConfigDescription,
getPublicExpoConfig,
readConfigJson,
} from '../Config';

jest.mock('fs');
jest.mock('resolve-from');
Expand Down Expand Up @@ -37,6 +42,83 @@ describe(`getProjectConfigDescription`, () => {
});
});

describe('getPublicExpoConfig', () => {
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 = getPublicExpoConfig('/private-data');

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 = getPublicExpoConfig('/no-private-data');
expect(exp).toMatchObject(appJsonNoPrivateData);
});
});

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