简体   繁体   English

如何从 npm 脚本动态编辑 a.env 文件?

[英]How can I dynamically edit a .env file from an npm script?

I've got a .env file for a project that I'm working on, I won't reveal the whole file because it contains sensitive data, but, in that file I have an item called 'STATUS'.我有一个我正在处理的项目的.env文件,我不会透露整个文件,因为它包含敏感数据,但是,在该文件中,我有一个名为“STATUS”的项目。

NOTE: This is for a Discord bot,注意:这是针对 Discord 机器人的,

The 'STATUS' variable looks like this: STATUS=DEVELOPMENT 'STATUS' 变量如下所示: STATUS=DEVELOPMENT

In my code I have a handler that deploys commands to all servers or just my specific server relative to the value of 'STATUS'.在我的代码中,我有一个处理程序可以将命令部署到所有服务器或仅相对于“STATUS”值的我的特定服务器。
example: if STATUS was equal to DEVELOPMENT than it would deploy the commands to the development server and if it was equal to PRODUCTION then it would deploy the commands to all of the servers the bot is in.示例:如果STATUS等于DEVELOPMENT ,那么它会将命令部署到开发服务器,如果它等于PRODUCTION ,那么它会将命令部署到机器人所在的所有服务器。

That code looks something like this:该代码看起来像这样:

if (STATUS == "DEVELOPMENT") {
  Routes.applicationGuildCommands(CLIENT_ID, process.env.DEVELOPMENT_GUILD_ID),
    { body: slashCommands },
    console.log(
      chalk.yellow(`Slash Commands • Registered Locally to 
  the development server`)
    );
} else if (STATUS == "PRODUCTION") {
  await rest.put(
    Routes.applicationCommands(CLIENT_ID),
    { body: slashCommands },
    console.log(chalk.yellow("Slash Commands • Registered Globally"))
  );
}

In my package.json file I would like to have two scripts that control if the commands get pushed to production or development.在我的package.json文件中,我希望有两个脚本来控制命令是被推送到生产还是开发。
example:例子:

"scripts": {
      "prod": "changes the 'STATUS' to 'PRODUCTION' and runs the file",
      "dev": "changes the 'STATUS' to 'DEVELOPMENT' and runs the file"
  },

You can just create a simple utility JS script to do the work:您可以只创建一个简单的实用 JS 脚本来完成这项工作:

// status.js
const fs = require("fs")
const path = require("path")

// get the first argument passed to this file:
const status = process.argv[3] // the first and second element will always be `node` and `filename.js`, respectively

if(!status) {
  throw new Error("You must supply a status to change to")
}

const envPath = path.join(process.cwd(), ".env") // resolve to the directory calling this script
// parse the environment variable file
let env = fs.readFileSync(envPath)
env = env.split(/\r?\n/g) // optional linefeed character

let prevExists = false
for(let lineNumber in env) {
  if(env[lineNumber].startsWith("STATUS=")) {
    prevExists = true
    env[lineNumber] = `STATUS=${status}`
    break
  }
}
if(!prevExists) env.push(`STATUS=${status}`)

const newEnv = env.join("\n")
fs.writeFileSync(envPath, newEnv)

console.log(`Successfully changed the status to "${status}"`)

Then in your package.json , you can put the following:然后在您的package.json中,您可以输入以下内容:

"scripts": {
  "prod": "node status.js PRODUCTION && command to deploy server",
  "dev": "node status.js DEVELOPMENT && command to deploy server"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM