diff --git a/.changeset/pretty-dragons-know.md b/.changeset/pretty-dragons-know.md new file mode 100644 index 000000000..419bd47a2 --- /dev/null +++ b/.changeset/pretty-dragons-know.md @@ -0,0 +1,5 @@ +--- +'skuba': patch +--- + +**configure:** Reserve skuba-managed sections in ignore files diff --git a/src/cli/configure/analysis/__snapshots__/project.test.ts.snap b/src/cli/configure/analysis/__snapshots__/project.test.ts.snap index d4954d7b9..20675e8ef 100644 --- a/src/cli/configure/analysis/__snapshots__/project.test.ts.snap +++ b/src/cli/configure/analysis/__snapshots__/project.test.ts.snap @@ -3,7 +3,8 @@ exports[`diffFiles works from scratch 1`] = ` Object { ".dockerignore": Object { - "data": ".idea/ + "data": "# managed by skuba +.idea/ .serverless/ .vscode/ node_modules*/ @@ -17,11 +18,13 @@ node_modules*/ .npmrc npm-debug.log yarn-error.log +# end managed by skuba ", "operation": "A", }, ".eslintignore": Object { - "data": ".idea/ + "data": "# managed by skuba +.idea/ .serverless/ .vscode/ node_modules*/ @@ -30,6 +33,7 @@ node_modules*/ /dist*/ /lib*/ /tmp*/ +# end managed by skuba ", "operation": "A", }, @@ -48,7 +52,8 @@ node_modules*/ "operation": "A", }, ".gitignore": Object { - "data": ".idea/ + "data": "# managed by skuba +.idea/ .serverless/ .vscode/ node_modules*/ @@ -64,11 +69,13 @@ node_modules*/ npm-debug.log package-lock.json yarn-error.log +# end managed by skuba ", "operation": "A", }, ".prettierignore": Object { - "data": ".idea/ + "data": "# managed by skuba +.idea/ .serverless/ .vscode/ node_modules*/ @@ -81,6 +88,7 @@ node_modules*/ # Gantry resource files support non-standard template syntax gantry*.yaml gantry*.yml +# end managed by skuba ", "operation": "A", }, diff --git a/src/cli/configure/processing/__snapshots__/ignoreFile.test.ts.snap b/src/cli/configure/processing/__snapshots__/ignoreFile.test.ts.snap index 3dd466b4d..e0a1a4a86 100644 --- a/src/cli/configure/processing/__snapshots__/ignoreFile.test.ts.snap +++ b/src/cli/configure/processing/__snapshots__/ignoreFile.test.ts.snap @@ -1,44 +1,58 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`mergeWithIgnoreFile difference 1`] = ` -"node_modules - -.DS_Store +exports[`mergeWithIgnoreFile empty provided 1`] = ` +"# managed by skuba +node_modules +# end managed by skuba " `; -exports[`mergeWithIgnoreFile empty 1`] = ` -" -" +exports[`mergeWithIgnoreFile identical 1`] = ` +"# managed by skuba +node_modules +# end managed by skuba" `; -exports[`mergeWithIgnoreFile empty base 1`] = ` -"node_modules -" -`; +exports[`mergeWithIgnoreFile provided with managed section and additional lines 1`] = ` +".idea +.vscode -exports[`mergeWithIgnoreFile empty provided 1`] = ` -"node_modules -" -`; +# managed by skuba +node_modules +# end managed by skuba -exports[`mergeWithIgnoreFile identical 1`] = ` -"node_modules +.DS_Store +node_modules " `; -exports[`mergeWithIgnoreFile mix 1`] = ` -".DS_Store +exports[`mergeWithIgnoreFile provided with no managed section 1`] = ` +"# managed by skuba +node_modules +# end managed by skuba -yarn-error.log +.DS_Store +" +`; +exports[`mergeWithIgnoreFile provided with outdated managed section 1`] = ` +"# managed by skuba node_modules +.DS_Store +# end managed by skuba " `; -exports[`mergeWithIgnoreFile whitespace 1`] = ` -"node_modules +exports[`mergeWithIgnoreFile provided with outdated managed section and additional lines 1`] = ` +".idea +.vscode + +# managed by skuba +node_modules +.DS_Store +# end managed by skuba .DS_Store +node_modules " `; diff --git a/src/cli/configure/processing/ignoreFile.test.ts b/src/cli/configure/processing/ignoreFile.test.ts index b0c1bf9e4..2b3be3778 100644 --- a/src/cli/configure/processing/ignoreFile.test.ts +++ b/src/cli/configure/processing/ignoreFile.test.ts @@ -1,17 +1,30 @@ import { mergeWithIgnoreFile } from './ignoreFile'; describe('mergeWithIgnoreFile', () => { + const baseTemplate = + '# managed by skuba\nnode_modules\n# end managed by skuba'; + const updatedBaseTemplate = + '# managed by skuba\nnode_modules\n.DS_Store\n# end managed by skuba'; + const cases = [ - ['difference', 'node_modules', '.DS_Store'], - ['empty', '', ''], - ['empty base', '', 'node_modules'], - ['empty provided', 'node_modules', ''], - ['identical', 'node_modules', 'node_modules'], - ['mix', '.DS_Store\n\nyarn-error.log', '\n\n.DS_Store\n\nnode_modules'], + ['empty provided', baseTemplate, ''], + + ['provided with no managed section', baseTemplate, '.DS_Store\n'], + [ + 'provided with outdated managed section', + updatedBaseTemplate, + `${baseTemplate}\n`, + ], + ['identical', baseTemplate, baseTemplate], + [ + 'provided with managed section and additional lines', + baseTemplate, + `.idea\n.vscode\n\n${baseTemplate}\n\n.DS_Store\nnode_modules\n`, + ], [ - 'whitespace', - '\n \n node_modules \n\n \n ', - '\n \n .DS_Store\n\n ', + 'provided with outdated managed section and additional lines', + updatedBaseTemplate, + `.idea\n.vscode\n\n${baseTemplate}\n\n.DS_Store\nnode_modules\n`, ], ] as const; diff --git a/src/cli/configure/processing/ignoreFile.ts b/src/cli/configure/processing/ignoreFile.ts index 67b76335c..49b8c3cf3 100644 --- a/src/cli/configure/processing/ignoreFile.ts +++ b/src/cli/configure/processing/ignoreFile.ts @@ -1,25 +1,20 @@ -const splitIntoSanitisedLines = (str: string) => - str - .trim() - .split(/\r?\n/) - .map((line) => line.trim()); - export const mergeWithIgnoreFile = (templateFile: string) => ( inputFile?: string, ) => { - const templateLines = splitIntoSanitisedLines(templateFile); - - const templateSet = new Set(templateLines.filter((line) => line !== '')); + if (typeof inputFile === 'undefined') { + return templateFile; + } - const inputLines = splitIntoSanitisedLines(inputFile ?? '').filter( - (line) => !templateSet.has(line), + const replacedFile = inputFile.replace( + /# managed by skuba[\s\S]*# end managed by skuba/, + templateFile, ); - const outputFile = [ - templateLines.join('\n').trim(), - inputLines.join('\n').trim(), - ] - .filter((blob) => blob !== '') + if (replacedFile.includes(templateFile)) { + return replacedFile; + } + + const outputFile = [templateFile.trim(), inputFile.trim()] .join('\n\n') .trim(); diff --git a/template/base/_.dockerignore b/template/base/_.dockerignore index bc56d0a6e..c2aa9db21 100644 --- a/template/base/_.dockerignore +++ b/template/base/_.dockerignore @@ -1,3 +1,4 @@ +# managed by skuba .idea/ .serverless/ .vscode/ @@ -12,3 +13,4 @@ node_modules*/ .npmrc npm-debug.log yarn-error.log +# end managed by skuba diff --git a/template/base/_.eslintignore b/template/base/_.eslintignore index 21fca81d3..a32542c71 100644 --- a/template/base/_.eslintignore +++ b/template/base/_.eslintignore @@ -1,3 +1,4 @@ +# managed by skuba .idea/ .serverless/ .vscode/ @@ -7,3 +8,4 @@ node_modules*/ /dist*/ /lib*/ /tmp*/ +# end managed by skuba diff --git a/template/base/_.gitignore b/template/base/_.gitignore index 54e802c71..0bb0933d6 100644 --- a/template/base/_.gitignore +++ b/template/base/_.gitignore @@ -1,3 +1,4 @@ +# managed by skuba .idea/ .serverless/ .vscode/ @@ -14,3 +15,4 @@ node_modules*/ npm-debug.log package-lock.json yarn-error.log +# end managed by skuba diff --git a/template/base/_.prettierignore b/template/base/_.prettierignore index 44890f1e7..f1256f149 100644 --- a/template/base/_.prettierignore +++ b/template/base/_.prettierignore @@ -1,3 +1,4 @@ +# managed by skuba .idea/ .serverless/ .vscode/ @@ -11,3 +12,4 @@ node_modules*/ # Gantry resource files support non-standard template syntax gantry*.yaml gantry*.yml +# end managed by skuba