Skip to content

Commit a49b049

Browse files
authored
Fix test faliure on Windows (#49)
1 parent 27eea2f commit a49b049

6 files changed

Lines changed: 72 additions & 80 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

package-lock.json

Lines changed: 48 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
"tiny-editorconfig": "^1.0.0",
5454
"tiny-jsonc": "^1.0.1",
5555
"tiny-readdir": "^2.7.4",
56-
"tiny-readdir-glob": "^1.23.1",
56+
"tiny-readdir-glob": "^1.23.2",
5757
"tiny-spinner": "^2.0.4",
5858
"worktank": "^2.7.3",
59-
"zeptomatch": "^2.0.0",
59+
"zeptomatch": "^2.0.1",
6060
"zeptomatch-escape": "^1.0.0",
6161
"zeptomatch-is-static": "^1.0.0"
6262
},

src/config_prettier.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import path from "node:path";
3+
import url from "node:url";
34
import JSONC from "tiny-jsonc";
45
import zeptomatch from "zeptomatch";
56
import Known from "./known.js";
@@ -15,7 +16,7 @@ const Loaders = {
1516
return loader(filePath);
1617
},
1718
js: async (filePath: string): Promise<unknown> => {
18-
const module = await import(filePath);
19+
const module = await import(url.pathToFileURL(filePath).href);
1920
return module.default || module.exports || module.prettier || module;
2021
},
2122
json: async (filePath: string): Promise<unknown> => {

src/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Known from "./known.js";
1212
import Logger from "./logger.js";
1313
import { makePrettier } from "./prettier.js";
1414
import { castArray, getExpandedFoldersPaths, getFoldersChildrenPaths, getPluginsVersions, getProjectPath, getStdin, getTargetsPaths } from "./utils.js";
15-
import { fastRelativePath, isNull, isString, isUndefined, negate, pluralize, trimFinalNewline, uniq, without } from "./utils.js";
15+
import { fastRelativePath, isNull, isString, isUndefined, negate, pluralize, trimFinalNewline, uniq, without, normalizeToPosix } from "./utils.js";
1616
import type { FormatOptions, Options, PluginsOptions } from "./types.js";
1717

1818
async function run(options: Options, pluginsDefaultOptions: PluginsOptions, pluginsCustomOptions: PluginsOptions): Promise<void> {
@@ -162,11 +162,11 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
162162
} else {
163163
totalUnformatted += 1;
164164
const filePath = filesPathsTargets[i];
165-
const fileRelativePath = fastRelativePath(rootPath, filePath);
165+
const fileNameToDisplay = normalizeToPosix(fastRelativePath(rootPath, filePath));
166166
if (options.check) {
167-
stderr.prefixed.warn(fileRelativePath);
167+
stderr.prefixed.warn(fileNameToDisplay);
168168
} else if (options.list || options.write) {
169-
stdout.warn(fileRelativePath);
169+
stdout.warn(fileNameToDisplay);
170170
}
171171
}
172172
}
@@ -180,12 +180,12 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
180180
totalErrored += 1;
181181
pathsErrored.push(filesPathsTargets[i]);
182182
const filePath = filesPathsTargets[i];
183-
const fileRelativePath = fastRelativePath(rootPath, filePath);
183+
const fileNameToDisplay = normalizeToPosix(fastRelativePath(rootPath, filePath));
184184
//TODO: Make sure the error is syntax-highlighted when possible
185185
if (options.check || options.write) {
186-
stderr.prefixed.error(`${fileRelativePath}: ${error}`);
186+
stderr.prefixed.error(`${fileNameToDisplay}: ${error}`);
187187
} else if (options.list) {
188-
stderr.error(fileRelativePath);
188+
stderr.error(fileNameToDisplay);
189189
}
190190
}
191191
}
@@ -197,9 +197,9 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
197197
stdout.prefixed.debug(`Files formatted: ${totalFormatted}`);
198198
stdout.prefixed.debug(`Files unformatted: ${totalUnformatted}`);
199199
stdout.prefixed.debug(`Files unknown: ${totalUnknown}`);
200-
stdout.prefixed.debug(() => pathsUnknown.map((filePath) => fastRelativePath(rootPath, filePath)).join("\n"));
200+
stdout.prefixed.debug(() => pathsUnknown.map((filePath) => normalizeToPosix(fastRelativePath(rootPath, filePath))).join("\n"));
201201
stdout.prefixed.debug(`Files errored: ${totalErrored}`);
202-
stdout.prefixed.debug(() => pathsErrored.map((filePath) => fastRelativePath(rootPath, filePath)).join("\n"));
202+
stdout.prefixed.debug(() => pathsErrored.map((filePath) => normalizeToPosix(fastRelativePath(rootPath, filePath))).join("\n"));
203203

204204
if (!totalMatched && !totalIgnored) {
205205
if (options.errorOnUnmatchedPattern) {

src/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolea
111111
}
112112

113113
async function getModule<T = unknown>(modulePath: string): Promise<T> {
114-
const moduleExports = await import(modulePath);
114+
const moduleExports = await import(url.pathToFileURL(modulePath).href);
115115
const module = moduleExports.default || moduleExports.exports || moduleExports;
116116
return module;
117117
}
@@ -733,6 +733,14 @@ function zipObjectUnless<T extends Key, U>(keys: T[], values: U[], unless: (valu
733733
return map;
734734
}
735735

736+
/**
737+
* Replace `\` with `/` on Windows
738+
*/
739+
const normalizeToPosix =
740+
path.sep === "\\"
741+
? (filepath: string): string => filepath.replaceAll("\\", "/")
742+
: (filepath: string): string => filepath;
743+
736744
export {
737745
castArray,
738746
fastJoinedPath,
@@ -744,7 +752,6 @@ export {
744752
getFoldersChildrenPaths,
745753
getExpandedFoldersPaths,
746754
getGlobPaths,
747-
getModule,
748755
getModulePath,
749756
getPlugin,
750757
getPluginOrExit,
@@ -777,6 +784,7 @@ export {
777784
normalizeFormatOptions,
778785
normalizePluginOptions,
779786
normalizePrettierOptions,
787+
normalizeToPosix,
780788
omit,
781789
once,
782790
pluralize,

0 commit comments

Comments
 (0)