addClassSchema.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
  2. import AddClassSchemaSchema from '@joystream/cd-schemas/schemas/extrinsics/AddClassSchema.schema.json'
  3. import { AddClassSchema } from '@joystream/cd-schemas/types/extrinsics/AddClassSchema'
  4. import { InputParser } from '@joystream/cd-schemas'
  5. import { JsonSchemaPrompter, JsonSchemaCustomPrompts } from '../../helpers/JsonSchemaPrompt'
  6. import { JSONSchema } from '@apidevtools/json-schema-ref-parser'
  7. import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
  8. import { Class } from '@joystream/types/content-directory'
  9. export default class AddClassSchemaCommand extends ContentDirectoryCommandBase {
  10. static description = 'Add a new schema to a class inside content directory. Requires lead access.'
  11. static flags = {
  12. ...IOFlags,
  13. }
  14. async run() {
  15. const account = await this.getRequiredSelectedAccount()
  16. await this.requireLead()
  17. await this.requestAccountDecoding(account)
  18. const { input, output } = this.parse(AddClassSchemaCommand).flags
  19. let inputJson = await getInputJson<AddClassSchema>(input)
  20. if (!inputJson) {
  21. let selectedClass: Class | undefined
  22. const customPrompts: JsonSchemaCustomPrompts = [
  23. [
  24. 'className',
  25. async () => {
  26. selectedClass = await this.promptForClass('Select a class to add schema to')
  27. return selectedClass.name.toString()
  28. },
  29. ],
  30. [
  31. 'existingProperties',
  32. async () => {
  33. const choices = selectedClass!.properties.map((p, i) => ({ name: `${i}: ${p.name.toString()}`, value: i }))
  34. if (!choices.length) {
  35. return []
  36. }
  37. return await this.simplePrompt({
  38. type: 'checkbox',
  39. message: 'Choose existing properties to keep',
  40. choices,
  41. })
  42. },
  43. ],
  44. [
  45. /^newProperties\[\d+\]\.property_type\.(Single|Vector\.vec_type)\.Reference/,
  46. async () => this.promptForClassReference(),
  47. ],
  48. [/^newProperties\[\d+\]\.property_type\.(Single|Vector\.vec_type)\.Text/, { message: 'Provide TextMaxLength' }],
  49. [
  50. /^newProperties\[\d+\]\.property_type\.(Single|Vector\.vec_type)\.Hash/,
  51. { message: 'Provide HashedTextMaxLength' },
  52. ],
  53. ]
  54. const prompter = new JsonSchemaPrompter<AddClassSchema>(
  55. AddClassSchemaSchema as JSONSchema,
  56. undefined,
  57. customPrompts
  58. )
  59. inputJson = await prompter.promptAll()
  60. }
  61. this.jsonPrettyPrint(JSON.stringify(inputJson))
  62. const confirmed = await this.simplePrompt({ type: 'confirm', message: 'Do you confirm the provided input?' })
  63. if (confirmed) {
  64. saveOutputJson(output, `${inputJson.className}Schema.json`, inputJson)
  65. const inputParser = new InputParser(this.getOriginalApi())
  66. this.log('Sending the extrinsic...')
  67. await this.sendAndFollowTx(account, await inputParser.parseAddClassSchemaExtrinsic(inputJson))
  68. }
  69. }
  70. }