Skip to content

Commit

Permalink
fix: use default branch instead of master
Browse files Browse the repository at this point in the history
  • Loading branch information
risu729 committed May 17, 2023
1 parent 93c402c commit 993b749
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ corepack enable
packages/a/ignored.md
```

- `[$username/$repo]`
- `[$username/$repo#$ref]`
- the content of these github files will be downloaded and appended to generated ignore file
- recommend using ignore patterns from [[github/gitignore]](https://github.com/github/gitignore)
- `$ref` is optional, default to the default branch

1. `npm run ignore-sync`
7 changes: 4 additions & 3 deletions src/cleanupIgnoreSyncFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import * as R from 'ramda'
import { COMMENT_CHAR, LINE_BREAK } from './constants.js'

const removeEmptyLines = R.reject((line) => line === '')
const removeTrailingSpacesAndComment = R.replace(
new RegExp(`\\s*(${COMMENT_CHAR}.*)?$`),
'',
const removeTrailingSpacesAndComment = R.ifElse(
R.test(/^\[(.*)\]/),
R.replace(new RegExp(`].*$`), ']'),
R.replace(new RegExp(`\\s*(${COMMENT_CHAR}.*)?$`), ''),
)

const cleanupIgnoreSyncFile = R.compose(
Expand Down
10 changes: 10 additions & 0 deletions src/cleanupIgnoreSyncFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ describe('cleanupIgnoreSyncFile', () => {
expect(cleanupIgnoreSyncFile('pattern #')).toBe('pattern')
})

test('should not remove comments inside source tags', () => {
expect(cleanupIgnoreSyncFile('[owner/repo#ref]')).toBe('[owner/repo#ref]')
expect(cleanupIgnoreSyncFile('[owner/repo#ref] # comment')).toBe(
'[owner/repo#ref]',
)
expect(cleanupIgnoreSyncFile('[owner/repo#ref] ')).toBe(
'[owner/repo#ref]',
)
})

test('should remove empty lines', () => {
expect(cleanupIgnoreSyncFile('\n\n\npat\n\n\ntern\n\n\n')).toBe('pat\ntern')
})
Expand Down
9 changes: 5 additions & 4 deletions src/generateIgnoreFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import highlightComments from './utils/highlightComments.js'
import joinLinesWithEOF from './utils/joinLinesWithEOF.js'
import { dynamicComposeP, promiseMap } from './utils/ramdaHelper.js'

const isGithubSource = R.test(/^([\w.-]+\/[\w.-]+)$/i)
const githubSourceRegex = /^([\w.-]+)\/([\w.-]+)(?:#(.+))?$/i

const prependAlert = R.concat([highlightComments(COMMENT_HEADER_ALERT), ''])
const sourceIs = (...args) => R.compose(...args, R.prop('source'))

const inlineSourceFetcher = R.compose(joinLinesWithEOF, R.prop('data'))
const githubSourceFetcher = async (block) => {
const [owner, repo] = block.source.split('/')
const [, owner, repo, ref] = block.source.match(githubSourceRegex)
const files = await Promise.all(
block.data.map((relativeFilePath) => {
return getGitHubContentFile({ owner, repo, path: relativeFilePath })
return getGitHubContentFile({ owner, repo, ref, path: relativeFilePath })
}),
)
return joinLinesWithEOF(files)
Expand Down Expand Up @@ -82,7 +83,7 @@ const generateIgnoreFile = (
sourceIs(R.equals('relative')),
(block) => relativeSourceFetcher(block, directory),
],
[sourceIs(isGithubSource), githubSourceFetcher],
[sourceIs(R.test(githubSourceRegex)), githubSourceFetcher],
[
R.T,
(block) => {
Expand Down
16 changes: 14 additions & 2 deletions src/utils/github.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import axios from 'axios'
import * as R from 'ramda'

const getDefaultBranch = R.memoizeWith(
({ owner, repo }) => `${owner}/${repo}`,
async ({ owner, repo }) => {
const { data: repoData } = await axios.get(
`https://api.github.com/repos/${owner}/${repo}`,
)
return repoData.default_branch
},
)

export const getGitHubContentFile = async ({
owner,
path,
ref = 'master', // commit/branch/tag
repo,
ref, // commit/branch/tag
path,
}) => {
ref = ref ?? (await getDefaultBranch({ owner, repo }))
const { data: file } = await axios.get(
`https://raw.githubusercontent.com/${owner}/${repo}/${ref}/${path}`,
)
Expand Down

0 comments on commit 993b749

Please sign in to comment.