Skip to content

Commit 43c5db4

Browse files
committed
Added a little error handling.
1 parent 670122e commit 43c5db4

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

bin/enforcepm.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const chosenPM = process.argv[2];
1212
if (lockFiles.hasOwnProperty(chosenPM)) {
1313
const allowedLock = lockFiles[chosenPM];
1414
const forbiddenLocks = Object.values(lockFiles).filter((lock) => lock !== allowedLock);
15-
const stagedFiles = git(['diff', '--name-only', '--cached']).stdout.split('\n');
16-
stagedFiles.pop();
15+
const stagedFiles = git(['diff', '--name-only', '--cached']).stdout.trim().split('\n');
1716
for (let lock of forbiddenLocks) {
1817
if (stagedFiles.indexOf(lock) !== -1) {
1918
git(['rm', '-f', lock]);
@@ -28,3 +27,6 @@ if (lockFiles.hasOwnProperty(chosenPM)) {
2827
}
2928
}
3029
}
30+
else {
31+
throw new Error(`"${chosenPM}" is not supported by enforcepm.\n\tFor a list of supported package managers, visit https://github.com/DaviDevMod/enforcepm#usage`);
32+
}

src/enforcepm.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
import cp = require('child_process');
33

4-
// A map of package managers and relative lock files supported by enforcepm
4+
// An hash table of the lock files of the supported package managers
55
const lockFiles = {
66
npm: 'package-lock.json',
77
yarn: 'yarn.lock',
@@ -18,12 +18,11 @@ if (lockFiles.hasOwnProperty(chosenPM)) {
1818

1919
const forbiddenLocks = Object.values(lockFiles).filter((lock) => lock !== allowedLock);
2020

21-
const stagedFiles = git(['diff', '--name-only', '--cached']).stdout.split('\n');
22-
stagedFiles.pop(); // pop trailing empty string
21+
const stagedFiles = git(['diff', '--name-only', '--cached']).stdout.trim().split('\n');
2322

2423
for (let lock of forbiddenLocks) {
2524
if (stagedFiles.indexOf(lock) !== -1) {
26-
// Remove the staged forbidden lock file
25+
// Remove the forbidden lock file
2726
git(['rm', '-f', lock]);
2827

2928
// Notify
@@ -37,4 +36,8 @@ if (lockFiles.hasOwnProperty(chosenPM)) {
3736
console.log(blue, '\t\t~ enforcepm ~', resetColor);
3837
}
3938
}
39+
} else {
40+
throw new Error(
41+
`"${chosenPM}" is not supported by enforcepm.\n\tFor a list of supported package managers, visit https://github.com/DaviDevMod/enforcepm#usage`
42+
);
4043
}

0 commit comments

Comments
 (0)