Socket
Socket
Sign inDemoInstall

tsc-watch

Package Overview
Dependencies
5
Maintainers
1
Versions
90
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsc-watch


Version published
Maintainers
1
Install size
265 kB
Created

Package description

What is tsc-watch?

The tsc-watch npm package is a utility that allows you to run the TypeScript compiler (tsc) in watch mode and execute custom scripts when certain events occur, such as when compilation starts, succeeds, or fails. This is particularly useful for automating tasks during development, such as running tests or restarting a server.

What are tsc-watch's main functionalities?

Run TypeScript compiler in watch mode

This feature allows you to run the TypeScript compiler in watch mode, which means it will automatically recompile your TypeScript files whenever they change. The `--onSuccess` flag specifies a command to run whenever the compilation succeeds. In this example, it runs a Node.js script located at `./dist/index.js`.

tsc-watch --onSuccess "node ./dist/index.js"

Run custom scripts on compilation failure

This feature allows you to specify a command to run whenever the TypeScript compilation fails. In this example, it simply echoes 'Compilation Failed!' to the console.

tsc-watch --onFailure "echo Compilation Failed!"

Run custom scripts on compilation start

This feature allows you to specify a command to run whenever the TypeScript compilation starts. In this example, it echoes 'Compilation Started!' to the console.

tsc-watch --onCompilationStarted "echo Compilation Started!"

Other packages similar to tsc-watch

Readme

Source

Build Status

The nodemon for TypeScript

tsc-watch starts a TypeScript compiler with --watch parameter, with the ability to react to compilation status. tsc-watch was created to allow an easy dev process with TypeScript. Commonly used to restart a node server, similar to nodemon but for TypeScript.

ArgumentDescription
--onSuccess COMMANDExecutes COMMAND on every successful compilation.
--onFirstSuccess COMMANDExecutes COMMAND on the first successful compilation.
--onFailure COMMANDExecutes COMMAND on every failed compilation.
--onCompilationStarted COMMANDExecutes COMMAND on every compilation start event (initial and incremental).
--onCompilationComplete COMMANDExecutes COMMAND on every successful or failed compilation.
--maxNodeMemCalls node with a specific memory limit max_old_space_size, to use if your project needs more memory.
--noColorsBy default tsc-watch adds colors the output with green
on success, and in red on failure.
Add this argument to prevent that.
--noClearIn watch mode the tsc compiler clears the screen before reporting
Add this argument to prevent that.
--silentDo not print any messages on stdout.
--compiler PATHThe PATH will be used instead of typescript compiler.
Default is typescript/bin/tsc

Notes:

  • That all the above COMMANDs will be killed on process exit. (Using SIGTERM)

  • A COMMAND is a single command and not multi command like script1.sh && script2.sh

  • Any child process (COMMAND) will be terminated before creating a new one.

Install

npm install tsc-watch --save-dev
## for command-line usage
npm install -g typescript tsc-watch

Usage

From Command-Line

## Watching a project (with tsconfig.json)
tsc-watch --onSuccess "node ./dist/server.js"

## Beep on failure
tsc-watch --onFailure "echo Beep! Compilation Failed"

## Watching a single file
tsc-watch server.ts --outDir ./dist --onSuccess "node ./dist/server.js"

## Custom compiler
tsc-watch --onSuccess "node ./dist/server.js" --compiler my-typescript/bin/tsc

From npm script

"dev-server": "tsc-watch --noClear -p ./src/tsconfig.json --onSuccess \"node ./dist/server.js\"",

From javascript

You can see a detailed example here

The client is implemented as an instance of Node.JS's EventEmitter, with the following events:

  • started - Emitted upon the compilation start (initial or incremental).
  • first_success - Emitted upon first successful compilation.
  • subsequent_success - Emitted upon every subsequent successful compilation.
  • compile_errors - Emitted upon every failing compilation.
  • file_emitted - Emitted upon every file transpiled if --listEmittedFiles is used.

Once subscribed to the relevant events, start the client by running watch.start()

To kill the client, run watch.kill()

Example usage:

const TscWatchClient = require('tsc-watch/client');
const watch = new TscWatchClient();

watch.on('started', () => {
  console.log('Compilation started');
});

watch.on('first_success', () => {
  console.log('First success!');
});

watch.on('success', () => {
  // Your code goes here...
});

watch.on('compile_errors', () => {
  // Your code goes here...
});

watch.start('--project', '.');

try {
  // do something...
} catch (e) {
  watch.kill(); // Fatal error, kill the compiler instance.
}

Notes:

  • The (onSuccess) COMMAND will not run if the compilation failed.
  • tsc-watch is using the currently installed TypeScript compiler.
  • tsc-watch is not changing the compiler, just adds the new arguments, compilation is the same, and all other arguments are the same.

Keywords

FAQs

Last updated on 03 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc