|
| 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 | +``` |
0 commit comments