Skip to content

Commit 7e0f319

Browse files
committed
Read default recipient for link sending from user settings
1 parent e3a463f commit 7e0f319

File tree

1 file changed

+31
-11
lines changed
  • react-native-scripts/src/scripts

1 file changed

+31
-11
lines changed

react-native-scripts/src/scripts/start.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
import { Android, Config, Project, ProjectSettings, Simulator, UrlUtils } from 'xdl';
3+
import { Android, Config, Project, ProjectSettings, Simulator, UrlUtils, UserSettings } from 'xdl';
44

55
import chalk from 'chalk';
66
import indent from 'indent-string';
@@ -138,34 +138,54 @@ async function handleKeypress(key) {
138138
const lanAddress = await UrlUtils.constructManifestUrlAsync(process.cwd(), {
139139
hostType: 'lan',
140140
});
141-
141+
const defaultRecipient = await UserSettings.getAsync('sendTo', null);
142142
const rl = readline.createInterface({
143143
input: process.stdin,
144-
output: process.stdout
144+
output: process.stdout,
145145
});
146-
147-
clearConsole();
148-
log.withTimestamp('Please enter your phone number or email address (empty to cancel):')
149-
rl.question('> ', async (sendTo) => {
146+
const handleKeypress = (chr, key) => {
147+
if (key && key.name === 'escape') {
148+
cleanup();
149+
cancel();
150+
}
151+
};
152+
const cleanup = () => {
150153
rl.close();
154+
process.stdin.removeListener('keypress', handleKeypress);
151155
startWaitingForCommand();
156+
};
157+
const cancel = () => {
158+
clearConsole();
159+
printUsage();
160+
};
161+
clearConsole();
162+
process.stdin.addListener('keypress', handleKeypress);
163+
log('Please enter your phone number or email address (press ESC to cancel) ');
164+
rl.question(defaultRecipient ? `[default: ${defaultRecipient}]> ` : '> ', async sendTo => {
165+
cleanup();
166+
if (!sendTo && defaultRecipient) {
167+
sendTo = defaultRecipient;
168+
}
169+
sendTo = sendTo && sendTo.trim();
152170
if (!sendTo) {
153-
clearConsole();
154-
printUsage();
171+
cancel();
155172
return;
156173
}
157-
158174
log.withTimestamp(`Sending ${lanAddress} to ${sendTo}...`);
159175

176+
let sent = false;
160177
try {
161178
await Exp.sendAsync(sendTo, lanAddress, true);
162179
log.withTimestamp(`Sent link successfully.`);
180+
sent = true;
163181
} catch (err) {
164182
log.withTimestamp(`Could not send link. ${err}`);
165183
}
166184
printUsage();
185+
if (sent) {
186+
await UserSettings.setAsync('sendTo', sendTo);
187+
}
167188
});
168-
169189
return;
170190
}
171191
case 'q':

0 commit comments

Comments
 (0)