From 00dd0ef1f862eada9a0028a272cf9dee7815bd96 Mon Sep 17 00:00:00 2001 From: Alex Young Date: Thu, 6 Oct 2022 19:32:18 +0100 Subject: [PATCH] feat: support referencing other ignore-sync files --- .npmignore | 2 ++ .npmignore-sync | 10 +++---- README.md | 2 ++ src/generateIgnoreFile.js | 58 ++++++++++++++++++++++++++++++--------- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/.npmignore b/.npmignore index 212f619..7b20027 100644 --- a/.npmignore +++ b/.npmignore @@ -200,6 +200,8 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk + + __fixtures__/ __mocks__/ __tests__/ diff --git a/.npmignore-sync b/.npmignore-sync index abdd868..42f2eaf 100644 --- a/.npmignore-sync +++ b/.npmignore-sync @@ -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__/ diff --git a/README.md b/README.md index 3c7f470..118bfbf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/generateIgnoreFile.js b/src/generateIgnoreFile.js index 50f20b5..893f0d0 100644 --- a/src/generateIgnoreFile.js +++ b/src/generateIgnoreFile.js @@ -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') @@ -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], @@ -65,7 +97,7 @@ const generateIgnoreFile = (ignoreSyncFile, directory) => { return dynamicComposeP( joinLinesWithEOF, - prependAlert, + isRootIgnoreSyncFile ? prependAlert : R.identity, fetchIgnorePatternsBySource, decodeIgnoreSyncFile, )(ignoreSyncFile)