Convenient wrapper for launching CLI applications for Deno.

Overview

deno_cliwrap

Convenient wrapper for launching CLI applications in Deno.

Module link: https://deno.land/x/cliwrap

Usage

In the following examples, we'll use deno executable as it's safe to assume you're familiar with it and you have it already installed.

Detect deno executable version

We'll call deno -V and capture standard output.

import {
  Cli,
  createPipeTargetToDelegate,
} from "https://deno.land/x/cliwrap/mod.ts";

let stdout = "";
const cmd = Cli.wrap("deno")
  .withArguments(["-V"])
  .withStandardOutputPipe(
    createPipeTargetToDelegate((data) => {
      stdout += data.text;
      if (data.eol) {
        stdout += "\r\n";
      }
    })
  )
  .execute();

await cmd.waitForExit();
console.log(stdout); // deno 1.23.2

Evaluate any code in Deno runtime

We'll call deno run -, where '-' means that script will be read from stdin.

import {
  Cli,
  createPipeSourceFromString,
  createPipeTargetToDelegate,
} from "https://deno.land/x/cliwrap/mod.ts";

const fnText = `console.log('Hello World'.replace('o', ''));`;

let stdout = "";
const cmd = Cli.wrap("deno")
  .withArguments(["run", "-"])
  .withStandardInputPipe(createPipeSourceFromString(fnText))
  .withStandardOutputPipe(
    createPipeTargetToDelegate((data) => {
      stdout += data.text;
      if (data.eol) {
        stdout += "\r\n";
      }
    })
  )
  .execute();

const cmdResult = await cmd.waitForExit();

console.log(cmdResult.code); // 0 - success
console.log(stdout); // "Hell World"

Capture stderr

We'll call deno run but with a script that contains syntax error. Our goal is to capture error message.

import {
  Cli,
  CommandResultValidation,
  createPipeSourceFromString,
  createPipeTargetToDelegate,
} from "https://deno.land/x/cliwrap/mod.ts";

const fnText = `console.log('Hello World'.eplace('o', ''));`;

let stderr = "";
const cmd = Cli.wrap("deno")
  .withArguments(["run", "-"])
  .withStandardInputPipe(createPipeSourceFromString(fnText))
  .withStandardErrorPipe(
    createPipeTargetToDelegate((data) => {
      stderr += data.text;
      if (data.eol) {
        stderr += "\r\n";
      }
    })
  )
  .withValidation(CommandResultValidation.None) // Required to prevent throwing Error
  .execute();

const cmdResult = await cmd.waitForExit();

console.log(cmdResult.code); // 1 - error
console.log(stderr); // error: Uncaught TypeError: "Hello World".eplace is not a function

Advanced: talk to SSH server

In this example, we'll talk to SSH server via plink.exe.

import {
  Cli,
  CommandResultValidation,
  createLivePipeSource,
  createRedirectedPipes,
} from "https://deno.land/x/cliwrap/mod.ts";

const target = "[email protected]";
const password = "secretpassword";

const stdin = createLivePipeSource();
let out = "";
let loggedIn = false;
const outPipes = createRedirectedPipes("server", ({ text, eol }) => {
  out += text;
  if (eol) {
    out += "\r\n";
  }

  if (
    !loggedIn &&
    out.endsWith(
      "Store key in cache? (y/n, Return cancels connection, i for more info) "
    )
  ) {
    stdin.write("n\n");
  }

  if (!loggedIn && out.endsWith(target + "'s password: ")) {
    loggedIn = true;
    stdin.write(password + "\r\n");
  }

  if (loggedIn && out.endsWith("~]$ ")) {
    stdin.write("exit\n");
  }
});

const cmd = Cli.wrap("plink")
  .withArguments([target])
  .withStandardInputPipe(stdin)
  .withStandardOutputPipe(outPipes.stdout)
  .withStandardErrorPipe(outPipes.stderr)
  .execute();

await cmd.waitForExit();

// Example output:
// ----
// server# The host key is not cached for this server:
// server#   192.168.1.110 (port 22)
// server# You have no guarantee that the server is the computer
// server# you think it is.
// server# The server's ssh-ed25519 key fingerprint is:
// server#   ssh-ed25519 255 SHA256:XXXXXXXXXXXXXXXXXXXXXXX
// server# If you trust this host, enter "y" to add the key to
// server# PuTTY's cache and carry on connecting.
// server# If you want to carry on connecting just once, without
// server# adding the key to the cache, enter "n".
// server# If you do not trust this host, press Return to abandon the
// server# connection.
// server# Store key in cache? (y/n, Return cancels connection, i for more info) Using username "comet".
// server> [email protected]'s password:
// server> Web console: https://comet:9090/
// server>
// server> Last login: Sun Jul  3 00:00:00 2022 from 192.168.1.100
// server> [comet@comet ~]$ exit
// logout>
You might also like...

A Roblox OpenCloud API wrapper for Deno (and NodeJS) written in TypeScript.

Active Development This module is currently in active development. Nothing is going to stay consistent as it reaches a stable release. Dynablox Docs |

Oct 28, 2022

TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy

Atlas SDK atlas_sdk is a TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy Links Docs Import Replace LATEST_VERSION with current latest versi

Dec 26, 2022

Deno bindings for yoga, using Deno FFI.

deno_yoga Deno bindings for yoga, using Deno FFI. Usage flags: --allow-ffi: Requires ffi access to "yogacore.dll", "libyogacore.so", "libyogacore.dyli

Feb 11, 2022

🛣️ A tiny and fast http request router designed for use with deno and deno deploy

Rutt Rutt is a tiny http router designed for use with deno and deno deploy. It is written in about 200 lines of code and is pretty fast, using an exte

Dec 10, 2022

A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

Wren Wren is a small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity. convenient aliases for HTTP responses au

Dec 12, 2022

deno-ja (Deno Japanese community) showcase

Showcase Deno本家よりも気軽に作ったものを公開できるようなShowcaseです。 スクリーンショットの撮影方法 短めのidを決めていただいて、下記のようにスクリプトを実行してください。 deno task screenshot [url] [id] ※エラーが出る場合は、下記を実行してみ

Oct 28, 2022

A command-line tool to manage Deno scripts installed via deno install

🏞️ nublar nublar is a command-line tool to manage your scripts installed via deno install. 🛳️ Installation deno install --allow-read --allow-write -

Dec 26, 2022

Deno client library for Bitwarden CLI's local REST API.

Bweno Think outside the bun 🌮 Bweno is a client library for Bitwarden CLI's local REST API. Pronounced as Spanish 'bueno', meaning 'good'. Requiremen

May 26, 2022

A server side rendering framework for Deno CLI and Deploy. 🦟 🦕

nat A server side rendering framework for Deno CLI and Deploy. Incorporating acorn, nano-jsx, and twind, it provides the tooling to provide a server c

Nov 17, 2022
Owner
null
A convenient tool to view the youtube superchat messages in the streaming.

Youtube 超級留言訊息實況小工具 一個可以用於實況中顯示Youtube 超級留言訊息的小工具。 下載頁面 可至Release頁面根據自身的作業系統下載最新版本。 檔名 作業系統 youtube-superchat-message-tool-win32-x64-{version}.zip Win

悠太翼 2 Apr 14, 2022
Vite plugin for more convenient Relay experience.

vite-plugin-relay Vite plugin for more convenient Relay experience. What this plugin does for you: Generates artifacts on code changes Transform codes

Hyeseong Kim 13 Nov 7, 2022
Webpack dev tools to make performance analysis, error investigation and loader development more convenient

build-tool-inspector Introduction Webpack dev tools to make performance analysis, error investigation and loader development more convenient. Provide

Modern JS 25 Nov 17, 2022
✏️ A new tab extension for convenient note-taking

MDTab Write quick notes in Markdown on any new tabs! Installation Right now MDTab has only been tested on Chrome (brave browser). You can install it f

Ian Huang (Shaoru) 8 Nov 11, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Node-cli-starter - Basic starter kit for building Node CLI applications with TypeScript.

node-cli-starter Minimal starter kit for building Node CLI applications with TypeScript. Getting Started To get started clone repo locally and run npm

Cory Rylan 7 May 17, 2022
Tiny Telegra.ph API wrapper for Deno

Telegra.ph API wrapper for Deno ?? This is a tiny Telegra.ph API wrapper for Deno written in TypeScript. All methods as listed here are available. See

Dunkan 9 Jul 28, 2022
This is a simple boilerplate for a Deno website, deployed with Deno Deploy.

Simple Deno Website Boilerplate This is a simple website boilerplate built using Deno and deployed using Deno Deploy. Demo at simple-deno-website-boil

Bruno Bernardino 15 Dec 3, 2022