Skip to content

Commit 9d9bed3

Browse files
authored
Merge pull request #9 from codemagic-ci-cd/ux-login-help
Managed-first CLI login and help cleanup
2 parents ecf009b + 0e62b54 commit 9d9bed3

6 files changed

Lines changed: 92 additions & 96 deletions

File tree

README.md

Lines changed: 45 additions & 64 deletions
Large diffs are not rendered by default.

script/command-executor.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -589,19 +589,35 @@ function link(command: cli.ILinkCommand): Promise<void> {
589589
}
590590

591591
function login(command: cli.ILoginCommand): Promise<void> {
592-
// Check if one of the flags were provided.
593592
if (command.accessKey) {
594-
sdk = getSdk(command.accessKey, CLI_HEADERS, command.serverUrl);
595-
return sdk.isAuthenticated().then((isAuthenticated: boolean): void => {
596-
if (isAuthenticated) {
597-
serializeConnectionInfo(command.accessKey, /*preserveAccessKeyOnLogout*/ true, command.serverUrl);
598-
} else {
599-
throw new Error("Invalid access key.");
600-
}
601-
});
602-
} else {
603-
return loginWithExternalAuthentication("login", command.serverUrl);
593+
return loginWithAccessKey(command.accessKey, command.serverUrl, /*preserveAccessKeyOnLogout*/ true);
594+
}
595+
596+
return requestAccessKey().then((accessKey: string): Promise<void> => {
597+
if (accessKey === null) {
598+
// The user has aborted the synchronous prompt (e.g.: via [CTRL]+[C]).
599+
return;
600+
}
601+
602+
return loginWithAccessKey(accessKey, command.serverUrl, /*preserveAccessKeyOnLogout*/ true);
603+
});
604+
}
605+
606+
function loginWithAccessKey(accessKey: string, serverUrl?: string, preserveAccessKeyOnLogout: boolean = true): Promise<void> {
607+
accessKey = accessKey ? accessKey.trim() : "";
608+
609+
if (!accessKey) {
610+
throw new Error("An access key must be specified.");
604611
}
612+
613+
sdk = getSdk(accessKey, CLI_HEADERS, serverUrl);
614+
return sdk.isAuthenticated().then((isAuthenticated: boolean): void => {
615+
if (isAuthenticated) {
616+
serializeConnectionInfo(accessKey, preserveAccessKeyOnLogout, serverUrl);
617+
} else {
618+
throw new Error("Invalid access key.");
619+
}
620+
});
605621
}
606622

607623
function loginWithExternalAuthentication(action: string, serverUrl?: string): Promise<void> {
@@ -614,15 +630,7 @@ function loginWithExternalAuthentication(action: string, serverUrl?: string): Pr
614630
return;
615631
}
616632

617-
sdk = getSdk(accessKey, CLI_HEADERS, serverUrl);
618-
619-
return sdk.isAuthenticated().then((isAuthenticated: boolean): void => {
620-
if (isAuthenticated) {
621-
serializeConnectionInfo(accessKey, /*preserveAccessKeyOnLogout*/ false, serverUrl);
622-
} else {
623-
throw new Error("Invalid access key.");
624-
}
625-
});
633+
return loginWithAccessKey(accessKey, serverUrl, /*preserveAccessKeyOnLogout*/ false);
626634
});
627635
}
628636

@@ -1422,14 +1430,16 @@ function requestAccessKey(): Promise<string> {
14221430
properties: {
14231431
response: {
14241432
description: chalk.cyan("Enter your access key: "),
1433+
hidden: true,
1434+
replace: "*",
14251435
},
14261436
},
14271437
},
14281438
(err: any, result: any): void => {
14291439
if (err) {
14301440
resolve(null);
14311441
} else {
1432-
resolve(result.response.trim());
1442+
resolve((result.response || "").trim());
14331443
}
14341444
}
14351445
);

script/command-parser.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ function deploymentHistory(commandName: string, yargs: yargs.Argv): void {
277277
}
278278

279279
yargs
280+
.scriptName("code-push")
280281
.usage(USAGE_PREFIX + " <command>")
281282
.demand(/*count*/ 1, /*max*/ 1) // Require exactly one non-option argument.
282283
.command("access-key", "View and manage the access keys associated with your account", (yargs: yargs.Argv) => {
@@ -334,7 +335,7 @@ yargs
334335

335336
addCommonConfiguration(yargs);
336337
})
337-
.command("collaborator", "View and manage app collaborators", (yargs: yargs.Argv) => {
338+
.command("collaborator", false, (yargs: yargs.Argv) => {
338339
isValidCommandCategory = true;
339340
yargs
340341
.usage(USAGE_PREFIX + " collaborator <command>")
@@ -413,7 +414,7 @@ yargs
413414

414415
addCommonConfiguration(yargs);
415416
})
416-
.command("link", "Link an additional authentication provider (e.g. GitHub) to an existing CodePush account", (yargs: yargs.Argv) => {
417+
.command("link", false, (yargs: yargs.Argv) => {
417418
isValidCommandCategory = true;
418419
isValidCommand = true;
419420
yargs
@@ -430,14 +431,13 @@ yargs
430431
yargs
431432
.usage(USAGE_PREFIX + " login [options]")
432433
.demand(/*count*/ 0, /*max*/ 1) //set 'max' to one to allow usage of serverUrl undocument parameter for testing
433-
.example("login", "Logs in to the CodePush server")
434-
.example("login --accessKey mykey", 'Logs in on behalf of the user who owns and created the access key "mykey"')
435-
.option("accessKey", {
434+
.example("login", "Prompts for an access key and logs in to the CodePush server")
435+
.example("login --access-key mykey", 'Logs in with the access key "mykey"')
436+
.option("access-key", {
436437
alias: "key",
437438
default: null,
438439
demand: false,
439-
description:
440-
"Access key to authenticate against the CodePush server with, instead of providing your username and password credentials",
440+
description: "Access key to authenticate against the CodePush server with",
441441
type: "string",
442442
})
443443
.check((argv: any, aliases: { [aliases: string]: string }): any => isValidCommand); // Report unrecognized, non-hyphenated command category.
@@ -585,7 +585,7 @@ yargs
585585

586586
addCommonConfiguration(yargs);
587587
})
588-
.command("register", "Register a new CodePush account", (yargs: yargs.Argv) => {
588+
.command("register", false, (yargs: yargs.Argv) => {
589589
isValidCommandCategory = true;
590590
isValidCommand = true;
591591
yargs
@@ -859,7 +859,7 @@ yargs
859859

860860
addCommonConfiguration(yargs);
861861
})
862-
.command("session", "View and manage the current login sessions associated with your account", (yargs: yargs.Argv) => {
862+
.command("session", false, (yargs: yargs.Argv) => {
863863
isValidCommandCategory = true;
864864
yargs
865865
.usage(USAGE_PREFIX + " session <command>")
@@ -1137,7 +1137,7 @@ export function createCommand(): cli.ICommand {
11371137
const loginCommand = <cli.ILoginCommand>cmd;
11381138

11391139
loginCommand.serverUrl = getServerUrl(arg1);
1140-
loginCommand.accessKey = argv["accessKey"] as any;
1140+
loginCommand.accessKey = argv["access-key"] as any;
11411141
break;
11421142

11431143
case "logout":

script/management-sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class AccountManager {
6262
OWNER: "Owner",
6363
COLLABORATOR: "Collaborator",
6464
};
65-
public static SERVER_URL = "http://localhost:3000";
65+
public static SERVER_URL = "https://codepush.pro";
6666

6767
private static API_VERSION: number = 2;
6868

test/resources/TestApp/ios/Podfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
platform :ios, '13.0'
2+
3+
target 'TestApp' do
4+
use_react_native!(:hermes_enabled => false)
5+
end
File renamed without changes.

0 commit comments

Comments
 (0)