* feat: add ability to export a single environment * refactor: export environment without id * feat: introducing zod for checking json format for environment variables * refactor: new zod specific type for HoppEnvPair * feat: add ability to export single environment in team environment * refactor: moved zod as a dependency to devDependency * refactor: separated repeating logic to helper file * refactor: removed unnecessary to string operation * chore: rearranged smart item placement * refactor: introduced error type when a bulk environment export is used in cli * refactor: removed unnecssary type exports and updated logic and variable names across most files * refactor: better logic for type shapes * chore: bump hoppscotch-cli package version --------- Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
import { error } from "../../types/errors";
|
|
import {
|
|
HoppEnvs,
|
|
HoppEnvPair,
|
|
HoppEnvKeyPairObject,
|
|
HoppEnvExportObject,
|
|
HoppBulkEnvExportObject,
|
|
} from "../../types/request";
|
|
import { readJsonFile } from "../../utils/mutators";
|
|
/**
|
|
* Parses env json file for given path and validates the parsed env json object.
|
|
* @param path Path of env.json file to be parsed.
|
|
* @returns For successful parsing we get HoppEnvs object.
|
|
*/
|
|
export async function parseEnvsData(path: string) {
|
|
const contents = await readJsonFile(path);
|
|
const envPairs: Array<HoppEnvPair> = [];
|
|
const HoppEnvKeyPairResult = HoppEnvKeyPairObject.safeParse(contents);
|
|
const HoppEnvExportObjectResult = HoppEnvExportObject.safeParse(contents);
|
|
const HoppBulkEnvExportObjectResult =
|
|
HoppBulkEnvExportObject.safeParse(contents);
|
|
|
|
// CLI doesnt support bulk environments export.
|
|
// Hence we check for this case and throw an error if it matches the format.
|
|
if (HoppBulkEnvExportObjectResult.success) {
|
|
throw error({ code: "BULK_ENV_FILE", path, data: error });
|
|
}
|
|
|
|
// Checks if the environment file is of the correct format.
|
|
// If it doesnt match either of them, we throw an error.
|
|
if (!(HoppEnvKeyPairResult.success || HoppEnvExportObjectResult.success)) {
|
|
throw error({ code: "MALFORMED_ENV_FILE", path, data: error });
|
|
}
|
|
|
|
if (HoppEnvKeyPairResult.success) {
|
|
for (const [key, value] of Object.entries(HoppEnvKeyPairResult.data)) {
|
|
envPairs.push({ key, value });
|
|
}
|
|
} else if (HoppEnvExportObjectResult.success) {
|
|
const { key, value } = HoppEnvExportObjectResult.data.variables[0];
|
|
envPairs.push({ key, value });
|
|
}
|
|
|
|
return <HoppEnvs>{ global: [], selected: envPairs };
|
|
}
|