英文:
Windows powershell environment variable in react-native .env file?
问题
我想在React Native项目中使用当前给定的本地IP地址。
为此,我创建了一个PowerShell脚本文件,用于查找并将IP地址保存到系统变量$env:IPADDR
中。
Write-Host "获取当前IP地址"
$env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Write-Host " ---->" $env:IPADDR
现在我想在我的项目的.env
文件中传递这个变量。
尽管$npm_package_name
工作正常,但$env:IPADDR
似乎不起作用。与评估先前定义的环境变量不同,输出结果是变量文字本身,例如 console.log(REACT_APP_API_URL) --> http://$env:IPADDR:3000/
,而不是评估结果 http://192.168.10.4:3000/
。
我的.env
文件是这样创建的。
REACT_APP_API_URL=http://$env:IPADDR:3000/
REACT_APP_NAME=$npm_package_name
那么,我在哪里出错了?如何使用PowerShell环境变量$env:IPADDR
来动态评估REACT_APP_API_URL
?
英文:
I want to use the currently given local IP address inside a React Native project.
For this reason, I created a Powershell script file that finds and save the IP Address to the system variable $env:IPADDR
.
Write-Host "Getting current IP Address"
$env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Write-Host " ---->" $env:IPADDR
Now I want to pass this variable inside my project's .env
file.
While the $npm_package_name
works fine, the $env:IPADDR
does not seem to work. Instead of evaluating the previously defined environmental variable, the output result is the variable literal text itself ie console.log(REACT_APP_API_URL) --> http://$env:IPADDR:3000/
instead of the evaluated result http://192.168.10.4:3000/
.
My .env
file is created like this.
REACT_APP_API_URL=http://$env:IPADDR:3000/
REACT_APP_NAME=$npm_package_name
So, where am I doing wrong here? How can I dynamically evaluate the REACT_APP_API_URL
using the PowerShell environmental variable $env:IPADDR
?
答案1
得分: 1
<!-- language-all: sh -->
我发现在 `.env` 文件中原生地扩展 Windows 环境变量的方法。相反,我扩展了 PowerShell 脚本,以在 `.env` 文件内直接工作来“替换”所需的变量。
这是最终的 `run.ps1` *PowerShell* 脚本文件。
#查找本地 IP 地址并将其保存到 $env:IPADDR 变量中
Write-Host "获取当前 IP 地址"
$env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Write-Host " ---->" $env:IPADDR
#使用新的服务器地址 http://xxx.xxx.xxx.xxx:3000/ 替换键 [REACT_APP_API_URL] 的值
$regex = '(?<=REACT_APP_API_URL=)[^=]*'
$file = '.env'
$addr = 'http://' + $env:IPADDR + ':3000/'
(Get-Content $file) -replace $regex, $addr | Set-Content $file
#启动 NPM 脚本
npm run start
英文:
<!-- language-all: sh -->
I found no way to expand Windows environmental variables natively inside .env
file. Instead, I extended the PowerShell script, to "replace" the desired variable by working directly inside the .env
file.
Here is the final run.ps1
PowerShell script file.
#Find local IP addr and save it to $env:IPADDR variable
Write-Host "Getting current IP Address"
$env:IPADDR = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Write-Host " ---->" $env:IPADDR
#Replace the value of the key [REACT_APP_API_URL] with the new server ie http://xxx.xxx.xxx.xxx:3000/
$regex = '(?<=REACT_APP_API_URL=)[^=]*'
$file = '.env'
$addr = 'http://' + $env:IPADDR + ':3000/'
(Get-Content $file) -replace $regex, $addr | Set-Content $file
#Start NPM script
npm run start
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论