This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.mjs
63 lines (57 loc) · 2.05 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { URL } = globalThis;
const { search: __search } = new URL(import.meta.url);
// https://github.com/mochajs/mocha/issues/4720
// This abomination solves the empty file extension issue caused by --experimental-loader.
// Indeed, this flag (seems to) cause the default loader to go from cjs to esm.
// And file without extension works fine with the cjs loader but not with the esm loader.
import {
readlink as readLinkAsync,
copyFile as copyFileAsync,
unlink as unlinkAsync,
symlink as symlinkAsync,
} from "fs/promises";
import {
extname as getExtension,
dirname as getDirectory,
resolve as resolvePath,
} from "path";
const { logInfo } = await import(`../../log/index.mjs${__search}`);
const { expectSuccessAsync, expect } = await import(
`../../expect/index.mjs${__search}`
);
const readMaybeLinkAsync = async (link) => {
try {
return await readLinkAsync(link);
} catch {
return null;
}
};
export const addLinkExtensionAsync = async (link) => {
const path = await readMaybeLinkAsync(link);
if (path === null) {
const extension = getExtension(link);
expect(
extension !== "",
"Cannot solve the empty extension issue because %j is not a symbolic link",
link,
);
} else {
const extension = getExtension(path);
if (extension === "") {
logInfo(
"Symbolic link %j refers to %j which has no extension, this is problematic for the node's esm loader which is enabled by --experimental-loader. To solve this issue, the agent is going to create copy of that file with a '.cjs' extension and overwrite the link to refer to that file. We hope this is okay with you...",
link,
path,
);
const absolute_path = resolvePath(getDirectory(link), path);
await expectSuccessAsync(
(async () => {
await copyFileAsync(absolute_path, `${absolute_path}.cjs`);
await unlinkAsync(link);
await symlinkAsync(`${path}.cjs`, link, "file");
})(),
"Something went wrong when resolving the missing file extension issue >> %O",
);
}
}
};