Skip to content

Commit 8c1e63c

Browse files
author
github-actions[bot]
committed
🐛 print non-errors, temp scripts names, examples
1 parent 73da15a commit 8c1e63c

File tree

6 files changed

+34
-29
lines changed

6 files changed

+34
-29
lines changed

bin/index.mjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ function printError(e){
9191
console.error(e.message);
9292
return $.exit(1);
9393
}
94-
if(!(e instanceof Error)){
95-
console.error(e);
96-
return $.exit(1);
97-
}
9894
Error.print(e);
9995
$.exit(e.exitCode || 1);
10096
}

bin/runEval.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function runEval(argv, is_print){
2525
input= out_arr.reverse().join(";");
2626
}
2727
input+= ";$.exit(0);";
28-
const filepath= $.xdg.temp`nodejsscript-${randomUUID()}.mjs`;
28+
const filepath= $.xdg.temp`nodejsscript-${is_print ? "print" : "eval"}-${randomUUID()}.mjs`;
2929
s.echo(input).to(filepath);
3030
return filepath;
3131
}

examples/eval_print.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,4 @@ ls -l | nodejsscript -p '$.stdin.lines().slice(1).reduce((acc, l)=> acc+Number(l
6868
history | nodejsscript -p 'const add= name=> acc=> Reflect.set(acc, name, Reflect.has(acc, name) ? acc[name]+1 : 1) && acc; $.stdin.lines().reduce((acc, l)=> add(l.trim().split(/ ?/)[1])(acc), {})'
6969
```
7070
## Aliases
71-
For now, just an idea:
72-
```bash
73-
# instead of
74-
echo '{"a":"A"}' | nodejsscript -p '$.stdin.json().a'
75-
# you can also use
76-
echo '{"a":"A"}' | nodejsscript -p '$.nojq.a'
77-
```
78-
```bash
79-
# instead of
80-
who | nodejsscript -p '$.stdin.text().split(/ +/)[0]'
81-
# you can also use
82-
who | nodejsscript -p '$.nosed.split(/ +/)[0]'
83-
```
84-
```bash
85-
# instead of
86-
apt list --installed | nodejsscript -p '$.stdin.lines().filter(l=> l.indexOf("libreoffice")!==-1).length'
87-
# you can also use
88-
apt list --installed | nodejsscript -p '$.noawk.filter(l=> l.indexOf("libreoffice")!==-1).length'
89-
```
71+
See [nodejsscriptrc.mjs](./nodejsscriptrc.md).

examples/nodejsscriptrc.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ You can define `nodejsscriptrc.mjs` file in your config location.
33
That corresponds to [`$.xdg.config("nodejsscript")`](../docs/modules/xdg_.xdg.md#config) folder.
44
In linux the full location should be `~/.config/nodejsscript/nodejsscriptrc.mjs`.
55

6+
## Ideas
67
This script corresponds to what the `.bashrc` if for bash. It is imported before
78
requested script.
89

9-
## Ideas
10-
1110
### Fallback
1211
In vresion ≥1 you can for example fallback:
1312

@@ -30,5 +29,31 @@ For shorter [`--eval`/`--print`](./eval_print.md#aliases):
3029
Reflect.defineProperty($, "nosed", { get(){ return this.stdin.text(""); }, });
3130
Reflect.defineProperty($, "nojq", { get(){ return this.stdin.json(null); }, });
3231
Reflect.defineProperty($, "noawk", { get(){ return this.stdin.lines([]); }, });
32+
```
33+
34+
So, than you can:
35+
```bash
36+
# instead of
37+
echo '{"a":"A"}' | nodejsscript -p '$.stdin.json().a'
38+
# you can also use
39+
echo '{"a":"A"}' | nodejsscript -p '$.nojq.a'
40+
```
41+
```bash
42+
# instead of
43+
who | nodejsscript -p '$.stdin.text().split(/ +/)[0]'
44+
# you can also use
45+
who | nodejsscript -p '$.nosed.split(/ +/)[0]'
46+
```
47+
```bash
48+
# instead of
49+
apt list --installed | nodejsscript -p '$.stdin.lines().filter(l=> l.indexOf("libreoffice")!==-1).length'
50+
# you can also use
51+
apt list --installed | nodejsscript -p '$.noawk.filter(l=> l.indexOf("libreoffice")!==-1).length'
52+
```
3353

54+
## Processing (modifications)
55+
You can use `norc` in environmental variable `NODEJSSCRIPT`, for example:
56+
```bash
57+
export NODEJSSCRIPT=norc
3458
```
59+
**The `nodejsscriptrc.mjs` is not processed when called locally (`npx nodejsscript`…).**

src/Error.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const ErrorOld= global.Error;
66
/*
77
* Replacig build-in Error, because of libraries we have not under control
88
* */
9-
global.Error= class extends ErrorOld{
9+
globalThis.Error= class extends ErrorOld{
1010
static stackToList(stack){ return stack.split(/^\s*at\s/m); }
1111
static listToStack(list){ return list.map(l=> " at "+l).join(""); }
1212
static exitCodeInfo(exitCode){ return exit_codes[exitCode || -1]; }
@@ -17,6 +17,7 @@ global.Error= class extends ErrorOld{
1717
"h2 { color: magenta; }",
1818
"pre { color: gray; }",
1919
);
20+
if(!e || typeof e.stack ==="undefined" || typeof e.message === "undefined") e= new this(e);
2021
const { stdout, stderr, name, message, stack, exitCode= 1 }= e;
2122
echo.use("-2", `%c${name}: ${message.trim()}`, css.h1);
2223
echo.use("-2", "%ccode: " + exitCode + ( Error.exitCodeInfo(exitCode) ? ` %c(${Error.exitCodeInfo(exitCode)})` : "%c" ), css.h2, "color: blue");
@@ -29,7 +30,8 @@ global.Error= class extends ErrorOld{
2930
super(message, options);
3031
const [ lm, ...l ]= Error.stackToList(this.stack);
3132
const i= l.findIndex(l=> l.indexOf("nodejsscript")===-1 || l.indexOf("nodejsscript/examples")!==-1);
32-
const l_out= l.slice(i).filter(l=> l.indexOf("node:internal")==-1 && l.indexOf("nodejsscript/bin/cli.mjs")===-1 && l.indexOf("async Promise.all (index 0)")===-1);
33+
const l_out= l.slice(i).filter(l=> !l.includes("node:internal") && !l.includes("nodejsscript/bin/") && !l.includes("async Promise.all (index 0)"));
34+
if(!l_out.length) l_out.push(process.argv[1]);
3335
this.stack= lm+Error.listToStack(l_out);
3436
}
3537
};

src/xdg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function globalPackage(pieces, ...vars){
5454
function out(folder, pieces, vars){
5555
if(!pieces) return folder;
5656
if(typeof pieces==="string") return join(folder, pieces);
57-
return join(folder, String.raw(pieces, vars));
57+
return join(folder, String.raw(pieces, ...vars));
5858
}
5959

6060
function fl(){

0 commit comments

Comments
 (0)