Skip to content

Commit 8c265ae

Browse files
authored
🎉 v0.6.0 (#1)
1 parent 7b3ef96 commit 8c265ae

75 files changed

Lines changed: 11376 additions & 310 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 20 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# NodeJS Script – write cross-platform scripts with ease
2-
This package provides very similar functionality such as [google/zx](https://github.com/google/zx).
3-
The key difference is to provide unix shell commands in cross-platform compatible way and suitable for javascript.
4-
This is done by using [shelljs/shelljs](https://github.com/shelljs/shelljs) library.
1+
**The package is in early stage, some changes (in API) may be made until version 1.0.0.!**
2+
3+
# NodeJS Script – Easy cross-platform scripting
4+
This package serves as an alternative to [google/zx](https://github.com/google/zx) for example.
5+
The key difference is to provide Unix shell commands in a cross-platform compatible way and usable inside JavaScript.
6+
This is primarily achieved by using [shelljs/shelljs](https://github.com/shelljs/shelljs) library.
57

68
You can compare the final script code to `zx` example:
79
```javascript
@@ -21,17 +23,20 @@ s.mkdir(join(s.tempdir(), name));
2123
```
2224

2325
## Installation
24-
**For now experiment!!!**
2526

26-
1. you need nodejs >=v17.0.1 ⇒ follow [nvm-sh/nvm: Node Version Manager](https://github.com/nvm-sh/nvm)[^node]
27+
1. tested/used on *NodeJS*: `node@v16.13.0` and `node@v17.9.1`for installation follow [nvm-sh/nvm: Node Version Manager](https://github.com/nvm-sh/nvm)[^OR]
2728
1. `npm install https://github.com/jaandrle/nodejsscript --global`
2829

2930
## Goods
30-
[s (shelljs)](#shelljs) · [cli()](#cli) · [ansi-colors](#ansi-colors-package) · [fetch()](#fetch) · [pipe()](#pipe) · [question()](#question) · [echo()](#echo) · [stdin()](#stdin) · [config](#config)
31-
31+
[s #shelljs](./docs/modules/s.md)
32+
· [cli](./docs/modules/cli.md) ([cli.api() #sade](./docs/modules/cli.md#api), [cli.read()](./docs/modules/cli.md#read), …)
33+
· [echo()](./docs/README.md#echo)
34+
· [fetch() #node-fetch](./docs/README.md#fetch)
35+
· [style #ansi-colors](./docs/modules/style.md)
36+
· [pipe()](./docs/README.md#pipe)
37+
· [cyclicLoop()](./docs/README.md#cyclicloop)
3238

3339
## Documentation
34-
3540
Write your scripts in a file with an `.mjs` extension in order to
3641
use `await` at the top level. If you prefer the `.js` extension,
3742
wrap your scripts in something like `(async function () {...})()`.
@@ -57,116 +62,13 @@ All function (`shelljs`, `fetch`, …) are exported by library, so use:
5762
```javascript
5863
import { … } from "nodejsscript";
5964
```
65+
*The entry point for documentation of all exported (**Public**) items is in the* [**docs/**](./docs/README.md).
6066

61-
### shelljs
62-
The `shelljs` extension is available as `s`. For docs visits [shelljs/shelljs](https://github.com/shelljs/shelljs).
63-
You can pipe commands when make sense by chaining, see **[Pipes](https://github.com/shelljs/shelljs#pipes)**.
64-
Available commands: [cat](https://github.com/shelljs/shelljs#catoptions-file--file-) · [cd](https://github.com/shelljs/shelljs#cddir) · [chmod](https://github.com/shelljs/shelljs#chmodoptions-octal_mode--octal_string-file) · [cp](https://github.com/shelljs/shelljs#cpoptions-source--source--dest)
65-
· [pushd](https://github.com/shelljs/shelljs#pushdoptions-dir---n--n) · [popd](https://github.com/shelljs/shelljs#popdoptions--n--n) · [dirs](https://github.com/shelljs/shelljs#dirsoptions--n---n) · [exec](https://github.com/shelljs/shelljs#execcommand--options--callback)
66-
· [find](https://github.com/shelljs/shelljs#findpath--path-) · [grep](https://github.com/shelljs/shelljs#grepoptions-regex_filter-file--file-) · [head](https://github.com/shelljs/shelljs#head-n-num-file--file-) · [ln](https://github.com/shelljs/shelljs#lnoptions-source-dest)
67-
· [ls](https://github.com/shelljs/shelljs#lsoptions-path-) · [mkdir](https://github.com/shelljs/shelljs#mkdiroptions-dir--dir-) · [mv](https://github.com/shelljs/shelljs#mvoptions--source--source--dest) · [pwd](https://github.com/shelljs/shelljs#pwd)
68-
· [rm](https://github.com/shelljs/shelljs#rmoptions-file--file-) · [sed](https://github.com/shelljs/shelljs#sedoptions-search_regex-replacement-file--file-) · [sort](https://github.com/shelljs/shelljs#sortoptions-file--file-)
69-
· [tail](https://github.com/shelljs/shelljs#tail-n-num-file--file-) · [tempdir](https://github.com/shelljs/shelljs#tempdir) · [test](https://github.com/shelljs/shelljs#testexpression) · [touch](https://github.com/shelljs/shelljs#touchoptions-file--file-)
70-
· [uniq](https://github.com/shelljs/shelljs#uniqoptions-input-output) · [which](https://github.com/shelljs/shelljs#whichcommand) · [exit](https://github.com/shelljs/shelljs#exitcode) · [error](https://github.com/shelljs/shelljs#error) · [errorCode](https://github.com/shelljs/shelljs#errorcode)
71-
72-
```js
73-
s.cat("./package.json").grep("version");
74-
```
75-
… this library adds two function `xargs` and `$`:
76-
#### `xargs(cmd, ...cmd_args)`
77-
#### `xargs("-I", pattern, cmd, ...cmd_args)`
78-
```js
79-
s.exec("git branch --show-current").xargs(s.exec, "dep deploy --branch={}");
80-
s.exec("git branch --show-current").xargs("-I", "§", s.exec, "dep deploy --branch=§");
81-
```
82-
83-
#### `$()`
84-
#### `$("-svf")`
85-
Modifies config for next command in chain. The `$()` runs next command in silent mode (compare to bash `var=$(echo Hi)`).
86-
87-
```js
88-
const branch= s.$().exec("git branch --show-current");
89-
echo(branch);
90-
91-
s.$("-vf").exec("gyt branch --show-current");
92-
```
93-
94-
### `pipe()`
95-
Function similar to [Ramda `R.pipe`](https://ramdajs.com/docs/#pipe)). Provides functional way to combine commands/functions.
96-
97-
```js
98-
pipe(
99-
Number,
100-
v=> style.greenBright(v+1),
101-
v=> `Result is: ${v}`,
102-
echo
103-
)(await question("Choose number:"));
104-
```
105-
106-
### `cli()`
107-
A wrapper around the [lukeed/sade: Smooth (CLI) Operator 🎶](https://github.com/lukeed/sade) package.
108-
In addition to the origin, `cli()` supports to fill script name from script file name.
109-
This should be good balance between [commander - npm](https://www.npmjs.com/package/commander) and parsing arguments and writing help texts by hand.
110-
For more complex scripts just create full npm package.
111-
112-
```js
113-
cli("", true)
114-
.version("0.1.0")
115-
.describe("NodeJS Script cli test")
116-
.action(echo);
117-
```
118-
119-
### `fetch()`
120-
A wrapper around the [node-fetch](https://www.npmjs.com/package/node-fetch) package.
121-
122-
```js
123-
const resp= await fetch('https://medv.io')
124-
```
125-
126-
### `question()`
127-
A wrapper around the [readline](https://nodejs.org/api/readline.html) package.
128-
129-
```js
130-
const bear= await question('What kind of bear is best? ')
131-
```
132-
133-
### `echo()`
134-
A `console.log()` alternative optimized for scripting.
135-
136-
```js
137-
const branch= exec$("git branch --show-current");
138-
echo('Current branch is', branch);
139-
```
140-
141-
### `stdin()`
142-
Returns the stdin as a string.
143-
144-
```js
145-
const content= JSON.parse(await stdin());
146-
```
147-
148-
### `config`
149-
Read/write global configuration.
150-
151-
- `silent` [`false`]: Suppresses all command output if `true`, except for `echo()` call.
152-
- `verbose` [`false`]: Will print each executed command to the screen.
153-
- `fatal` [`false`]: If `true`, the script will throw a JavaScript error when any `shell.js` command encounters an error. This is analogous to Bash's `set -e`.
154-
- `noglob` [`false`]: Disable filename expansion (globbing)
155-
- `globOptions` [`{}`]: Options for [`glob.sync()`](https://github.com/isaacs/node-glob/tree/af57da21c7722bb6edb687ccd4ad3b99d3e7a333#options).
156-
157-
```js
158-
const { verbose, fatal, noglob }= config;
159-
config.silent= true;
160-
const config.assign({ verbose: true, silent: false });
161-
162-
```
163-
164-
### `ansi-colors` package
165-
The [doowb/ansi-colors](https://github.com/doowb/ansi-colors) package as `style`.
166-
67+
Note that there are also built-in `'node:*'` modules:
16768
```js
168-
style.theme({ info: style.blue });
169-
echo(style.info('Hello world!'));
69+
import { setTimeout } from "node:timers/promises";
70+
import { join, resolve } from "node:path";
17071
```
72+
…and more, see [Node.js v17.9.1 Documentation](https://nodejs.org/docs/latest-v17.x/api/documentation.html#stability-overview).
17173

172-
[^node]: Alternatively `curl -sL install-node.vercel.app/17.0.1 | bash`
74+
[^OR]: Alternatively `curl -sL install-node.vercel.app/16.13.0 | bash`

bin/cli.mjs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,52 @@
44
import { resolve } from "node:path";
55
import { argv } from "node:process";
66
import url from "node:url";
7+
import { echo, exit, style, s, cli } from "../index.js";
78

8-
const candidate= argv.splice(2, 1)[0];
9-
const filepath= candidate.startsWith('/') ? candidate : ( candidate.startsWith('file:///') ? url.fileURLToPath(candidate) : resolve(candidate) );
10-
argv[1]= filepath;
11-
await import(url.pathToFileURL(filepath).toString());
9+
(async function main(){
10+
const candidate= argv.splice(2, 1)[0];
11+
if(candidate[0]==="-") handleMyArgvs(candidate);
12+
13+
const filepath= candidate.startsWith('/') ? candidate : ( candidate.startsWith('file:///') ? url.fileURLToPath(candidate) : resolve(candidate) );
14+
argv[1]= filepath;
15+
try{
16+
if(!s.test("-f", filepath)) cli.error(`File '${candidate}' not found.`);
17+
await import(url.pathToFileURL(filepath).toString());
18+
} catch(e){
19+
const error= e instanceof cli.Error ? e.message : e;
20+
console.error(error);
21+
exit(1);
22+
}
23+
})();
24+
25+
function handleMyArgvs(candidate){
26+
if(['--version', '-v', '-V'].includes(candidate)){
27+
echo(info("version")[0]);
28+
return exit(0);
29+
}
30+
if(['--help', '-h'].includes(candidate))
31+
return printUsage();
32+
}
33+
function printUsage(){
34+
style.theme({ n: style.blueBright, v: style.greenBright, info: style.yellow, code: style.italic });
35+
const [ n, v, d ]= info("name", "version", "description");
36+
echo(`
37+
${style.n(n)}@${style.v(v)}
38+
${d}
39+
${style.info("Usage")}:
40+
${n} [options] <script>
41+
${style.info('Options')}:
42+
--version, -v print current zx version
43+
--help, -h print help
44+
${style.info('Examples')}:
45+
${n} script.js
46+
${n} --help
47+
${style.info('Usage in scripts')}:
48+
Just start the file with: ${style.code('#!/usr/bin/env nodejsscript')}
49+
`);
50+
exit(0);
51+
}
52+
function info(...keys){
53+
const info= s.cat("package.json").xargs(JSON.parse);
54+
return keys.map(key=> Reflect.get(info, key));
55+
}

docs/.nojekyll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.

0 commit comments

Comments
 (0)