Closed evantahler closed 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();
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
Will close this.
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?
Where process[Symbol.for('ts-node.register.instance')]
is set?
@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.
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
Added process.env.TS_NODE_DEV
set.
How would this work with jest or any other testing framework, because the variable does not seem to be set when running test.
Hello! This project is wonderful.
One of the newer features
ts-node
has added is a symbol on theprocess
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?