Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into pm
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronMoat committed May 6, 2024
2 parents 25b4f80 + 94f5633 commit 80df158
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changeset/brave-cars-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'skuba': minor
---

lint: Skip generation of config files when present in `.gitignore`

`skuba lint` and `skuba format` now skip the generation of config files, like `.dockerignore` and `.npmrc`, if they are ignored by `.gitignore` files.
5 changes: 5 additions & 0 deletions .changeset/rude-trainers-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': minor
---

api: Add Git.isFileGitIgnored
1 change: 0 additions & 1 deletion .env

This file was deleted.

2 changes: 0 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ jobs:

- name: Test
run: pnpm test:ci
env:
NODE_OPTIONS: --experimental-vm-modules

- name: Lint
run: pnpm lint
Expand Down
1 change: 1 addition & 0 deletions integration/base/unfixable/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.dockerignore
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
"lint:md": "remark --frail --quiet .",
"release": "pnpm --silent build && changeset publish",
"skuba": "pnpm --silent build && pnpm --silent skuba:exec",
"skuba:exec": "node --env-file=.env lib/skuba",
"skuba:exec": "node --experimental-vm-modules lib/skuba",
"stage": "changeset version && node ./.changeset/inject.js && pnpm format",
"test": "pnpm --silent skuba test --selectProjects unit",
"test": "pnpm --silent skuba test --selectProjects unit --",
"test:ci": "pnpm --silent skuba test --runInBand",
"test:int": "pnpm --silent skuba test --runInBand --selectProjects integration",
"test:int": "pnpm --silent skuba test --selectProjects integration --runInBand",
"test:template": "scripts/test-template.sh",
"test:watch": "pnpm --silent skuba test --runInBand --watch"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-skuba/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"format": "pnpm --silent skuba format",
"lint": "pnpm --silent skuba lint",
"skuba": "node --env-file=../../.env ../../lib/skuba"
"skuba": "node --experimental-vm-modules ../../lib/skuba"
},
"dependencies": {
"@types/eslint": "^8.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/skuba-dive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"format": "pnpm skuba format",
"lint": "pnpm skuba lint",
"prepack": "pnpm skuba build",
"skuba": "node --env-file=../../.env ../../lib/skuba",
"skuba": "node --experimental-vm-modules ../../lib/skuba",
"test": "pnpm skuba test",
"test:ci": "pnpm skuba test --coverage"
},
Expand Down
1 change: 1 addition & 0 deletions src/api/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { getOwnerAndRepo } from './remote';
export { push } from './push';
export { fastForwardBranch } from './pull';
export { reset } from './reset';
export { isFileGitIgnored } from './isFileGitIgnored';
17 changes: 17 additions & 0 deletions src/api/git/isFileGitIgnored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path';

import fs from 'fs-extra';
import git from 'isomorphic-git';

export const isFileGitIgnored = ({
absolutePath,
gitRoot,
}: {
absolutePath: string;
gitRoot: string;
}): Promise<boolean> =>
git.isIgnored({
dir: gitRoot,
filepath: path.relative(gitRoot, absolutePath),
fs,
});
3 changes: 1 addition & 2 deletions src/cli/__snapshots__/format.int.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Patch skipped: Upgrade Node.js Distroless Docker image to -debian12 variant - no
Patch skipped: Add keepAliveTimeout to server listener - no listener file found
Patch skipped: Move .npmrc out of the .gitignore managed section - no .gitignore file found
Patch skipped: Move .npmrc out of the .gitignore managed section - not ignored
Patch skipped: Move .npmrc out of the .dockerignore managed section - no .dockerignore file found
Expand All @@ -205,7 +205,6 @@ Refreshed .eslintignore. refresh-config-files
Refreshed .gitignore. refresh-config-files
Refreshed .prettierignore. refresh-config-files
Refreshed .npmrc. refresh-config-files
Refreshed .dockerignore. refresh-config-files
Processed skuba lints in <random>s.
ESLint
Expand Down
1 change: 1 addition & 0 deletions src/cli/lint.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const stdout = (randomMatcher: RegExp) => {
const prepareTempDirectory = async (baseDir: string, tempDir: string) => {
await copy(baseDir, tempDir);
process.chdir(tempDir);
await git.init({ fs, dir: tempDir });
const result = await refreshConfigFiles('format', {
bold: jest.fn(),
dim: jest.fn(),
Expand Down
68 changes: 68 additions & 0 deletions src/cli/lint/internalLints/refreshConfigFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';

import * as fsExtra from 'fs-extra';

import { Git } from '../../..';
import { log } from '../../../utils/logging';
import { detectPackageManager } from '../../../utils/packageManager';
import * as project from '../../configure/analysis/project';
Expand All @@ -28,6 +29,13 @@ jest.mock('../../../utils/template', () => ({

jest.mock('../../configure/analysis/project');

jest.mock('../../..', () => ({
Git: {
isFileGitIgnored: jest.fn(),
findRoot: () => Promise.resolve('/path/to/git/root'),
},
}));

const givenMockPackageManager = (command: 'pnpm' | 'yarn') => {
jest
.mocked(detectPackageManager)
Expand Down Expand Up @@ -193,6 +201,37 @@ The .npmrc file is out of date. Run \`pnpm exec skuba format\` to update it. ref

expect(writeFile).not.toHaveBeenCalled();
});

it('should not flag creation of files that are `.gitignore`d', async () => {
jest
.mocked(Git.isFileGitIgnored)
.mockImplementation(({ absolutePath }) =>
Promise.resolve(absolutePath.endsWith('.eslintignore')),
);

setupDestinationFiles({
'.eslintignore': undefined,
'.dockerignore': undefined,
});

await expect(refreshConfigFiles('lint', log)).resolves.toEqual({
ok: false,
fixable: true,
annotations: [
{
message:
'The .dockerignore file is out of date. Run `pnpm exec skuba format` to update it.',
path: '.dockerignore',
},
],
});

expect(`\n${stdout()}`).toBe(`
The .dockerignore file is out of date. Run \`pnpm exec skuba format\` to update it. refresh-config-files
`);

expect(writeFile).not.toHaveBeenCalled();
});
});

describe('format mode', () => {
Expand Down Expand Up @@ -309,6 +348,35 @@ Refreshed .npmrc. refresh-config-files
expect(writeFile).not.toHaveBeenCalled();
});

it('should not create files that are `.gitignore`d', async () => {
jest
.mocked(Git.isFileGitIgnored)
.mockImplementation(({ absolutePath }) =>
Promise.resolve(absolutePath.endsWith('.eslintignore')),
);

setupDestinationFiles({
'.eslintignore': undefined,
'.dockerignore': undefined,
});

await expect(refreshConfigFiles('format', log)).resolves.toEqual({
ok: true,
fixable: false,
annotations: [],
});

expect(`\n${stdout()}`).toBe(`
Refreshed .dockerignore. refresh-config-files
`);

expect(writeFile).toHaveBeenCalledTimes(1);
expect(writeFile).toHaveBeenCalledWith(
path.join(process.cwd(), '.dockerignore'),
'# managed by skuba\nfake content for _.dockerignore\n# end managed by skuba',
);
});

it('should format an extraneous !.npmrc', async () => {
setupDestinationFiles({
'.gitignore':
Expand Down
19 changes: 17 additions & 2 deletions src/cli/lint/internalLints/refreshConfigFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inspect } from 'util';
import { writeFile } from 'fs-extra';
import stripAnsi from 'strip-ansi';

import { Git } from '../../..';
import type { Logger } from '../../../utils/logging';
import { NPMRC_LINES, hasNpmrcSecret } from '../../../utils/npmrc';
import {
Expand Down Expand Up @@ -74,7 +75,10 @@ export const refreshConfigFiles = async (
mode: 'format' | 'lint',
logger: Logger,
) => {
const manifest = await getDestinationManifest();
const [manifest, gitRoot] = await Promise.all([
getDestinationManifest(),
Git.findRoot({ dir: process.cwd() }),
]);

const destinationRoot = path.dirname(manifest.path);

Expand All @@ -93,11 +97,22 @@ export const refreshConfigFiles = async (
return { needsChange: false };
}

const [inputFile, templateFile] = await Promise.all([
const [inputFile, templateFile, isGitIgnored] = await Promise.all([
readDestinationFile(filename),
readBaseTemplateFile(`_${filename}`),
gitRoot
? Git.isFileGitIgnored({
gitRoot,
absolutePath: path.join(destinationRoot, filename),
})
: false,
]);

// If the file is gitignored and doesn't exist, don't make it
if (inputFile === undefined && isGitIgnored) {
return { needsChange: false };
}

const data = additionalMapping(
inputFile
? mergeWithConfigFile(templateFile, fileType)(inputFile)
Expand Down

0 comments on commit 80df158

Please sign in to comment.