Skip to content

Commit

Permalink
fix: code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
haifeng-li-at-salesforce committed Oct 26, 2024
1 parent 3ab436e commit 3539e20
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lsp/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ connection.onInitialize((params: InitializeParams) => {
const workspaceFolders = params.workspaceFolders;

// Sets workspace folder to WorkspaceUtils
WorkspaceUtils.setWorkSpaceFolders(workspaceFolders);
WorkspaceUtils.initWorkspaceFolders(workspaceFolders);
extensionTitle = params.initializationOptions?.extensionTitle;
updateDiagnosticsSettingCommand =
params.initializationOptions?.updateDiagnosticsSettingCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { TextDocument } from 'vscode-languageserver-textdocument';
import { suite, test } from 'mocha';
import { suite, test, beforeEach, afterEach } from 'mocha';
import Account from '../../../../testFixture/objectInfos/Account.json';
import User from '../../../../testFixture/objectInfos/User.json';
import Contact from '../../../../testFixture/objectInfos/Contact.json';
Expand Down
2 changes: 1 addition & 1 deletion lsp/server/src/test/utils/orgUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ suite('OrgUtils Test Suite - Server', () => {
);
});

test('ObjectInfo is fetched if auth status is connection is available and auth status is authorized', async () => {
test('ObjectInfo is fetched if connection is available and auth status is authorized', async () => {
// user exists

createConfigStub = stubCreateConfig(sandbox, true);
Expand Down
34 changes: 20 additions & 14 deletions lsp/server/src/utils/orgUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum AuthStatus {
}

export class OrgUtils {
private static orgName: string = '';
private static orgName: string | undefined;
private static objectInfoFolder = 'objectInfos';
private static entityListFileName = 'entity_list.json';
private static connection: Connection | undefined;
Expand Down Expand Up @@ -60,7 +60,7 @@ export class OrgUtils {
return path.join(os.homedir(), this.sfFolder);
}

// Retrieves default organiztion's name.
// Retrieve default organiztion's name.
private static async getDefaultOrg(): Promise<string | undefined> {
const aggregator = await ConfigAggregator.create();

Expand All @@ -70,11 +70,9 @@ export class OrgUtils {
OrgConfigProperties.TARGET_ORG
);

if (currentUserConfig.value) {
this.orgName = currentUserConfig.value.toString();
return Promise.resolve(this.orgName);
}
return undefined;
return currentUserConfig.value
? currentUserConfig.value.toString()
: undefined;
}

private static async getDefaultUserName(): Promise<string | undefined> {
Expand All @@ -93,18 +91,26 @@ export class OrgUtils {
}
}

// Updates the auth state async
// Update the auth state async
private static async checkAuthStatus(): Promise<AuthStatus> {
if (this.authStatus !== AuthStatus.UNKNOWN) {
return this.authStatus;
}
try {
this.orgName = await this.getDefaultOrg();
} catch (e) {
this.orgName = undefined;
}
const connection = await this.getConnection();
if (connection === undefined) {
//It is possible that orgName exists and connection expires
this.orgName = '';
this.orgName = undefined;
this.authStatus = AuthStatus.UNAUTHORIZED;
} else {
this.authStatus = AuthStatus.AUTHORIZED;
if (this.orgName === undefined) {
throw new Error('Authorized Org does not have org name!');
}
// Fetches entity list once.
const entityListFile = path.join(
this.objectInfoFolderPath(),
Expand All @@ -125,7 +131,7 @@ export class OrgUtils {
return this.authStatus;
}

// Retrieves the Connection which fetches ObjectInfo remotely.
// Retrieve the Connection which fetches ObjectInfo remotely.
private static async getConnection(): Promise<Connection | undefined> {
if (
this.connection !== undefined &&
Expand Down Expand Up @@ -159,7 +165,7 @@ export class OrgUtils {
return globalResult.sobjects.map((sobjetResult) => sobjetResult.name);
}

// Retrieves objectInfo folder path, which is '<projectRoot>/.sf/orgName/objectInfos/'
// Retrieve objectInfo folder path, which is '<projectRoot>/.sfMobile/orgName/objectInfos/'
private static objectInfoFolderPath(): string {
const projectPath = WorkspaceUtils.getWorkspaceDir();
if (this.orgName === undefined || this.orgName.length === 0) {
Expand Down Expand Up @@ -195,14 +201,14 @@ export class OrgUtils {
private static getObjectInfoFromCache(
objectApiName: string
): ObjectInfoRepresentation | undefined {
// Checks mem cache
// Check mem cache
let objectInfo = this.objectInfoInMemoCache.get(objectApiName);

if (objectInfo !== undefined) {
return objectInfo;
}

// Checks disk cache
// Check disk cache
objectInfo = this.fetchObjectInfoFromDisk(objectApiName);
if (objectInfo !== undefined) {
this.objectInfoInMemoCache.set(objectApiName, objectInfo);
Expand All @@ -211,7 +217,7 @@ export class OrgUtils {
return undefined;
}

// Acquires ObjectInfo data by first searching in memory, then on disk, and finally over the network.
// Acquire ObjectInfo data by first searching in memory, then on disk, and finally over the network.
public static async getObjectInfo(
objectApiName: string
): Promise<ObjectInfoRepresentation | undefined> {
Expand Down
51 changes: 24 additions & 27 deletions lsp/server/src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
Expand All @@ -11,7 +11,8 @@ import * as path from 'path';
import { WorkspaceFolder } from 'vscode-languageserver';

export class WorkspaceUtils {
private static workspaceFolders: WorkspaceFolder[] | undefined | null = [];
// WorkspaceFolders is null if Workspace is not configured
private static workspaceFolders: WorkspaceFolder[] | null;

static readonly DEFAULT_APP_PATH = path.join(
'force-app',
Expand All @@ -24,47 +25,43 @@ export class WorkspaceUtils {
'lwc'
);

public static setWorkSpaceFolders(
workSpaceFolders: WorkspaceFolder[] | null | undefined
/**
* WorkspaceFolders must be initialized before they can be used.
* @param workspaceFolders workspaceFolders passed from LSP server connection.
*/
public static initWorkspaceFolders(
workspaceFolders: WorkspaceFolder[] | null | undefined
) {
this.workspaceFolders = workSpaceFolders;
if (workspaceFolders === undefined) {
throw new Error('LSP client does no support workspace folder');
}
this.workspaceFolders = workspaceFolders;
}

// LSP server has a different way to fetch WorkSpaceDir than root project which relis on vscode workspace.
// The LSP server uses a different method to retrieve the WorkSpaceDir than the root project, which depends on the VSCode workspace.
static getWorkspaceDir(): string {
if (!this.workspaceFolders || this.workspaceFolders.length === 0) {
throw new NoWorkspaceError(
'No workspace defined for this project.'
);
if (
this.workspaceFolders === null ||
this.workspaceFolders.length === 0
) {
throw new Error('No workspace defined for this project.');
}
return new URL(this.workspaceFolders[0].uri).pathname;
}

static lwcFolderExists(): boolean {
try {
return fs.existsSync(
path.join(this.getWorkspaceDir(), WorkspaceUtils.LWC_PATH)
);
} catch {
return false;
}
return this.isWorkSpacePathExists(WorkspaceUtils.LWC_PATH);
}

static isSfdxProjectOpened(): boolean {
private static isWorkSpacePathExists(filePath: string): boolean {
try {
return fs.existsSync(
path.join(this.getWorkspaceDir(), SFDX_PROJECT_FILE)
);
return fs.existsSync(path.join(this.getWorkspaceDir(), filePath));
} catch {
return false;
}
}
}

export class NoWorkspaceError extends Error {
constructor(message?: string) {
super(message);
this.name = this.constructor.name;
Object.setPrototypeOf(this, NoWorkspaceError.prototype);
static isSfdxProjectOpened(): boolean {
return this.isWorkSpacePathExists(SFDX_PROJECT_FILE);
}
}

0 comments on commit 3539e20

Please sign in to comment.