Skip to content

Commit

Permalink
fix: support for modules inside a mono-repo
Browse files Browse the repository at this point in the history
The "github"- and "withPackageOf"-helper now use the
"repository.directory" value of a dependency's
package.json to compute the url to files correctly
if the dependency is located in a subdirectory of
the git repository.
  • Loading branch information
nknapp committed Jan 27, 2020
1 parent 29f566c commit 1346e33
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
16 changes: 14 additions & 2 deletions handlebars/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
}

Expand All @@ -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}`
}
}

Expand All @@ -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
}
1 change: 0 additions & 1 deletion test/fixtures/include/github.default.md

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/include/github.dependency.md

This file was deleted.

10 changes: 7 additions & 3 deletions test/helper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
)
})

Expand Down Expand Up @@ -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'
)
)
})
})
Expand Down

0 comments on commit 1346e33

Please sign in to comment.