Skip to content

Commit

Permalink
chore: fix Windows compatible issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Feb 27, 2024
1 parent 37de595 commit c253f93
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions @commitlint/resolve-extends/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,42 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
return ('default' in imported && imported.default) || imported;
};

/**
* Fake file name to provide {@link moduleResolve} a filename to resolve from the configuration cwd
*/
const FAKE_FILE_NAME_FOR_RESOLVER = '__';
const pathSuffixes = [
'',
'.js',
'.json',
`${path.sep}index.js`,
`${path.sep}index.json`,
];

const specifierSuffixes = ['', '.js', '.json', '/index.js', '/index.json'];

/**
* @see moduleResolve
*/
export const resolveFrom = (specifier: string, parent?: string): string => {
let resolved: URL;
export const resolveFrom = (lookup: string, parent?: string): string => {
if (path.isAbsolute(lookup)) {
for (const suffix of pathSuffixes) {
const filename = lookup + suffix;
if (fs.existsSync(filename)) {
return filename;
}
}
}

let resolveError: Error | undefined;

const base = pathToFileURL(
parent
? fs.statSync(parent).isDirectory()
? path.join(parent, FAKE_FILE_NAME_FOR_RESOLVER)
? path.join(parent, 'noop.js')
: parent
: import.meta.url
);

for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) {
for (const suffix of specifierSuffixes) {
try {
resolved = moduleResolve(specifier + suffix, base);
return fileURLToPath(resolved);
return fileURLToPath(moduleResolve(lookup + suffix, base));
} catch (err) {
if (!resolveError) {
resolveError = err as Error;
Expand Down

0 comments on commit c253f93

Please sign in to comment.