Gustavo Lopes
Gustavo Lopes

Reputation: 4174

Transpile Typescript without CLI

How can I transpile Typescript without using the command line?

I want to have a script build.js that will build my application.

So far, I found that the typescript package has a transpile(inputArgument, compileOptions) method, but I couldn't find anything about this on their documentation.

I can have my project built using tsc -p server, so I imagined the following would work:

const tsc = require('typescript');

tsc.transpile("", {
    project: "server",
});

Then I'd use node build.js.

I couldn't get the tsc.transpile() to work (I couldn't figure out what the inputArgument was) and I couldn't find on their documentation how to do this: transpile from code, without the CLI.

Upvotes: 2

Views: 231

Answers (1)

hoangdv
hoangdv

Reputation: 16127

You could use child_processmodule

In your case:

const { spawn } = require('child_process');

const ls = spawn('tsc', ['-p', 'server']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

Upvotes: 2

Related Questions