From d5a74207825ba0b43eec8ddc7204397609e15c06 Mon Sep 17 00:00:00 2001 From: Patryk Ziemkowski Date: Tue, 5 Sep 2023 12:26:19 +0200 Subject: [PATCH] Create simple set-env and get-env commands with persistence layer --- packages/internal/cli/src/commands/get-env.ts | 18 ++++++++++++++++++ packages/internal/cli/src/commands/set-env.ts | 13 +++++++++---- packages/internal/cli/src/config/index.ts | 8 +++----- 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 packages/internal/cli/src/commands/get-env.ts diff --git a/packages/internal/cli/src/commands/get-env.ts b/packages/internal/cli/src/commands/get-env.ts new file mode 100644 index 000000000..330596a25 --- /dev/null +++ b/packages/internal/cli/src/commands/get-env.ts @@ -0,0 +1,18 @@ +import { Command } from '@oclif/core'; + +import { getConfigStorage, getEnvStageKey } from '../config'; + +export default class GetEnv extends Command { + static description = 'Get currently selected ENV stage'; + + static examples = [`$ saas get-env`]; + + async run(): Promise { + await this.parse(GetEnv); + const storage = await getConfigStorage(); + const envStageKey = getEnvStageKey(); + const currentEnvStage = (await storage.getItem(envStageKey)) ?? 'none'; + + this.log(currentEnvStage); + } +} diff --git a/packages/internal/cli/src/commands/set-env.ts b/packages/internal/cli/src/commands/set-env.ts index af1cb361b..8418ae3c6 100644 --- a/packages/internal/cli/src/commands/set-env.ts +++ b/packages/internal/cli/src/commands/set-env.ts @@ -1,6 +1,6 @@ import { Args, Command } from '@oclif/core'; -import { ConfOptions, getConfigStorage } from '../config'; +import { getConfigStorage, getEnvStageKey } from '../config'; export default class SetEnv extends Command { static description = 'Select ENV stage'; @@ -16,10 +16,15 @@ export default class SetEnv extends Command { async run(): Promise { const { args, flags } = await this.parse(SetEnv); - const storage = await getConfigStorage(); - await storage.setItem(ConfOptions.EnvStage, args.envStage); + const envStageKey = getEnvStageKey(); + const currentEnvStage = await storage.getItem(envStageKey); - this.log(`hello ${args.envStage}`); + if (currentEnvStage === args.envStage) { + this.log(`Your environment stage is already set to ${args.envStage}.`); + return; + } + await storage.setItem(envStageKey, args.envStage); + this.log(`Switched environment stage to ${args.envStage}.`); } } diff --git a/packages/internal/cli/src/config/index.ts b/packages/internal/cli/src/config/index.ts index 8e76d5258..301427db5 100644 --- a/packages/internal/cli/src/config/index.ts +++ b/packages/internal/cli/src/config/index.ts @@ -1,13 +1,11 @@ import * as storage from 'node-persist'; import * as childProcess from 'child_process'; -import { promisify } from 'util'; -import { resolve } from 'path'; +import {promisify} from 'util'; +import {resolve} from 'path'; const exec = promisify(childProcess.exec); -export enum ConfOptions { - EnvStage = 'envStage', -} +export const getEnvStageKey = () => `${process.ppid}-envStage`; export const getConfigStorage = async () => { const { stdout } = await exec('pnpm root -w');