Heim > Web-Frontend > js-Tutorial > Hauptteil

Migration vom Shell-Skript zum „Bun-Skript'

Barbara Streisand
Freigeben: 2024-09-28 06:18:29
Original
608 Leute haben es durchsucht

Migrating from shell script to \

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
}
Nach dem Login kopieren

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 });
Nach dem Login kopieren

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);
  }
};
Nach dem Login kopieren

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.

Das obige ist der detaillierte Inhalt vonMigration vom Shell-Skript zum „Bun-Skript'. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!