Skip to content

Commit

Permalink
feat: add solo-init
Browse files Browse the repository at this point in the history
  • Loading branch information
another-guy committed Sep 24, 2024
1 parent 77e2f8f commit b40adfd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.';
Expand Down Expand Up @@ -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,
}
43 changes: 43 additions & 0 deletions src/commands/command-solo-init.ts
Original file line number Diff line number Diff line change
@@ -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: '<your-email-login-here>',
token: '<your-personal-access-token-here>',
org: '<OPTIONAL-your-organization-here>',
proj: '<OPTIONAL-your-project-here>',
},
};
fs.writeFileSync(cliConfigLocation, JSON.stringify(newConfig, null, 2));
};

export const command: CliCommandMetadata = {
name: commandName,
description: `Initialize CLI tool.`,
options: {},
impl: soloInitCommand,
}
4 changes: 3 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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(
[
Expand All @@ -18,6 +19,7 @@ export const allCommands = sort(
listDeploys,
listRemoteRepos,
runMany,
soloInit,
],
(a, b) => compareStrings(a.name, b.name),
);
8 changes: 8 additions & 0 deletions src/commands/solorc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface SoloRcConfig {
ado: {
login: string;
token: string;
org: string;
proj: string;
};
}

0 comments on commit b40adfd

Please sign in to comment.