@@ -8,14 +8,13 @@ This is primarily achieved by using [shelljs/shelljs](https://github.com/shelljs
88You can compare the final script code to ` zx ` example:
99``` javascript
1010#! / usr/ bin/ env nodejsscript
11- import { s , echo } from " nodejsscript" ;
1211echo (s .grep (" name" , " package.json" ));
1312
14- s .exec (" git branch --show-current" ).xargs (s .exec , " dep deploy --branch={}" );
13+ s .run (" git branch --show-current" ).xargs (s .run , " dep deploy --branch={}" );
1514
16- s .exec (" sleep 1; echo 1" );
17- s .exec (" sleep 2; echo 2" );
18- s .exec (" sleep 3; echo 3" );
15+ s .run (" sleep 1; echo 1" );
16+ s .run (" sleep 2; echo 2" );
17+ s .run (" sleep 3; echo 3" );
1918
2019import { join } from " node:path" ;
2120const name = " foo bar" ;
@@ -27,10 +26,11 @@ s.mkdir(join(s.tempdir(), name));
2726
28271 . tested/used on
* NodeJS
* :
` [email protected] ` and
` [email protected] ` ⇒ for installation follow
[ nvm-sh/nvm: Node Version Manager
] ( https://github.com/nvm-sh/nvm ) [ ^ OR ] 29281 . ` npm install https://github.com/jaandrle/nodejsscript --global ` (** will be registered also in npm repository** )
29+ 1 . alternatively install locally
3030
3131## Goods
3232[ s #shelljs] ( ./docs/modules/s.md )
33- · [ cli] ( ./docs/modules/cli.md ) ([ cli.api() #sade] ( ./docs/modules/cli.md#api ) , [ cli.read()] ( ./docs/modules/cli.md#read ) , …)
33+ · [ cli] ( ./docs/modules/cli.md ) ([ cli.api() #sade] ( ./docs/modules/cli.md#api ) , [ cli.read()] ( ./docs/modules/cli.md#read ) , [ cli.xdg ] ( ./docs/modules/xdg_.xdg.md ) , …)
3434 · [ echo()] ( ./docs/README.md#echo )
3535 · [ fetch() #node-fetch] ( ./docs/README.md#fetch )
3636 · [ style #ansi-colors] ( ./docs/modules/style.md )
@@ -59,17 +59,65 @@ Or via the `nodejsscript` executable:
5959nodejsscript ./script.mjs
6060```
6161
62- All function (` shelljs ` , ` fetch ` , …) are exported by library, so use:
63- ``` javascript
64- import { … } from " nodejsscript" ;
62+ <details >
63+ <summary >Alternatively when installed locally</summary >
64+
65+ ``` bash
66+ #! /usr/bin/env -S npx nodejsscript
67+ ```
68+ ``` bash
69+ npx nodejsscript ./script.mjs
6570```
66- … * The entry point for documentation of all exported (** Public** ) items is in the* [ ** docs/** ] ( ./docs/README.md ) .
71+
72+ </details >
73+
74+ All function (` shelljs ` , ` fetch ` , …) are registered as global namespaces/functions:
75+ … * The entry point for documentation of all ** Public** items is in the* [ ** docs/** ] ( ./docs/README.md ) .
6776
6877Note that there are also built-in ` 'node:*' ` modules:
6978``` js
7079import { setTimeout } from " node:timers/promises" ;
7180import { join , resolve } from " node:path" ;
81+
82+ // current file url
83+ import .meta.url;
84+ // url to path
85+ import { fileURLToPath } from " node:url" ;
86+ const file_path = fileURLToPath (import .meta.url);
7287` ` `
7388…and more, see [Node.js v17.9.1 Documentation](https://nodejs.org/docs/latest-v17.x/api/documentation.html#stability-overview).
7489
90+ ## Security guidelines
91+ **` run ()` command injection**: this advice applies to ` child_process .exec ()` just as
92+ much as it applies to ` s .run ()` . It is potentially risky to run commands passed
93+ for example by user input:
94+ ` ` ` js
95+ function curlUnsafe (urlToDownload ){ return s .run (' curl ' + urlToDownload); }
96+ curlUnsafe (' https://some/url ; rm -rf $HOME' ); // => curl https://some/url ; rm -rf $HOME
97+ ` ` `
98+ Therefore, ` nodejsscript` s ` s .run ()` provide way to escapes untrusted parameters:
99+ ` ` ` js
100+ function curl (url ){ return s .run (" run ::url::" , { url }); }
101+ curl (' https://some/url ; rm -rf $HOME' ); // => curl 'https://some/url ; rm -rf $HOME'
102+ ` ` `
103+ …*Note: The ['xargs()'](../interfaces/s.XargsFunction.md) by default also escapes piped strings.*
104+
105+ *…Note 2: ` s .run (…cmd, …vars)` is also helpul for escaping parameters passed as variables (e.g. arrays).*
106+
107+ *…Note 3: ShellJS also provides ` s .exec ` , but ` s .run ` should be prefered way to execute commands.*
108+
109+ **Glob injection (all commands)**: Most ShellJS commands support [glob](https://github.com/isaacs/node-glob) expansion,
110+ expanding wildcards such as ` * ` to match files. While this is very powerful,
111+ dependent modules should exercise caution. Unsanitized user input may contain
112+ wildcard characters. Consider for example that the ` * .txt ` is valid file name,
113+ however the ` s .rm (" *.txt" )` by default (using the globbing) delete all ` txt` files.
114+ Keep in mind that you can always turn off this for next command by using:
115+ ` ` ` js
116+ s .$ (" -g" ).rm (" *.txt" );
117+ ` ` `
118+
119+ ## Contribute
120+ - [Contributor Covenant Code of Conduc](./CODE_OF_CONDUCT.md)
121+ - [How to contribute](./CONTRIBUTING.md)
122+
75123[^OR]: Alternatively ` curl - sL install- node .vercel .app / 16.13 .0 | bash`
0 commit comments