Skip to content

Commit

Permalink
fix: remove simple-git dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Aug 7, 2024
1 parent 220497d commit 921f831
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"@oclif/core": "^3.18.1",
"@salesforce/core": "^6.4.7",
"@salesforce/sf-plugins-core": "^7.1.3",
"isomorphic-git": "^1.27.1",
"simple-git": "^3.24.0"
"isomorphic-git": "^1.27.1"
},
"devDependencies": {
"@commitlint/cli": "^18.6.0",
Expand All @@ -19,6 +18,7 @@
"husky": "^9.0.6",
"oclif": "^4.3.4",
"shx": "0.3.4",
"simple-git": "^3.25.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
Expand Down
4 changes: 2 additions & 2 deletions src/service/extractTestClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function extractTestClasses(
regex: string
): Promise<{ validatedClasses: string; warnings: string[] }> {
const testClasses: Set<string> = new Set();
const matchedMessages = await retrieveCommitMessages(fromRef, toRef, regex);
const { repoRoot, matchedMessages } = await retrieveCommitMessages(fromRef, toRef, regex);

matchedMessages.forEach((message: string) => {
// Split the commit message by commas or spaces
Expand All @@ -28,7 +28,7 @@ export async function extractTestClasses(
let validatedClasses: string = '';
const result =
unvalidatedClasses.length > 0
? await validateClassPaths(unvalidatedClasses, toRef)
? await validateClassPaths(unvalidatedClasses, toRef, repoRoot)
: { validatedClasses: new Set(), warnings: [] };
let sortedClasses: string[] = [];
if (result.validatedClasses.size > 0) {
Expand Down
6 changes: 2 additions & 4 deletions src/service/getPackageDirectories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';

import { SfdxProject } from './types.js';
import { getRepoRoot } from './getRepoRoot.js';

export async function getPackageDirectories(): Promise<{ repoRoot: string; packageDirectories: string[] }> {
const repoRoot = await getRepoRoot();
export async function getPackageDirectories(repoRoot: string): Promise<string[]> {
const dxConfigFilePath = resolve(repoRoot, 'sfdx-project.json');
if (!existsSync(dxConfigFilePath)) {
throw Error(`Cannot find sfdx-project.json in the root folder: ${repoRoot}`);
Expand All @@ -18,5 +16,5 @@ export async function getPackageDirectories(): Promise<{ repoRoot: string; packa
const sfdxProjectRaw: string = await readFile(dxConfigFilePath, 'utf-8');
const sfdxProject: SfdxProject = JSON.parse(sfdxProjectRaw) as SfdxProject;
const packageDirectories = sfdxProject.packageDirectories.map((directory) => directory.path);
return { repoRoot, packageDirectories };
return packageDirectories;
}
53 changes: 36 additions & 17 deletions src/service/retrieveCommitMessages.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
'use strict';

import { readFileSync } from 'node:fs';
import { simpleGit, SimpleGit, SimpleGitOptions, DefaultLogFields, LogResult } from 'simple-git';
import { promises as fsPromises, readFile, stat, readdir } from 'node:fs';
import git from 'isomorphic-git';

import { getRepoRoot } from './getRepoRoot.js';

export async function retrieveCommitMessages(
fromCommit: string,
toCommit: string,
regexFilePath: string
): Promise<string[]> {
const options: Partial<SimpleGitOptions> = {
baseDir: process.cwd(),
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: true,
};
const git: SimpleGit = simpleGit(options);
const result: LogResult<string | DefaultLogFields> = await git.log({ from: fromCommit, to: toCommit, format: '%s' });

// Filter only entries that match the DefaultLogFields type
const commitMessages: string[] = (result.all as DefaultLogFields[]).map((commit) => commit.message);
): Promise<{ repoRoot: string; matchedMessages: string[] }> {
const repoRoot = await getRepoRoot();
process.chdir(repoRoot);
const fs = { promises: fsPromises, readFile, stat, readdir };

// Retrieve the commit logs between the specified commits
const commits = await git.log({
fs,
dir: repoRoot,
ref: toCommit,
});

const commitMessages: string[] = [];
let collectMessages = false;

for (const commit of commits) {
if (commit.oid === toCommit) {
collectMessages = true;
}

if (collectMessages) {
commitMessages.push(commit.commit.message);
}

if (commit.oid === fromCommit) {
break;
}
}

// Read and compile the regex from the specified file
let regex: RegExp;
let regexPattern = '';
const regexPattern: string = (await fsPromises.readFile(regexFilePath, 'utf-8')).trim();
try {
regexPattern = readFileSync(regexFilePath, 'utf-8').trim();
regex = new RegExp(regexPattern, 'g');
} catch (err) {
throw Error(`The regular expression in '${regexFilePath}' is invalid.`);
}

// Filter messages that match the regex
const matchedMessages: string[] = [];
commitMessages.forEach((message) => {
let match;
Expand All @@ -39,5 +58,5 @@ export async function retrieveCommitMessages(
}
});

return matchedMessages;
return { repoRoot, matchedMessages };
}
6 changes: 3 additions & 3 deletions src/service/validateClassPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { getPackageDirectories } from './getPackageDirectories.js';

export async function validateClassPaths(
unvalidatedClasses: string[],
toCommitHash: string
toCommitHash: string,
repoRoot: string
): Promise<{ validatedClasses: Set<string>; warnings: string[] }> {
const { repoRoot, packageDirectories } = await getPackageDirectories();
process.chdir(repoRoot);
const packageDirectories = await getPackageDirectories(repoRoot);
const fs = { promises: fsPromises, readFile, stat, readdir };
const repoFiles = await git.listFiles({ fs, dir: repoRoot, ref: toCommitHash });
const warnings: string[] = [];
Expand Down
21 changes: 14 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1362,14 +1362,14 @@

"@kwsites/file-exists@^1.1.1":
version "1.1.1"
resolved "https://registry.npmjs.org/@kwsites/file-exists/-/file-exists-1.1.1.tgz"
resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
dependencies:
debug "^4.1.1"

"@kwsites/promise-deferred@^1.1.1":
version "1.1.1"
resolved "https://registry.npmjs.org/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz"
resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==

"@nodelib/fs.scandir@2.1.5":
Expand Down Expand Up @@ -3899,6 +3899,13 @@ debug@^3.2.7:
dependencies:
ms "^2.1.1"

debug@^4.3.5:
version "4.3.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.6.tgz#2ab2c38fbaffebf8aa95fdfe6d88438c7a13c52b"
integrity sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==
dependencies:
ms "2.1.2"

debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/debuglog/-/debuglog-1.0.1.tgz"
Expand Down Expand Up @@ -8177,14 +8184,14 @@ simple-get@^4.0.1:
once "^1.3.1"
simple-concat "^1.0.0"

simple-git@^3.24.0:
version "3.24.0"
resolved "https://registry.npmjs.org/simple-git/-/simple-git-3.24.0.tgz"
integrity sha512-QqAKee9Twv+3k8IFOFfPB2hnk6as6Y6ACUpwCtQvRYBAes23Wv3SZlHVobAzqcE8gfsisCvPw3HGW3HYM+VYYw==
simple-git@^3.25.0:
version "3.25.0"
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.25.0.tgz#3666e76d6831f0583dc380645945b97e0ac4aab6"
integrity sha512-KIY5sBnzc4yEcJXW7Tdv4viEz8KyG+nU0hay+DWZasvdFOYKeUZ6Xc25LUHHjw0tinPT7O1eY6pzX7pRT1K8rw==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"
debug "^4.3.4"
debug "^4.3.5"

simple-swizzle@^0.2.2:
version "0.2.2"
Expand Down

0 comments on commit 921f831

Please sign in to comment.