createClass.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
  2. import CreateClassSchema from 'cd-schemas/schemas/extrinsics/CreateClass.schema.json'
  3. import { CreateClass } from 'cd-schemas/types/extrinsics/CreateClass'
  4. import { InputParser } from '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. export default class CreateClassCommand extends ContentDirectoryCommandBase {
  9. static description = 'Create class inside content directory. Requires lead access.'
  10. static flags = {
  11. ...IOFlags,
  12. }
  13. async run() {
  14. const account = await this.getRequiredSelectedAccount()
  15. await this.requireLead()
  16. await this.requestAccountDecoding(account)
  17. const { input, output } = this.parse(CreateClassCommand).flags
  18. const existingClassnames = (await this.getApi().availableClasses()).map(([, aClass]) => aClass.name.toString())
  19. let inputJson = await getInputJson<CreateClass>(input, CreateClassSchema as JSONSchema)
  20. if (!inputJson) {
  21. const customPrompts: JsonSchemaCustomPrompts<CreateClass> = [
  22. [
  23. 'name',
  24. {
  25. validate: (className) => existingClassnames.includes(className) && 'A class with this name already exists!',
  26. },
  27. ],
  28. ['class_permissions.maintainers', () => this.promptForCuratorGroups('Select class maintainers')],
  29. ]
  30. const prompter = new JsonSchemaPrompter<CreateClass>(CreateClassSchema as JSONSchema, undefined, customPrompts)
  31. inputJson = await prompter.promptAll()
  32. }
  33. this.jsonPrettyPrint(JSON.stringify(inputJson))
  34. const confirmed = await this.simplePrompt({ type: 'confirm', message: 'Do you confirm the provided input?' })
  35. if (confirmed) {
  36. saveOutputJson(output, `${inputJson.name}Class.json`, inputJson)
  37. this.log('Sending the extrinsic...')
  38. const inputParser = new InputParser(this.getOriginalApi())
  39. await this.sendAndFollowTx(account, inputParser.parseCreateClassExtrinsic(inputJson))
  40. }
  41. }
  42. }