Skip to content

Commit ed43851

Browse files
committed
fix: refactor imports and ensure async/promises are used
1 parent cbd0369 commit ed43851

File tree

9 files changed

+42
-37
lines changed

9 files changed

+42
-37
lines changed

src/commands/apex-tests-git-delta/delta.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import * as fs from 'node:fs';
3+
import { writeFile } from 'node:fs/promises';
44

55
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
66
import { Messages } from '@salesforce/core';
@@ -64,7 +64,7 @@ export default class ApexTestDelta extends SfCommand<TestDeltaResult> {
6464
const result = await extractTestClasses(fromGitRef, toGitRef, regExFile, sfdxConfigFile);
6565
const tests = result.validatedClasses;
6666
const warnings = result.warnings;
67-
fs.writeFileSync(output, tests);
67+
await writeFile(output, tests);
6868
if (warnings.length > 0) {
6969
warnings.forEach((warning) => {
7070
this.warn(warning);

src/service/extractTestClasses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
23
import { retrieveCommitMessages } from './retrieveCommitMessages.js';
34
import { validateClassPaths } from './validateClassPaths.js';
45

src/service/getPackageDirectories.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
'use strict';
22
/* eslint-disable no-await-in-loop */
33

4-
import * as fs from 'node:fs';
5-
import * as promises from 'node:fs/promises';
4+
import { existsSync } from 'node:fs';
5+
import { readFile } from 'node:fs/promises';
66

77
interface SfdxProject {
88
packageDirectories: Array<{ path: string }>;
99
}
1010

1111
export async function getPackageDirectories(dxConfigFile: string): Promise<string[]> {
12-
if (!fs.existsSync(dxConfigFile)) {
12+
if (!existsSync(dxConfigFile)) {
1313
throw Error(`Salesforce DX Config File does not exist in this path: ${dxConfigFile}`);
1414
}
1515

16-
const sfdxProjectRaw: string = await promises.readFile(dxConfigFile, 'utf-8');
16+
const sfdxProjectRaw: string = await readFile(dxConfigFile, 'utf-8');
1717
const sfdxProject: SfdxProject = JSON.parse(sfdxProjectRaw) as SfdxProject;
1818
const packageDirectories = sfdxProject.packageDirectories.map((directory) => directory.path);
1919
return packageDirectories;

src/service/retrieveCommitMessages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
23
import { readFileSync } from 'node:fs';
34
import { simpleGit, SimpleGit, SimpleGitOptions, DefaultLogFields, LogResult } from 'simple-git';
45

src/service/validateClassPaths.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22
/* eslint-disable no-await-in-loop */
33

4-
import * as promises from 'node:fs/promises';
5-
import * as path from 'node:path';
4+
import { readdir, stat } from 'node:fs/promises';
5+
import { join } from 'node:path';
66

77
import { getPackageDirectories } from './getPackageDirectories.js';
88

@@ -33,10 +33,10 @@ export async function validateClassPaths(
3333
}
3434

3535
async function searchRecursively(fileName: string, dxDirectory: string): Promise<string | undefined> {
36-
const files = await promises.readdir(dxDirectory);
36+
const files = await readdir(dxDirectory);
3737
for (const file of files) {
38-
const filePath = path.join(dxDirectory, file);
39-
const stats = await promises.stat(filePath);
38+
const filePath = join(dxDirectory, file);
39+
const stats = await stat(filePath);
4040
if (stats.isDirectory()) {
4141
const result = await searchRecursively(fileName, filePath);
4242
if (result) {

test/commands/delta/createTemporaryCommit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import * as promises from 'node:fs/promises';
3+
import { writeFile } from 'node:fs/promises';
44
import { SimpleGit } from 'simple-git';
55

66
export async function createTemporaryCommit(
@@ -9,7 +9,7 @@ export async function createTemporaryCommit(
99
content: string,
1010
git: SimpleGit
1111
): Promise<string> {
12-
await promises.writeFile(filePath, content);
12+
await writeFile(filePath, content);
1313

1414
// Stage the file
1515
await git.add(filePath);

test/commands/delta/empty.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
import * as fs from 'node:fs';
3+
import { mkdtempSync } from 'node:fs';
4+
import { mkdir, writeFile, rm } from 'node:fs/promises';
45
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';
56

67
import { TestContext } from '@salesforce/core/lib/testSetup.js';
@@ -16,7 +17,7 @@ describe('scan commit messages without the regex and return an empty string.', (
1617
let fromSha: string;
1718
let toSha: string;
1819
const originalDir = process.cwd();
19-
const tempDir = fs.mkdtempSync('../git-temp-');
20+
const tempDir = mkdtempSync('../git-temp-');
2021

2122
before(async () => {
2223
process.chdir(tempDir);
@@ -27,11 +28,11 @@ describe('scan commit messages without the regex and return an empty string.', (
2728
trimmed: false,
2829
};
2930
const git: SimpleGit = simpleGit(options);
30-
fs.mkdirSync('force-app/main/default/classes', { recursive: true });
31-
fs.mkdirSync('packaged/classes', { recursive: true });
31+
await mkdir('force-app/main/default/classes', { recursive: true });
32+
await mkdir('packaged/classes', { recursive: true });
3233
await git.init();
33-
fs.writeFileSync(regExFile, regExFileContents);
34-
fs.writeFileSync(sfdxConfigFile, sfdxConfigJsonString);
34+
await writeFile(regExFile, regExFileContents);
35+
await writeFile(sfdxConfigFile, sfdxConfigJsonString);
3536
let userName;
3637
let userEmail;
3738

@@ -64,9 +65,9 @@ describe('scan commit messages without the regex and return an empty string.', (
6465
$$.restore();
6566
});
6667

67-
after(() => {
68+
after(async () => {
6869
process.chdir(originalDir);
69-
fs.rmdirSync(tempDir, { recursive: true });
70+
await rm(tempDir, { recursive: true });
7071
});
7172

7273
it('return an empty test string with no warnings.', async () => {

test/commands/delta/unit.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
import * as fs from 'node:fs';
3+
import { mkdtempSync } from 'node:fs';
4+
import { mkdir, writeFile, rm } from 'node:fs/promises';
45
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';
56

67
import { TestContext } from '@salesforce/core/lib/testSetup.js';
@@ -16,7 +17,7 @@ describe('return the delta tests between git commits', () => {
1617
let fromSha: string;
1718
let toSha: string;
1819
const originalDir = process.cwd();
19-
const tempDir = fs.mkdtempSync('../git-temp-');
20+
const tempDir = mkdtempSync('../git-temp-');
2021

2122
before(async () => {
2223
process.chdir(tempDir);
@@ -27,11 +28,11 @@ describe('return the delta tests between git commits', () => {
2728
trimmed: false,
2829
};
2930
const git: SimpleGit = simpleGit(options);
30-
fs.mkdirSync('force-app/main/default/classes', { recursive: true });
31-
fs.mkdirSync('packaged/classes', { recursive: true });
31+
await mkdir('force-app/main/default/classes', { recursive: true });
32+
await mkdir('packaged/classes', { recursive: true });
3233
await git.init();
33-
fs.writeFileSync(regExFile, regExFileContents);
34-
fs.writeFileSync(sfdxConfigFile, sfdxConfigJsonString);
34+
await writeFile(regExFile, regExFileContents);
35+
await writeFile(sfdxConfigFile, sfdxConfigJsonString);
3536
let userName;
3637
let userEmail;
3738

@@ -74,9 +75,9 @@ describe('return the delta tests between git commits', () => {
7475
$$.restore();
7576
});
7677

77-
after(() => {
78+
after(async () => {
7879
process.chdir(originalDir);
79-
fs.rmdirSync(tempDir, { recursive: true });
80+
await rm(tempDir, { recursive: true });
8081
});
8182

8283
it('scan the temporary commits and return the delta test class string without any warnings.', async () => {

test/commands/delta/warnings.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
import * as fs from 'node:fs';
3+
import { mkdtempSync } from 'node:fs';
4+
import { mkdir, writeFile, rm } from 'node:fs/promises';
45
import { simpleGit, SimpleGit, SimpleGitOptions } from 'simple-git';
56

67
import { TestContext } from '@salesforce/core/lib/testSetup.js';
@@ -16,7 +17,7 @@ describe('confirm warnings are generated when files cannot be found in a package
1617
let fromSha: string;
1718
let toSha: string;
1819
const originalDir = process.cwd();
19-
const tempDir = fs.mkdtempSync('../git-temp-');
20+
const tempDir = mkdtempSync('../git-temp-');
2021

2122
before(async () => {
2223
process.chdir(tempDir);
@@ -27,11 +28,11 @@ describe('confirm warnings are generated when files cannot be found in a package
2728
trimmed: false,
2829
};
2930
const git: SimpleGit = simpleGit(options);
30-
fs.mkdirSync('force-app/main/default/classes', { recursive: true });
31-
fs.mkdirSync('packaged/classes', { recursive: true });
31+
await mkdir('force-app/main/default/classes', { recursive: true });
32+
await mkdir('packaged/classes', { recursive: true });
3233
await git.init();
33-
fs.writeFileSync(regExFile, regExFileContents);
34-
fs.writeFileSync(sfdxConfigFile, sfdxConfigJsonString);
34+
await writeFile(regExFile, regExFileContents);
35+
await writeFile(sfdxConfigFile, sfdxConfigJsonString);
3536
let userName;
3637
let userEmail;
3738

@@ -73,9 +74,9 @@ describe('confirm warnings are generated when files cannot be found in a package
7374
$$.restore();
7475
});
7576

76-
after(() => {
77+
after(async () => {
7778
process.chdir(originalDir);
78-
fs.rmdirSync(tempDir, { recursive: true });
79+
await rm(tempDir, { recursive: true });
7980
});
8081

8182
it('confirm warnings are generated and no delta tests are in the log output.', async () => {

0 commit comments

Comments
 (0)