作为一名长期奋战在前端开发一线的工程师,我深知环境配置这个"脏活累活"有多让人头疼。每次换新机器或者重装系统,都要重复安装Node.js、Git、Yarn等一堆工具,配置各种环境变量,这个过程既耗时又容易出错。今天我要分享的是一个用PowerShell编写的自动化脚本,它能一键完成Web前端开发环境的完整配置。
这个脚本的诞生源于我去年换了新工作电脑后的痛苦经历。当时花了整整两天时间才把开发环境完全配置好,期间还因为版本不兼容问题重装了三次Node.js。痛定思痛后,我决定写一个自动化脚本来解决这个问题。经过半年多的迭代优化,现在这个脚本已经非常稳定,在我们团队内部广泛使用。
在Windows平台下,我们有多种脚本语言可选,比如传统的批处理(.bat)、Python、甚至Node.js本身。我最终选择PowerShell主要基于以下几点考虑:
脚本主要实现以下功能模块:
提示:脚本设计为模块化结构,每个功能模块都可以单独启用或禁用,方便根据实际需求定制。
脚本采用函数式编程风格,主要结构如下:
powershell复制# 脚本配置区
$config = @{
InstallChocolatey = $true
InstallGit = $true
InstallNode = $true
# 其他配置项...
}
# 工具函数定义
function Test-CommandExists {
param([string]$command)
# 实现略...
}
# 主安装函数
function Install-DevEnvironment {
# 安装逻辑实现
}
# 脚本入口
Install-DevEnvironment
Chocolatey是Windows下的包管理器,相当于Linux的apt或yum。安装代码如下:
powershell复制function Install-Chocolatey {
if (-not (Test-CommandExists "choco")) {
Write-Host "正在安装Chocolatey..."
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
# 验证安装
if (Test-CommandExists "choco") {
Write-Host "Chocolatey安装成功" -ForegroundColor Green
} else {
throw "Chocolatey安装失败"
}
} else {
Write-Host "Chocolatey已安装" -ForegroundColor Yellow
}
}
Node.js是前端开发的核心依赖,安装时需要注意版本管理:
powershell复制function Install-Node {
if (-not (Test-CommandExists "node")) {
Write-Host "正在安装Node.js..."
choco install nodejs-lts --yes
# 配置npm镜像源
npm config set registry https://registry.npmmirror.com
npm config set disturl https://npmmirror.com/dist
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
# 安装常用全局工具
npm install -g yarn pnpm @vue/cli create-react-app
Write-Host "Node.js环境配置完成" -ForegroundColor Green
} else {
Write-Host "Node.js已安装" -ForegroundColor Yellow
}
}
Git是版本控制必备工具,安装后需要进行一些基础配置:
powershell复制function Install-Git {
if (-not (Test-CommandExists "git")) {
Write-Host "正在安装Git..."
choco install git --yes --params "/GitAndUnixToolsOnPath /NoAutoCrlf"
# 基础配置
git config --global core.autocrlf false
git config --global core.safecrlf warn
git config --global core.ignorecase false
git config --global pull.rebase true
Write-Host "Git安装配置完成" -ForegroundColor Green
} else {
Write-Host "Git已安装" -ForegroundColor Yellow
}
}
以下是整合后的完整脚本代码:
powershell复制<#
.SYNOPSIS
Web前端开发环境一键配置脚本
.DESCRIPTION
自动安装配置前端开发所需的各种工具和环境
.NOTES
版本: 1.2.0
作者: 你的名字
日期: 2023-08-20
#>
# 配置项 - 可根据需要修改
$config = @{
# 基础工具
InstallChocolatey = $true
InstallGit = $true
InstallNode = $true
# 开发工具
InstallVSCode = $true
InstallYarn = $true
InstallPnpm = $true
# 可选组件
InstallDocker = $false
InstallWSL = $false
}
# 检查命令是否存在
function Test-CommandExists {
param([string]$command)
try {
if (Get-Command $command -ErrorAction SilentlyContinue) {
return $true
}
} catch {
return $false
}
return $false
}
# 安装Chocolatey
function Install-Chocolatey {
if (-not (Test-CommandExists "choco")) {
Write-Host "正在安装Chocolatey..." -ForegroundColor Cyan
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
if (Test-CommandExists "choco") {
Write-Host "Chocolatey安装成功" -ForegroundColor Green
} else {
throw "Chocolatey安装失败"
}
} else {
Write-Host "Chocolatey已安装" -ForegroundColor Yellow
}
}
# 安装Git
function Install-Git {
if (-not (Test-CommandExists "git")) {
Write-Host "正在安装Git..." -ForegroundColor Cyan
choco install git --yes --params "/GitAndUnixToolsOnPath /NoAutoCrlf"
git config --global core.autocrlf false
git config --global core.safecrlf warn
git config --global core.ignorecase false
git config --global pull.rebase true
Write-Host "Git安装配置完成" -ForegroundColor Green
} else {
Write-Host "Git已安装" -ForegroundColor Yellow
}
}
# 安装Node.js
function Install-Node {
if (-not (Test-CommandExists "node")) {
Write-Host "正在安装Node.js..." -ForegroundColor Cyan
choco install nodejs-lts --yes
npm config set registry https://registry.npmmirror.com
npm config set disturl https://npmmirror.com/dist
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
npm install -g yarn pnpm @vue/cli create-react-app
Write-Host "Node.js环境配置完成" -ForegroundColor Green
} else {
Write-Host "Node.js已安装" -ForegroundColor Yellow
}
}
# 安装VS Code
function Install-VSCode {
if (-not (Test-Path "$env:ProgramFiles\Microsoft VS Code\Code.exe")) {
Write-Host "正在安装VS Code..." -ForegroundColor Cyan
choco install vscode --yes
# 安装常用扩展
code --install-extension esbenp.prettier-vscode
code --install-extension dbaeumer.vscode-eslint
code --install-extension octref.vetur
code --install-extension msjsdiag.debugger-for-chrome
Write-Host "VS Code安装完成" -ForegroundColor Green
} else {
Write-Host "VS Code已安装" -ForegroundColor Yellow
}
}
# 主安装函数
function Install-DevEnvironment {
try {
# 安装Chocolatey
if ($config.InstallChocolatey) {
Install-Chocolatey
}
# 安装Git
if ($config.InstallGit) {
Install-Git
}
# 安装Node.js
if ($config.InstallNode) {
Install-Node
}
# 安装VS Code
if ($config.InstallVSCode) {
Install-VSCode
}
# 安装Docker
if ($config.InstallDocker) {
choco install docker-desktop --yes
}
# 安装WSL
if ($config.InstallWSL) {
choco install wsl2 --yes
}
Write-Host "`n所有组件安装完成!" -ForegroundColor Green
Write-Host "建议重启计算机使所有配置生效" -ForegroundColor Yellow
} catch {
Write-Host "安装过程中出错: $_" -ForegroundColor Red
exit 1
}
}
# 执行安装
Install-DevEnvironment
Setup-Frontend-Env.ps1Set-ExecutionPolicy RemoteSigned -Scope CurrentUser(首次运行需要).\Setup-Frontend-Env.ps1如果遇到执行策略限制错误,可以临时修改策略:
powershell复制Set-ExecutionPolicy Bypass -Scope Process -Force
部分组件需要从国外服务器下载,如果遇到网络问题:
powershell复制$env:HTTP_PROXY="http://your-proxy:port"
$env:HTTPS_PROXY="http://your-proxy:port"
如果某个组件安装失败:
脚本开头的$config哈希表可以自定义要安装的组件:
powershell复制$config = @{
InstallChocolatey = $true # 安装Chocolatey包管理器
InstallGit = $true # 安装Git
InstallNode = $true # 安装Node.js
InstallVSCode = $false # 不安装VS Code
# 其他配置...
}
Start-Job实现组件并行安装在我们团队内部使用这个脚本后,新成员入职配置开发环境的时间从平均4小时缩短到30分钟以内。更重要的是,避免了因手动配置导致的版本不一致问题,大大提高了团队开发环境的一致性。
对于个人开发者来说,这个脚本同样能节省大量时间。特别是在需要频繁重装系统或使用多台开发机的情况下,一键配置的优势更加明显。
为了保持脚本的可用性,我建议:
这个脚本我已经开源在GitHub上,欢迎大家一起贡献改进。通过社区的力量,我们可以让它变得更加强大和易用。