From b40adfdc609b66c5efbdef11fecdb2d5294acf32 Mon Sep 17 00:00:00 2001 From: Igor Soloydenko Date: Tue, 24 Sep 2024 16:40:40 -0700 Subject: [PATCH] feat: add solo-init --- ...health.ts => command-solo-check-health.ts} | 7 +-- src/commands/command-solo-init.ts | 43 +++++++++++++++++++ src/commands/index.ts | 4 +- src/commands/solorc.ts | 8 ++++ 4 files changed, 58 insertions(+), 4 deletions(-) rename src/commands/{command-check-health.ts => command-solo-check-health.ts} (90%) create mode 100644 src/commands/command-solo-init.ts create mode 100644 src/commands/solorc.ts diff --git a/src/commands/command-check-health.ts b/src/commands/command-solo-check-health.ts similarity index 90% rename from src/commands/command-check-health.ts rename to src/commands/command-solo-check-health.ts index 52c6a8f..e2f3d57 100644 --- a/src/commands/command-check-health.ts +++ b/src/commands/command-solo-check-health.ts @@ -2,10 +2,11 @@ import fs from 'fs'; import path from 'path'; import { createExecutionContext, parseCommonOptions } from '../cli'; import { CliCommandMetadata } from './cli-option'; +import { SoloRcConfig } from './solorc'; const commandName = `solo-check-health`; -async function checkHealthAsyncCommand(this: any, str: any, options: any) { +async function soloCheckHealthAsyncCommand(this: any, str: any, options: any) { const executionContext = createExecutionContext(parseCommonOptions(options)); const { logger } = executionContext; @@ -23,7 +24,7 @@ async function checkHealthAsyncCommand(this: any, str: any, options: any) { return; } - const config = JSON.parse(fs.readFileSync(cliConfigLocation, 'utf8')); + const config = JSON.parse(fs.readFileSync(cliConfigLocation, 'utf8')) as SoloRcConfig; const { ado } = config; if (!ado) { const message = 'ADO config (ado) not found in config.'; @@ -54,5 +55,5 @@ export const command: CliCommandMetadata = { description: `Self-check the health of the CLI tool. Validates the presence and the correctness of the config.`, options: {}, - impl: checkHealthAsyncCommand, + impl: soloCheckHealthAsyncCommand, } diff --git a/src/commands/command-solo-init.ts b/src/commands/command-solo-init.ts new file mode 100644 index 0000000..ae714fa --- /dev/null +++ b/src/commands/command-solo-init.ts @@ -0,0 +1,43 @@ +import fs from 'fs'; +import path from 'path'; +import { createExecutionContext, parseCommonOptions } from '../cli'; +import { CliCommandMetadata } from './cli-option'; +import { SoloRcConfig } from './solorc'; + +const commandName = `solo-init`; + +async function soloInitCommand(this: any, str: any, options: any) { + const executionContext = createExecutionContext(parseCommonOptions(options)); + const { logger } = executionContext; + + const homeDir = process.env.HOME || process.env.USERPROFILE; + if (!homeDir) { + const message = 'HOME or USERPROFILE environment variable not set.'; + logger.error(message); + return; + } + + const cliConfigLocation = path.join(homeDir, '.solorc.json'); + if (fs.existsSync(cliConfigLocation)) { + const message = `Config file ${cliConfigLocation} already exists.`; + logger.error(message); + return; + } + + const newConfig: SoloRcConfig = { + ado: { + login: '', + token: '', + org: '', + proj: '', + }, + }; + fs.writeFileSync(cliConfigLocation, JSON.stringify(newConfig, null, 2)); +}; + +export const command: CliCommandMetadata = { + name: commandName, + description: `Initialize CLI tool.`, + options: {}, + impl: soloInitCommand, +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 3125c30..703067d 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1,12 +1,13 @@ import { compareStrings, sort } from '../algos/sort'; import { command as analyze } from './command-analyze'; import { command as auditNpm } from './command-audit-npm'; -import { command as checkHealth } from './command-check-health'; import { command as csvStats } from './command-csv-stats'; import { command as gitRepoStats } from './command-git-repo-stats'; import { command as listDeploys } from './command-list-deploys'; import { command as listRemoteRepos } from './command-list-remote-repos'; import { command as runMany } from './command-run-many'; +import { command as checkHealth } from './command-solo-check-health'; +import { command as soloInit } from './command-solo-init'; export const allCommands = sort( [ @@ -18,6 +19,7 @@ export const allCommands = sort( listDeploys, listRemoteRepos, runMany, + soloInit, ], (a, b) => compareStrings(a.name, b.name), ); diff --git a/src/commands/solorc.ts b/src/commands/solorc.ts new file mode 100644 index 0000000..9e93bd1 --- /dev/null +++ b/src/commands/solorc.ts @@ -0,0 +1,8 @@ +export interface SoloRcConfig { + ado: { + login: string; + token: string; + org: string; + proj: string; + }; +}