How to Write Node.js in Shell Script
The scripts commonly used by iOS developers are Ruby
, such as Homebrew
, CocoaPods
, Fastlane
, etc. are all Ruby
scripts used.
Python
is also a commonly used scripting language, basically you can write a little bit more or less.
Shell
is also a commonly used script under Linux/Unix. For front-end development, Node.js
is probably the more common tool.
At present, cross-end
and hybrid
solutions are also widely used in the field of mobile development, such as React Native
.
My company uses React Native extensively to develop mobile applications. Both front-end and native developers are involved, and the technology stack is more like a front-end technology stack.
Some continuous integration platforms provide many Shell-based plug-ins, such as jenkins
.
There are already many complete automated construction tools under Node.js
, such as gulp
.
However, there are still some scenarios where you need to use the shell
, and the shell
itself is not so friendly to some processing, such as Json
processing and so on.
Especially for some front-end developers, even if they are forced to use shell
, they still expect to use the Node.js
they are used to to write some script logic.
Here is a brief description of the approximate writing method. Of course, you can also put all the logic into the JS
file, and then use the Node.js
command to run the js
script directly in the shell
, which is also very good. Here is just an example of hybrid writing and does not represent best practice. It is not recommended in actual production.
For example, we now have such a Json file
filename: test.json
1 | { |
We need to find the element whose name
is equal to a under the list
node inside, change the age
to 15
, and write the file back.
The following is the code implementation
1 |
|
Summarize
To sum up, in the shell
script, use the node
command to execute the JS
code snippet.
It is convenient to write some simple tool scripts in this way, and there is no need to do too much engineering work.
But the shortcomings are also obvious, lack of sufficient grammar checks, and it is difficult for automated tools to do verification for us. Version management and subsequent maintenance of scripts are cumbersome.
Generally speaking, it is not recommended to use in production environment.