zCloud에서 프로세스 자동화 및 인프라에 초점을 맞춘 프로젝트를 진행하면서 검증 및 공통 프로세스를 수행하기 위해 여러 기능을 생성해야 하는 경우가 자주 발생합니다. 하나의 운영 체제만 사용하면 모든 것이 잘 작동하지만, 둘 이상의 시스템이 관련되면 상황이 복잡해집니다.
우리의 경우 대부분의 개발이 Linux에서 이루어지지만 macOS와의 호환성도 보장해야 합니다. 이로 인해 코드 비호환성이 발생하는 경우가 많습니다.
이 문제를 해결하기 위해 Bun을 인터프리터로 사용하여 쉘 스크립트 기능을 JavaScript 파일로 마이그레이션하고 있습니다. 우리는 Bun이 Shell 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 }||
; 그 다음에
0을 반환
fi
로컬 not_pristine=0
로컬 수정_파일=""
# 단계적이지만 커밋되지 않은 변경 사항을 확인합니다.
staged_changes=$(git diff --name-only --cached)
if [ -n "$staged_changes" ]; 그 다음에
not_pristine=1
Modified_files ="단계적 변경:\n$staged_changes"
fi
# 단계적이지 않은 변경 사항을 확인합니다.
unstaged_changes=$(git diff --이름만)
if [ -n "$unstaged_changes" ]; 그 다음에
not_pristine=1
Modified_files ="단계화되지 않은 변경 사항:\n$unstaged_changes"
fi
# 추적되지 않는 파일을 확인하세요
untracked_files=$(git ls-files --others --exclude-standard)
if [ -n "$untracked_files" ]; 그 다음에
not_pristine=1
수정된_files ="추적되지 않은 파일:\n$untracked_files"
fi
# 현재 분기가 원격 분기보다 앞에 있는지 확인합니다.
before_commits=$(git log @{u}.. --oneline)
if [ -n "$ahead_commits" ]; 그 다음에
not_pristine=1
수정된_files ="원격보다 먼저 커밋:\n$ahead_commits\n\n"
fi
if [ $not_pristine -eq 1 ]; 그 다음에
echo -e "$modified_files"
1을 반환
fi
0을 반환
}
#!/usr/bin/env bun // @language JavaScript import { checkPristineGit } from '../js/helpers/helpers.js'; await checkPristineGit({ currentEnv: process.env.ZC_CURRENT_ENV });
#!/usr/bin/env 롤빵 // @언어 자바스크립트 import { checkPristineGit } from '../js/helpers/helpers.js'; wait checkPristineGit({ currentEnv: process.env.ZC_CURRENT_ENV });
우리는 Bun을 인터프리터로 사용하고 있음을 나타내기 위해 shebang #!/usr/bin/env bun을 사용했습니다.
IDE에서 파일을 JavaScript로 인식할 수 있도록 // @언어 JavaScript 주석을 추가했습니다(저희는 주로 Jetbrains 도구를 사용합니다).
그런 다음 실제로 실행될 함수를 가져왔습니다.
셸에서 JavaScript로 변환된 함수 구현:
#!/usr/bin/env bun // @language JavaScript import { checkPristineGit } from '../js/helpers/helpers.js'; await checkPristineGit({ currentEnv: process.env.ZC_CURRENT_ENV });
export const checkPristineGit = async ({ currentEnv }) => { exitOnError(() => { notEmpty(currentEnv, 'currentEnv가 필요합니다'); }); if (['staging', 'dev'].includes(currentEnv)) { 반품; } notPristine = 0으로 설정합니다. 수정된 파일 = ''; // 단계적이지만 커밋되지 않은 변경 사항을 확인합니다. const stagedChanges = $`git diff --name-only --cached`.text()를 기다립니다. if (stagedChanges !== '') { notPristine = 1; ModifiedFiles = `단계적 변경:\n${stagedChanges}`; } // 단계적이지 않은 변경 사항을 확인합니다. const unstagedChanges = $`git diff --name-only`.text()를 기다립니다. if (unstagedChanges !== '') { notPristine = 1; ModifiedFiles = `단계화되지 않은 변경 사항:\n${unstagedChanges}`; } //추적되지 않은 파일을 확인합니다. const untrackedFiles = $`git ls-files --others --exclude-standard`.text()를 기다립니다. if (untrackedFiles !== '') { notPristine = 1; 수정된Files = `추적되지 않은 파일:\n${untrackedFiles}`; } // 현재 분기가 원격 분기보다 앞에 있는지 확인합니다. const beforeCommits = $`git log @{u}.. --oneline`.text()를 기다립니다. if (aheadCommits !== '') { notPristine = 1; ModifiedFiles = `원격보다 먼저 커밋:\n${aheadCommits}`; } if (notPristine) { console.warn('오류: 저장소가 원래 상태인 경우에만 프로덕션 환경에 변경 사항을 적용할 수 있습니다.'); console.warn(수정파일); 프로세스.종료(1); } };
제공된 예시에는 구현되지 않은 함수 호출(exitOnError, notEmpty)이 있습니다.
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3