-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added --user flag to run-android command (#1869)
* feat: added userId to run-android command * Update packages/cli-platform-android/src/commands/runAndroid/listAndroidUsers.ts Co-authored-by: Szymon Rybczak <szymon.rybczak@gmail.com> * Update docs/commands.md Co-authored-by: homersimpsons <guillaume.alabre@gmail.com> * fix: code review fixes * chore: update docs * fix: ts and tests --------- Co-authored-by: Szymon Rybczak <szymon.rybczak@gmail.com> Co-authored-by: homersimpsons <guillaume.alabre@gmail.com>
- Loading branch information
Showing
7 changed files
with
118 additions
and
10 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
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
25 changes: 25 additions & 0 deletions
25
packages/cli-platform-android/src/commands/runAndroid/__tests__/checkUsers.test.ts
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,25 @@ | ||
import execa from 'execa'; | ||
import {checkUsers} from '../listAndroidUsers'; | ||
|
||
// output of "adb -s ... shell pm users list" command | ||
const gradleOutput = ` | ||
Users: | ||
UserInfo{0:Homersimpsons:c13} running | ||
UserInfo{10:Guest:404} | ||
`; | ||
|
||
jest.mock('execa', () => { | ||
return {sync: jest.fn()}; | ||
}); | ||
|
||
describe('check android users', () => { | ||
it('should correctly parse recieved users', () => { | ||
(execa.sync as jest.Mock).mockReturnValueOnce({stdout: gradleOutput}); | ||
const users = checkUsers('device', 'adbPath'); | ||
|
||
expect(users).toStrictEqual([ | ||
{id: '0', name: 'Homersimpsons'}, | ||
{id: '10', name: 'Guest'}, | ||
]); | ||
}); | ||
}); |
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
57 changes: 57 additions & 0 deletions
57
packages/cli-platform-android/src/commands/runAndroid/listAndroidUsers.ts
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,57 @@ | ||
import {logger} from '@react-native-community/cli-tools'; | ||
import execa from 'execa'; | ||
import prompts from 'prompts'; | ||
|
||
type User = { | ||
id: string; | ||
name: string; | ||
}; | ||
|
||
export function checkUsers(device: string, adbPath: string) { | ||
try { | ||
const adbArgs = ['-s', device, 'shell', 'pm', 'list', 'users']; | ||
|
||
logger.debug(`Checking users on "${device}"...`); | ||
const {stdout} = execa.sync(adbPath, adbArgs, {encoding: 'utf-8'}); | ||
const regex = new RegExp( | ||
/^\s*UserInfo\{(?<userId>\d+):(?<userName>.*):(?<userFlags>[0-9a-f]*)}/, | ||
); | ||
const users: User[] = []; | ||
|
||
const lines = stdout.split('\n'); | ||
for (const line of lines) { | ||
const res = regex.exec(line); | ||
if (res?.groups) { | ||
users.push({id: res.groups.userId, name: res.groups.userName}); | ||
} | ||
} | ||
|
||
if (users.length > 1) { | ||
logger.debug( | ||
`Available users are:\n${users | ||
.map((user) => `${user.name} - ${user.id}`) | ||
.join('\n')}`, | ||
); | ||
} | ||
|
||
return users; | ||
} catch (error) { | ||
logger.error('Failed to check users of device.', error as any); | ||
return []; | ||
} | ||
} | ||
|
||
export async function promptForUser(users: User[]) { | ||
const {selectedUser}: {selectedUser: User} = await prompts({ | ||
type: 'select', | ||
name: 'selectedUser', | ||
message: 'Which profile would you like to launch your app into?', | ||
choices: users.map((user: User) => ({ | ||
title: user.name, | ||
value: user, | ||
})), | ||
min: 1, | ||
}); | ||
|
||
return selectedUser; | ||
} |
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