From 26b5fa74448ca988d8e79dbb7582872573332b89 Mon Sep 17 00:00:00 2001 From: Andreas Windt Date: Mon, 4 Jul 2016 14:01:00 +0200 Subject: [PATCH] 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 --- lib/hook.template.js | 5 ++--- lib/runner.js | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/hook.template.js b/lib/hook.template.js index 877955f..055189c 100644 --- a/lib/hook.template.js +++ b/lib/hook.template.js @@ -1,10 +1,9 @@ const fs = require('fs') -const resolve = require('path').resolve -const path = require('path') +const {resolve, join} = require('path') exports.generatedMessage = 'Generated by ghooks. Do not edit this file.' exports.content = fs .readFileSync(resolve(`${__dirname}/hook.template.raw`), 'UTF-8') .replace('{{generated_message}}', exports.generatedMessage) - .replace('{{node_modules_path}}', path.join(process.cwd(), '..')) + .replace('{{node_modules_path}}', join(process.cwd(), '..').replace(/\\/g, '\\')) diff --git a/lib/runner.js b/lib/runner.js index c934b3a..003c6af 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -1,7 +1,7 @@ const clone = require('lodash.clone') const managePath = require('manage-path') const spawn = require('spawn-command') -const resolve = require('path').resolve +const {resolve, basename} = require('path') const findup = require('findup') const fs = require('fs') @@ -13,7 +13,7 @@ module.exports = function run(nodeModulesPath, filename, env) { } function hook(filename) { - return filename.match(/\/([^\/]+)\/?$/)[1] + return basename(filename) } // replace any instance of $1 or $2 etc. to that item as an process.argv