Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { deprecate } from "node:util";
import { ReadableStream } from "node:stream/web";

import globParent from "glob-parent";
Expand Down Expand Up @@ -32,12 +33,12 @@ function isPositiveGlob(glob) {

function validateUniqueBy({ uniqueBy } = {}) {
if (typeof uniqueBy === "function") {
return true;
return;
}

if (typeof uniqueBy === "string") {
if (uniqueBy === "cwd" || uniqueBy === "base" || uniqueBy === "path") {
return true;
return;
} else {
throw new Error("The `uniqueBy` string must be 'cwd', 'base', or 'path'");
}
Expand All @@ -46,6 +47,14 @@ function validateUniqueBy({ uniqueBy } = {}) {
throw new Error("The `uniqueBy` option must be a string or function");
}

function isDefined(item) {
return item != null;
}
const showIgnoreDeprecation = deprecate(
() => {},
"The `ignore` option is deprecated - use `exclude` instead",
);

function validateOptions(opts) {
if (typeof opts.cwd !== "string") {
throw new Error("The `cwd` option must be a string");
Expand All @@ -63,6 +72,14 @@ function validateOptions(opts) {

if (!Array.isArray(opts.ignore)) {
throw new Error("The `ignore` option must be a string or array");
} else {
if (opts.ignore.some(isDefined)) {
showIgnoreDeprecation();
}
}

if (!Array.isArray(opts.exclude)) {
throw new Error("The `exclude` option must be a string or array");
}
}

Expand Down Expand Up @@ -120,6 +137,7 @@ export default function globStream(globs, opt) {
cwd: process.cwd(),
cwdbase: false,
uniqueBy: "path",
exclude: [],
// TODO: deprecate `ignore` option
ignore: [],
},
Expand All @@ -132,6 +150,9 @@ export default function globStream(globs, opt) {
// Normalize `ignore` to array
ourOpt.ignore =
typeof ourOpt.ignore === "string" ? [ourOpt.ignore] : ourOpt.ignore;
// Normalize `exclude` to array
ourOpt.exclude =
typeof ourOpt.exclude === "string" ? [ourOpt.exclude] : ourOpt.exclude;

validateOptions(ourOpt);

Expand All @@ -151,8 +172,7 @@ export default function globStream(globs, opt) {
positiveGlobs.push(globString);
}
}
// TODO: Support explicit `exclude` option
ourOpt.exclude = [...negativeGlobs, ...ourOpt.ignore];
ourOpt.exclude = [...negativeGlobs, ...ourOpt.ignore, ...ourOpt.exclude];

const isUnique = uniqueBy(ourOpt.uniqueBy);

Expand All @@ -179,7 +199,6 @@ export default function globStream(globs, opt) {
base: path.normalize(basePath),
path: filepath,
};
// TODO: Add some sort of "stat cache" for performance improvements if stats exist on file

// TODO: Attach to opts so this can be hoisted outside the function?
const unique = isUnique(obj);
Expand Down
86 changes: 86 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,12 @@ function suite(moduleName, load) {
message: /must be a string or array/,
},
);
t.assert.throws(
() => globStream("./fixtures/stuff/*.dmc", { exclude: 1234 }),
{
message: /must be a string or array/,
},
);
});

describe("cwd", () => {
Expand Down Expand Up @@ -1090,6 +1096,86 @@ function suite(moduleName, load) {
});
});

describe("exclude", () => {
it("accepts a string (in addition to array)", async (t) => {
const expected = [
{
cwd: dirname,
base: path.join(dirname, "/fixtures/stuff"),
path: path.join(dirname, "/fixtures/stuff/run.dmc"),
},
];

function assertion(pathObjs) {
t.assert.containsAllEqual(pathObjs, expected);
}

await pipeline(
globStream("./fixtures/stuff/*.dmc", {
cwd: dirname,
exclude: "./fixtures/stuff/test.dmc",
}),
concat(assertion),
);
});

it("supports the exclude option instead of negation", async (t) => {
const expected = [
{
cwd: dirname,
base: path.join(dirname, "/fixtures/stuff"),
path: path.join(dirname, "/fixtures/stuff/run.dmc"),
},
];

function assertion(pathObjs) {
t.assert.containsAllEqual(pathObjs, expected);
}

await pipeline(
globStream("./fixtures/stuff/*.dmc", {
cwd: dirname,
exclude: ["./fixtures/stuff/test.dmc"],
}),
concat(assertion),
);
});

it("supports the exclude option with dot extglob", async (t) => {
const expected = [];

function assertion(pathObjs) {
t.assert.containsAllEqual(pathObjs, expected);
}

await pipeline(
globStream("./fixtures/*(.)swag", {
cwd: dirname,
exclude: ["./fixtures/**"],
}),
concat(assertion),
);
});

it("can use both exclude option and negative globs", async (t) => {
const expected = [];

const globs = ["./fixtures/stuff/*.dmc", "!./fixtures/stuff/test.dmc"];

function assertion(pathObjs) {
t.assert.containsAllEqual(pathObjs, expected);
}

await pipeline(
globStream(globs, {
cwd: dirname,
exclude: ["./fixtures/stuff/run.dmc"],
}),
concat(assertion),
);
});
});

it("accepts a file path", async (t) => {
const expected = [
{
Expand Down
Loading