Library of Node.js abstractions for side effects at the edges of software.
Reduce accidental complexity and focus on application business logic.
npm install @paulshryock/abstractions
Reads and writes messages to and from the command line.
-
options
:Record<string, boolean | number | string>
All short and long options from the current process. Boolean strings (
'true'
|'false'
) are converted to boolean. -
positionalArguments
:string[]
Positional arguments from the current process.
question
:<string>
Question to print to output stream.- Returns:
<Promise<string>>
Answer read from input stream.
Prints question to output stream and reads answer from input stream.
message
:<string>
Message for output stream.options
:<Object>
trace
:<boolean>
Whether or not to include a stack trace.
- Returns:
<void>
Writes message to output stream. Optionally includes a stack trace.
error
:string
Error message for error stream.options
:<Object>
trace
:<boolean>
Whether or not to include a stack trace.
- Returns:
<void>
Writes error message to error stream. Optionally includes a stack trace.
import { CommandLine } from '@paulshryock/abstractions'
class MyClass {
public construct(private commandLine: CommandLine) {}
/** Gets all short and long options from the current process. */
public getOptions(): Record<string, boolean | number | string> {
return this.commandLine.options
}
/** Gets positional arguments from the current process. */
public getPositionalArguments(): string[] {
return this.commandLine.positionalArguments
}
/** Prints 'Hello, world!' to stdout. */
public printHelloWorldToStdout(): void {
this.commandLine.out('Hello, world!')
}
/**
* Prints 'What is your name?' to stdout and waits for an answer. Then
* prints 'Hello, Paul!' to stdout (if the name given is 'Paul').
*/
public async printHelloNameToStdout(): Promise<void> {
const name = await this.commandLine.ask('What is your name?')
this.commandLine.out(`Hello, ${name}!`)
}
/** Prints 'Hello, error!' with a stack trace to stderr. */
public printHelloErrorToStderr(): void {
this.commandLine.error('Hello, error!', { trace: true })
}
}
const myClass = new MyClass(new CommandLine())
const options = myClass.getOptions()
const positionalArguments = myClass.getPositionalArguments()
myClass.printHelloWorldToStdout()
myClass.printHelloNameToStdout()
myClass.printHelloErrorToStderr()
- Returns:
Promise<boolean>
Possibility to read from the file system.
Checks if it is possible to read from the file system.
path
:<string>
Path to file.- Returns:
<Promise<string>>
File contents.
Reads a file.
path
:<string>
Path to file.content
:<string>
Content to write to file.- Returns:
<Promise<void>>
Writes to a file.
path
:<string>
Path to file.content
:<string>
Content to write to file.- Returns:
<Promise<void>>
Appends to a file.
path
:<string>
Path to file.- Returns:
<Promise<void>>
Deletes a file.
path
:<string>
Path to file.- Returns:
<Promise<boolean>>
Whether or not the path is a file.
Checks if a path is a file.
path
:<string>
Path to directory.- Returns:
<Promise<void>>
Creates a directory.
path
:<string>
Path to directory.- Returns:
<Promise<string[]>>
Contents of the directory.
Lists directory contents.
path
:<string>
Path to directory.- Returns:
<Promise<string[]>>
Contents of the directory.
Lists directory contents. Alias of list().
path
:<string>
Path to directory.- Returns:
<Promise<string[]>>
Recursive contents of the directory.
Lists directory contents recursively.
path
:<string>
Path to directory.- Returns:
<Promise<string[]>>
Recursive contents of the directory.
Lists directory contents recursively. Alias of listRecursive().
path
:<string>
Path to directory.- Returns:
<Promise<void>>
Deletes a directory.
path
:<string>
Path to directory.- Returns:
<Promise<boolean>>
Whether or not the path is a directory.
Checks if a path is a directory.
src
:<string>
Source path.dest
:<string>
Destination path.- Returns:
<Promise<void>>
Copies a file or directory to another location.
When src
and dest
are path names to files, the program copies the contents of the first file to the second file, creating the second file if necessary.
When src
is a path name of a file and dest
is a path to a directory, then the program copies the source file into the destination directory, creating the file if necessary.
When src
and dest
are both the path names to two directories, the program copies the source directory into the destination directory, creating any files or directories needed. If the destination directory already exists, the source is copied into the destination, while a new directory is created if the destination does not exist.
src
:<string>
Source path.dest
:<string>
Destination path.- Returns:
<Promise<void>>
Moves a file or directory to another location.
path
:<string>
Path to directory.- Returns:
<Promise<boolean>>
Whether or not the path is an existing file or directory.
Checks if a file or directory exists.
import { LocalFileSystem, VirtualFileSystem } from '@paulshryock/abstractions'
class MyClass {
public constructor(private fileSystem = VirtualFileSystem) {}
}
const myClass = new MyClass(new LocalFileSystem())
Sends requests and returns responses.
request
:<Request>
HTTP request to send.- Returns:
<Promise<Response>>
HTTP response.
Sends a request and returns a response.
Contributions are welcome! Read the contribution guidelines, and then submit a pull request.