@kosko/cli
Version:
Organize Kubernetes manifests in JavaScript.
42 lines • 1.73 kB
JavaScript
import { localImportDefault, localRequireDefault } from "./require.mjs";
import { isESMSupported } from "@kosko/require";
import { createCLIEnvReducer } from "./set-option.mjs";
const KOSKO_ENV = "@kosko/env";
function excludeFalsyInArray(input) {
return input.filter(Boolean);
}
function pickEnvArray(envs) {
if (envs.length > 1)
return envs;
return envs[0];
}
export async function setupEnv(config, args) {
var _a;
const cwd = args.cwd;
const envs = [await localRequireDefault(KOSKO_ENV, cwd)];
if (await isESMSupported()) {
// Why `@kosko/env` package has to be imported twice? Because the cache on
// CommonJS and ESM are separated, which means we have two isolated
// instances of `Environment`, and each of them must be initialized
// in order to make sure users can access the environment in both CommonJS
// and ESM environment.
envs.push(await localImportDefault(KOSKO_ENV, cwd));
}
const paths = ((_a = config.paths) === null || _a === void 0 ? void 0 : _a.environment) || {};
const setReducer = args.set && args.set.length ? createCLIEnvReducer(args.set) : undefined;
// Initialize all environments
for (const env of envs) {
env.cwd = cwd;
env.env = pickEnvArray(excludeFalsyInArray([config.baseEnvironment, args.env]));
if (config.extensions)
env.extensions = [...config.extensions];
if (paths.global)
env.paths.global = paths.global;
if (paths.component)
env.paths.component = paths.component;
if (setReducer) {
env.setReducers((reducers) => [...reducers, setReducer]);
}
}
}
//# sourceMappingURL=env.mjs.map