wswD is a tool to write scripts with Deno. (Inspired from google/zx).
Scripting with Deno gives the advantages of TypeScript and of easy imports.
This module was made in the goal of learning and is not production-ready (missing tests for example)! You can use dx or deno-script.
Cache the module locally: deno cache https://denopkg.com/n1c00o/wswD/mod.ts
Create a .ts
file in which you will write your script. At the beginning of
this file, add:
#!/usr/bin/env -S deno run --allow-all --unstable
import {} from "https://denopkg.com/n1c00o/wswD/mod.ts";
(See docs/example.ts)
chmod u+x file.ts;
./file.ts
Online documentation on doc.deno.land
async function $(
cmd: string,
env?: Record<string, string>,
prefix = DEFAULT_PREFIX,
shell = DEFAULT_SHELL,
): Promise<string>;
Execute a command and return stdout.
cmd
- Command to execute.env
- Env to set before executingcmd
.prefix
- Command to execute before executingcmd
. Defaults toDEFAULT_PREFIX
.shell
- Shell to executecmd
on. Defaults toDEFAULT_SHELL
.
Throws ExecutionErrorReturn if the exit code is not 0.
Return: stdout.
Example:
import { $, echo } from "https://denopkg.com/n1c00o/wswD/mod.ts";
const run: string = await $("ls /");
echo(run);
async function* $a(
cmd: string,
env?: Record<string, string>,
prefix = DEFAULT_PREFIX,
shell = DEFAULT_SHELL,
): AsyncGenerator<string>;
Execute a command and convert output into async iterable lines.
cmd
- Command to execute.env
- Env to set before executingcmd
.prefix
- Command to execute before executingcmd
. Defaults toDEFAULT_PREFIX
.shell
- Shell to executecmd
on. Defaults toDEFAULT_SHELL
.
Throws ExecutionErrorReturn if the exit code is not 0.
Return: stdout as async iterable lines.
Example:
import { $a, echo } from "https://denopkg.com/n1c00o/wswD/mod.ts";
for await (const line of $a("ls /")) {
echo(line);
}
async function $no(
cmd: string,
env?: Record<string, string>,
prefix,
shell,
): Promise<void>;
Execute a command but does not return anything.
-
cmd
- Command to execute. -
env
- Env to set before executingcmd
. -
prefix
- Command to execute before executingcmd
. Defaults toDEFAULT_PREFIX
. -
shell
- Shell to executecmd
on. Defaults toDEFAULT_SHELL
.Throws ExecutionErrorReturn if the exit code is not 0.
Example:
import { $no } from "https://denopkg.com/n1c00o/wswD/mod.ts";
await $no("ls /");
function cat(...paths: string[]): string[];
Returns the content of given files.
paths
- Paths of the file to cat.
Return: content of the files.
Example:
import { cat, echo, resolvePath } from "https://denopkg.com/n1c00o/wswD/mod.ts";
const content: string = cat(
resolvePath(import.meta.url, "./file.txt"),
);
echo(content);
function cd(path): string | void;
Change the current working directory to the one gived.
Support for ~ (home directory).
If the given path is a "-" the command changes to the previous working directory and return its name.
When no path is given, the command changes to the home directory.
path
- Location to move to.
Return: New working directory when using cd("-")
Example:
import { cd, echo, pwd } from "https://denopkg.com/n1c00o/wswD/mod.ts";
cd("~");
echo(pwd());
echo(cd("-"));
function cp(sources: CopyOptions[], target: string): void;
Copy files and/or directories.
sources
- Files or directories to copy with specific optionstarget
- Destination
Example :
import { cp, resolvePath } from "https://denopkg.com/n1c00o/wswD/mod.ts";
cp([{
path: resolvePath(import.meta.url, "./file.txt"),
overwrite: true,
}], resolvePath(import.meta.url, "./folder/new_file.txt"));
function exports(vars: ExportOptions[]): ExportOptions[];
Set an environment variable. Function is named exports instead of export since export is a Javascript-reserved keyword.
vars
- Variables to set. SeeExportOptions
.
Return: Exported variables.
Example:
import { echo, exports } from "https://denopkg.com/n1c00o/wswD/mod.ts";
exports([{
name: "PASSWORD",
value: "P@sSW0rd",
}]);
echo(Deno.env.get("PASSWORD"));
function mkdir(dirs: DirectoryCreator[]): string[];
Make directories and creates needed directories (if specified)
dirs
- paths of directories to create. SeeDirectoryCreator
.
Return: paths of created directories.
Example:
import { mkdir, resolvePath } from "https://denopkg.com/n1c00o/wswD/mod.ts";
mkdir([
{
path: resolvePath(
import.meta.url,
"./new_folder/new_child_folder",
),
recursive: true, // like mkdir -p
},
]);
function mv(sources: MoveOptions[], target: string): void;
Move files and/or directories.
sources
- Files and/or directories to move. SeeMoveOptions
.target
- Destination
Example:
import { mv, resolvePath } from "https://denopkg.com/n1c00o/wswD/mod.ts";
mv(
[
{
path: resolvePath(
import.meta.url,
"./files/",
),
overwrite: true,
},
],
resolvePath(
import.meta.url,
"./new_folder/",
),
);
function pwd(): string;
Gets the current working directory path and returns it
Return: Current working directory
Example:
import { echo, pwd } from "https://denopkg.com/n1c00o/wswD/mod.ts";
echo(pwd());
function read(msg, defaultValue?: string): string | null;
Reads from stdin and return input.
msg
- String to show before waiting for the user input.defaultValue
- Value retrned if the user does not enter any input.
Return: User input, or defaultValue (if one), or null.
Example:
import { echo, read } from "https://denopkg.com/n1c00o/wswD/mod.ts";
const name: string = read("Enter your name -> ", "Deno");
echo(`Hello ${name}!`);
function resolvePath(dir: string, filename: string): string;
Return the resolved path of a file with it parent folder when not giving an absolute path.
dir
- Should beimport.meta.url
.filename
- Path of the file, relative to the script file.
Return: resolved path.
Example:
import { echo, resolvePath } from "https://denopkg.com/n1c00o/wswD/mod.ts";
echo(resolvePath(import.meta.url, "../../file.txt"));
function rm(sources: RemoveOptions[]): void;
Removes files and/or directories
sources
- Files and/or directories to delete. SeeRemoveOptions
.
Example:
import { resolvePath, rm } from "https://denopkg.com/n1c00o/wswD/mod.ts";
rm([
{
path: resolvePath(import.meta.url, "./non_enpty_folder/"),
recursive: true, // remove child items
},
{
path: resolvePath(import.meta.url, "./work/"),
},
]);
async function sleep(time: number): Promise<void>;
Suspends execution for a certain amount of time (in seconds).
time
- A non-negative decimal integer specifying the number of seconds for which to suspend execution.
Example:
import { echo, sleep } from "https://denopkg.com/n1c00o/wswD/mod.ts";
echo(new Date().getSeconds());
await sleep(10);
echo(new Date().getSeconds());
const DEFAULT_PREFIX: "set -euo pipefail;";
Default command which will be prefixed to all commands run. (strict mode).
const DEFAULT_SHELL: "bash";
Default shell used to execute commands.
const __arg0: string;
Equivalent as $0
const __arg1: string;
Equivalent of $1 in bash
const __arg2: string | undefined;
Equivalent of $2 in bash
const __arg3: string | undefined;
Equivalent of $3 in bash
const __arg4: string | undefined;
Equivalent of $4 in bash
const __arg5: string | undefined;
Equivalent of $5 in bash
const __arg6: string | undefined;
Equivalent of $6 in bash
const __arg7: string | undefined;
Equivalent of $7 in bash
const __arg8: string | undefined;
Equivalent of $8 in bash
const __arg9: string | undefined;
Equivalent of $9 in bash
const __args: string[];
Equivalent of $@ in bash
const __argsAsString: string;
Equivalent of "$*" in bash
const __argsLength: number;
Equivalent of $# in bash
const echo;
Prints to stdout. If no string is given, prints a <newline>
interface CopyOptions
Options to copy a file or a directory
Properties:
path:
string;
src the file/directory path. Note that if src is a directory it will copy everything inside of this directory, not the entire directory itself
overwrite?: boolean
overwrite existing file or directory. Default is false
preserveTimestamps?: boolean
When true, will set last modification and access times to the ones of the
original source files. When false
, timestamp behavior is OS-dependent. Default
is false
.
interface DirectoryCreator
Represents a directory to create.
Properties:
path:
string;
Path of the directory
recursive?: boolean
Defaults to false
.
If set to true
, means that any intermediate directories will also be created
(as with the shell command mkdir -p). Intermediate directories are created
with the same permissions.
When recursive is set to true
, succeeds silently (without changing any
permissions) if a directory already exists at the path, or if the path is a
symlink to an existing directory.
mode?: number
Permissions to use when creating the directory (defaults to 0o777
, before the
process's umask). Ignored on Windows.
interface ExecutionErrorReturn
Return type of an execution function when an error happened (exitCode is not 0)
Properties:
code:
number;
stdout?: string
stderr?: string
interface ExportOptions
Options to export a variable
Properties:
name:
string;
Name of the variable
value:
string;
Value of the variable
interface MoveOptions
Options to move a file or a directory
Properties:
path:
string;
Path of the file or directory to move
overwrite?: boolean
Overwrite existing file or directory. Defaults to false
interface RemoveOptions
Options to remove a file and/or a directory
Properties:
path:
string;
Path of the file or directory
recursive?: boolean
Defaults to false. If set to true, path will be removed even if it's a non-empty directory.
type OutputMode: "piped" | "inherit" | "null"
Mode for stderr and stdout.
piped
- A new pipe should be arranged to connect the parent and child sub-processes.inherit
- The default if unspecified. The child inherits from the corresponding parent descriptor.null
- This stream will be ignored. This is the equivalent of attaching the stream to/dev/null
.
See CONTRIBUTING.md
This project is under the Apache-2.0 license. See LICENSE.