Skip to content

Commit

Permalink
chore: Add remove project config command
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed May 31, 2024
1 parent f5dc7ad commit 56e9171
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 62 deletions.
42 changes: 0 additions & 42 deletions src/commands/project/config.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/commands/project/config/generate.ts
Original file line number Diff line number Diff line change
@@ -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())
})
}
14 changes: 14 additions & 0 deletions src/commands/project/config/index.ts
Original file line number Diff line number Diff line change
@@ -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())
}
19 changes: 19 additions & 0 deletions src/commands/project/config/remove.ts
Original file line number Diff line number Diff line change
@@ -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>', '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)
}
})
}
12 changes: 12 additions & 0 deletions src/commands/project/config/view.ts
Original file line number Diff line number Diff line change
@@ -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())
})
}
2 changes: 1 addition & 1 deletion src/commands/project/index.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
16 changes: 0 additions & 16 deletions src/commands/project/remove.ts

This file was deleted.

19 changes: 18 additions & 1 deletion src/config/project.config.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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.')
Expand Down
4 changes: 2 additions & 2 deletions src/utils/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 56e9171

Please sign in to comment.