DEV Community

Edimar Cardoso
Edimar Cardoso

Posted on

Migrating from shell script to "Bun script"

When working on a project focused on process automation and infrastructure at zCloud, we frequently encounter the need to create multiple functions to perform validations and common processes. Everything works fine when using only one operating system, but the situation becomes complicated when more than one system is involved.

In our case, most of the development happens on Linux, but we also need to ensure compatibility with macOS. This often results in code incompatibilities.

To address this issue, we are migrating our shell script functions to JavaScript files, using Bun as the interpreter. We chose Bun because it provides a simple way to run commands like a shell through its Shell API functionality.

Below is an example of a function we use to check for any code changes before applying infrastructure modifications.

Shell script code:

function zc_check_pristine_git() {
    if [ "$ZC_CURRENT_ENV" = "staging" ] || [ "$ZC_CURRENT_ENV" = "dev" ]; then
      return 0
    fi

    local not_pristine=0
    local modified_files=""

    # Check for staged but uncommitted changes
    staged_changes=$(git diff --name-only --cached)
    if [ -n "$staged_changes" ]; then
        not_pristine=1
        modified_files+="Staged changes:\n$staged_changes"
    fi

    # Check for unstaged changes
    unstaged_changes=$(git diff --name-only)
    if [ -n "$unstaged_changes" ]; then
        not_pristine=1
        modified_files+="Unstaged changes:\n$unstaged_changes"
    fi

    # Check for untracked files
    untracked_files=$(git ls-files --others --exclude-standard)
    if [ -n "$untracked_files" ]; then
        not_pristine=1
        modified_files+="Untracked files:\n$untracked_files"
    fi

    # Check if the current branch is ahead of the remote
    ahead_commits=$(git log @{u}.. --oneline)
    if [ -n "$ahead_commits" ]; then
        not_pristine=1
        modified_files+="Commits ahead of the remote:\n$ahead_commits\n\n"
    fi

    if [ $not_pristine -eq 1 ]; then
        echo -e "$modified_files"
        return 1
    fi

    return 0
}
Enter fullscreen mode Exit fullscreen mode

To convert this code to JavaScript, we created a file named zc_check_pristine_git in the project’s bin directory (which is already in the PATH) with the following content:

#!/usr/bin/env bun
// @language JavaScript

import { checkPristineGit } from '../js/helpers/helpers.js';

await checkPristineGit({ currentEnv: process.env.ZC_CURRENT_ENV });
Enter fullscreen mode Exit fullscreen mode

We used the shebang #!/usr/bin/env bun to indicate that we are using Bun as the interpreter.

We added the comment // @language JavaScript so that the IDE recognizes the file as JavaScript (we mainly use Jetbrains tools).

Then, we imported the function that will actually be executed.

Implementation of the function converted from shell to JavaScript:

export const checkPristineGit = async ({ currentEnv }) => {
  exitOnError(() => {
    notEmpty(currentEnv, 'currentEnv is required');
  });
  if (['staging', 'dev'].includes(currentEnv)) {
    return;
  }

  let notPristine = 0;
  let modifiedFiles = '';

  // Check for staged but uncommitted changes
  const stagedChanges = await $`git diff --name-only --cached`.text();
  if (stagedChanges !== '') {
    notPristine = 1;
    modifiedFiles += `Staged changes:\n${stagedChanges}`;
  }

  // Check for unstaged changes
  const unstagedChanges = await $`git diff --name-only`.text();
  if (unstagedChanges !== '') {
    notPristine = 1;
    modifiedFiles += `Unstaged changes:\n${unstagedChanges}`;
  }

  // Check for untracked files
  const untrackedFiles = await $`git ls-files --others --exclude-standard`.text();
  if (untrackedFiles !== '') {
    notPristine = 1;
    modifiedFiles += `Untracked files:\n${untrackedFiles}`;
  }

  // Check if the current branch is ahead of the remote
  const aheadCommits = await $`git log @{u}.. --oneline`.text();
  if (aheadCommits !== '') {
    notPristine = 1;
    modifiedFiles += `Commits ahead of the remote:\n${aheadCommits}`;
  }

  if (notPristine) {
    console.warn('Error: You can only apply changes in production environments if the repository is in a pristine state.');
    console.warn(modifiedFiles);
    process.exit(1);
  }
};
Enter fullscreen mode Exit fullscreen mode

This way, we have standardized JavaScript code that will be executed like a shell script.


There are calls to functions (exitOnError, notEmpty) that are not implemented in the provided example.

Top comments (1)

Collapse
 
fhperuchi profile image
Fábio Peruchi

Amazing @edimarlnx ! Really nice post.

Thanks.