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 }
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 });
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); } };
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.
以上是從 shell 腳本遷移到'Bun 腳本”的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。