Skip to content

Commit c49cfdf

Browse files
committed
Added removal of lock files that are not sitting at root of the repo.
1 parent 43c5db4 commit c49cfdf

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

bin/enforcepm.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ if (lockFiles.hasOwnProperty(chosenPM)) {
1414
const forbiddenLocks = Object.values(lockFiles).filter((lock) => lock !== allowedLock);
1515
const stagedFiles = git(['diff', '--name-only', '--cached']).stdout.trim().split('\n');
1616
for (let lock of forbiddenLocks) {
17-
if (stagedFiles.indexOf(lock) !== -1) {
18-
git(['rm', '-f', lock]);
17+
const forbiddenLocksFound = stagedFiles.filter((sf) => sf.endsWith(lock));
18+
if (forbiddenLocksFound.length) {
19+
for (const flf of forbiddenLocksFound)
20+
git(['rm', '-f', flf]);
1921
const yellow = '\x1b[1;33m';
2022
const green = '\x1b[0;32m';
2123
const blue = '\x1b[0;34m';
2224
const resetColor = '\x1b[0m';
2325
const forbiddenPMUsed = Object.keys(lockFiles).find((pm) => lockFiles[pm] === lock);
2426
console.log(yellow, `WARNING: It looks like you used ${forbiddenPMUsed}. Please remember to use ${chosenPM}.`);
25-
console.log(green, `"${lock}" has been deleted prior to commit.`);
27+
for (const flf of forbiddenLocksFound)
28+
console.log(green, `"${flf}" has been deleted prior to commit.`);
2629
console.log(blue, '\t\t~ enforcepm ~', resetColor);
2730
}
2831
}

src/enforcepm.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ const chosenPM = process.argv[2];
1515

1616
if (lockFiles.hasOwnProperty(chosenPM)) {
1717
const allowedLock = lockFiles[chosenPM as keyof typeof lockFiles];
18-
1918
const forbiddenLocks = Object.values(lockFiles).filter((lock) => lock !== allowedLock);
20-
2119
const stagedFiles = git(['diff', '--name-only', '--cached']).stdout.trim().split('\n');
2220

2321
for (let lock of forbiddenLocks) {
24-
if (stagedFiles.indexOf(lock) !== -1) {
25-
// Remove the forbidden lock file
26-
git(['rm', '-f', lock]);
22+
// Array of all the staged lock files for a given forbidden package manager.
23+
const forbiddenLocksFound = stagedFiles.filter((sf) => sf.endsWith(lock));
24+
25+
if (forbiddenLocksFound.length) {
26+
// Remove the forbidden lock files
27+
for (const flf of forbiddenLocksFound) git(['rm', '-f', flf]);
2728

2829
// Notify
2930
const yellow = '\x1b[1;33m';
@@ -32,7 +33,7 @@ if (lockFiles.hasOwnProperty(chosenPM)) {
3233
const resetColor = '\x1b[0m';
3334
const forbiddenPMUsed = Object.keys(lockFiles).find((pm) => lockFiles[pm as keyof typeof lockFiles] === lock);
3435
console.log(yellow, `WARNING: It looks like you used ${forbiddenPMUsed}. Please remember to use ${chosenPM}.`);
35-
console.log(green, `"${lock}" has been deleted prior to commit.`);
36+
for (const flf of forbiddenLocksFound) console.log(green, `"${flf}" has been deleted prior to commit.`);
3637
console.log(blue, '\t\t~ enforcepm ~', resetColor);
3738
}
3839
}

0 commit comments

Comments
 (0)