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

my-termux-api

v0.0.4

Published

A wrapper module for Termux-API

Downloads

5

Readme

Termux-API Wrapper for Node.JS

This project aims to create a simple interface to invoke Termux-API commands directly from Node.JS applications. To use it you need to have Termux and Termux-API installed on your Android, and Node.JS installed on the Termux environment.

What is Termux?

From Termux Website: "Termux is a terminal emulator and Linux environment bringing powerful terminal access to Android."

With Termux you can run a small linux environment on your Android device, that means that you can run a small command-line utilities or a full-featured webserver directly from your Android phone.

Install Termux from Google Play

Termux-API

Termux-API is a companion app to Termux, it creates an interface from Termux to your device features, with Termux-API you can take photos from your phone camera, get location from GPS, read or send SMS, and a lot of other features. Everything directly from the emulated Linux terminal.

Install Termux-API from Google Play

Node.JS on Termux

Termux brings the possibily of running Node.JS on your Android device by simply installing it with APT (without rooting):

apt update
apt install nodejs

Usage

Install the package on yout NPM project by:

npm install --save termux-api

Import the default object on JavaScript:

var api = require('termux-api').default;

Or with TypeScript:

import default as api from 'termux-api';

Now you have a instance of the class TermuxApi as the api variable. To invoke API commands you must build it using the method createCommand().

The method createCommand() returns an instance of the ApiCommandFactory where you find one method for each API command you could invoke on Termux-API app. For example, to show a small popup, or toast for those familiarized with Android UI you can use:

api.createCommand()
    .toast()
    .setText('Can you see me?')
    .shortDuration()
    .build()
    .run();
  • line 1: api.createCommand() returns an instance of ApiCommandFactory class.
  • line 2: ApiCommandFactory.toast() returns an instance of ToastBuilder class.
  • line 3: ToastBuilder.setText() sets the text to be displayed and returns the ToastBuilder himself.
  • line 4: ToastBuilder.shortDuration() configures the toast to be displayed for a short while and returns himself (ToastBuilder).
  • line 5: ToastBuilder.build() builds and return a ApiCommand instance.
  • line 6: The command is run.

The process for calling other commands are very similar, you only need to change the command (line 2) and the options (lines 3 and 4). For example, to vibrate the phone for 1 second (1000 ms):

api.createCommand()
    .vibrate()
    .setDuration(1000)
    .build()
    .run();

You can see all commands available on ApiCommandFactory source-code. And options for each command are availabe on the correspondent class in the builders folder.

Returning data from the API

Some API commands return data as String or JSON. To gather that data you could use the instance of ApiResult class returned by the method run() by invoking the methods getOutputObject() or getOutputString().

Notice that those 2 methods executes asynchronously returning ES6 Promises that resolves to an Object ou a String, respectively.

For example to get a location update from GPS and show the returned object on console:

let result = api.createCommand()
            .location()
            .fromGPSProvider()
            .requestOnce()
            .build()
            .run();

result.getOutputObject()
    .then(function(location) {
        console.log('Last known location: ', location);
    });