Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
feat: Implement --only and --ignore (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jan 21, 2024
1 parent eaccf25 commit 4f0bd60
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@mole-inc/bin-wrapper": "^8.0.1",
"commander": "^7.1.0",
"fast-glob": "^3.2.5",
"minimatch": "^9.0.3",
"semver": "^7.3.8",
"slash": "3.0.0",
"source-map": "^0.7.3"
Expand Down
2 changes: 2 additions & 0 deletions src/swc/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const createDefaultResult = (): ParserArgsReturn => ({
// @ts-expect-error
filename: undefined,
filenames: ["src"],
ignore: [],
includeDotfiles: false,
only: [],
// @ts-expect-error
outDir: undefined,
// @ts-expect-error
Expand Down
12 changes: 6 additions & 6 deletions src/swc/__tests__/sources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ describe("globSources", () => {
});

it("exclude dotfiles sources when includeDotfiles=false", async () => {
const files = await globSources([".dotfile"], false);
const files = await globSources([".dotfile"], [], [], false);

expect([...files]).toEqual([]);
});

it("include dotfiles sources when includeDotfiles=true", async () => {
(fs as any).setMockStats({ ".dotfile": { isDirectory: () => false } });
const files = await globSources([".dotfile"], true);
const files = await globSources([".dotfile"], [], [], true);

expect([...files]).toEqual([".dotfile"]);
});

it("include multiple file sources", async () => {
(fs as any).setMockStats({ ".dotfile": { isDirectory: () => false } });
(fs as any).setMockStats({ file: { isDirectory: () => false } });
const files = await globSources([".dotfile", "file"], true);
const files = await globSources([".dotfile", "file"], [], [], true);

expect([...files]).toEqual([".dotfile", "file"]);
});

it("exclude files that errors on stats", async () => {
(fs as any).setMockStats({ ".dotfile": { isDirectory: () => false } });
(fs as any).setMockStats({ file: new Error("Failed stat") });
const files = await globSources([".dotfile", "file"], true);
const files = await globSources([".dotfile", "file"], [], [], true);

expect([...files]).toEqual([".dotfile"]);
});
Expand All @@ -44,7 +44,7 @@ describe("globSources", () => {
(fs as any).setMockStats({ file: { isDirectory: () => false } });

(glob as unknown as jest.Mock).mockResolvedValue(["fileDir1", "fileDir2"]);
const files = await globSources(["file", "directory"], true);
const files = await globSources(["file", "directory"], [], [], true);

expect([...files]).toEqual(["file", "fileDir1", "fileDir2"]);
});
Expand All @@ -54,7 +54,7 @@ describe("globSources", () => {
(fs as any).setMockStats({ file: { isDirectory: () => false } });

(glob as unknown as jest.Mock).mockRejectedValue(new Error("Failed"));
const files = await globSources(["file", "directory"], true);
const files = await globSources(["file", "directory"], [], [], true);

expect([...files]).toEqual(["file"]);
});
Expand Down
9 changes: 8 additions & 1 deletion src/swc/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,19 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
sync,
quiet,
watch,
only,
ignore,
} = cliOptions;

const results = new Map<string, CompileStatus>();

const start = process.hrtime();
const sourceFiles = await globSources(filenames, includeDotfiles);
const sourceFiles = await globSources(
filenames,
only,
ignore,
includeDotfiles
);
const [compilable, copyable] = splitCompilableAndCopyable(
sourceFiles,
extensions,
Expand Down
2 changes: 2 additions & 0 deletions src/swc/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export default async function ({

for (const filename of await globSources(
cliOptions.filenames,
cliOptions.only,
cliOptions.ignore,
cliOptions.includeDotfiles
)) {
if (isCompilableExtension(filename, cliOptions.extensions)) {
Expand Down
5 changes: 5 additions & 0 deletions src/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ export interface CliOptions {
readonly includeDotfiles: boolean;
readonly deleteDirOnStart: boolean;
readonly quiet: boolean;

readonly only: string[];
readonly ignore: string[];
}

export default function parserArgs(args: string[]) {
Expand Down Expand Up @@ -269,6 +272,8 @@ export default function parserArgs(args: string[]) {
includeDotfiles: !!opts.includeDotfiles,
deleteDirOnStart: Boolean(opts.deleteDirOnStart),
quiet: !!opts.quiet,
only: opts.only || [],
ignore: opts.ignore || [],
};
return {
swcOptions,
Expand Down
15 changes: 12 additions & 3 deletions src/swc/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import glob from "fast-glob";
import slash from "slash";
import { stat } from "fs";
import { join, basename, extname } from "path";
import { minimatch } from "minimatch";

/**
* Find all input files based on source globs
*/
export async function globSources(
sources: string[],
only: string[],
ignore: string[],
includeDotfiles = false
): Promise<string[]> {
const globConfig = {
const globConfig: glob.Options = {
dot: includeDotfiles,
nodir: true,
ignore,
};

const files = await Promise.all(
Expand Down Expand Up @@ -40,7 +43,13 @@ export async function globSources(
})
);

return Array.from(new Set<string>(files.flat()));
const f = files.flat().filter(filename => {
return (
only.length === 0 || only.some(only => minimatch(slash(filename), only))
);
});

return Array.from(new Set<string>(f));
}

type Split = [compilable: string[], copyable: string[]];
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,13 @@ brace-expansion@^1.1.7:
balanced-match "^1.0.0"
concat-map "0.0.1"

brace-expansion@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.1.tgz#1edc459e0f0c548486ecf9fc99f2221364b9a0ae"
integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==
dependencies:
balanced-match "^1.0.0"

braces@^3.0.2, braces@~3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107"
Expand Down Expand Up @@ -2229,6 +2236,13 @@ minimatch@^3.0.4, minimatch@^3.1.1:
dependencies:
brace-expansion "^1.1.7"

minimatch@^9.0.3:
version "9.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825"
integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==
dependencies:
brace-expansion "^2.0.1"

[email protected]:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
Expand Down

0 comments on commit 4f0bd60

Please sign in to comment.