Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Cloud): allow skipping updating npmrc on bit login #8724

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 26 additions & 16 deletions scopes/cloud/cloud/cloud.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ export class CloudMain {
setupAuthListener({
port: portFromParams,
clientId = v4(),
skipConfigUpdate,
}: {
port?: number;
clientId?: string;
skipConfigUpdate?: boolean;
} = {}): Promise<CloudAuthListener | null> {
return new Promise((resolve, reject) => {
const port = portFromParams || this.getLoginPort();
Expand Down Expand Up @@ -282,21 +284,27 @@ export class CloudMain {

const onLoggedInFns = this.onSuccessLoginSlot.values();

this.updateNpmConfig({ authToken: token as string, username: username as string })
.then((configUpdates) => {
onLoggedInFns.forEach((fn) => fn({ username, token: token as string, npmrcUpdateResult: configUpdates }));
})
.catch((error) => {
onLoggedInFns.forEach((fn) =>
fn({
username,
token: token as string,
npmrcUpdateResult: {
error: new Error(`failed to update npmrc. error ${error?.toString}`),
},
})
);
});
if (!skipConfigUpdate) {
this.updateNpmConfig({ authToken: token as string, username: username as string })
.then((configUpdates) => {
onLoggedInFns.forEach((fn) =>
fn({ username, token: token as string, npmrcUpdateResult: configUpdates })
);
})
.catch((error) => {
onLoggedInFns.forEach((fn) =>
fn({
username,
token: token as string,
npmrcUpdateResult: {
error: new Error(`failed to update npmrc. error ${error?.toString}`),
},
})
);
});
} else {
onLoggedInFns.forEach((fn) => fn({ username, token: token as string }));
}

if (this.REDIRECT_URL) return res.redirect(this.REDIRECT_URL);
if (typeof redirectUri === 'string' && redirectUri) return res.redirect(redirectUri);
Expand Down Expand Up @@ -424,7 +432,8 @@ export class CloudMain {
suppressBrowserLaunch?: boolean,
machineName?: string,
cloudDomain?: string,
redirectUrl?: string
redirectUrl?: string,
skipConfigUpdate?: boolean
): Promise<{
isAlreadyLoggedIn?: boolean;
username?: string;
Expand Down Expand Up @@ -473,6 +482,7 @@ export class CloudMain {
try {
this.setupAuthListener({
port: Number(port),
skipConfigUpdate,
})
.then(promptLogin)
.catch((e) => reject(e));
Expand Down
18 changes: 15 additions & 3 deletions scopes/cloud/cloud/login.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class LoginCmd implements Command {
group = 'general';
alias = '';
options = [
['', 'skip-config-update', 'skip writing to the .npmrc file'],
['d', 'cloud-domain <domain>', 'login cloud domain (default bit.cloud)'],
['p', 'port <port>', 'port number to open for localhost server (default 8085)'],
['', 'no-browser', 'do not open a browser for authentication'],
Expand Down Expand Up @@ -37,20 +38,29 @@ export class LoginCmd implements Command {
suppressBrowserLaunch,
noBrowser,
machineName,
skipConfigUpdate,
}: {
cloudDomain?: string;
port: string;
suppressBrowserLaunch?: boolean;
noBrowser?: boolean;
machineName?: string;
skipConfigUpdate?: boolean;
}
): Promise<string> {
noBrowser = noBrowser || suppressBrowserLaunch;

const result = await this.cloud.login(port || this.port, noBrowser, machineName, cloudDomain, undefined);
const result = await this.cloud.login(
port || this.port,
noBrowser,
machineName,
cloudDomain,
undefined,
skipConfigUpdate
);
let message = chalk.green(`Logged in as ${result?.username}`);

if (result?.isAlreadyLoggedIn) {
if (result?.isAlreadyLoggedIn || skipConfigUpdate) {
return message;
}

Expand All @@ -72,18 +82,20 @@ export class LoginCmd implements Command {
suppressBrowserLaunch,
noBrowser,
machineName,
skipConfigUpdate,
}: {
cloudDomain?: string;
port: string;
suppressBrowserLaunch?: boolean;
noBrowser?: boolean;
machineName?: string;
skipConfigUpdate?: boolean;
}
): Promise<{ username?: string; token?: string; successfullyUpdatedNpmrc?: boolean }> {
if (suppressBrowserLaunch) {
noBrowser = true;
}
const result = await this.cloud.login(port, noBrowser, machineName, cloudDomain);
const result = await this.cloud.login(port, noBrowser, machineName, cloudDomain, undefined, skipConfigUpdate);
return {
username: result?.username,
token: result?.token,
Expand Down