[common/features] feat: 환경 변수 URL 방식 도입
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { findUp } from "find-up";
|
||||
import { config } from "dotenv";
|
||||
import fs from "fs";
|
||||
import yargs from "yargs";
|
||||
import { hideBin } from "yargs/helpers";
|
||||
|
||||
async function parseDotenv(nodeEnv) {
|
||||
const envFilePath = await findUp(`.env.${nodeEnv}`);
|
||||
|
||||
const parsedEnv = config({ path: envFilePath }).parsed || {};
|
||||
|
||||
return parsedEnv;
|
||||
}
|
||||
|
||||
function writeEnv(parsedEnv) {
|
||||
const scriptFilePath = `${fs.realpathSync(process.cwd())}/public/__ENV__.js`;
|
||||
|
||||
fs.writeFileSync(
|
||||
scriptFilePath,
|
||||
`window.__ENV__ = ${JSON.stringify(parsedEnv)}`,
|
||||
);
|
||||
}
|
||||
|
||||
yargs(hideBin(process.argv))
|
||||
.command(
|
||||
"next-env",
|
||||
"Create Next.js runtime environment js",
|
||||
function builder(y) {
|
||||
return y.option("env", {
|
||||
alias: "e",
|
||||
type: "string",
|
||||
description: "Environment name(ex: development, production)",
|
||||
});
|
||||
},
|
||||
async function handler(args) {
|
||||
const nodeEnv = args.e || args.env || "development";
|
||||
|
||||
const parsedEnv = await parseDotenv(nodeEnv); // dotenv 파싱
|
||||
writeEnv(parsedEnv); // 환경 변수 스크립트 파일 생성
|
||||
|
||||
return parsedEnv;
|
||||
},
|
||||
)
|
||||
.parse();
|
||||
Reference in New Issue
Block a user