diff --git a/handlebars/helpers/index.js b/handlebars/helpers/index.js index 2bce7da..ba3139b 100644 --- a/handlebars/helpers/index.js +++ b/handlebars/helpers/index.js @@ -485,8 +485,9 @@ function arr(...args) { function _githubUrl(resolvedPackageRoot) { const { packageJson, relativeFile } = resolvedPackageRoot const url = repoWebUrl(packageJson && packageJson.repository && packageJson.repository.url) + const relativePathWithinRepo = _relativePathWithinRepository(packageJson, relativeFile) if (url && url.match(/github.com/)) { - return `${url}/blob/v${packageJson.version}/${relativeFile}` + return `${url}/blob/v${packageJson.version}/${relativePathWithinRepo}` } } @@ -497,8 +498,10 @@ function _githubUrl(resolvedPackageRoot) { function _rawGithubUrl(resolvedPackageRoot) { const { packageJson, relativeFile } = resolvedPackageRoot const orgRepo = _githubOrgRepo(packageJson && packageJson.repository && packageJson.repository.url) + const relativePathWithinRepo = _relativePathWithinRepository(packageJson, relativeFile) + if (orgRepo) { - return `https://raw.githubusercontent.com/${orgRepo}/v${packageJson.version}/${relativeFile}` + return `https://raw.githubusercontent.com/${orgRepo}/v${packageJson.version}/${relativePathWithinRepo}` } } @@ -515,3 +518,12 @@ function _githubOrgRepo(gitUrl) { const match = gitUrl.match(/.*?(:\/\/|@)github\.com[/:](.*?)(#.*?)?$/) return match && match[2] && match[2].replace(/\.git$/, '') } + +function _relativePathWithinRepository(packageJson, fileRelativeToPackageJson) { + const directory = packageJson && packageJson.repository && packageJson.repository.directory + if (directory) { + // This is a monorepo and the package.json is in a sub-directory + return `${directory}/${fileRelativeToPackageJson}` + } + return fileRelativeToPackageJson +} diff --git a/test/fixtures/include/github.default.md b/test/fixtures/include/github.default.md deleted file mode 100644 index b1104fc..0000000 --- a/test/fixtures/include/github.default.md +++ /dev/null @@ -1 +0,0 @@ -https://github.com/nknapp/thought/blob/THOUGHT_VERSION/test/fixtures/shout.js \ No newline at end of file diff --git a/test/fixtures/include/github.dependency.md b/test/fixtures/include/github.dependency.md deleted file mode 100644 index fabbc7b..0000000 --- a/test/fixtures/include/github.dependency.md +++ /dev/null @@ -1 +0,0 @@ -https://github.com/bootprint/customize/blob/CUSTOMIZE_VERSION/helpers-io.js \ No newline at end of file diff --git a/test/helper-spec.js b/test/helper-spec.js index f312570..eb594d0 100644 --- a/test/helper-spec.js +++ b/test/helper-spec.js @@ -319,7 +319,9 @@ describe('thought-helpers:', function() { return expectHbs('{{#withPackageOf file}}{{@rawUrl}}{{/withPackageOf}}', { file: require.resolve('customize/helpers-io.js') }).to.eventually.equal( - versions('https://raw.githubusercontent.com/bootprint/customize/CUSTOMIZE_VERSION/helpers-io.js') + versions( + 'https://raw.githubusercontent.com/bootprint/bootprint-monorepo/CUSTOMIZE_VERSION/packages/customize/helpers-io.js' + ) ) }) @@ -351,13 +353,15 @@ describe('thought-helpers:', function() { describe('The "github"-helper', function() { it('should create a url to file on github (based on the current package version)', function() { return expectHbs('{{github file}}', { file: 'test/fixtures/shout.js' }).to.eventually.equal( - versions(fixture('include/github.default.md')) + versions('https://github.com/nknapp/thought/blob/THOUGHT_VERSION/test/fixtures/shout.js') ) }) it('should create a url files in dependency projects (based on the their current package version)', function() { return expectHbs('{{github file}}', { file: require.resolve('customize/helpers-io.js') }).to.eventually.equal( - versions(fixture('include/github.dependency.md')) + versions( + 'https://github.com/bootprint/bootprint-monorepo/blob/CUSTOMIZE_VERSION/packages/customize/helpers-io.js' + ) ) }) })