Skip to content

Commit

Permalink
Update skuba managed sections when merging ignore files (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
lumishrestha authored Jun 17, 2020
1 parent 93cdf6c commit 72c2e2c
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-dragons-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**configure:** Reserve skuba-managed sections in ignore files
16 changes: 12 additions & 4 deletions src/cli/configure/analysis/__snapshots__/project.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
exports[`diffFiles works from scratch 1`] = `
Object {
".dockerignore": Object {
"data": ".idea/
"data": "# managed by skuba
.idea/
.serverless/
.vscode/
node_modules*/
Expand All @@ -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*/
Expand All @@ -30,6 +33,7 @@ node_modules*/
/dist*/
/lib*/
/tmp*/
# end managed by skuba
",
"operation": "A",
},
Expand All @@ -48,7 +52,8 @@ node_modules*/
"operation": "A",
},
".gitignore": Object {
"data": ".idea/
"data": "# managed by skuba
.idea/
.serverless/
.vscode/
node_modules*/
Expand All @@ -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*/
Expand All @@ -81,6 +88,7 @@ node_modules*/
# Gantry resource files support non-standard template syntax
gantry*.yaml
gantry*.yml
# end managed by skuba
",
"operation": "A",
},
Expand Down
58 changes: 36 additions & 22 deletions src/cli/configure/processing/__snapshots__/ignoreFile.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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
"
`;
31 changes: 22 additions & 9 deletions src/cli/configure/processing/ignoreFile.test.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
27 changes: 11 additions & 16 deletions src/cli/configure/processing/ignoreFile.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
2 changes: 2 additions & 0 deletions template/base/_.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# managed by skuba
.idea/
.serverless/
.vscode/
Expand All @@ -12,3 +13,4 @@ node_modules*/
.npmrc
npm-debug.log
yarn-error.log
# end managed by skuba
2 changes: 2 additions & 0 deletions template/base/_.eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# managed by skuba
.idea/
.serverless/
.vscode/
Expand All @@ -7,3 +8,4 @@ node_modules*/
/dist*/
/lib*/
/tmp*/
# end managed by skuba
2 changes: 2 additions & 0 deletions template/base/_.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# managed by skuba
.idea/
.serverless/
.vscode/
Expand All @@ -14,3 +15,4 @@ node_modules*/
npm-debug.log
package-lock.json
yarn-error.log
# end managed by skuba
2 changes: 2 additions & 0 deletions template/base/_.prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# managed by skuba
.idea/
.serverless/
.vscode/
Expand All @@ -11,3 +12,4 @@ node_modules*/
# Gantry resource files support non-standard template syntax
gantry*.yaml
gantry*.yml
# end managed by skuba

0 comments on commit 72c2e2c

Please sign in to comment.