8-bit-env 中文文档教程
8 Bit Env 帮助您轻松管理,安全地存储和公开您的应用程序所需的所有环境变量和敏感密钥。 它还可以让您将所有环境数据存储在版本控制中的代码旁边。
Installation
# with npm
npm i -D 8-bit-env
# or with yarn
yarn add -D 8-bit-env
Usage
8 Bit Env 有一个交互式命令行工具,这是最简单的使用方法:
npx 8-bit-env
Initializing 8 Bit Env
当您运行初始化脚本时,您将首先被要求提供一个主密钥。 主密钥是 8 Bit Env 用来加密所有环境数据的,它本质上是一个密码。
输入主密钥后,8 Bit Env 将在项目的根目录下创建一个名为 .8bitenv
的文件夹,并将主密钥放入名为 master.key
。 然后,此文件将与任何环境文件的条目一起添加到您的 .gitignore 文件中。
然后,系统会要求您输入要为其存储环境数据的环境的名称。 在上面的示例中,我们创建了两个,development
和 staging
。
将为每个环境创建一个 *.env
文件并存储在 .8bitenv
文件夹中。
Adding & Saving Environment Variables
在您在上一步中创建的 *.env
文件中,您可以放置任何环境变量或您需要跟踪的信息片段。 全部保存后,运行 save
命令。
当您保存文件时,它们将使用 master.key
中的密钥进行加密,并作为 *.enc
文件存储在 .enc/
中文件夹。 原始的 *.env
文件将被删除(最好不要让这些文件随处可见)。
这些加密文件可以安全地签入版本控制。
Updating Environment Variables
当需要更新您的环境变量时,只需运行 update
函数,它将使用 master.key
中的密钥解密所有加密的环境文件,并将它们输出为.8bitenv
文件夹中的*.env
文件。
解密这些文件时,重要的是
master.key
中的密钥与加密它们时的密钥相同。
拥有 *.env
文件后,您可以进行任何需要进行的更改,然后再次运行保存功能。
Exposing Environment Variables
最终,您会希望在运行代码时使用这些环境变量,并且根据您运行的环境,您会希望使用不同的环境变量。
运行 expose
将允许您将特定环境的变量导出到您选择的文件中。
这会派上用场的常见情况是使用 dotenv。 Dotenv 期望在项目的根目录中有一个名为 .env
的文件,它将从中加载环境变量。 例如,您可以使用 expose
将所有开发变量放在 .env
文件中,dotenv
将获取该文件。
Creating new Environments
如果你想添加一个新的环境,只需运行 create
命令,它会在 .8bitenv
中为你创建一个 *.env
文件> 文件夹。
Running from the command line
上面的使用部分演示了如何将 8 Bit Env 与交互式终端应用程序一起使用,但它也可以通过简单的命令行命令运行。
npx 8-bit-env init <master_key> [envName1,envName2,envName3]
npx 8-bit-env save
npx 8-bit-env update
npx 8-bit-env expose <envNameToExpose> <targetFile>
npx 8-bit-env create <envName1,envName2,envName3>
根据环境文件的状态,您可能无法执行所有这些操作。 例如,如果您没有任何加密的环境文件,则无法公开
或更新
。
Running in Code
8 位 Env 为 init
、save
、update
、expose
和 create< 导出函数/代码>。
import { init, save, update, expose, create } from '8-bit-env'
// initialize 8 bit env
init()
// save any *.env files
save()
// decrypt all encrypted environment files
update()
// export a particular environments variables to a file
expose('environmentName', 'path/to/export')
// create new environment files
create(['envName1', 'envName2'])
Managing the Master Key
任何时候您的代码从版本控制中被拉下,您都必须再次添加主密钥。 主密钥是一致的很重要,无论您在保存环境文件时使用什么密钥,都是您解密它们所需要的密钥。
通过运行 init
命令轻松添加主密钥,或者简单地将名为 master.key
的文件添加到 .8bitenv
如果你想更改主密钥,只需运行 update
命令来解密所有环境文件,然后更改密钥并运行 save
。
8 Bit Env helps you easily manage, securely store and expose all of the environment variables and sensitive keys you need for your app. It also let's you store all your environment data next to your code in version control.
Installation
# with npm
npm i -D 8-bit-env
# or with yarn
yarn add -D 8-bit-env
Usage
8 Bit Env has an interactive command line tool which is the easiest way to use it:
npx 8-bit-env
Initializing 8 Bit Env
When you run the init script you'll first be asked to provide a master key. The master key is what 8 Bit Env uses to encrypt all of your environment data, it's essentially a password.
Once you enter your master key, 8 Bit Env will create a folder at the root directory of your project called .8bitenv
and put the master key inside in a file called master.key
. This file will then be added to your .gitignore file along with an entry for any environment files.
You'll then be asked to enter the names of the environments you want to store environment data for. In the example above, we're creating two, development
and staging
.
A *.env
file will be created for each environment and stored inside the .8bitenv
folder.
Adding & Saving Environment Variables
Inside the *.env
files you created in the last step, you can place any environment variables or pieces of info you need to keep track of. Once it's all in there, run the save
command.
When you save the files, they will be encrypted using the key inside master.key
and stored as *.enc
files in the .enc/
folder. The original *.env
files will then be deleted (better not to have these lying around).
These encrypted files can be safely checked into version control.
Updating Environment Variables
When it's time to update your environment variables, simply run the update
function which will decrypt all of the encrypted environment files using the key inside master.key
, and output them as *.env
files in the .8bitenv
folder.
When decrypting these files, it's important the the key inside
master.key
is the same as when you encrypted them.
Once you have the *.env
files, you can make any changes you need to make and then run the save function again.
Exposing Environment Variables
Eventually, you'll want to use these environment variables when you run your code, and depending on the environment you're running in, you'll want to use different ones.
Running expose
will allow you to export the variables for a specific environment into a file of your choosing.
A common situation where this will come in handy is when using dotenv. Dotenv expects a file called .env
at the root directory of your project, which it will load environment variables from. You can, for example, use expose
to place all your development variables in a .env
file, which dotenv
will pick up on.
Creating new Environments
If you want to add a new environment, simply run the create
command, which will create a *.env
file for you in the .8bitenv
folder.
Running from the command line
The usage section above demonstrates how to use 8 Bit Env with the interactive terminal app, but it can also be run with simple command line commands.
npx 8-bit-env init <master_key> [envName1,envName2,envName3]
npx 8-bit-env save
npx 8-bit-env update
npx 8-bit-env expose <envNameToExpose> <targetFile>
npx 8-bit-env create <envName1,envName2,envName3>
Depending on the state of your environment files, you may not be able to perform all of these. For example, if you don't have any encrypted environment files, you can't expose
or update
.
Running in Code
8 Bit Env exports functions for init
, save
, update
, expose
and create
.
import { init, save, update, expose, create } from '8-bit-env'
// initialize 8 bit env
init()
// save any *.env files
save()
// decrypt all encrypted environment files
update()
// export a particular environments variables to a file
expose('environmentName', 'path/to/export')
// create new environment files
create(['envName1', 'envName2'])
Managing the Master Key
Anytime your code is pulled down from version control, you'll have to add in the master key again. It's important that the master key is consistant, whetever key you used when you saved the environment files, is the one you need to decrypt them.
Easily add the master key back in by running the init
command, or simply adding a file called master.key
to .8bitenv
If you want to change the master key, simply run the update
command to decrypt all your environment files, then change the key and run save
.