Skip to content

Commit d7629e1

Browse files
author
Alexander Lisianoi
committed
Add getProjectPath() to compute path to parent project
Previously, process.cwd() did the job just fine but with node v8.1.2 (compared to 7.10.0 at least) the output has changed. So, use the following hack: when installing a node package, npm adds it to PATH. Since we know: a) the suffix of that path entry (commitplease/node_modules/.bin) b) the fact that the other project is installing commitplease then find that entry and use its prefix as parent project path Refs: jquery/jquery#3708
1 parent c219f17 commit d7629e1

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,37 @@ var validate = require('./lib/validate')
1010
var sanitize = require('./lib/sanitize')
1111
var defaults = require('./lib/defaults')
1212

13+
// Need to find the path to the project that is installing
14+
// commitplease. Previously, process.cwd() made the job easy but its
15+
// output changed with node v8.1.2
16+
function getProjectPath () {
17+
// Use the fact that npm will inject a path that ends with
18+
// commitplease/node_modules/.bin into PATH
19+
var p = process.env.PATH.split(':').filter(
20+
function (p) {
21+
return p.endsWith(path.join('commitplease', 'node_modules', '.bin'))
22+
}
23+
)
24+
25+
if (p.length !== 1) {
26+
console.error(chalk.red('Failed to find project path\n'))
27+
}
28+
29+
// Removing suffix node_modules/commitplease/node_modules/.bin will
30+
// give the absolute path to the project root
31+
p = p[0].split(path.sep)
32+
p = p.slice(0, p.length - 4)
33+
34+
return path.sep + p.join(path.sep)
35+
}
36+
1337
function getOptions () {
14-
var pkg = path.join(process.cwd(), 'package.json')
15-
var npm = path.join(process.cwd(), '.npmrc')
38+
var projectPath = getProjectPath()
39+
40+
console.log(projectPath)
41+
42+
var pkg = path.join(projectPath, 'package.json')
43+
var npm = path.join(projectPath, '.npmrc')
1644

1745
pkg = fs.existsSync(pkg) && require(pkg) || {}
1846
npm = fs.existsSync(npm) && ini.parse(fs.readFileSync(npm, 'utf8')) || {}

0 commit comments

Comments
 (0)