@@ -11,31 +11,44 @@ var validate = require('./lib/validate')
11
11
var sanitize = require ( './lib/sanitize' )
12
12
var defaults = require ( './lib/defaults' )
13
13
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
+
14
29
// Need to find the path to the project that is installing
15
30
// commitplease. Previously, process.cwd() made the job easy but its
16
31
// output changed with node v8.1.2 (at least compared to 7.10.0)
17
32
function getProjectPath ( ) {
18
33
// Use the fact that npm will inject a path that ends with
19
34
// 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' )
24
37
)
25
38
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
31
41
}
32
42
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
+ }
37
49
38
- return path . sep + p . join ( path . sep )
50
+ console . error ( chalk . red ( 'Could not find project path' ) )
51
+ process . exit ( 0 )
39
52
}
40
53
41
54
function getOptions ( ) {
0 commit comments