@luban-cli/cli-plugin-service
Version:
A development runtime environment dependency
53 lines • 2.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadAndSetEnv = void 0;
const path_1 = __importDefault(require("path"));
const dotenv_expand_1 = __importDefault(require("dotenv-expand"));
const dotenv_1 = require("dotenv");
const cli_shared_utils_1 = require("@luban-cli/cli-shared-utils");
const chalk_1 = __importDefault(require("chalk"));
function loadAndSetEnv(mode, context, commandName) {
const basePath = path_1.default.resolve(context, ".env");
const baseModePath = path_1.default.resolve(context, `.env.${mode}`);
const localModePath = `${baseModePath}.local`;
const load = (path) => {
const env = dotenv_1.config({ path });
dotenv_expand_1.default(env);
if (!env.error) {
cli_shared_utils_1.info(`loaded dotenv file ${chalk_1.default.green(path)} successfully`);
}
cli_shared_utils_1.log();
};
// this load order is important
// env.[mode].local has first priority, env.[mode] has second priority and .env has lowest priority
// if the three files exist
load(localModePath);
load(baseModePath);
load(basePath);
const writeEnv = (key, value) => {
Object.defineProperty(process.env, key, {
value: value,
writable: false,
configurable: false,
enumerable: true,
});
};
if (mode && commandName === "inspect") {
const _mode = mode === "development" ? "development" : "production";
writeEnv("NODE_ENV", _mode);
writeEnv("BABEL_ENV", _mode);
}
if (commandName === "serve") {
writeEnv("NODE_ENV", "development");
writeEnv("BABEL_ENV", "development");
}
if (commandName === "build") {
writeEnv("NODE_ENV", "production");
writeEnv("BABEL_ENV", "production");
}
}
exports.loadAndSetEnv = loadAndSetEnv;
//# sourceMappingURL=loadAndSetEnv.js.map