import { ICli, ICliRoutes } from '../types'; import { ProjectsCli } from "../cli"; import { CollectionService, InquirerService } from '../services'; class CollectionCli implements ICli { public routes: ICliRoutes[] = []; constructor() { this.routes.push({ command: 'collections', description: 'List, create collection', subCommands: [ { command: 'list', description: 'List all the collection', handler: this.listCollections, options: [ { flags: '-p, --project_id ', }, { flags: '-i, --interactive', } ] }, { command: 'create', description: 'Create collection', handler: this.createCollection, options: [ { flags: '-p, --project_id ', }, { flags: '-c, --collection_name ', }, { flags: '-i, --interactive', } ] } ] }); } public listCollections = async (options) => { if (options.interactive) { return this.listCollectionInteractive(); } if (!options.project_id) { options.project_id = await ProjectsCli.selectProjectInteractive(); } await this.loadCollections(options.project_id); } public createCollection = async (options) => { if (options.interactive) { return this.createCollectionInteractive(); } if (!options.collection_name) { options.collection_name = await InquirerService.takeInput('Collection name : '); } if (!options.project_id) { options.project_id = await ProjectsCli.selectProjectInteractive(); } const response = await CollectionService.createCollection({ name: options.collection_name, projectId: options.project_id }); console.log(response); } public async createCollectionInteractive() { const projectId = await ProjectsCli.selectProjectInteractive(); const collectionName = await InquirerService.takeInput('Collection name : '); const response = await CollectionService.createCollection({ name: collectionName, projectId }); console.log(response); } private listCollectionInteractive = async () => { const projectId = await ProjectsCli.selectProjectInteractive(); await this.loadCollections(projectId); } private async loadCollections(projectId: string) { const collections = await CollectionService.listCollections(projectId); if (!collections.length) { console.log(`Oops.. you don\'t have any collections.\n`); return; } console.log(collections); } } export default new CollectionCli();