ホームページ  >  記事  >  ウェブフロントエンド  >  シェル スクリプトから「Bun スクリプト」への移行

シェル スクリプトから「Bun スクリプト」への移行

Barbara Streisand
Barbara Streisandオリジナル
2024-09-28 06:18:29608ブラウズ

Migrating from shell script to

zCloud でプロセスの自動化とインフラストラクチャに焦点を当てたプロジェクトに取り組んでいる場合、検証と共通のプロセスを実行するために複数の関数を作成する必要性に頻繁に遭遇します。オペレーティング システムが 1 つだけ使用されている場合はすべて問題なく動作しますが、複数のシステムが関係する場合は状況が複雑になります。

私たちの場合、開発のほとんどは Linux 上で行われますが、macOS との互換性も確保する必要があります。これにより、コードの互換性が失われることがよくあります。

この問題に対処するために、Bun をインタープリタとして使用して、シェル スクリプト関数を JavaScript ファイルに移行しています。 Bun を選択したのは、シェル API 機能を通じてシェルのようにコマンドを実行する簡単な方法が提供されるためです。

以下は、インフラストラクチャの変更を適用する前にコードの変更をチェックするために使用する関数の例です。

シェルスクリプトコード:

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
}

このコードを JavaScript に変換するために、次の内容を含む zc_check_pristine_git という名前のファイルをプロジェクトの bin ディレクトリ (すでに PATH 内にあります) に作成しました。

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

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

await checkPristineGit({ currentEnv: process.env.ZC_CURRENT_ENV });

Bun をインタプリタとして使用していることを示すために、shebang #!/usr/bin/env bun を使用しました。

IDE がファイルを JavaScript として認識できるように、コメント // @ language JavaScript を追加しました (主に Jetbrains ツールを使用します)。

次に、実際に実行する関数をインポートしました。

シェルから 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);
  }
};

このようにして、シェル スクリプトのように実行される JavaScript コードを標準化しました。


提供された例では実装されていない関数の呼び出し (exitOnError、notEmpty) があります。

以上がシェル スクリプトから「Bun スクリプト」への移行の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。