-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write compiled output to stdout #1226
Comments
Would a node package of the compiler with an API that allows you to pass strings and get strings back do the job? |
That'd definitely be useful, but I'd much rather have an option to output to stdout in the cli. |
so what happens if you have multiple outputs? how do you indicate file begin and end? and where do errors go? or should it just be a JSON.stringify output? |
I think it'd be reasonable to only support It'd also be nice if it could take input through stdin, so |
If you're on a Unix system, you can look into mkfifo as a temporary workaround. I _think_ the following might work. mkfifo -m 600 my_fifo
tsc file1.ts file2.ts --out my_fifo
YOUR_COMMAND_HERE < my_fifo
# or...
cat my_fifo | YOUR_COMMAND_HERE but I can admittedly see the utility in having standard output as a target. |
Open questions:
|
Another question: how do you differentiate between output and message output? I guess we could use standard error as well. |
Following @jbowens's suggestion to invariably apply While there's a hypothetical backward-compatibility concern -- currently, As for the "Unix-centricness": I wouldn't expect Windows users to object, either. |
I wanted this functionality for compiling little inline snippets of TypeScript within my templates which I could inject back into |
have you considered typestring? |
I agree with @mklement0's proposal. This would be very helpful. @RyanCavanaugh It seems like you should treat stdout the same as if any filename was passed to the out argument. That will cover the majority of use cases anyway. The rationale for this is wanting to pipe the output to something else, for example a minifier or gzip or ng-annotate or whatever. I think it is likely that people will generally be using this against single files, and using downstream tools to concatenate or whatever. |
I agree. You can see the behavior of related node packages such as node-sass, less, coffeescript. Based on the commonalities, I propose:
to print to stdout instead of carrying out the unwanted/unintended IO op with presumed destination name "blah.js"..
should write to anotherBlah.js without requiring the There was a proposal somewhere in node community to provide some kind of standard CLI interface for transpiler and linter packages. That would have prevented everyone inventing their own way. |
That is a major breaking change in behavior, so we wouldn't be able to do that. |
Perhaps this would be best implemented as a wrapper package. |
@nicksloan How would you implement it as an external tool? Have I think they should just add an If you need further control, a Node package as @mhegazy suggested would be ideal. AFAIK, |
@teppeis has made typescript-simple which could be used for this purpose. You could also use our compiler API. |
@DanielRosenwasser I assumed there would be a way to get at the compiler API, and it looks like you guys have made it easy enough. Typescript-simple makes it even easier. Either way, this is a perfectly viable approach. I wonder if the Typescript team would be open to bundling such a thing as an alternative executable within Typescript once it showed some degree of maturity? |
Supporting emitting to stdout by default would have been a good idea a few years back when tsc first came out; but now changing the behavior is a huge breaking change that we can not justify to our customers. bundling another tool that does mostly what tsc does except slightly different would not be something we would pursue either. I do not see any harm in taking a dependency on another package that wraps tsc or some of its functionality (e.g. typescript-simple). I think this is a decision you make based on your workflow and other tools you need to support. We have invested in making the API simple and usable to simplify such scenarios, and we will continue to invest into this space moving forward. |
@mhegazy Is it possible to add this to the 2.0 release? As npm evolves, I think more people will use |
@YoungRoger i would be new typescript-cli tool that does that, or possibly adding a commandline flag to support this if there is enough user demand. |
I can only see writing to stdout helpful if that works in watch mode too, but I have no idea how. Writing to stdout is a performance optimization, as is watch mode. Use case:
I can't think of an use case of reading from stdin.
Output errors to console: tsc |
@awerlang As I said before, reading from stdin and writing to stdout is useful for composing multiple commands to create a pipeline. It's not a performance optimization, it's a usability improvement. |
If typescript wants to be a good Unix citizen, outputting to stdout is a minimal requirement. For reference, the following does not work, while it should at the very least be supported.
|
@sebastien i like this proposal. i think this will resolve the issue and is not a breaking change. i have filed #4841 to track it. |
PRs for #4841 are also welcomed. |
@mhegazy This won't resolve the issue for Windows users. |
@mnpenner we have not had a complete proposal for this issue so far. |
@mnpenner, @mhegazy: Note that going with the POSIX convention of using For instance, reading from stdin and writing to stdout would then take the form
There is a hypothetical backward-compatibility issue, if there are people out there currently using files literally named As an added convenience, if the input comes from stdin, you could consider outputting to stdout by default, given that no meaningful output filename can be derived; this would:
|
Using |
@sebastien Agreed. Fortunately, it looks like the behavior is an incidental byproduct of A naïve fix that implements
A potential problem is that the Node.js documentation states that P.S.: Here's a simple test that you can run in the Node.js REPL to reproduce the problem:
Interestingly, Linux reports error 'ESPIPE, invalid seek', while OSX reports 'ENXIO, no such device or address'. |
So this issue is not really about writing the compiled output to stdout anymore, that was fixed by #5454. This is about making it nice to use, so reading from stdin, writing errors to stderr and accepting |
Following the POSIX guideline 13: > For utilities that use operands to represent files to be opened for > either reading or writing, the '-' operand should be used only to > mean > standard input (or standard output when it is clear from context > that > an output file is being specified). http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02 See issue microsoft#1226
That would be a nice addition, given that it's standard practice in UNIX . So yes, |
@sebastien with #5454 |
👍
|
This issue hasn't had any activity for quite some time. I think that Windows should be supported better, as *NIX can only output to stdout at this point (by specifying It seems like the best solution to this, which has been proposed in a few locations (1, 2), is to allow a special value Example usage:
If a compiler error occurs, the errors will be appended after the emitted output. I've worked on a very simple implementation for this issue which is essentially just the following in compiler/sys/writeFile // If `-` is provided as the fileName, output to stdout
if (fileName === '-') {
process.stdout.write(data, "utf8");
return;
} I'm wondering how to author tests for a change like this, and what kind of documentation changes need to happen, and where those go. Thanks. |
This would definitely be the expected behaviour from a Unix perspective. |
Using --outfile=/dev/stdout works now, but if there is an error on compilation, the error is printed in-place of/alongside the compiled output. This, again, is not standard practice and can cause issues when scripting. To work around this, I use the following.
Because the errors are printed on stdout, I'm telling tsc to print code on stderr, and then switching the file descriptors on it, resulting in errors on stderr, and code on stdout. Hope this helps someone! |
Declining this one - |
This becomes a necessity again. I am currently experimenting with deno. As you know deno and typescript go hand in hand. Node modules on the other hand not really. So |
Printing to tsc --module none --outFile /dev/stdout some-script.ts | node , but it is not portable. Yes, Mind boggling. |
…mment); 用F:\i686-4.9.2-release-win32-dwarf-rt_v4-rev4\mingw32\bin编译出可正常执行的tsc.exe, F:\i686-8.1.0-release-posix-dwarf-rt_v6-rev0\mingw32\bin编译的不行
Given a ts file:
one can get the generated JavaScript to stdout as @gh-andre pointed out: tsc --module none --outFile /dev/stdout sample.json.ts
var foo = {
"id": 13,
"name": "horst",
"cars": [{
"brand": "VW",
"maxSpeed": 120,
"isWastingGazoline": true
}]
} Unfortunately, it doesn't work for getting the declarations onto stdout: tsc --emitDeclarationOnly --declaration --module none --outFile /dev/stdout sample.json.ts error TS5033: Could not write file '/dev/stdout.d.ts': EACCES: permission denied, open '/dev/stdout.d.ts'.
Found 1 error. I am interested in getting the typescript declaration from a json file
|
This doesn't work if npx tsc --target esnext --outFile /dev/stdout --module none --moduleResolution node script.ts | node --input-type module |
@Soberia I struggled with this and other things TypeScript for a bit and then moved my DevOps scripts to JavaScript. Far from ideal, but I cut down DevOps scripts enough to justify some duplication between the main TypeScript app and DevOps scripts. Will probably need to revisit it this year, as both codebases grow, but so far it's simpler to manage for my use cases. |
It doesn't seem possible to compile and output to stdout. There was some discussion on CodePlex before the migration to github: https://typescript.codeplex.com/workitem/600
The text was updated successfully, but these errors were encountered: