Skip to content

Commit

Permalink
fix!: correctly detect if file is outside base path on Windows (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
fasttime and mdjermanovic authored Oct 24, 2024
1 parent 80eb545 commit f93aa4c
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 468 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock = false
@jsr:registry=https://npm.jsr.io
10 changes: 5 additions & 5 deletions packages/config-array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default [

In this example, the array contains both config objects and a config array. When a config array is normalized (see details below), it is flattened so only config objects remain. However, the order of evaluation remains the same.

If the `files` array contains a function, then that function is called with the absolute path of the file and is expected to return `true` if there is a match and `false` if not. (The `ignores` array can also contain functions.)
If the `files` array contains a function, then that function is called with the path of the file as it was passed in. The function is expected to return `true` if there is a match and `false` if not. (The `ignores` array can also contain functions.)

If the `files` array contains an item that is an array of strings and functions, then all patterns must match in order for the config to match. In the preceding examples, both `*.test.*` and `*.js` must match in order for the config object to be used.

Expand Down Expand Up @@ -273,7 +273,7 @@ await configs.normalizeSync({
To get the config for a file, use the `getConfig()` method on a normalized config array and pass in the filename to get a config for:

```js
// pass in absolute filename
// pass in filename
const fileConfig = configs.getConfig(
path.resolve(process.cwd(), "package.json"),
);
Expand All @@ -283,14 +283,14 @@ The config array always returns an object, even if there are no configs matching

A few things to keep in mind:

- You must pass in the absolute filename to get a config for.
- If a filename is not an absolute path, it will be resolved relative to the base path directory.
- The returned config object never has `files`, `ignores`, or `name` properties; the only properties on the object will be the other configuration options specified.
- The config array caches configs, so subsequent calls to `getConfig()` with the same filename will return in a fast lookup rather than another calculation.
- A config will only be generated if the filename matches an entry in a `files` key. A config will not be generated without matching a `files` key (configs without a `files` key are only applied when another config with a `files` key is applied; configs without `files` are never applied on their own). Any config with a `files` key entry that is `*` or ends with `/**` or `/*` will only be applied if another entry in the same `files` key matches or another config matches.

## Determining Ignored Paths

You can determine if a file is ignored by using the `isFileIgnored()` method and passing in the absolute path of any file, as in this example:
You can determine if a file is ignored by using the `isFileIgnored()` method and passing in the path of any file, as in this example:

```js
const ignored = configs.isFileIgnored("/foo/bar/baz.txt");
Expand All @@ -304,7 +304,7 @@ A file is considered ignored if any of the following is true:
- **If it matches an entry in `files` and also in `ignores`.** For example, if `**/*.js` is in `files` and `**/a.js` is in `ignores`, then `foo/a.js` and `foo/baz/a.js` are considered ignored.
- **The file is outside the `basePath`.** If the `basePath` is `/usr/me`, then `/foo/a.js` is considered ignored.

For directories, use the `isDirectoryIgnored()` method and pass in the absolute path of any directory, as in this example:
For directories, use the `isDirectoryIgnored()` method and pass in the path of any directory, as in this example:

```js
const ignored = configs.isDirectoryIgnored("/foo/bar/");
Expand Down
26 changes: 26 additions & 0 deletions packages/config-array/fix-std__path-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Replace import specifiers in "dist" modules to use the bundled versions of "@jsr/std__path".
*
* In "dist/cjs/index.cjs":
* - '@jsr/std__path/posix' → './std__path/posix.cjs'
* - '@jsr/std__path/windows' → './std__path/windows.cjs'
*
* In "dist/esm/index.js":
* - '@jsr/std__path/posix' → './std__path/posix.js'
* - '@jsr/std__path/windows' → './std__path/windows.js'
*/

import { readFile, writeFile } from "node:fs/promises";

async function replaceInFile(file, search, replacement) {
let text = await readFile(file, "utf-8");
text = text.replace(search, replacement);
await writeFile(file, text);
}

const SEARCH_REGEXP = /'@jsr\/std__path\/(.+?)'/gu;

await Promise.all([
replaceInFile("dist/cjs/index.cjs", SEARCH_REGEXP, "'./std__path/$1.cjs'"),
replaceInFile("dist/esm/index.js", SEARCH_REGEXP, "'./std__path/$1.js'"),
]);
2 changes: 2 additions & 0 deletions packages/config-array/jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"dist/esm/index.d.ts",
"dist/esm/types.ts",
"dist/esm/types.d.ts",
"dist/esm/std__path/posix.js",
"dist/esm/std__path/windows.js",
"README.md",
"jsr.json",
"LICENSE"
Expand Down
4 changes: 3 additions & 1 deletion packages/config-array/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"scripts": {
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
"build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"",
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
"build:std__path": "rollup -c rollup.std__path-config.js && node fix-std__path-imports",
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts && npm run build:std__path",
"test:jsr": "npx jsr@latest publish --dry-run",
"pretest": "npm run build",
"test": "mocha tests/",
Expand All @@ -51,6 +52,7 @@
"minimatch": "^3.1.2"
},
"devDependencies": {
"@jsr/std__path": "^1.0.4",
"@types/minimatch": "^3.0.5",
"c8": "^9.1.0",
"mocha": "^10.4.0",
Expand Down
32 changes: 32 additions & 0 deletions packages/config-array/rollup.std__path-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createRequire } from "node:module";

const { resolve } = createRequire(import.meta.url);

export default [
{
input: resolve("@jsr/std__path/posix"),
output: [
{
file: "./dist/cjs/std__path/posix.cjs",
format: "cjs",
},
{
file: "./dist/esm/std__path/posix.js",
format: "esm",
},
],
},
{
input: resolve("@jsr/std__path/windows"),
output: [
{
file: "./dist/cjs/std__path/windows.cjs",
format: "cjs",
},
{
file: "./dist/esm/std__path/windows.js",
format: "esm",
},
],
},
];
Loading

0 comments on commit f93aa4c

Please sign in to comment.