Skip to content

Commit

Permalink
webassembly/api: Fix importing micropython.mjs module from node REPL.
Browse files Browse the repository at this point in the history
Fixes issue micropython#14363.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed May 6, 2024
1 parent a521df2 commit 9681a66
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions ports/webassembly/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,25 @@ if (
typeof process.versions === "object" &&
typeof process.versions.node === "string"
) {
// Check if this module is ron from the command line.
// Check if this module is run from the command line via `node micropython.mjs`.
//
// See https://stackoverflow.com/questions/6398196/detect-if-called-through-require-or-directly-by-command-line/66309132#66309132
//
// Note:
// - `resolve()` is used to handle symlinks
// - `includes()` is used to handle cases where the file extension was omitted when passed to node

const path = await import("path");
const url = await import("url");
if (process.argv.length > 1) {
const path = await import("path");
const url = await import("url");

const pathToThisFile = path.resolve(url.fileURLToPath(import.meta.url));
const pathPassedToNode = path.resolve(process.argv[1]);
const isThisFileBeingRunViaCLI = pathToThisFile.includes(pathPassedToNode);
const pathToThisFile = path.resolve(url.fileURLToPath(import.meta.url));
const pathPassedToNode = path.resolve(process.argv[1]);
const isThisFileBeingRunViaCLI =
pathToThisFile.includes(pathPassedToNode);

if (isThisFileBeingRunViaCLI) {
runCLI();
if (isThisFileBeingRunViaCLI) {
runCLI();
}
}
}

0 comments on commit 9681a66

Please sign in to comment.