Skip to content

Commit f93aa4c

Browse files
fix!: correctly detect if file is outside base path on Windows (#59)
Co-authored-by: Milos Djermanovic <[email protected]>
1 parent 80eb545 commit f93aa4c

File tree

8 files changed

+540
-468
lines changed

8 files changed

+540
-468
lines changed

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
package-lock = false
2+
@jsr:registry=https://npm.jsr.io

packages/config-array/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export default [
170170

171171
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.
172172

173-
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.)
173+
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.)
174174

175175
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.
176176

@@ -273,7 +273,7 @@ await configs.normalizeSync({
273273
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:
274274

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

284284
A few things to keep in mind:
285285

286-
- You must pass in the absolute filename to get a config for.
286+
- If a filename is not an absolute path, it will be resolved relative to the base path directory.
287287
- The returned config object never has `files`, `ignores`, or `name` properties; the only properties on the object will be the other configuration options specified.
288288
- The config array caches configs, so subsequent calls to `getConfig()` with the same filename will return in a fast lookup rather than another calculation.
289289
- 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.
290290

291291
## Determining Ignored Paths
292292

293-
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:
293+
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:
294294

295295
```js
296296
const ignored = configs.isFileIgnored("/foo/bar/baz.txt");
@@ -304,7 +304,7 @@ A file is considered ignored if any of the following is true:
304304
- **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.
305305
- **The file is outside the `basePath`.** If the `basePath` is `/usr/me`, then `/foo/a.js` is considered ignored.
306306

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

309309
```js
310310
const ignored = configs.isDirectoryIgnored("/foo/bar/");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Replace import specifiers in "dist" modules to use the bundled versions of "@jsr/std__path".
3+
*
4+
* In "dist/cjs/index.cjs":
5+
* - '@jsr/std__path/posix' → './std__path/posix.cjs'
6+
* - '@jsr/std__path/windows' → './std__path/windows.cjs'
7+
*
8+
* In "dist/esm/index.js":
9+
* - '@jsr/std__path/posix' → './std__path/posix.js'
10+
* - '@jsr/std__path/windows' → './std__path/windows.js'
11+
*/
12+
13+
import { readFile, writeFile } from "node:fs/promises";
14+
15+
async function replaceInFile(file, search, replacement) {
16+
let text = await readFile(file, "utf-8");
17+
text = text.replace(search, replacement);
18+
await writeFile(file, text);
19+
}
20+
21+
const SEARCH_REGEXP = /'@jsr\/std__path\/(.+?)'/gu;
22+
23+
await Promise.all([
24+
replaceInFile("dist/cjs/index.cjs", SEARCH_REGEXP, "'./std__path/$1.cjs'"),
25+
replaceInFile("dist/esm/index.js", SEARCH_REGEXP, "'./std__path/$1.js'"),
26+
]);

packages/config-array/jsr.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"dist/esm/index.d.ts",
99
"dist/esm/types.ts",
1010
"dist/esm/types.d.ts",
11+
"dist/esm/std__path/posix.js",
12+
"dist/esm/std__path/windows.js",
1113
"README.md",
1214
"jsr.json",
1315
"LICENSE"

packages/config-array/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"scripts": {
3434
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
3535
"build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"",
36-
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
36+
"build:std__path": "rollup -c rollup.std__path-config.js && node fix-std__path-imports",
37+
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts && npm run build:std__path",
3738
"test:jsr": "npx jsr@latest publish --dry-run",
3839
"pretest": "npm run build",
3940
"test": "mocha tests/",
@@ -51,6 +52,7 @@
5152
"minimatch": "^3.1.2"
5253
},
5354
"devDependencies": {
55+
"@jsr/std__path": "^1.0.4",
5456
"@types/minimatch": "^3.0.5",
5557
"c8": "^9.1.0",
5658
"mocha": "^10.4.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { createRequire } from "node:module";
2+
3+
const { resolve } = createRequire(import.meta.url);
4+
5+
export default [
6+
{
7+
input: resolve("@jsr/std__path/posix"),
8+
output: [
9+
{
10+
file: "./dist/cjs/std__path/posix.cjs",
11+
format: "cjs",
12+
},
13+
{
14+
file: "./dist/esm/std__path/posix.js",
15+
format: "esm",
16+
},
17+
],
18+
},
19+
{
20+
input: resolve("@jsr/std__path/windows"),
21+
output: [
22+
{
23+
file: "./dist/cjs/std__path/windows.cjs",
24+
format: "cjs",
25+
},
26+
{
27+
file: "./dist/esm/std__path/windows.js",
28+
format: "esm",
29+
},
30+
],
31+
},
32+
];

0 commit comments

Comments
 (0)