A tools to use Typescript files as npm scripts
before
{
"scripts": {
"setup": "ts-node --project ./scripts/tsconfig.json ./scripts/setup.ts some-argument",
"build": "ts-node --project ./scripts/tsconfig.json ./scripts/build.ts"
}
}
after
{
"scripts": {
"setup": "run-ts some-argument",
"build": "run-ts"
}
}
npm install --save-dev run-ts
# or
yarn add --dev run-ts
mkdir scripts
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"baseUrl": "."
}
}
const foo: number = 42;
console.log(foo);
{
"scripts": {
"my-script": "run-ts"
}
}
That's it, now when you run npm run my-script
the scripts/my-script.ts
file is executed.
If your file export a function as default
, run-ts
will execute this function with the arguments as first parameter:
// scripts/setup.ts
export default function setup(args: Array<string>) {
console.log(args);
}
Note: You can also use process.argv
but you have to strip the first two items (node
and run-ts
)