bash 如何使用 TypeScript 制作 shell 可执行节点文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23298295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:18:11  来源:igfitidea点击:

How to make a shell executable node file using TypeScript

node.jsbashtypescriptcommand-line-interface

提问by Tom Larkworthy

Normally in node files I just put

通常在我刚刚放置的节点文件中

#!/usr/bin/env node 

at the top and make it executable to create a file that can be run from a bash terminal. However if I do that in a Typescript file, the compiler says "error TS1001: Unexpected character "#"" and refuses to compile it. So how can I make a shell executable node file with Typescript?

在顶部并使其可执行以创建可以从 bash 终端运行的文件。但是,如果我在 Typescript 文件中这样做,编译器会说“错误 TS1001:意外字符“#””并拒绝编译它。那么如何使用 Typescript 制作一个 shell 可执行节点文件呢?

采纳答案by sampablokuper

You were right to report the bug to Microsoft, and they were wrong to close it as wontfix.

您向 Microsoft 报告该错误是正确的,而将其关闭为wontfix是错误的。

Until it isfixed, here's a workaround. Paste the following into a text file and save it as shebangify:

直到它固定的,这里有一个解决方法。将以下内容粘贴到文本文件中并将其另存为shebangify

#!/usr/bin/env node
var fs = require('fs');
var path = process.argv[2];
var data = "#!/usr/bin/env node\n\n";
data += fs.readFileSync(path);
fs.writeFileSync(path, data);

(N.B. To keep this answer concise, the code above doesn't have any error-checking or other refinements, so use at your own risk or use thisinstead. Also, see this SO questionfor more info about prepending to files.)

(注意,为了保持这个答案简洁,上面的代码没有任何错误检查或其他改进,因此请自行承担风险或改用。另外,请参阅此 SO 问题以获取有关附加到文件的更多信息。)

Make the file executable with by using a terminal to navigate to the file's directory and executing:

通过使用终端导航到文件目录并执行以下命令,使文件可执行:

$ chmod +x shebangify

Once you have created a Typescript program (e.g. called myscript.ts) that you wish to compile and turn into a shell script (e.g. called myscript), do so by executing a sequence along these lines in your terminal:

一旦你创建了一个myscript.ts你希望编译并转换成 shell 脚本(例如调用myscript)的 Typescript 程序(例如调用),通过在终端中沿着这些行执行序列来实现:

$ tsc --out myscript myscript.ts ; ./shebangify myscript ; chmod +x myscript

回答by basarat

See https://github.com/Microsoft/TypeScript/blob/master/bin/tscfor an example. Basically have a dummy file without the .jsextension and just require the actual .jsfile.

有关示例,请参见https://github.com/Microsoft/TypeScript/blob/master/bin/tsc。基本上有一个没有.js扩展名的虚拟文件,只需要实际.js文件。

E.g. In file named tsc:

例如在名为的文件中tsc

#!/usr/bin/env node
require('./tsc.js')

回答by Jonathan

I don't have enough reputation points to post a comment, but I'd just thought it'd be good for everyone to know that I opened a new issue on GitHub since that's what the Typescript devs are using to track things like this: https://github.com/Microsoft/TypeScript/issues/2749.

我没有足够的声望点来发表评论,但我只是认为让每个人都知道我在 GitHub 上打开了一个新问题是件好事,因为 Typescript 开发人员正在使用它来跟踪这样的事情:https://github.com/Microsoft/TypeScript/issues/2749

回答by Breck

If you have TypeScript and ts-node installed globally:

如果您全局安装了 TypeScript 和 ts-node:

npm install typescript ts-node -g

You can now easily do this with:

您现在可以轻松地做到这一点:

#!/usr/bin/env ts-node

console.log('Hello world')

回答by ShadSterling

I've never been able to get ts-nodeto work, so I finally made my own way to write shell scripts in TypeScript. If there were a package manager for Bash I would make a package, but there isn't, so I just put this script in my path as ts-exec:

我一直无法开始ts-node工作,所以我终于用自己的方式在 TypeScript 中编写了 shell 脚本。如果有 Bash 的包管理器,我会制作一个包,但没有,所以我只是把这个脚本放在我的路径中ts-exec

#!/usr/bin/env bash

file_to_run=""
basename=`basename ""`
tmp_prefix=`basename "$BASH_SOURCE"`

TMPDIR=`mktemp -d -t "$tmp_prefix-XXXXXXXXXX"`
pushd "$TMPDIR" > /dev/null

cp "" "$basename.ts"
tsc "$basename"
node "$basename.js"

popd > /dev/null
rm -rf "$TMPDIR"

And now I can do things like this:

现在我可以做这样的事情:

#!/usr/bin/env ts-exec

let greeting: string = "Hello World!";

console.log( greeting );

And it works.

它有效。

Of course, it does have some limitations

当然,它也有一些限制

  • It's only suitable for scripts that are confined to a single file
  • It doesn't do any error checking
  • It has implicit dependencies
  • It doesn't have an installer
  • 它只适用于限制在单个文件中的脚本
  • 它不做任何错误检查
  • 它具有隐式依赖关系
  • 它没有安装程序

... so basically it's for bash nerds who want to use TypeScript for small scripts that would be a pain to write as Bash scripts. I'm still baffled that ts-nodedoesn't cover this case, and I'd rather not have to futz with temp files that might get left behind and waste space if there's an error, but so far this covers my use-case. (Besides, I've got that cronjob that deletes everything in ~/tmpthat's more than 31622400 seconds old every night, so stray temp files can't eat my whole system.)

...所以基本上它适用于想要将 TypeScript 用于小脚本的 bash 书呆子,这些脚本编写为 Bash 脚本会很痛苦。我仍然ts-node对没有涵盖这种情况感到困惑,而且我宁愿不必处理临时文件,如果出现错误,这些临时文件可能会被遗留下来并浪费空间,但到目前为止,这涵盖了我的用例。(此外,我有一个 cronjob,它会删除~/tmp每晚超过 31622400 秒的所有内容,所以杂散的临时文件不能吃掉我的整个系统。)