Skip to content

Commit 00dd0ef

Browse files
committed
feat: support referencing other ignore-sync files
1 parent 4962c07 commit 00dd0ef

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ $RECYCLE.BIN/
200200
# Windows shortcuts
201201
*.lnk
202202

203+
204+
203205
__fixtures__/
204206
__mocks__/
205207
__tests__/

.npmignore-sync

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
[github/gitignore]
2-
Node.gitignore
3-
Global/macOS.gitignore
4-
Global/Linux.gitignore
5-
Global/Windows.gitignore
1+
[local]
2+
.gitignore-sync
3+
4+
[relative]
5+
./src/.testignore-sync
66

77
[inline]
88
__fixtures__/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ yarn.lock
5858
- `[local]`
5959
- the content of these local files will be copied directly to generated ignore file
6060
- support glob pattern, e.g. `packages/**/.gitignore`
61+
- support referencing other ignore-sync files, e.g. referencing `.gitignore-sync` in `.npmignore-sync`
6162
- `[relative]`
6263

6364
- the content of these local files will be copied with **relative path prefix** to generated ignore file
6465
- support glob pattern, e.g. `packages/**/.gitignore`
66+
- support referencing other ignore-sync files, e.g. referencing `.gitignore-sync` in `.npmignore-sync`
6567
- example
6668

6769
```ini

src/generateIgnoreFile.js

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const decodeIgnoreSyncFile = require('./decodeIgnoreSyncFile')
77
const formatRelativeIgnoreFile = require('./utils/formatRelativeIgnoreFile')
88
const github = require('./utils/github')
99
const highlightComments = require('./utils/highlightComments')
10+
const isIgnoreSyncFile = require('./isIgnoreSyncFile')
1011
const joinLinesWithEOF = require('./utils/joinLinesWithEOF')
1112
const { COMMENT_HEADER_ALERT } = require('./constants')
1213
const { dynamicComposeP, promiseMap } = require('./utils/ramdaHelper')
@@ -19,29 +20,60 @@ const sourceIs = (...args) => R.compose(...args, R.prop('source'))
1920
const inlineSourceFetcher = R.compose(joinLinesWithEOF, R.prop('data'))
2021
const githubSourceFetcher = async (block) => {
2122
const [owner, repo] = block.source.split('/')
22-
const files = await promiseMap(
23-
(relativePath) =>
24-
github.getContentFile({ owner, repo, path: relativePath }),
25-
block.data,
23+
const files = await Promise.all(
24+
block.data.map((relativeFilePath) => {
25+
return github.getContentFile({ owner, repo, path: relativeFilePath })
26+
}),
2627
)
2728
return joinLinesWithEOF(files)
2829
}
2930
const localSourceFetcher = async (block, directory) => {
30-
const files = await promiseMap(
31-
(relativeFilePath) => readFile(path.join(directory, relativeFilePath)),
32-
block.data,
31+
const files = await Promise.all(
32+
block.data.map(async (relativeFilePath) => {
33+
const fileContent = await readFile(path.join(directory, relativeFilePath))
34+
if (isIgnoreSyncFile(relativeFilePath)) {
35+
return generateIgnoreFile(fileContent, directory, {
36+
isRootIgnoreSyncFile: false,
37+
})
38+
} else {
39+
return fileContent
40+
}
41+
}),
3342
)
3443
return joinLinesWithEOF(files)
3544
}
3645
const relativeSourceFetcher = async (block, directory) => {
37-
const files = await promiseMap(async (relativeFilePath) => {
38-
const fileContent = await readFile(path.join(directory, relativeFilePath))
39-
return formatRelativeIgnoreFile(fileContent, path.dirname(relativeFilePath))
40-
}, block.data)
46+
const files = await Promise.all(
47+
block.data.map(async (relativeFilePath) => {
48+
const fileContent = await readFile(path.join(directory, relativeFilePath))
49+
if (isIgnoreSyncFile(relativeFilePath)) {
50+
const ignoreFileContent = await generateIgnoreFile(
51+
fileContent,
52+
directory,
53+
{
54+
isRootIgnoreSyncFile: false,
55+
},
56+
)
57+
return formatRelativeIgnoreFile(
58+
ignoreFileContent,
59+
path.dirname(relativeFilePath),
60+
)
61+
} else {
62+
return formatRelativeIgnoreFile(
63+
fileContent,
64+
path.dirname(relativeFilePath),
65+
)
66+
}
67+
}),
68+
)
4169
return joinLinesWithEOF(files)
4270
}
4371

44-
const generateIgnoreFile = (ignoreSyncFile, directory) => {
72+
const generateIgnoreFile = (
73+
ignoreSyncFile,
74+
directory,
75+
{ isRootIgnoreSyncFile = true } = {},
76+
) => {
4577
const fetchIgnorePatternsBySource = promiseMap(
4678
R.cond([
4779
[sourceIs(R.equals('inline')), inlineSourceFetcher],
@@ -65,7 +97,7 @@ const generateIgnoreFile = (ignoreSyncFile, directory) => {
6597

6698
return dynamicComposeP(
6799
joinLinesWithEOF,
68-
prependAlert,
100+
isRootIgnoreSyncFile ? prependAlert : R.identity,
69101
fetchIgnorePatternsBySource,
70102
decodeIgnoreSyncFile,
71103
)(ignoreSyncFile)

0 commit comments

Comments
 (0)