Browse Source

addClassSchema - existing properties multichoice

Leszek Wiesner 4 years ago
parent
commit
6e8883acbc

+ 7 - 6
cli/src/base/ContentDirectoryCommandBase.ts

@@ -3,6 +3,7 @@ import AccountsCommandBase from './AccountsCommandBase'
 import { WorkingGroups, NamedKeyringPair } from '../Types'
 import { ReferenceProperty } from 'cd-schemas/types/extrinsics/AddClassSchema'
 import { BOOL_PROMPT_OPTIONS } from '../helpers/JsonSchemaPrompt'
+import { Class } from '@joystream/types/content-directory'
 
 /**
  * Abstract base class for commands related to working groups
@@ -18,13 +19,13 @@ export default abstract class ContentDirectoryCommandBase extends AccountsComman
     }
   }
 
-  async promptForClassName(message = 'Select a class'): Promise<string> {
+  async promptForClass(message = 'Select a class'): Promise<Class> {
     const classes = await this.getApi().availableClasses()
-    const choices = classes.map(([, c]) => ({ name: c.name.toString(), value: c.name.toString() }))
+    const choices = classes.map(([, c]) => ({ name: c.name.toString(), value: c }))
 
-    const selected = await this.simplePrompt({ message, type: 'list', choices })
+    const selectedClass = await this.simplePrompt({ message, type: 'list', choices })
 
-    return selected
+    return selectedClass
   }
 
   async promptForCuratorGroups(message = 'Select a curator group'): Promise<number> {
@@ -40,8 +41,8 @@ export default abstract class ContentDirectoryCommandBase extends AccountsComman
   }
 
   async promptForClassReference(): Promise<ReferenceProperty['Reference']> {
-    const className = await this.promptForClassName()
+    const selectedClass = await this.promptForClass()
     const sameOwner = await this.simplePrompt({ message: 'Same owner required?', ...BOOL_PROMPT_OPTIONS })
-    return { className, sameOwner }
+    return { className: selectedClass.name.toString(), sameOwner }
   }
 }

+ 18 - 1
cli/src/commands/content-directory/addClassSchema.ts

@@ -5,6 +5,7 @@ import { InputParser } from 'cd-schemas/scripts/helpers/InputParser'
 import { JsonSchemaPrompter, JsonSchemaCustomPrompts } from '../../helpers/JsonSchemaPrompt'
 import { JSONSchema } from '@apidevtools/json-schema-ref-parser'
 import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
+import { Class } from '@joystream/types/content-directory'
 
 export default class AddClassSchemaCommand extends ContentDirectoryCommandBase {
   static description = 'Add a new schema to a class inside content directory. Requires lead access.'
@@ -21,8 +22,24 @@ export default class AddClassSchemaCommand extends ContentDirectoryCommandBase {
 
     let inputJson = getInputJson<AddClassSchema>(input)
     if (!inputJson) {
+      let selectedClass: Class | undefined
       const customPrompts: JsonSchemaCustomPrompts = [
-        ['className', async () => this.promptForClassName('Select a class to add schema to')],
+        [
+          'className',
+          async () => {
+            selectedClass = await this.promptForClass('Select a class to add schema to')
+            return selectedClass.name.toString()
+          },
+        ],
+        [
+          'existingProperties',
+          async () =>
+            this.simplePrompt({
+              type: 'checkbox',
+              message: 'Choose existing properties to keep',
+              choices: selectedClass!.properties.map((p, i) => ({ name: `${i}: ${p.name.toString()}`, value: i })),
+            }),
+        ],
         [/^newProperties\[\d+\]\.property_type\.Single\.Reference/, async () => this.promptForClassReference()],
       ]