microsoft / microsoft/TypeScript

hook into `tsc --watch` using stdout -- the unix way

Open
#19,584 0 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

This is a feature request.

I would like to launch tsc -w and then read from stdout to know when tsc -w has started and completed a build. Like so:

const cp = require('child_process');

const k = cp.spawn('bash');

setImmediate(function(){
     k.stdin.end(`\n  tsc --watch  --project x  \n`);      
});

let stdout = '';
k.stdout.on('data', function(d){
    stdout += String(d);
    if(/foobar/.test(stdout)){
        makeMyDay();
    }
});

Right now, tsc --watch outputs stdout that is mostly just human readable, and doesn't provide very useful information:

1:33:44 PM - File change detected. Starting incremental compilation...

../suman-types/dts/it.d.ts(6,18): error TS2430: Interface 'ITestDataObj' incorrectly extends interface 'ITestOrHookBase'.
  Property 'cb' is optional in type 'ITestDataObj' but required in type 'ITestOrHookBase'.
../suman-types/dts/test-suite.d.ts(13,23): error TS2503: Cannot find namespace 'Chai'.
1:33:44 PM - Compilation complete. Watching for file changes.

Proposal:

tsc --watch should have a new flag, that tells tsc --watch to output machine readable data to stdout, instead of human readable data. For example:

tsc --watch --machine-stdio

Using this new flag, no existing users will be affected.

Ideally, output JSON to stdout, something like this upon a file change:

const data = {source: '@tscwatch', event: 'file_change', srcfilePath: filePath, willTranspile: true/false, destinationFilePath: null / destinationFilePath};
console.log(JSON.stringify(data);

then after transpilation finishes, write something like this to stdout:

const data = {source: '@tscwatch', event: 'compilation_complete', errors: null / errors:[]};
console.log(JSON.stringify(data));

if it's behind a flag (--machine-stdio) then it won't affect any current users, so should be safe.
I need a truly unique field value like '@tscwatch' so that I know that the JSON is coming from a certain process. In general, it's possible to be parsing more than one JSON stream from a process, so having a unique field in each JSON object makes this better.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the tsc --watch command path and the existing stdout messages for file changes, compilation completion, and errors. Determine the machine-readable event schema and flag behavior, then verify that --machine-stdio emits identifiable JSON events without changing default human-readable output.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.