Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit f11e670

Browse files
committed
Create paths that work cross-platform
Use proper file URLs when dynamically importing JS files, instead of relying on absolute macOS file paths looking like parts of URLs. Generate paths only in config.js.
1 parent 89f2d50 commit f11e670

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

bin/write-sources.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import fs from "fs";
22
import path from "path";
3+
import makeDir from "make-dir";
34
import * as utils from "../src/core/utils.js";
45
import logger from "../logger.js";
56
import identitySource from "../identity-source.js";
6-
import makeDir from "make-dir";
7-
8-
const __dirname = path.dirname(import.meta.url.split(":")[1]);
9-
const __root = path.join(__dirname, "..");
7+
import { ROOT_DIRECTORY, ROOT_URL, SOURCES_DIRECTORY } from "../src/config.js";
108

11-
const exportPath = path.join(__root, "tmp");
9+
const exportPath = path.join(ROOT_DIRECTORY, "tmp");
1210

1311
function dealWithCrossWalk(crosswalk) {
1412
let tmpCrosswalk = crosswalk;
@@ -22,8 +20,8 @@ function dealWithCrossWalk(crosswalk) {
2220
const handleSource = async (sourceName) => {
2321
await makeDir(exportPath);
2422

25-
const country = (await import(path.join(__root, `/sources/${sourceName}`)))
26-
.default;
23+
const importURL = new URL(`/sources/${sourceName}`, ROOT_URL);
24+
const country = (await import(importURL)).default;
2725
const countryLength = country.length;
2826

2927
logger.info(`${sourceName}; Length: ${countryLength}`);
@@ -62,7 +60,7 @@ const handleSource = async (sourceName) => {
6260
};
6361

6462
const handleSources = async () => {
65-
const sources = await utils.asyncReadDir(path.join(__root, "sources"));
63+
const sources = await utils.asyncReadDir(SOURCES_DIRECTORY);
6664
await Promise.all(sources.map(handleSource));
6765
};
6866

src/config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import path from "path";
22
import dotenv from "dotenv";
3+
import { fileURLToPath } from 'url';
34

45
dotenv.config();
56

67
// Paths -----------------------------------------------------------------------
7-
const __dirname = path.dirname(import.meta.url.split(":")[1]);
8+
// Create a cross-platform version of __dirname that works in ESM.
9+
// https://stackoverflow.com/a/55944697/4200446
10+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
11+
export const ROOT_URL = String(new URL("..", import.meta.url));
812
export const ROOT_DIRECTORY = path.join(__dirname, "..");
13+
export const SOURCES_URL =
14+
// Include the trailing slash on sources/ to treat it as a directory.
15+
process.env.SOURCES_URL || String(new URL("sources/", ROOT_URL));
916
export const SOURCES_DIRECTORY =
1017
process.env.SOURCES_DIRECTORY || path.join(ROOT_DIRECTORY, "sources");
1118
export const DATA_DIRECTORY =

src/core/sources.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ import * as utils from "./utils.js";
4646
import * as config from "../config.js";
4747

4848
const filenames = await utils.asyncReadDir(config.SOURCES_DIRECTORY);
49-
50-
const promises = filenames.map((name) => {
51-
return import(path.join(config.SOURCES_DIRECTORY, name));
52-
});
49+
const promises = filenames.map((name) => import(new URL(name, config.SOURCES_URL)));
5350
const imports = await Promise.all(promises);
5451

5552
export const raw = imports.map((m) => m.default).flat();

0 commit comments

Comments
 (0)