简体   繁体   English

如何通过 CLI 自定义命令运行节点脚本?

[英]How to run a node script by CLI custom command?

Currently, I'm working on a personal project and I'd like to create an alias for some commands that I have to run... Similar to:目前,我正在做一个个人项目,我想为一些我必须运行的命令创建一个别名......类似于:

jest...笑话...

node...节点...

tsc... TS...

I think that for my project would be so helpful and cool to have something like我认为对于我的项目来说,拥有类似的东西会非常有帮助和酷

foo...噗...

I'm working with Node, Typescript, and JS.我正在使用 Node、Typescript 和 JS。

I took a look on the inte.net and read saw some people teaching how to create some alias I tried and it works:) I already have the alias working on my local machine cause I added the alias on the .bashrc file.我在 inte.net 上看了一下,看到有人教我如何创建一些别名,我试过并且它有效:) 我已经在我的本地机器上使用了别名,因为我在.bashrc文件中添加了别名。

alias foo = command...别名 foo = 命令...

However, I also tried to put it on my package.json scripts section, like:但是,我也尝试将它放在我的package.json脚本部分,例如:

"scripts": { "runFoo": "foo", “脚本”:{ “runFoo”:“富”,

But when I run npm run runFoo it says that "foo" is not recognized... Are any way to do that?但是当我运行npm run runFoo时,它说“foo”无法识别……有什么办法吗? How the tools like jest do that? jest 之类的工具如何做到这一点?

I would be thankful for any direction about what to study for that.对于为此学习的任何方向,我将不胜感激。

Extra: There is any way to run all the .js from a folder by using any node command without knowing the name of the files?额外:有什么方法可以在不知道文件名的情况下使用任何节点命令从文件夹中运行所有.js Like:喜欢:

node *.js节点*.js

It can help while I don't figure out how to do the alias...虽然我不知道如何做别名,但它可以提供帮助......

Edit: What I want to do is: https://developer.okta.com/blog/2019/06/18/command-line-app-with-nodejs编辑:我想做的是: https://developer.okta.com/blog/2019/06/18/command-line-app-with-nodejs

The answer helped me to find this post, and following it, it worked here.答案帮助我找到了这篇文章,并跟随它,它在这里起作用。

it says that "foo" is not recognized它说“foo”未被识别

That is because shell aliases do not work in npm scripts (they are only valid when directly called in said shell), see eg How can I pipe to a bash alias from an npm script?那是因为 shell 别名在 npm 脚本中不起作用(它们仅在直接在所述 shell 中调用时才有效),请参见例如如何将 pipe 转换为 npm 脚本中的 bash 别名?

Aliases are meant to reduce typing, and are not automatically shared across environments, on the contrary of npm scripts which are committed with package.json .别名旨在减少输入,并且不会自动跨环境共享,与 npm 脚本相反,这些脚本使用package.json Hence if you try running that script in another environment, the latter may not know that alias (or worse, use it for something else.).因此,如果您尝试在另一个环境中运行该脚本,后者可能不知道该别名(或者更糟的是,将其用于其他用途。)。


How the tools like jest do that? jest 之类的工具如何做到这一点?

They do not... Their command is not an alias , but an actual executable binary or script.他们不...他们的命令不是别名,而是实际的可执行二进制文件或脚本。

When installed as dependencies, you will find links to their executable in node_modules/.bin/ folder, see What is the purpose of.bin folder in node_modules?当作为依赖项安装时,您会在node_modules/.bin/文件夹中找到它们的可执行文件的链接,请参阅 node_modules 中 .bin 文件夹的用途是什么?

You can easily create such executable scripts, even written in JavaScript to be executed by a Node.js engine, see eg Appropriate hashbang for Node.js scripts你可以很容易地创建这样的可执行脚本,甚至是用 JavaScript 编写的,由 Node.js 引擎执行,参见例如适用于 Node.js 脚本的 hashbang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM