▸ pipe(...funs
): (input
: any
) => any
Function similar to Ramda R.pipe
. Provides functional way to combine commands/functions.
pipe(
Number,
v=> `Result is: ${v}`,
echo
)(await question("Choose number:"));
Name | Type |
---|---|
...funs |
Function [] |
fn
▸ (input
): any
Name | Type |
---|---|
input |
any |
any
▸ fetch(url
, init?
): Response
The node-fetch
package can be used in cases when fetch
is not available natively.
// BASIC
const response1= await fetch('https://medv.io');
// PROMISE CHAINING
const text1= await fetch('https://github.com/').then(r=> r.text());
// AWAITING
const response2= await fetch('https://api.github.com/users/github');
const json1= await response.json();
// POST METHOD
const json2= await fetch('https://httpbin.org/post', { method: 'POST', body: 'a=1' }).then(r=> r.json());
// POST METHOD WITH JSON
const response3= await fetch('https://httpbin.org/post', {
method: 'post',
body: JSON.stringify({ a: 1 }),
headers: {'Content-Type': 'application/json'}
});
// ERRORS
fetch('https://domain.invalid/').catch(echo);
try{
await fetch('https://domain.invalid');
} catch(error){
echo(error);
}
Name | Type | Description |
---|---|---|
url |
string | Request |
The URL to fetch. |
init? |
RequestInit |
Request parameters. |
▸ echo(message?
, ...optionalParams
): ShellString
This is mixed function between bash’s echo
and console.log
.
By default, works more like console.log
with partial supports
for styling mimic CSS and console.log
in the web browser. See echo.css
(internally uses css-in-console - npm).
The 'echo.use' provides more echo
way,
the first argument accepts options string starting with -
:
-n
: Don’t append new line-1
/-2
: Outputs tostdout
/stderr
-c
: Don’t colorize output (e.g. objects)-P
: Outputs objects in prettier format-R
/-r
: Starts/Ends rewritable mode (for spinners, progress bars, etc.). Mode can be ended with any otherecho
without-R
.
There is also
// as console.log
const count = 5;
echo('count: %d', count);
// Prints: count: 5, to stdout
echo('count:', count);
// Prints: count: 5, to stdout
echo({ count });
// Prints: { count: 5 }, to stdout
echo(new Error("Test"));
// Prints: 'Error: Test', when `config.verbose= false`
echo("%cRed", "color: red");
// Prints 'Red' in red
echo.use("-R", "0%");
// …
echo.use("-r", "100%");
// combination
echo.use("-2cP", { a: "A" });
echo("Hi").to("./test.txt");
// Prints: 'Hi' & save to file 'test.txt'
Name | Type | Description |
---|---|---|
message? |
any |
The text to print. |
...optionalParams |
any [] |
- |
Returns processed string with additional utility methods like .to().
▸ __sade(usage
, isSingle?
): Sade
Name | Type |
---|---|
usage |
string |
isSingle? |
boolean |
▸ _exit(code?
): never
The process.exit()
method instructs Node.js to terminate the process
synchronously with an exit status of code
. If code
is omitted, exit uses
either the 'success' code 0
or the value of process.exitCode
if it has been
set. Node.js will not terminate until all the 'exit'
event listeners are
called.
To exit with a 'failure' code:
import { exit } from 'node:process';
exit(1);
The shell that executed Node.js should see the exit code as 1
.
Calling process.exit()
will force the process to exit as quickly as possible
even if there are still asynchronous operations pending that have not yet
completed fully, including I/O operations to process.stdout
andprocess.stderr
.
In most situations, it is not actually necessary to call process.exit()
explicitly. The Node.js process will exit on its own if there is no additional
work pending in the event loop. The process.exitCode
property can be set to
tell the process which exit code to use when the process exits gracefully.
For instance, the following example illustrates a misuse of theprocess.exit()
method that could lead to data printed to stdout being
truncated and lost:
import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}
The reason this is problematic is because writes to process.stdout
in Node.js
are sometimes asynchronous and may occur over multiple ticks of the Node.js
event loop. Calling process.exit()
, however, forces the process to exit before those additional writes to stdout
can be performed.
Rather than calling process.exit()
directly, the code should set theprocess.exitCode
and allow the process to exit naturally by avoiding
scheduling any additional work for the event loop:
import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}
If it is necessary to terminate the Node.js process due to an error condition,
throwing an uncaught error and allowing the process to terminate accordingly
is safer than calling process.exit()
.
In Worker
threads, this function stops the current thread rather
than the current process.
Since
v0.1.13
Name | Type | Description |
---|---|---|
code? |
number |
The exit code. For string type, only integer strings (e.g.,'1') are allowed. |
never
• _env: ProcessEnv
The process.env
property returns an object containing the user environment.
See environ(7)
.
An example of this object looks like:
{
TERM: 'xterm-256color',
SHELL: '/usr/local/bin/bash',
USER: 'maciej',
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
PWD: '/Users/maciej',
EDITOR: 'vim',
SHLVL: '1',
HOME: '/Users/maciej',
LOGNAME: 'maciej',
_: '/usr/local/bin/node'
}
It is possible to modify this object, but such modifications will not be
reflected outside the Node.js process, or (unless explicitly requested)
to other Worker
threads.
In other words, the following example would not work:
node -e 'process.env.foo = "bar"' && echo $foo
While the following will:
import { env } from 'node:process';
env.foo = 'bar';
console.log(env.foo);
Assigning a property on process.env
will implicitly convert the value
to a string. This behavior is deprecated. Future versions of Node.js may
throw an error when the value is not a string, number, or boolean.
import { env } from 'node:process';
env.test = null;
console.log(env.test);
// => 'null'
env.test = undefined;
console.log(env.test);
// => 'undefined'
Use delete
to delete a property from process.env
.
import { env } from 'node:process';
env.TEST = 1;
delete env.TEST;
console.log(env.TEST);
// => undefined
On Windows operating systems, environment variables are case-insensitive.
import { env } from 'node:process';
env.TEST = 1;
console.log(env.test);
// => 1
Unless explicitly specified when creating a Worker
instance,
each Worker
thread has its own copy of process.env
, based on its
parent thread's process.env
, or whatever was specified as the env
option
to the Worker
constructor. Changes to process.env
will not be visible
across Worker
threads, and only the main thread can make changes that
are visible to the operating system or to native add-ons. On Windows, a copy ofprocess.env
on a Worker
instance operates in a case-sensitive manner
unlike the main thread.
Since
v0.1.27