Skip to content

Ashu11-A/js-to-sh

Repository files navigation

Transpiler Js to Sh

license-info stars-infoa Last-Comitt

Comitts Year reposize-info

Warning

This project is still under development and may not work as expected.

🤨 | 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:

npx js-to-sh -D -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:

// Esm
import { Transpiler } from 'js-to-sh'
// Communjs - Not Recommended
//const { Transpiler } = require('js-to-sh')

// This is not recursive yet
const output = new Transpiler({ path: 'src/test.js', debug: true }).parser()
console.log(output)

🌎 | 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')

// Esm
import 'js-to-sh'
// 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"