Skip to content

Commit

Permalink
Move utility functions to dedicated space (#1074)
Browse files Browse the repository at this point in the history
This moves all the utility files to a dedicated folder and renames the files to be more descriptive. It also removes a few functions that were used back when the extension downloaded terraform-ls on demand, but are no longer needed now that we don't.
  • Loading branch information
jpogran committed Apr 26, 2022
1 parent 538ff98 commit e3a9edd
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 81 deletions.
6 changes: 3 additions & 3 deletions src/clientHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
ServerOptions,
State,
} from 'vscode-languageclient/node';
import { ServerPath } from './serverPath';
import { config } from './utils/vscode';
import { ServerPath } from './utils/serverPath';
import { PartialManifest, CustomSemanticTokens } from './features/semanticTokens';
import { ShowReferencesFeature } from './features/showReferences';
import { TelemetryFeature } from './features/telemetry';
import { config } from './vscodeUtils';
import { CustomSemanticTokens, PartialManifest } from './features/semanticTokens';

export interface TerraformLanguageClient {
commandPrefix: string;
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { ClientHandler, TerraformLanguageClient } from './clientHandler';
import { GenerateBugReportCommand } from './commands/generateBugReport';
import { ModuleCallsDataProvider } from './providers/moduleCalls';
import { ModuleProvidersDataProvider } from './providers/moduleProviders';
import { ServerPath } from './serverPath';
import { config, getActiveTextEditor, isTerraformFile } from './vscodeUtils';
import { ServerPath } from './utils/serverPath';
import { config, getActiveTextEditor, isTerraformFile } from './utils/vscode';

const brand = `HashiCorp Terraform`;
const outputChannel = vscode.window.createOutputChannel(brand);
Expand Down
2 changes: 1 addition & 1 deletion src/installer/detector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { exec } from '../utils';
import { exec } from '../utils/helpers';

export async function getLsVersion(binPath: string): Promise<string | undefined> {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/moduleCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as vscode from 'vscode';
import { ExecuteCommandParams, ExecuteCommandRequest } from 'vscode-languageclient';
import { Utils } from 'vscode-uri';
import { ClientHandler } from '../clientHandler';
import { getActiveTextEditor, isTerraformFile } from '../vscodeUtils';
import { getActiveTextEditor, isTerraformFile } from '../utils/vscode';

/* eslint-disable @typescript-eslint/naming-convention */
interface ModuleCall {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/moduleProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Utils } from 'vscode-uri';
import { ExecuteCommandParams, ExecuteCommandRequest } from 'vscode-languageclient';

import { ClientHandler } from '../clientHandler';
import { getActiveTextEditor, isTerraformFile } from '../vscodeUtils';
import { getActiveTextEditor, isTerraformFile } from '../utils/vscode';

/* eslint-disable @typescript-eslint/naming-convention */
interface ModuleProvidersResponse {
Expand Down
2 changes: 1 addition & 1 deletion src/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import { runTests } from '@vscode/test-electron';
import { TestOptions } from '@vscode/test-electron/out/runTest';
import { exec } from '../utils';
import { exec } from '../utils/helpers';

async function terraformInit() {
const cwd = process.cwd();
Expand Down
4 changes: 2 additions & 2 deletions src/test/unit/detector.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exec as execOrg } from '../../utils';
import { exec as execOrg } from '../../utils/helpers';
import { getLsVersion } from '../../installer/detector';

jest.mock('../../utils');
jest.mock('../../utils/helpers');

const exec = jest.mocked(execOrg);

Expand Down
46 changes: 0 additions & 46 deletions src/utils.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as cp from 'child_process';

export function exec(cmd: string, args: readonly string[]): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
cp.execFile(cmd, args, (err, stdout, stderr) => {
if (err) {
return reject(err);
}
return resolve({ stdout, stderr });
});
});
}
File renamed without changes.
24 changes: 0 additions & 24 deletions src/vscodeUtils.ts → src/utils/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ export function config(section: string, scope?: vscode.ConfigurationScope): vsco
return vscode.workspace.getConfiguration(section, scope);
}

export function getFolderName(folder: vscode.WorkspaceFolder): string {
return normalizeFolderName(folder.uri.toString());
}

// Make sure that folder uris always end with a slash
export function normalizeFolderName(folderName: string): string {
if (folderName.charAt(folderName.length - 1) !== '/') {
folderName = folderName + '/';
}
return folderName;
}

export function getWorkspaceFolder(folderName: string): vscode.WorkspaceFolder | undefined {
return vscode.workspace.getWorkspaceFolder(vscode.Uri.parse(folderName));
}
Expand Down Expand Up @@ -52,15 +40,3 @@ export function isTerraformFile(document?: vscode.TextDocument): boolean {
// be safe and default to false
return false;
}

export function sortedWorkspaceFolders(): string[] {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders) {
return workspaceFolders
.map((f) => getFolderName(f))
.sort((a, b) => {
return a.length - b.length;
});
}
return [];
}

0 comments on commit e3a9edd

Please sign in to comment.