diff --git a/apps/cli/package.json b/apps/cli/package.json index dfc859ca..fa9d1990 100644 --- a/apps/cli/package.json +++ b/apps/cli/package.json @@ -2,13 +2,13 @@ "name": "cli", "version": "1.0.0", "description": "CLI for keyshade", - "main": "index.ts", + "main": "dist/src/index.js", "private": false, "type": "module", "scripts": { "build": "tsc && tsc-alias", "start": "node dist/src/index.js", - "dev": "pnpm build && node dist/index.js", + "dev": "pnpm build && node dist/src/index.js", "lint": "eslint \"src/**/*.ts\" --fix" }, "keywords": [], diff --git a/apps/cli/src/commands/base.command.ts b/apps/cli/src/commands/base.command.ts index 9863e4fb..1e98590c 100644 --- a/apps/cli/src/commands/base.command.ts +++ b/apps/cli/src/commands/base.command.ts @@ -6,7 +6,7 @@ import type { import { fetchProfileConfig } from '@/util/configuration' import { Logger } from '@/util/logger' import { getDefaultProfile } from '@/util/profile' -import type { Command } from 'commander' +import { Option, type Command } from 'commander' import ControllerInstance from '@/util/controller-instance' /** @@ -55,13 +55,18 @@ export default abstract class BaseCommand { } }) - this.getOptions().forEach((option) => - command.option( + this.getOptions().forEach((option) => { + const newOption: Option = new Option( `${option.short}, ${option.long}`, - option.description, - option.defaultValue - ) - ) + option.description + ).default(option.defaultValue) + + option.choices && + option.choices.length > 0 && + newOption.choices(option.choices) + + command.addOption(newOption) + }) this.getArguments().forEach((argument) => command.argument(argument.name, argument.description) ) diff --git a/apps/cli/src/commands/project/create.project.ts b/apps/cli/src/commands/project/create.project.ts index 269958c9..d8419ac1 100644 --- a/apps/cli/src/commands/project/create.project.ts +++ b/apps/cli/src/commands/project/create.project.ts @@ -1,4 +1,12 @@ +import type { + CommandActionData, + CommandArgument, + CommandOption +} from '@/types/command/command.types' import BaseCommand from '../base.command' +import { text } from '@clack/prompts' +import ControllerInstance from '@/util/controller-instance' +import { Logger } from '@/util/logger' export default class CreateProject extends BaseCommand { getName(): string { @@ -8,4 +16,88 @@ export default class CreateProject extends BaseCommand { getDescription(): string { return 'Creates a project' } + + getArguments(): CommandArgument[] { + return [ + { + name: '', + description: + 'Slug of the workspace under which you want to create the project' + } + ] + } + + getOptions(): CommandOption[] { + return [ + { + short: '-n', + long: '--name ', + description: 'Name of the project' + }, + { + short: '-d', + long: '--description ', + description: 'Description of the project. Defaults to project name' + }, + { + short: '-k', + long: '--store-private-key', + description: 'Store the private key in the project. Defaults to true', + defaultValue: true + }, + { + short: '-a', + long: '--access-level ', + description: 'Access level of the project. Defaults to PRIVATE.', + defaultValue: 'PRIVATE', + choices: ['GLOBAL', 'PRIVATE', 'INTERNAL'] + } + ] + } + + async action({ args, options }: CommandActionData): Promise { + const [workspaceSlug] = args + + const parsedData = await this.parseOptions(options) + + const { data, error, success } = + await ControllerInstance.getInstance().projectController.createProject( + { + workspaceSlug, + ...parsedData + }, + this.headers + ) + + if (success) { + Logger.info(`Project ${data.name} (${data.slug}) created successfully!`) + Logger.info(`Created at ${data.createdAt}`) + Logger.info(`Updated at ${data.updatedAt}`) + } else { + Logger.error(`Failed to create project: ${error.message}`) + } + } + + async parseOptions(options: CommandActionData['options']): Promise<{ + name: string + description?: string + storePrivateKey: boolean + accessLevel: string + }> { + let { name, description } = options + const { storePrivateKey, accessLevel } = options + + if (!name) { + name = await text({ + message: 'Enter the name of the Project', + placeholder: 'My Project' + }) + } + + if (!description) { + description = name + } + + return { name, description, storePrivateKey, accessLevel } + } } diff --git a/apps/cli/src/commands/project/update.project.ts b/apps/cli/src/commands/project/update.project.ts index 1a90b999..352b6916 100644 --- a/apps/cli/src/commands/project/update.project.ts +++ b/apps/cli/src/commands/project/update.project.ts @@ -1,4 +1,11 @@ +import type { + CommandActionData, + CommandArgument, + CommandOption +} from '@/types/command/command.types' import BaseCommand from '../base.command' +import ControllerInstance from '@/util/controller-instance' +import { Logger } from '@/util/logger' export default class UpdateProject extends BaseCommand { getName(): string { @@ -8,4 +15,62 @@ export default class UpdateProject extends BaseCommand { getDescription(): string { return 'Updates a project' } + + getArguments(): CommandArgument[] { + return [ + { + name: '', + description: 'Slug of the project that you want to update' + } + ] + } + + getOptions(): CommandOption[] { + return [ + { + short: '-n', + long: '--name ', + description: 'Name of the project' + }, + { + short: '-d', + long: '--description ', + description: 'Description of the project. Defaults to project name' + }, + { + short: '-k', + long: '--store-private-key', + description: 'Store the private key in the project. Defaults to true', + defaultValue: true + }, + { + short: '-a', + long: '--access-level ', + description: 'Access level of the project. Defaults to PRIVATE.', + defaultValue: 'PRIVATE', + choices: ['GLOBAL', 'PRIVATE', 'INTERNAL'] + } + ] + } + + async action({ args, options }: CommandActionData): Promise { + const [projectSlug] = args + + const { data, error, success } = + await ControllerInstance.getInstance().projectController.updateProject( + { + projectSlug, + ...options + }, + this.headers + ) + + if (success) { + Logger.info(`Project ${data.name} (${data.slug}) updated successfully!`) + Logger.info(`Created at ${data.createdAt}`) + Logger.info(`Updated at ${data.updatedAt}`) + } else { + Logger.error(`Failed to update project: ${error.message}`) + } + } } diff --git a/apps/cli/src/types/command/command.types.d.ts b/apps/cli/src/types/command/command.types.d.ts index c42ff378..b6b89afd 100644 --- a/apps/cli/src/types/command/command.types.d.ts +++ b/apps/cli/src/types/command/command.types.d.ts @@ -3,6 +3,7 @@ export interface CommandOption { long: string description: string defaultValue?: string | boolean + choices?: string[] } export interface CommandArgument {