diff --git a/.yarn/cache/dotenv-npm-16.0.3-4cc29121dc-afcf03f373.zip b/.yarn/cache/dotenv-npm-16.0.3-4cc29121dc-afcf03f373.zip new file mode 100644 index 0000000000..90a95535b4 Binary files /dev/null and b/.yarn/cache/dotenv-npm-16.0.3-4cc29121dc-afcf03f373.zip differ diff --git a/libs/vscode/nx-cloud-view/src/lib/nx-cloud-service/nx-cloud-api.service.ts b/libs/vscode/nx-cloud-view/src/lib/nx-cloud-service/nx-cloud-api.service.ts index c67c62a41f..089ccddc29 100644 --- a/libs/vscode/nx-cloud-view/src/lib/nx-cloud-service/nx-cloud-api.service.ts +++ b/libs/vscode/nx-cloud-view/src/lib/nx-cloud-service/nx-cloud-api.service.ts @@ -1,4 +1,4 @@ -import { getNxWorkspace } from '@nx-console/vscode/nx-workspace'; +import { getNxCloudRunnerOptions } from '@nx-console/vscode/nx-workspace'; import request, { gql } from 'graphql-request'; import { authentication } from 'vscode'; import { @@ -17,11 +17,9 @@ export class NxCloudApiService { async claimCloudWorkspace( orgId: string ): Promise { - const workspaceConfig = (await getNxWorkspace()).workspace; - const nxAccessToken = - workspaceConfig.tasksRunnerOptions?.['default']?.options['accessToken']; + const nxAccessToken = (await getNxCloudRunnerOptions())?.accessToken; - const authAccessToken = await this.getAccessToken(); + const authAccessToken = await this.getAuthAccessToken(); if (!authAccessToken) { throw new Error('No auth access token found'); @@ -58,11 +56,9 @@ export class NxCloudApiService { } async getWorkspaceDetailsByToken(): Promise { - const workspaceConfig = (await getNxWorkspace()).workspace; - const nxAcessToken = - workspaceConfig.tasksRunnerOptions?.['default']?.options['accessToken']; + const nxAccessToken = (await getNxCloudRunnerOptions())?.accessToken; - if (!nxAcessToken) { + if (!nxAccessToken) { throw new Error('No nx access token found.'); } @@ -84,7 +80,7 @@ export class NxCloudApiService { `; const variables = { - accessToken: nxAcessToken, + accessToken: nxAccessToken, }; const session = await authentication.getSession('nxCloud', [], { @@ -148,7 +144,7 @@ export class NxCloudApiService { workspaceId: workspaceId, }; - const authAccessToken = await this.getAccessToken(); + const authAccessToken = await this.getAuthAccessToken(); const headers = authAccessToken ? { Authorization: `Bearer ${authAccessToken}`, @@ -234,7 +230,7 @@ export class NxCloudApiService { return 'disconnected'; } - private async getAccessToken() { + private async getAuthAccessToken() { return await authentication .getSession('nxCloud', [], { silent: true }) .then((session) => session?.accessToken); diff --git a/libs/vscode/nx-workspace/src/lib/get-nx-workspace.ts b/libs/vscode/nx-workspace/src/lib/get-nx-workspace.ts index 5658234ea5..0f07e773cc 100644 --- a/libs/vscode/nx-workspace/src/lib/get-nx-workspace.ts +++ b/libs/vscode/nx-workspace/src/lib/get-nx-workspace.ts @@ -2,6 +2,9 @@ import { ProjectConfiguration } from '@nrwl/devkit'; import { NxWorkspaceRequest } from '@nx-console/language-server/types'; import { NxWorkspace } from '@nx-console/shared/types'; import { sendRequest } from '@nx-console/vscode/lsp-client'; +import { parse } from 'dotenv'; +import { join } from 'path'; +import { readFileSync } from 'fs'; export function getNxWorkspace(reset?: boolean): Promise { return sendRequest(NxWorkspaceRequest, { reset }); @@ -20,13 +23,36 @@ export async function getNxWorkspaceProjects(reset?: boolean): Promise<{ export async function getNxCloudRunnerOptions(): Promise< { accessToken: string; url?: string } | undefined > { - const nxConfig = (await getNxWorkspace()).workspace; + const nxWorkspace = await getNxWorkspace(); + const workspaceConfig = nxWorkspace.workspace; - if (!nxConfig.tasksRunnerOptions) { + if (!workspaceConfig.tasksRunnerOptions) { return; } - const nxCloudTaskRunner = Object.values(nxConfig.tasksRunnerOptions).find( - (r) => r.runner == '@nrwl/nx-cloud' - ); - return nxCloudTaskRunner?.options; + const nxCloudTaskRunner = Object.values( + workspaceConfig.tasksRunnerOptions + ).find((r) => r.runner == '@nrwl/nx-cloud'); + + if (!nxCloudTaskRunner) { + return undefined; + } + + // check if nx-cloud.env exists and use that access token if it does + const env = getNxCloudEnv(nxWorkspace.workspacePath); + const accessToken = env.NX_CLOUD_AUTH_TOKEN || env.NX_CLOUD_ACCESS_TOKEN; + + if (!accessToken) { + return nxCloudTaskRunner.options; + } + + return { ...nxCloudTaskRunner.options, accessToken }; +} + +function getNxCloudEnv(workspacePath: string): any { + try { + const envContents = readFileSync(join(workspacePath, 'nx-cloud.env')); + return parse(envContents); + } catch (e) { + return {}; + } } diff --git a/package.json b/package.json index 8ce0b9c931..67aa666677 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "@vscode/webview-ui-toolkit": "^1.2.0", "@yarnpkg/fslib": "2.6.1-rc.5", "@yarnpkg/libzip": "2.2.3-rc.5", + "dotenv": "^16.0.3", "fast-glob": "^3.2.11", "find-cache-dir": "^3.3.2", "graphql": "^16.6.0", diff --git a/yarn.lock b/yarn.lock index 8aa4ab54ba..f5be6317d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12119,6 +12119,13 @@ __metadata: languageName: node linkType: hard +"dotenv@npm:^16.0.3": + version: 16.0.3 + resolution: "dotenv@npm:16.0.3" + checksum: afcf03f373d7a6d62c7e9afea6328e62851d627a4e73f2e12d0a8deae1cd375892004f3021883f8aec85932cd2834b091f568ced92b4774625b321db83b827f8 + languageName: node + linkType: hard + "dotenv@npm:^8.0.0": version: 8.6.0 resolution: "dotenv@npm:8.6.0" @@ -20682,6 +20689,7 @@ __metadata: "@yarnpkg/libzip": 2.2.3-rc.5 "@yarnpkg/pnp": ^3.1.1-rc.12 cypress: ^10.2.0 + dotenv: ^16.0.3 esbuild: ^0.14.54 esbuild-copy-files-plugin: ^1.1.0 eslint: 8.15.0