45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
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();
|