wclr / ts-node-dev

Compiles your TS app and restarts when files are modified.
MIT License
3.43k stars 123 forks source link

Detecting that we are in typescript or not? #108

Closed evantahler closed 4 years ago

evantahler commented 4 years ago

Hello! This project is wonderful.

One of the newer features ts-node has added is a symbol on the process that can be used to detect if we are running in typescript or JS. This is very helpful when developing frameworks (like www.actionherojs.com) to know which types of files to load in... even when running as a distributed javascript package.

The ts-node converstation: https://github.com/TypeStrong/ts-node/pull/858

Could this be ported that to ts-node-dev?

evantahler commented 4 years ago

As of this writing, here's a best-guess using process.execArgv which would indicate that we are running ts-ndoe-dev from the shim in /tmp.

import * as path from "path";

function isTypescript(): boolean {
  // if this file is typescript, we are running typescript :D
  // this is the best check, but fails when actionhero is compiled to js though...
  const extension = path.extname(__filename);
  if (extension === ".ts") {
    return true;
  }

  // are we running via a ts-node/ts-node-dev shim?
  const lastArg = process.execArgv[process.execArgv.length - 1];
  if (lastArg && path.parse(lastArg).name.indexOf("ts-node") > 0) {
    return true;
  }

  try {
    /**
     * Are we running in typescript at the moment?
     * see https://github.com/TypeStrong/ts-node/pull/858 for more details
     */
    return process[Symbol.for("ts-node.register.instance")] ||
      (process.env.NODE_ENV === "test" &&
        process.env.ACTIONHERO_TEST_FILE_EXTENSION !== "js")
      ? true
      : false;
  } catch (error) {
    console.error(error);
    return false;
  }
}

export const typescript = isTypescript();
mpvosseller commented 4 years ago

I'm looking for this also. Need to select path (src or dist) depending on the executing environment (compiled or not) like the author of this ts-node issue. https://github.com/TypeStrong/ts-node/pull/858

For the time being I used const isTypeScript = __filename.endsWith('ts') which works well for my use case. Thanks for the tip @evantahler

Leo7654 commented 4 years ago

https://github.com/TypeStrong/ts-node/issues/846

wclr commented 4 years ago

Will close this.

apiel commented 4 years ago

For me, process[Symbol.for('ts-node.register.instance')] is only working with ts-node and not ts-node-dev. How Should I do, to get it work ts-node-dev?

wclr commented 4 years ago

Where process[Symbol.for('ts-node.register.instance')] is set?

apiel commented 4 years ago

@whitecolor, if you look at the comment from @Leo7654 there was a solve issue in ts-node TypeStrong/ts-node#846 This is working well with ts-node but it is not recognize when I am using ts-node-dev. Normally, you should be able to use this process[Symbol.for('ts-node.register.instance')] anywhere in your code.

wclr commented 4 years ago

Yes this won't work because we are not running ts-node env only using its transpilation service.

Maybe worth invoke something alike in a child. https://github.com/TypeStrong/ts-node/commit/b681e2457c7acc28fcbba638a8dac6ecf27846e3#diff-6b623d6bcf5e7f06c466aa060ec9c4b6

wclr commented 4 years ago

Added process.env.TS_NODE_DEV set.

sushovannits commented 3 years ago

How would this work with jest or any other testing framework, because the variable does not seem to be set when running test.