简体   繁体   中英

How to run a TypeScript function from a package.json script

Currently I am running a javascript function from my package.json script, but I was looking to change it to a typescript function in a typescript file and was wondering how to adjust for that?

In javascript I have the following, which works and runs the desired function:

"scripts": {
    "command": "node -e 'require(\"./javaScriptFileName\").functionName()'"
}

The typescript file I am exporting the function from looks like this:

const fs = require("fs");

module.exports.functionName = function(path: string): void
{
   if(fs.exists(path))
   {
       console.log("Exists");
   }
}

I've tried getting the ts-node package and replacing "node" with "ts-node", which doesn't seem to work.

Assuming that javaScriptFileName.ts is structured like:

export default {
  functionName: () => console.log('whatever')
}

Then can just run it via ts-node like you would in any typescript file: import the file, then run the function.

ts-node -e "import myLib from './javaScriptFileName'; myLib.foo()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM