Skip to content

Commit

Permalink
Create simple set-env and get-env commands with persistence layer
Browse files Browse the repository at this point in the history
  • Loading branch information
pziemkowski committed Sep 5, 2023
1 parent 70b44c4 commit d5a7420
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
18 changes: 18 additions & 0 deletions packages/internal/cli/src/commands/get-env.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await this.parse(GetEnv);
const storage = await getConfigStorage();
const envStageKey = getEnvStageKey();
const currentEnvStage = (await storage.getItem(envStageKey)) ?? 'none';

this.log(currentEnvStage);
}
}
13 changes: 9 additions & 4 deletions packages/internal/cli/src/commands/set-env.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,10 +16,15 @@ export default class SetEnv extends Command {

async run(): Promise<void> {
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}.`);
}
}
8 changes: 3 additions & 5 deletions packages/internal/cli/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down

0 comments on commit d5a7420

Please sign in to comment.