Skip to content

Commit

Permalink
feat: support referencing other ignore-sync files
Browse files Browse the repository at this point in the history
  • Loading branch information
foray1010 committed Oct 6, 2022
1 parent 4962c07 commit 00dd0ef
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk



__fixtures__/
__mocks__/
__tests__/
Expand Down
10 changes: 5 additions & 5 deletions .npmignore-sync
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[github/gitignore]
Node.gitignore
Global/macOS.gitignore
Global/Linux.gitignore
Global/Windows.gitignore
[local]
.gitignore-sync

[relative]
./src/.testignore-sync

[inline]
__fixtures__/
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ yarn.lock
- `[local]`
- the content of these local files will be copied directly to generated ignore file
- support glob pattern, e.g. `packages/**/.gitignore`
- support referencing other ignore-sync files, e.g. referencing `.gitignore-sync` in `.npmignore-sync`
- `[relative]`

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

```ini
Expand Down
58 changes: 45 additions & 13 deletions src/generateIgnoreFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const decodeIgnoreSyncFile = require('./decodeIgnoreSyncFile')
const formatRelativeIgnoreFile = require('./utils/formatRelativeIgnoreFile')
const github = require('./utils/github')
const highlightComments = require('./utils/highlightComments')
const isIgnoreSyncFile = require('./isIgnoreSyncFile')
const joinLinesWithEOF = require('./utils/joinLinesWithEOF')
const { COMMENT_HEADER_ALERT } = require('./constants')
const { dynamicComposeP, promiseMap } = require('./utils/ramdaHelper')
Expand All @@ -19,29 +20,60 @@ 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 files = await promiseMap(
(relativePath) =>
github.getContentFile({ owner, repo, path: relativePath }),
block.data,
const files = await Promise.all(
block.data.map((relativeFilePath) => {
return github.getContentFile({ owner, repo, path: relativeFilePath })
}),
)
return joinLinesWithEOF(files)
}
const localSourceFetcher = async (block, directory) => {
const files = await promiseMap(
(relativeFilePath) => readFile(path.join(directory, relativeFilePath)),
block.data,
const files = await Promise.all(
block.data.map(async (relativeFilePath) => {
const fileContent = await readFile(path.join(directory, relativeFilePath))
if (isIgnoreSyncFile(relativeFilePath)) {
return generateIgnoreFile(fileContent, directory, {
isRootIgnoreSyncFile: false,
})
} else {
return fileContent
}
}),
)
return joinLinesWithEOF(files)
}
const relativeSourceFetcher = async (block, directory) => {
const files = await promiseMap(async (relativeFilePath) => {
const fileContent = await readFile(path.join(directory, relativeFilePath))
return formatRelativeIgnoreFile(fileContent, path.dirname(relativeFilePath))
}, block.data)
const files = await Promise.all(
block.data.map(async (relativeFilePath) => {
const fileContent = await readFile(path.join(directory, relativeFilePath))
if (isIgnoreSyncFile(relativeFilePath)) {
const ignoreFileContent = await generateIgnoreFile(
fileContent,
directory,
{
isRootIgnoreSyncFile: false,
},
)
return formatRelativeIgnoreFile(
ignoreFileContent,
path.dirname(relativeFilePath),
)
} else {
return formatRelativeIgnoreFile(
fileContent,
path.dirname(relativeFilePath),
)
}
}),
)
return joinLinesWithEOF(files)
}

const generateIgnoreFile = (ignoreSyncFile, directory) => {
const generateIgnoreFile = (
ignoreSyncFile,
directory,
{ isRootIgnoreSyncFile = true } = {},
) => {
const fetchIgnorePatternsBySource = promiseMap(
R.cond([
[sourceIs(R.equals('inline')), inlineSourceFetcher],
Expand All @@ -65,7 +97,7 @@ const generateIgnoreFile = (ignoreSyncFile, directory) => {

return dynamicComposeP(
joinLinesWithEOF,
prependAlert,
isRootIgnoreSyncFile ? prependAlert : R.identity,
fetchIgnorePatternsBySource,
decodeIgnoreSyncFile,
)(ignoreSyncFile)
Expand Down

0 comments on commit 00dd0ef

Please sign in to comment.