|
1 | 1 | import { fileURLToPath } from "url";
|
| 2 | +import nodePath from "path"; |
| 3 | +import nodeUrl from "url"; |
2 | 4 | import { host } from "@jsdevtools/host-environment";
|
3 | 5 |
|
4 | 6 | const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
|
5 | 7 | const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath;
|
6 | 8 |
|
7 |
| -let helpers; |
8 |
| -if (host.node) { |
9 |
| - helpers = await filesystemPathHelpers();; |
10 |
| -} |
11 |
| -else { |
12 |
| - helpers = urlPathHelpers(); |
13 |
| -} |
14 |
| -export default helpers; |
| 9 | +const pathHelpers = { |
| 10 | + filesystem: filesystemPathHelpers(), |
| 11 | + url: urlPathHelpers() |
| 12 | +}; |
15 | 13 |
|
16 | 14 | /**
|
17 | 15 | * Helper functions for getting local filesystem paths in various formats
|
18 | 16 | */
|
19 |
| -async function filesystemPathHelpers () { |
20 |
| - const nodePath = await import("path"); |
21 |
| - const nodeUrl = await import("url"); |
| 17 | +function filesystemPathHelpers () { |
22 | 18 |
|
23 |
| - const testsDir = nodePath.resolve(fileURLToPath(new URL(".", import.meta.url)), ".."); |
| 19 | + if (host.node) { |
| 20 | + const testsDir = nodePath.resolve(fileURLToPath(new URL(".", import.meta.url)), ".."); |
24 | 21 |
|
25 |
| - // Run all tests from the "test" directory |
26 |
| - process.chdir(testsDir); |
| 22 | + // Run all tests from the "test" directory |
| 23 | + process.chdir(testsDir); |
| 24 | + } |
27 | 25 |
|
28 | 26 | const path = {
|
29 | 27 | /**
|
@@ -85,6 +83,10 @@ async function filesystemPathHelpers () {
|
85 | 83 | * Helper functions for getting URLs in various formats
|
86 | 84 | */
|
87 | 85 | function urlPathHelpers () {
|
| 86 | + if (host.node) { |
| 87 | + return |
| 88 | + } |
| 89 | + |
88 | 90 | // Get the URL of the "test" directory
|
89 | 91 | let filename = document.querySelector('script[src*="/fixtures/"]').src;
|
90 | 92 | let testsDir = filename.substr(0, filename.indexOf("/fixtures/")) + "/";
|
@@ -149,3 +151,25 @@ function urlPathHelpers () {
|
149 | 151 |
|
150 | 152 | return path;
|
151 | 153 | }
|
| 154 | + |
| 155 | +export default { |
| 156 | + rel (file) { |
| 157 | + return host.node ? pathHelpers.filesystem.rel(...arguments) : pathHelpers.url.rel(...arguments) |
| 158 | + }, |
| 159 | + |
| 160 | + abs (file) { |
| 161 | + return host.node ? pathHelpers.filesystem.abs(...arguments) : pathHelpers.url.abs(...arguments) |
| 162 | + }, |
| 163 | + |
| 164 | + unixify (file) { |
| 165 | + return host.node ? pathHelpers.filesystem.unixify(...arguments) : pathHelpers.url.unixify(...arguments) |
| 166 | + }, |
| 167 | + |
| 168 | + url (file) { |
| 169 | + return host.node ? pathHelpers.filesystem.url(...arguments) : pathHelpers.url.url(...arguments) |
| 170 | + }, |
| 171 | + |
| 172 | + cwd () { |
| 173 | + return host.node ? pathHelpers.filesystem.cwd(...arguments) : pathHelpers.url.cwd(...arguments) |
| 174 | + } |
| 175 | +} |
0 commit comments