Skip to content

hook into tsc --watch using stdout -- the unix way #19584

Open
@ORESoftware

Description

@ORESoftware

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions