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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"name": "ckq_test",
"version": "1.0.2",
"description": "ckq_test_desc",
"list": [
{
"name": "a",
"age": 10
},
{
"name": "b",
"age": 20
}
]
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
if [ -n "$projectRoot" ]; then
cd $projectRoot
fi
node_exec='node --abort-on-uncaught-exception -pe '

file='./test.json'

test_content=`${node_exec} "
var content = require('${file}');
if (content) {
JSON.stringify(content);
} else {
'';
}
"`

#check file
if [ -z "$test_content" ]; then
echo "Failed: ${file} is empty!"
exit 1
fi

name="a"
echo "name [${name}]"

new_content=`${node_exec} "
var content = ${test_content};
var name = '${name}';
var find = '';
if (content && content.list && Array.isArray(content.list)) {
for (var item of content.list) {
if (item.name === '${name}') {
find = item;
break;
}
}
}

if(find) {
find.age = 15;
JSON.stringify(content, null, 2);
} else {
'';
}
"`

echo "new content [${new_content}]";

if [ -z "${new_content}" ]; then
echo "Failed: cannot find person [${name}]!"
exit 1;
fi

# write back file
echo "${new_content}" > ${file}
echo "Success"

exit 0;

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.