Skip to content

Commit

Permalink
feat: auto discard git-ignored files from watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine-coulon committed Jan 13, 2024
1 parent 8ecbf8e commit c83accc
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 18 deletions.
13 changes: 12 additions & 1 deletion packages/skott/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ dist
fixtures
*.txt
build-local.sh
.skott
.skott


# integration tests fixtures

!test/integration/**/*

!test/integration/index.js
!test/integration/main.ts
!test/integration/some-module.ts
!test/integration/sub_directory
!test/integration/git-ignore-sandbox
50 changes: 48 additions & 2 deletions packages/skott/bin/watch-mode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import fs from "node:fs/promises";
import path from "node:path";

import watcher from "@parcel/watcher";
import kleur from "kleur";
// @ts-ignore
import gitignoreParse from "parse-gitignore";

import { defaultIgnoredDirs } from "../src/modules/resolvers/base-resolver.js";

Expand All @@ -13,7 +18,38 @@ export const watchModeStatus = {
changes_detected: "Changes detected"
};

export function registerWatchMode({
async function* deepRetrieveGitIgnorePaths(cwd: string) {
try {
const files = await fs.readdir(cwd, { recursive: true });

for (const f of files) {
if (f.includes(".gitignore")) {
yield path.join(cwd, f.toString());
}
}
} catch {}
}

async function retrieveGitIgnoredEntries(cwd: string) {
const gitIgnoreFilePaths: string[] = [];

for await (const gitIgnorePath of deepRetrieveGitIgnorePaths(cwd)) {
gitIgnoreFilePaths.push(gitIgnorePath);
}

const gitIgnoreEntries = await Promise.all(
gitIgnoreFilePaths.map((filePath) =>
fs
.readFile(filePath)
.then((c) => gitignoreParse(c).patterns)
.catch(() => {})
)
);

return gitIgnoreEntries.flat();
}

export async function registerWatchMode({
cwd,
ignorePattern,
fileExtensions
Expand All @@ -23,22 +59,32 @@ export function registerWatchMode({
fileExtensions: string[];
}) {
console.log(
`\n ${kleur.bold().yellow(watchModeStatus.watching_for_changes)}`
`\n ${kleur.bold().yellow("Searching for all entries to ignore...")}`
);

const gitIgnoredEntries = await retrieveGitIgnoredEntries(cwd);

const listOfWatchableFileExtensions = fileExtensions
.map(toDotlessExtension)
.join(",");

const ignoreList = [
// Whitelist the provided `fileExtensions` API option (fallbacks to a default list).
`!**/*.{${listOfWatchableFileExtensions}}`,
// Discard all entries ignored by gitignore
...gitIgnoredEntries,
// Discard all directories ignored by default
...defaultIgnoredDirs
];

if (ignorePattern) {
ignoreList.push(ignorePattern);
}

console.log(
`\n ${kleur.bold().yellow(watchModeStatus.watching_for_changes)}`
);

return watcher.subscribe(
cwd,
(_err, _events) => {
Expand Down
1 change: 1 addition & 0 deletions packages/skott/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"meriyah": "^4.3.7",
"minimatch": "^9.0.3",
"ora": "^6.3.1",
"parse-gitignore": "^2.0.0",
"polka": "^0.5.2",
"sirv": "^2.0.3",
"skott-webapp": "workspace:^",
Expand Down
9 changes: 0 additions & 9 deletions packages/skott/test/integration/__fixtures__/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this will be created/unlinked during integration tests
with-gitignore

This file was deleted.

46 changes: 41 additions & 5 deletions packages/skott/test/integration/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("When running skott cli", () => {
"--exitCodeOnCircularDependencies=0",
`--cwd=${fixturesPath}`
],
useTimeout(2_500)
useTimeout(1_500)
),
doneSuccess,
doneFailure,
Expand Down Expand Up @@ -85,7 +85,7 @@ describe("When running skott cli", () => {
"--exitCodeOnCircularDependencies=0",
`--cwd=${fixturesPath}`
],
useTimeout(2_500)
useTimeout(1_500)
),
doneSuccess,
doneFailure,
Expand Down Expand Up @@ -143,7 +143,7 @@ describe("When running skott cli", () => {
"--exitCodeOnCircularDependencies=0",
`--cwd=${fixturesPath}`
],
useTimeout(2_500)
useTimeout(1_500)
),
doneSuccess,
doneFailure,
Expand Down Expand Up @@ -180,7 +180,7 @@ describe("When running skott cli", () => {
"--exitCodeOnCircularDependencies=0",
`--cwd=${fixturesPath}`
],
useTimeout(2_500)
useTimeout(1_500)
),
doneSuccess,
doneFailure,
Expand All @@ -192,6 +192,42 @@ describe("When running skott cli", () => {
}));
});

describe("When the entry is git-ignored", () => {
test("Should not re-run analysis", () =>
new Promise((doneSuccess, doneFailure) => {
const gitIgnoreSandbox = path.join(
fixturesPath,
"git-ignore-sandbox",
"with-gitignore"
);
const fixtureFilePath = path.join(gitIgnoreSandbox, "index.js");

const finalizeTest = runFinalizer(gitIgnoreSandbox);

fs.mkdirSync(gitIgnoreSandbox, {
recursive: true
});

expectChangesNotToBeDetected({
skottCliProcess: runKeepAliveSkottCli(
[
"--watch",
"--displayMode=raw",
"--exitCodeOnCircularDependencies=0",
`--cwd=${path.join(fixturesPath, "git-ignore-sandbox")}`
],
useTimeout(1_500)
),
doneSuccess,
doneFailure,
actionThatShouldTriggerChanges: () => {
fs.writeFileSync(fixtureFilePath, "function main() {}");
},
finalizer: finalizeTest
});
}));
});

describe("When the entry is part of the ones ignored by the ignorePattern provided", () => {
test("Should not re-run analysis", () =>
new Promise((doneSuccess, doneFailure) => {
Expand All @@ -218,7 +254,7 @@ describe("When running skott cli", () => {
`--cwd=${fixturesPath}`,
"--ignorePattern=voluntarily-ignored/**/*"
],
useTimeout(2_500)
useTimeout(1_500)
),
doneSuccess,
doneFailure,
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c83accc

Please sign in to comment.