Skip to content

Commit 7c428fb

Browse files
author
Alexander Lisianoi
committed
Add sliceEnvPath to reuse the slicing hack in 2 places:
- during install, look for `node_modules/commitplease/node_modules/.bin` - during usual trigger, look for `node_modules/.bin`
1 parent 67f5ab1 commit 7c428fb

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

index.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,44 @@ var validate = require('./lib/validate')
1111
var sanitize = require('./lib/sanitize')
1212
var defaults = require('./lib/defaults')
1313

14+
function sliceEnvPath (suffix) {
15+
var p = process.env.PATH.split(':').filter(
16+
function (p) {return endsWith(p, suffix)}
17+
)
18+
19+
if (p.length === 1) {
20+
p = p[0].split(path.sep)
21+
22+
p = p.slice(0, p.length - suffix.split(path.sep).length)
23+
return p.join(path.sep)
24+
}
25+
26+
return undefined
27+
}
28+
1429
// Need to find the path to the project that is installing
1530
// commitplease. Previously, process.cwd() made the job easy but its
1631
// output changed with node v8.1.2 (at least compared to 7.10.0)
1732
function getProjectPath () {
1833
// Use the fact that npm will inject a path that ends with
1934
// commitplease/node_modules/.bin into process.env.PATH
20-
var p = process.env.PATH.split(':').filter(
21-
function (p) {
22-
return endsWith(p, path.join('commitplease', 'node_modules', '.bin'))
23-
}
35+
var p = sliceEnvPath(
36+
path.join('node_modules', 'commitplease', 'node_modules', '.bin')
2437
)
2538

26-
if (p.length !== 1) {
27-
console.error(chalk.red('Failed to find project path\n'))
28-
29-
// Just leave with zero so as not to interrupt install
30-
process.exit(0)
39+
if (p !== undefined) {
40+
return p
3141
}
3242

33-
// Removing suffix node_modules/commitplease/node_modules/.bin will
34-
// give the absolute path to the project root
35-
p = p[0].split(path.sep)
36-
p = p.slice(0, p.length - 4)
43+
// Try a less specific suffix:
44+
p = sliceEnvPath(path.join('node_modules', '.bin'))
45+
46+
if (p !== undefined) {
47+
return p
48+
}
3749

38-
return path.sep + p.join(path.sep)
50+
console.error(chalk.red('Could not find project path'))
51+
process.exit(0)
3952
}
4053

4154
function getOptions () {

0 commit comments

Comments
 (0)