npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@loopmode/xpty

v0.5.7

Published

Provides xterm.js and node-pty helpers for react electron apps

Downloads

8

Readme

@loopmode/pty

A react component and some helpers for building terminals in electron apps.
Based on xterm and node-pty.

Prerequisites

Make sure to include the xterm stylesheet in your app:

import 'xterm/css/xterm.css';

Also, have a look at the troubleshooting section.

Usage

There are two parts to using this:

  1. A child process that is managed by node-pty and
  2. A view that is managed by xterm

Creating a pty

There are multiple ways to create a pty.

  • There is a lower-level ptyManager class that you can use to connect to the same pty from multiple callsites
  • There is a usePtyCommand hook that uses ptyManager under the hood. The pty is reusable if you provide a custom id option.
  • There is a usePty hook for simpler cases. It creates a pty "inline" and returns it - you can not connect to it from multipe callsites.

Rendering a terminal

There is an XTerminal component that expects an existing pty as a prop. You're free to choose how and where you create that, which depends on your use case.

Example: usePtyCommand

import React from 'react';
import { XTerminal, usePtyCommand } from '@loopmode/xpty';

export const Example = () => {
  const { pty, execute } = usePtyCommand({
    cmd: 'ls -lah'
    cwd: '~'
  });
  return (
    <>
      <XTerminal pty={pty}>
      <button onClick={execute}>Execute</button>
    </>
  )
};

Here's an example with custom id and multiple components using the same pty

export const ExampleCommandScreen = () => {
  const { pty, execute } = usePtyCommand({
    id: 'my-command',
    cmd: 'ls -lah'
  });
  return (
    <>
      <XTerminal pty={pty}>
      <button onClick={execute}>Execute</button>
    </>
  );
};
export const ExampleSidebarItem = () => {
  const { pty, execute } = usePtyCommand({
    id: 'my-command'
  });
  return (
    <>
      <button onClick={execute}>Execute</button>
    </>
  )
}

Example: usePty

This simpler variant allows you to simply execute() some CLI command.

import React from 'react';
import { XTerminal, usePty } from '@loopmode/xpty';

export const Example = () => {
  const { pty, execute } = usePty();
  const run = () => execute('ls -lah')
  return (
    <>
      <XTerminal pty={pty}>
      <button onClick={run}>Execute</button>
    </>
  )
}

TODO

  • docs for existing features and props
  • more features and props
  • tests

Troubleshooting

rebuild native module

After installation, the native node-pty module will be compiled for your local system. However, it needs to be compiled for the node version of used by electron, not necessarily the one on your local system.
You can use the electron-builder package and its install-app-deps command to do this:

npx electron-builder install-app-deps

Better yet: Add electron-builder locally and use a package script.

yarn add electron-builder --dev
{
  "scripts": {
    "rebuild": "electron-builder install-app-deps"
  }
}

webpack

If you use this in an electron app with webpack, make sure to add node-pty to webpack externals:

// webpack.config.js
module.exports = {
  // ...
  externals: ['node-pty']
};