This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
currentFullName
to the public config (#3376)
* Added `currentFullName` to the public config * Update User.ts
- Loading branch information
Showing
8 changed files
with
176 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const os = jest.requireActual('os'); | ||
|
||
os.homedir = jest.fn(() => '/home'); | ||
|
||
module.exports = os; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ensureDir } from 'fs-extra'; | ||
|
||
import { getAccountUsername } from '../getCurrentFullName'; | ||
import { getExpoHomeDirectory, getUserState } from '../getUserState'; | ||
|
||
jest.mock('os'); | ||
jest.mock('fs'); | ||
|
||
describe(getAccountUsername, () => { | ||
beforeEach(() => { | ||
delete process.env.EXPO_CLI_USERNAME; | ||
delete process.env.EAS_BUILD_USERNAME; | ||
}); | ||
|
||
it(`gets the account name from EXPO_CLI_USERNAME`, () => { | ||
process.env.EXPO_CLI_USERNAME = 'expo-cli-username'; | ||
process.env.EAS_BUILD_USERNAME = 'eas-build-username'; | ||
expect(getAccountUsername()).toBe('expo-cli-username'); | ||
}); | ||
it(`gets the account name from EAS_BUILD_USERNAME`, () => { | ||
process.env.EAS_BUILD_USERNAME = 'eas-build-username'; | ||
expect(getAccountUsername()).toBe('eas-build-username'); | ||
}); | ||
it(`gets the account name from owner`, () => { | ||
process.env.EXPO_CLI_USERNAME = 'expo-cli-username'; | ||
process.env.EAS_BUILD_USERNAME = 'eas-build-username'; | ||
expect(getAccountUsername({ owner: 'owner-username' })).toBe('owner-username'); | ||
}); | ||
it(`gets the account name from owner 2`, () => { | ||
expect(getAccountUsername({ owner: 'owner-username' })).toBe('owner-username'); | ||
}); | ||
it(`uses anonymous name`, () => { | ||
// Ensure the test doesn't interact with the developer's state.json | ||
expect(getExpoHomeDirectory()).toBe('/home/.expo'); | ||
expect(getAccountUsername()).toBe('anonymous'); | ||
}); | ||
it(`uses previously authenticated username`, async () => { | ||
// Ensure the test doesn't interact with the developer's state.json | ||
expect(getExpoHomeDirectory()).toBe('/home/.expo'); | ||
// Ensure the dir exists | ||
await ensureDir(getExpoHomeDirectory()); | ||
// Set a username... | ||
await getUserState().setAsync('auth', { username: 'bacon-boi' }); | ||
// Check the username... | ||
expect(getAccountUsername()).toBe('bacon-boi'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { getExpoHomeDirectory } from '../getUserState'; | ||
|
||
jest.mock('os'); | ||
jest.mock('fs'); | ||
|
||
describe(getExpoHomeDirectory, () => { | ||
beforeEach(() => { | ||
delete process.env.__UNSAFE_EXPO_HOME_DIRECTORY; | ||
delete process.env.EXPO_STAGING; | ||
delete process.env.EXPO_LOCAL; | ||
}); | ||
|
||
it(`gets the default state directory`, () => { | ||
expect(getExpoHomeDirectory()).toBe('/home/.expo'); | ||
}); | ||
it(`gets the staging state directory`, () => { | ||
process.env.EXPO_STAGING = 'true'; | ||
expect(getExpoHomeDirectory()).toBe('/home/.expo-staging'); | ||
}); | ||
it(`gets the local state directory`, () => { | ||
process.env.EXPO_LOCAL = 'true'; | ||
expect(getExpoHomeDirectory()).toBe('/home/.expo-local'); | ||
}); | ||
it(`gets the custom state directory`, () => { | ||
process.env.__UNSAFE_EXPO_HOME_DIRECTORY = '/foobar/yolo'; | ||
expect(getExpoHomeDirectory()).toBe('/foobar/yolo'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ExpoConfig } from '@expo/config-types'; | ||
|
||
import { getUserState } from './getUserState'; | ||
|
||
const ANONYMOUS_USERNAME = 'anonymous'; | ||
|
||
/** | ||
* Used in expo-constants to generate the `id` property statically for an app in custom managed workflow. | ||
* This `id` is used for legacy Expo services AuthSession proxy and Expo notifications device ID. | ||
* | ||
* @param manifest | ||
* @returns | ||
*/ | ||
export function getCurrentFullName(manifest: Pick<ExpoConfig, 'owner' | 'slug'>): string { | ||
const username = getAccountUsername(manifest); | ||
return `@${username}/${manifest.slug}`; | ||
} | ||
|
||
export function getAccountUsername(manifest: Pick<ExpoConfig, 'owner'> = {}): string { | ||
// TODO: Must match what's generated in Expo Go. | ||
const username = | ||
manifest.owner || process.env.EXPO_CLI_USERNAME || process.env.EAS_BUILD_USERNAME; | ||
if (username) { | ||
return username; | ||
} | ||
// Statically get the username from the global user state. | ||
return getUserState().read().auth?.username || ANONYMOUS_USERNAME; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import JsonFile from '@expo/json-file'; | ||
import { boolish } from 'getenv'; | ||
import { homedir } from 'os'; | ||
import * as path from 'path'; | ||
|
||
export type UserSettingsData = { | ||
developmentCodeSigningId?: string; | ||
appleId?: string; | ||
accessToken?: string; | ||
auth?: UserData | null; | ||
ignoreBundledBinaries?: string[]; | ||
openDevToolsAtStartup?: boolean; | ||
PATH?: string; | ||
sendTo?: string; | ||
uuid?: string; | ||
}; | ||
|
||
export type UserData = { | ||
appleId?: string; | ||
userId?: string; | ||
username?: string; | ||
currentConnection?: ConnectionType; | ||
sessionSecret?: string; | ||
}; | ||
|
||
export type ConnectionType = | ||
| 'Access-Token-Authentication' | ||
| 'Username-Password-Authentication' | ||
| 'facebook' | ||
| 'google-oauth2' | ||
| 'github'; | ||
|
||
// The ~/.expo directory is used to store authentication sessions, | ||
// which are shared between EAS CLI and Expo CLI. | ||
export function getExpoHomeDirectory() { | ||
const home = homedir(); | ||
|
||
if (process.env.__UNSAFE_EXPO_HOME_DIRECTORY) { | ||
return process.env.__UNSAFE_EXPO_HOME_DIRECTORY; | ||
} else if (boolish('EXPO_STAGING', false)) { | ||
return path.join(home, '.expo-staging'); | ||
} else if (boolish('EXPO_LOCAL', false)) { | ||
return path.join(home, '.expo-local'); | ||
} | ||
return path.join(home, '.expo'); | ||
} | ||
|
||
export function getUserStatePath() { | ||
return path.join(getExpoHomeDirectory(), 'state.json'); | ||
} | ||
|
||
export function getUserState() { | ||
return new JsonFile<UserSettingsData>(getUserStatePath(), { | ||
jsonParseErrorDefault: {}, | ||
// This will ensure that an error isn't thrown if the file doesn't exist. | ||
cantReadFileDefault: {}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters