createChannel.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { getInputJson } from '../../helpers/InputOutput'
  2. import { ChannelInputParameters } from '../../Types'
  3. import { metadataToBytes, channelMetadataFromInput } from '../../helpers/serialization'
  4. import { flags } from '@oclif/command'
  5. import { CreateInterface } from '@joystream/types'
  6. import { ChannelCreationParameters } from '@joystream/types/content'
  7. import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
  8. import UploadCommandBase from '../../base/UploadCommandBase'
  9. import ExitCodes from '../../ExitCodes'
  10. export default class CreateChannelCommand extends UploadCommandBase {
  11. static description = 'Create channel inside content directory.'
  12. static flags = {
  13. context: ContentDirectoryCommandBase.ownerContextFlag,
  14. input: flags.string({
  15. char: 'i',
  16. required: true,
  17. description: `Path to JSON file to use as input`,
  18. }),
  19. }
  20. async run() {
  21. let { context, input } = this.parse(CreateChannelCommand).flags
  22. // Context
  23. if (!context) {
  24. context = await this.promptForOwnerContext()
  25. }
  26. const account = await this.getRequiredSelectedAccount()
  27. const actor = await this.getActor(context)
  28. await this.requestAccountDecoding(account)
  29. const channelInput = await getInputJson<ChannelInputParameters>(input)
  30. const meta = channelMetadataFromInput(channelInput)
  31. const { coverPhotoPath, avatarPhotoPath } = channelInput
  32. if (!coverPhotoPath || !avatarPhotoPath) {
  33. // TODO: Handle with json schema validation?
  34. this.error('Invalid input! coverPhotoPath and avatarPhotoPath are required!', { exit: ExitCodes.InvalidInput })
  35. }
  36. const inputAssets = await this.prepareInputAssets([coverPhotoPath, avatarPhotoPath], input)
  37. const assets = inputAssets.map(({ parameters }) => ({ Upload: parameters }))
  38. // Set assets indexes in the metadata
  39. meta.setCoverPhoto(0)
  40. meta.setAvatarPhoto(1)
  41. const channelCreationParameters: CreateInterface<ChannelCreationParameters> = {
  42. assets,
  43. meta: metadataToBytes(meta),
  44. reward_account: channelInput.rewardAccount,
  45. }
  46. this.jsonPrettyPrint(JSON.stringify({ assets, metadata: meta.toObject() }))
  47. await this.requireConfirmation('Do you confirm the provided input?', true)
  48. await this.sendAndFollowNamedTx(account, 'content', 'createChannel', [actor, channelCreationParameters])
  49. await this.uploadAssets(inputAssets)
  50. }
  51. }