createChannelCategory.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
  2. import { getInputJson } from '../../helpers/InputOutput'
  3. import { ChannelCategoryInputParameters } from '../../Types'
  4. import { asValidatedMetadata, metadataToBytes } from '../../helpers/serialization'
  5. import { flags } from '@oclif/command'
  6. import { CreateInterface } from '@joystream/types'
  7. import { ChannelCategoryCreationParameters, ChannelCategoryId } from '@joystream/types/content'
  8. import { ChannelCategoryInputSchema } from '../../schemas/ContentDirectory'
  9. import chalk from 'chalk'
  10. import { ChannelCategoryMetadata } from '@joystream/metadata-protobuf'
  11. export default class CreateChannelCategoryCommand extends ContentDirectoryCommandBase {
  12. static description = 'Create channel category inside content directory.'
  13. static flags = {
  14. context: ContentDirectoryCommandBase.categoriesContextFlag,
  15. input: flags.string({
  16. char: 'i',
  17. required: true,
  18. description: `Path to JSON file to use as input`,
  19. }),
  20. ...ContentDirectoryCommandBase.flags,
  21. }
  22. async run(): Promise<void> {
  23. const { context, input } = this.parse(CreateChannelCategoryCommand).flags
  24. const [actor, address] = context ? await this.getContentActor(context) : await this.getCategoryManagementActor()
  25. const channelCategoryInput = await getInputJson<ChannelCategoryInputParameters>(input, ChannelCategoryInputSchema)
  26. const meta = asValidatedMetadata(ChannelCategoryMetadata, channelCategoryInput)
  27. const channelCategoryCreationParameters: CreateInterface<ChannelCategoryCreationParameters> = {
  28. meta: metadataToBytes(ChannelCategoryMetadata, meta),
  29. }
  30. this.jsonPrettyPrint(JSON.stringify(channelCategoryInput))
  31. await this.requireConfirmation('Do you confirm the provided input?', true)
  32. const result = await this.sendAndFollowNamedTx(
  33. await this.getDecodedPair(address),
  34. 'content',
  35. 'createChannelCategory',
  36. [actor, channelCategoryCreationParameters]
  37. )
  38. if (result) {
  39. const categoryId: ChannelCategoryId = this.getEvent(result, 'content', 'ChannelCategoryCreated').data[0]
  40. this.log(chalk.green(`ChannelCategory with id ${chalk.cyanBright(categoryId.toString())} successfully created!`))
  41. }
  42. }
  43. }