Skip to content

etienne-graveyard/run-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

run-ts

A tools to use Typescript files as npm scripts

Gist

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"
  }
}

How to

1. Install run-ts

npm install --save-dev run-ts
# or
yarn add --dev run-ts

2. Create a scripts folder

mkdir scripts

3. Create a scripts/tsconfig.json file

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "baseUrl": "."
  }
}

4. Create a scripts/my-script.ts file

const foo: number = 42;
console.log(foo);

5. Create a my-script scripts in your package.json

{
  "scripts": {
    "my-script": "run-ts"
  }
}

That's it, now when you run npm run my-script the scripts/my-script.ts file is executed.

Using args

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)