From 56e91710424227076b83a1098d320bd805fa53d2 Mon Sep 17 00:00:00 2001 From: "Angel M. Adames" Date: Fri, 31 May 2024 16:20:36 -0400 Subject: [PATCH] chore: Add remove project config command --- src/commands/project/config.ts | 42 ------------------------- src/commands/project/config/generate.ts | 12 +++++++ src/commands/project/config/index.ts | 14 +++++++++ src/commands/project/config/remove.ts | 19 +++++++++++ src/commands/project/config/view.ts | 12 +++++++ src/commands/project/index.ts | 2 +- src/commands/project/remove.ts | 16 ---------- src/config/project.config.ts | 19 ++++++++++- src/utils/file-system.ts | 4 +-- 9 files changed, 78 insertions(+), 62 deletions(-) delete mode 100644 src/commands/project/config.ts create mode 100644 src/commands/project/config/generate.ts create mode 100644 src/commands/project/config/index.ts create mode 100644 src/commands/project/config/remove.ts create mode 100644 src/commands/project/config/view.ts delete mode 100644 src/commands/project/remove.ts diff --git a/src/commands/project/config.ts b/src/commands/project/config.ts deleted file mode 100644 index 2ee8ff6..0000000 --- a/src/commands/project/config.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Command } from 'commander' -import { projectConfig } from '../../config/project.config' -import logger from '../../utils/log' - -function viewProjectConfigCommand() { - return new Command() - .name('view') - .aliases(['get', 'show']) - .summary('Shows the current project config') - .action(() => { - console.log(projectConfig.get()) - }) -} - -function generateProjectConfigCommand() { - return new Command() - .name('generate') - .alias('create') - .summary('Generate a new deafult project config') - .action(() => { - projectConfig.save(projectConfig.default()) - }) -} - -function removeProjectConfigCommand() { - return new Command() - .name('remove') - .alias('delete') - .summary('Removes the config for the current project') -} - -export function projectConfigCommand() { - return new Command() - .name('config') - .alias('settings') - .summary('Manages the current project configuration') - .addCommand(viewProjectConfigCommand()) - .addCommand(generateProjectConfigCommand()) - .action(() => { - logger.info('Hey! from: "project config".') - }) -} diff --git a/src/commands/project/config/generate.ts b/src/commands/project/config/generate.ts new file mode 100644 index 0000000..12db5f0 --- /dev/null +++ b/src/commands/project/config/generate.ts @@ -0,0 +1,12 @@ +import { Command } from 'commander' +import { projectConfig } from '../../../config/project.config' + +export function generateProjectConfigCommand() { + return new Command() + .name('generate') + .alias('create') + .summary('Generate a new deafult project config') + .action(() => { + projectConfig.save(projectConfig.default()) + }) +} diff --git a/src/commands/project/config/index.ts b/src/commands/project/config/index.ts new file mode 100644 index 0000000..28c3d90 --- /dev/null +++ b/src/commands/project/config/index.ts @@ -0,0 +1,14 @@ +import { Command } from 'commander' +import { generateProjectConfigCommand } from './generate' +import { removeProjectCommand } from './remove' +import { viewProjectConfigCommand } from './view' + +export function projectConfigCommand() { + return new Command() + .name('config') + .alias('settings') + .summary('Manages the current project configuration') + .addCommand(viewProjectConfigCommand()) + .addCommand(generateProjectConfigCommand()) + .addCommand(removeProjectCommand()) +} diff --git a/src/commands/project/config/remove.ts b/src/commands/project/config/remove.ts new file mode 100644 index 0000000..b90e985 --- /dev/null +++ b/src/commands/project/config/remove.ts @@ -0,0 +1,19 @@ +import { confirm } from '@inquirer/prompts' +import { Command } from 'commander' +import { projectConfig } from '../../../config/project.config' + +export function removeProjectCommand() { + return new Command() + .name('remove') + .aliases(['destroy', 'delete']) + .summary('Removes a project from DEMS') + .argument('', 'Project name to be removed') + .action(async (project) => { + const deletionConfirmation = await confirm({ + message: `Do you want to remove project ${project}?`, + }) + if (deletionConfirmation) { + projectConfig.remove(project) + } + }) +} diff --git a/src/commands/project/config/view.ts b/src/commands/project/config/view.ts new file mode 100644 index 0000000..c56c081 --- /dev/null +++ b/src/commands/project/config/view.ts @@ -0,0 +1,12 @@ +import { Command } from 'commander' +import { projectConfig } from '../../../config/project.config' + +export function viewProjectConfigCommand() { + return new Command() + .name('view') + .aliases(['get', 'show']) + .summary('Shows the current project config') + .action(() => { + console.log(projectConfig.get()) + }) +} diff --git a/src/commands/project/index.ts b/src/commands/project/index.ts index bd11e4f..092b476 100644 --- a/src/commands/project/index.ts +++ b/src/commands/project/index.ts @@ -1,7 +1,7 @@ import { Command } from 'commander' import { activeProjectCommand } from './active' import { projectConfigCommand } from './config' -import { removeProjectCommand } from './remove' +import { removeProjectCommand } from './config/remove' import { setActiveProjectCommand } from './set' export function projectCommand() { diff --git a/src/commands/project/remove.ts b/src/commands/project/remove.ts deleted file mode 100644 index e505a71..0000000 --- a/src/commands/project/remove.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Command } from 'commander' -import { projectConfig } from '../../config/project.config' -import { deletePath } from '../../utils/file-system' -import logger from '../../utils/log' - -export function removeProjectCommand() { - return new Command() - .name('remove') - .aliases(['destroy', 'delete']) - .summary('Removes a project from DEMS') - .argument('', 'Project name to be removed') - .action((project) => { - logger.info(`Removing project: ${project}...`) - deletePath({ path: `${projectConfig.projectRootPath}` }) - }) -} diff --git a/src/config/project.config.ts b/src/config/project.config.ts index 4177364..12e59e3 100644 --- a/src/config/project.config.ts +++ b/src/config/project.config.ts @@ -1,7 +1,13 @@ import fs from 'node:fs' import { homedir } from 'node:os' import { join } from 'node:path' -import { createFile, createPath, isFile } from '../utils/file-system' +import { + createFile, + createPath, + deletePath, + isDirectory, + isFile, +} from '../utils/file-system' import logger from '../utils/log' import { CONFIG_PATH, cliConfig } from './cli.config' @@ -86,6 +92,17 @@ export const projectConfig = { }) }, + remove(project: string) { + const projectPath = join(CONFIG_PATH, project) + if (isDirectory(projectPath)) { + deletePath({ path: projectPath, force: true }) + return + } + + logger.warn(`Config path for '${project}' is not a valid directory.`) + logger.warn(`Project '${project}' is most likely not initialized.`) + }, + check(configFile = PROJECT_CONFIG_FILE) { if (isFile(configFile)) return logger.warn('Project config file does not exist or is not a valid file.') diff --git a/src/utils/file-system.ts b/src/utils/file-system.ts index 7afc58a..3e41935 100644 --- a/src/utils/file-system.ts +++ b/src/utils/file-system.ts @@ -8,11 +8,11 @@ import type { import logger from './log' export const isFile = (path: string) => { - return fs.lstatSync(path).isFile() + return fs.existsSync(path) && fs.lstatSync(path).isFile() } export const isDirectory = (path: string) => { - return fs.lstatSync(path).isDirectory() + return fs.existsSync(path) && fs.lstatSync(path).isDirectory() } export const copyFile = ({ source, target }: SourceTargetOperation) => {