createChannel.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
  2. import ChannelEntitySchema from 'cd-schemas/schemas/entities/ChannelEntity.schema.json'
  3. import { ChannelEntity } from 'cd-schemas/types/entities/ChannelEntity'
  4. import { InputParser } from 'cd-schemas'
  5. import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
  6. import { JSONSchema } from '@apidevtools/json-schema-ref-parser'
  7. import { JsonSchemaCustomPrompts, JsonSchemaPrompter } from '../../helpers/JsonSchemaPrompt'
  8. export default class CreateChannelCommand extends ContentDirectoryCommandBase {
  9. static description = 'Create a new channel on Joystream (requires a membership).'
  10. static flags = {
  11. ...IOFlags,
  12. }
  13. async run() {
  14. const account = await this.getRequiredSelectedAccount()
  15. const memberId = await this.getRequiredMemberId()
  16. const actor = { Member: memberId }
  17. await this.requestAccountDecoding(account)
  18. const channelJsonSchema = (ChannelEntitySchema as unknown) as JSONSchema
  19. const { input, output } = this.parse(CreateChannelCommand).flags
  20. let inputJson = await getInputJson<ChannelEntity>(input, channelJsonSchema)
  21. if (!inputJson) {
  22. const customPrompts: JsonSchemaCustomPrompts = [
  23. ['language', () => this.promptForEntityId('Choose channel language', 'Language', 'name')],
  24. ['isCensored', async () => undefined],
  25. ]
  26. const prompter = new JsonSchemaPrompter<ChannelEntity>(channelJsonSchema, undefined, customPrompts)
  27. inputJson = await prompter.promptAll()
  28. }
  29. this.jsonPrettyPrint(JSON.stringify(inputJson))
  30. const confirmed = await this.simplePrompt({ type: 'confirm', message: 'Do you confirm the provided input?' })
  31. if (confirmed) {
  32. saveOutputJson(output, `${inputJson.title}Channel.json`, inputJson)
  33. const inputParser = InputParser.createWithKnownSchemas(this.getOriginalApi(), [
  34. {
  35. className: 'Channel',
  36. entries: [inputJson],
  37. },
  38. ])
  39. const operations = await inputParser.getEntityBatchOperations()
  40. await this.sendAndFollowNamedTx(account, 'contentDirectory', 'transaction', [actor, operations])
  41. }
  42. }
  43. }