Skip to content

Commit 26b5fa7

Browse files
ta2edchimpKent C. Dodds
authored andcommitted
fix(hook): handle node module path os agnostically (#114)
* fix(hook): detemine hook name path separator agnostically Former `hook` name determination was a simple regex using *nix style path separator `/`, using `path.basename`, the required last part gets returned, independent of an os specific path separator. * fix(hook): correctly escape rendered `node_modules_path` string
1 parent 32edf9c commit 26b5fa7

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

lib/hook.template.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const fs = require('fs')
2-
const resolve = require('path').resolve
3-
const path = require('path')
2+
const {resolve, join} = require('path')
43

54
exports.generatedMessage = 'Generated by ghooks. Do not edit this file.'
65

76
exports.content = fs
87
.readFileSync(resolve(`${__dirname}/hook.template.raw`), 'UTF-8')
98
.replace('{{generated_message}}', exports.generatedMessage)
10-
.replace('{{node_modules_path}}', path.join(process.cwd(), '..'))
9+
.replace('{{node_modules_path}}', join(process.cwd(), '..').replace(/\\/g, '\\'))

lib/runner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const clone = require('lodash.clone')
22
const managePath = require('manage-path')
33
const spawn = require('spawn-command')
4-
const resolve = require('path').resolve
4+
const {resolve, basename} = require('path')
55
const findup = require('findup')
66
const fs = require('fs')
77

@@ -13,7 +13,7 @@ module.exports = function run(nodeModulesPath, filename, env) {
1313
}
1414

1515
function hook(filename) {
16-
return filename.match(/\/([^\/]+)\/?$/)[1]
16+
return basename(filename)
1717
}
1818

1919
// replace any instance of $1 or $2 etc. to that item as an process.argv

0 commit comments

Comments
 (0)