diff --git a/handlebars/helpers/index.js b/handlebars/helpers/index.js index bd0e6b8..59118f6 100644 --- a/handlebars/helpers/index.js +++ b/handlebars/helpers/index.js @@ -230,7 +230,7 @@ function renderTree(object, options) { * @memberOf helpers */ function withPackageOf(filePath, options) { - return resolvePackageRoot(path.resolve(filePath)).then(function (resolvedPackageRoot) { + return resolvePackageRoot(path.resolve(filePath), { posix: true }).then(function (resolvedPackageRoot) { const data = Handlebars.createFrame(options.data) data.url = _githubUrl(resolvedPackageRoot) data.package = resolvedPackageRoot.packageJson @@ -426,7 +426,7 @@ function transformTree(object, fn) { */ function github(filePath) { // Build url to correct version and file in githubs - return resolvePackageRoot(path.resolve(filePath)).then(_githubUrl) + return resolvePackageRoot(path.resolve(filePath), { posix: true }).then(_githubUrl) } /** diff --git a/lib/utils/resolve-package-root.js b/lib/utils/resolve-package-root.js index 488d259..b07d2ee 100644 --- a/lib/utils/resolve-package-root.js +++ b/lib/utils/resolve-package-root.js @@ -12,15 +12,17 @@ module.exports = { resolvePackageRoot } * * **relativeFile**: The path of "file" relative to the packageRoot * * **packageJson**: The required package.json * - * @param file + * @param + * @param {boolean} posix if set to true, use POSIX separator (i.e. "/") instead of OS dependent one. * @return {{packageRoot: string, packageJson: object, relativeFile: string}} the path to the package.json */ -function resolvePackageRoot(file) { +function resolvePackageRoot(file, { posix = false } = {}) { try { - const fullPath = path.resolve(file) - for (let lead = fullPath; path.dirname(lead) !== lead; lead = path.dirname(lead)) { + const chosenPath = posix ? path.posix : path + const fullPath = chosenPath.resolve(file) + for (let lead = fullPath; chosenPath.dirname(lead) !== lead; lead = chosenPath.dirname(lead)) { debug('Looking for package.json in ' + lead) - const packagePath = path.join(lead, 'package.json') + const packagePath = chosenPath.join(lead, 'package.json') try { if (fs.statSync(packagePath).isFile()) { return fs @@ -28,8 +30,8 @@ function resolvePackageRoot(file) { .then(JSON.parse) .then(packageJson => { return { - packageRoot: path.relative(process.cwd(), lead) || '.', - relativeFile: path.relative(lead, fullPath), + packageRoot: chosenPath.relative(process.cwd(), lead) || '.', + relativeFile: chosenPath.relative(lead, fullPath), packageJson } })