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

js-to-sh

v1.1.8

Published

Turn javascript code into shellscript

Downloads

826

Readme

Transpiler Js to Sh

license-info stars-infoa Last-Comitt

Comitts Year reposize-info

🤨 | What's that?

This project uses ATS (abstract syntax tree) to format the javascript for shellscript syntax, but errors can happen, don't expect it to always work, for example, classes are converted to functions for their operation, but more complex things have not yet been implemented and may not work, if you want to help open a pull request!

🤷‍♂️ | Why did you do that?

With a deficit in generating high-performance, easy-to-maintain shell scripts, I decided to create this project. Of course, it's still in development and, for the time being, it's just a big improvisation, but I hope I don't leave any bugs behind!

📥 | Installation

Installing this package is as easy as using it. Just run:

npm i -g js-to-sh
# OR
npm i js-to-sh

🔎 | How to use

📟 | Terminal:

js-to-sh -f src/test.ts -o test.sh
# OR
npx tjss -f src/test.ts -o test.sh

📄 | Help

Usage: tjss [options]

  Options:

   -D --debug  Activates debug mode.
   -d --dir    Directory for fetching and transpiling .js files
   -f --file   File to be transpiled.
   -o --output Output directory or file to save the transpiled files.

👨‍💻 | Code:

// build.ts - Esm
import { Transpiler } from 'js-to-sh'

const AST = await new Transpiler({ path: 'src/test.js' }).loader()
const code = Transpiler.parser(AST)

console.log(code)
// build.ts - Communjs
const { Transpiler } = require('js-to-sh'); // <-- yes, you need that here ”;”

(async () => {
    const ast = await (new Transpiler({ path: 'src/test.js', cwd: process.cwd() })).loader()
    const code = Transpiler.parser(ast)

    console.log(code)
})()

🌎 | Global variables

These global variables are associated with static functions, which are imported during the build process of the file. You should use these functions if you need to leverage shellscript behaviors.

// To use these functions in JavaScript, without the conversion, for backward compatibility (work in JavaScript and ShellScript), you should use:

// Communjs
require('js-to-sh/loader')

// Esm
import 'js-to-sh/globals'
// Checks if a specific command exists in the operating system where the script is running.
isCommand(command)

// Checks if the provided path is a directory.
isDir(path)

// Validates that the given input is empty.
isEmpty(content)

// Validates that the specified file has execute permissions.
isExecutable(filePath)

// Checks if the provided path points to a file.
isFile(filePath)

// Validates that the input is a number.
isNumber(number)

// Checks if the provided path has read permissions.
isReadable(path)

// Validates that the specified path has write permissions.
isWritable(path)

💡 | Example

Input:

// src/test.js
function some (num1, num2) {
  return num1 + num2
}

function string (str1, str2) {
  return `${str1} ${str2}`
}

const result = some(1, 2)

console.log(string('test', 'test'))
console.log(result)

const func = (str) => console.log(str)
const func2 = (str) => {
  console.log(str)
  console.log(str)
}

func('ArrowFunctionExpression')
func2('ArrowFunctionExpression')

Output:

#!/bin/bash

function some() {
  local num1=$1
  local num2=$2
  echo "$(( "$num1" + "$num2" ))"
}

function string() {
  local str1=$1
  local str2=$2
  echo ""$str1" "$str2""
}

result=$(some 1 2)
echo "$(string test test)"
echo "$result"

function func () {
  local str=$1
  echo "$str"
}

function func2 () {
  local str=$1
  echo "$str"
  echo "$str"
}

func "ArrowFunctionExpression"
func2 "ArrowFunctionExpression"