Skip to content

Commit 622a1c2

Browse files
committed
💥 💡
1 parent efc97d9 commit 622a1c2

3 files changed

Lines changed: 120 additions & 4 deletions

File tree

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# NodeJSScript – write cross-platform scripts with ease
2+
This package provides very similar functionality such as [google/zx](https://github.com/google/zx#chalk-package).
3+
Tke key difference is to provide unix shell commands in cross-platform commpatible way.
4+
This is done by using [shelljs/shelljs](https://github.com/shelljs/shelljs) library.
5+
6+
You can compare the final script code similar to `zx`:
7+
```javascript
8+
#!/usr/bin/env nodejsscript
9+
import { exec, grep, echo, mkdir, tempdir } from "nodejsscript";
10+
11+
echo(grep("name", "../package.json").trim());
12+
13+
const branch= exec("git branch --show-current").trim();
14+
exec(`dep deploy --branch=${branch}`);
15+
16+
exec("sleep 1; echo 1");
17+
exec("sleep 2; echo 2");
18+
exec("sleep 3; echo 3");
19+
20+
const name= "foo bar";
21+
mkdir(tempdir()+"/"+name);
22+
```
23+
24+
## Instalation
25+
**For now experiment!!!**
26+
27+
1. you need nodejs >=v17.0.1 ⇒ folows [nvm-sh/nvm: Node Version Manager](https://github.com/nvm-sh/nvm)[^node]
28+
1. `npm install https://github.com/jaandrle/nodejsscript --global`
29+
30+
## Goods
31+
[shelljs/shelljs: ls, find, dirs, cd, pwd, pushd, popd, cp, rm, mv, mkdir, ln, touch, tempdir, chmod, test, error, cat, sed, grep, sort, head, tail, uniq, which, exit, env, set, exec, config](https://github.com/shelljs/shelljs#tempdir)
32+
· [fetch()](#fetch)
33+
· [question()](#question)
34+
· [echo()](#echo)
35+
· [stdin()](#stdin)
36+
· [chalk](#chalk-package)
37+
38+
39+
## Documentation
40+
41+
Write your scripts in a file with an `.mjs` extension in order to
42+
use `await` at the top level. If you prefer the `.js` extension,
43+
wrap your scripts in something like `(async function () {...})()`.
44+
45+
Add the following shebang to the beginning of your `zx` scripts:
46+
```bash
47+
#!/usr/bin/env nodejsscript
48+
```
49+
50+
Now you will be able to run your script like so:
51+
```bash
52+
chmod +x ./script.mjs
53+
./script.mjs
54+
```
55+
56+
Or via the `nodejsscript` executable:
57+
58+
```bash
59+
nodejsscript ./script.mjs
60+
```
61+
62+
All function (`shelljs`, `fetch`, …) are exported by library, so use:
63+
```javascript
64+
import { … } from "nodejsscript";
65+
```
66+
67+
### `fetch()`
68+
A wrapper around the [node-fetch](https://www.npmjs.com/package/node-fetch) package.
69+
70+
```js
71+
const resp= await fetch('https://medv.io')
72+
```
73+
74+
### `question()`
75+
A wrapper around the [readline](https://nodejs.org/api/readline.html) package.
76+
77+
```js
78+
const bear= await question('What kind of bear is best? ')
79+
```
80+
### `echo()`
81+
A `console.log()` alternative optimalozed for scripting.
82+
83+
```js
84+
const branch= exec("git branch --show-current");
85+
echo('Current branch is', branch);
86+
```
87+
88+
### `stdin()`
89+
Returns the stdin as a string.
90+
91+
```js
92+
const content= JSON.parse(await stdin())
93+
```
94+
95+
### `chalk`
96+
The [chalk](https://www.npmjs.com/package/chalk) package. Also as shorthand **s**.
97+
98+
```js
99+
echo(chalk.blue('Hello world!'));
100+
echo(s.blue('Hello world!'));
101+
```

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020
"exports": {
2121
".": "./index.js"
2222
},
23+
"files": [
24+
"bin/cli.mjs",
25+
"index.js"
26+
],
2327
"scripts": {
2428
"test": "echo \"Error: no test specified\" && exit 1"
2529
},
30+
"engines": {
31+
"node": ">= 16.0.0"
32+
},
2633
"keywords": [
2734
"cli",
2835
"nodejs"

test/run.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#!/usr/bin/env nodejsscript
22
/* jshint esversion: 8,-W097, -W040, node: true, expr: true, undef: true */
3-
import { cd, exec, echo, s } from "nodejsscript";
3+
import { exec, grep, echo, mkdir, tempdir } from "nodejsscript";
44

55
(async function main(){
6-
cd("../../jaaJSU");
7-
const git= exec("git status", { silent: true });
8-
echo(s.hex("#fc0")(git.trim()));
6+
echo(grep("name", "../package.json").trim());
7+
8+
const branch= exec("git branch --show-current").trim();
9+
exec(`echo deploy --branch=${branch}`);
10+
11+
exec("sleep 1; echo 1");
12+
exec("sleep 2; echo 2");
13+
exec("sleep 3; echo 3");
14+
15+
const name= "foo bar";
16+
echo(mkdir(tempdir()+"/"+name));
917
})();

0 commit comments

Comments
 (0)