Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: Is the current file being run from the shell? #2545

Closed
rauschma opened this issue Feb 25, 2018 · 2 comments
Closed

Example: Is the current file being run from the shell? #2545

rauschma opened this issue Feb 25, 2018 · 2 comments

Comments

@rauschma
Copy link

Suggestion: On the page https://bucklescript.github.io/docs/en/nodejs-special-variables.html , add the following example:

/**
 * Is the current file being run from the shell?
 * @param _module Call with [%bs.node _module]
 */
let isMain = (_module: option(Node.node_module)) => {
  let require: option(Node.node_require) = [%bs.node require];
  
  switch (require, _module) {
  | (Some(require'), Some(_module')) =>
    switch (Js.undefinedToOption(require'##main)) {
    | Some(main') =>
      main' === _module'
    | None => Js.Exn.raiseError("Illegal state")
    };
  | _ => Js.Exn.raiseError("Illegal state")
  };
};

Created with help from Glenn Slotte. https://reasonml.chat/t/where-are-node-module-and-node-require/191

@glennsl
Copy link
Contributor

glennsl commented Feb 25, 2018

Webpage source is here btw, if you'd like to make it a PR.

@rauschma
Copy link
Author

@glennsl Right. Let’s pause this and I’ll submit a PR with (a simplified version) of this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants