Jelajahi Sumber

CLI: fix eslint checks

iorveth 4 tahun lalu
induk
melakukan
6e11da959a

+ 21 - 2
cli/src/base/ContentDirectoryCommandBase.ts

@@ -3,13 +3,15 @@ import { WorkingGroups } from '../Types'
 import { Channel, CuratorGroup, CuratorGroupId, ContentActor } from '@joystream/types/content'
 import { Worker } from '@joystream/types/working-group'
 import { CLIError } from '@oclif/errors'
-import _ from 'lodash'
 import { RolesCommandBase } from './WorkingGroupsCommandBase'
 import { createType } from '@joystream/types'
 import { flags } from '@oclif/command'
 
 const CONTEXTS = ['Member', 'Curator', 'Lead'] as const
+const OWNER_CONTEXTS = ['Member', 'Curator'] as const
+
 type Context = typeof CONTEXTS[number]
+type OwnerContext = typeof OWNER_CONTEXTS[number]
 
 /**
  * Abstract base class for commands related to content directory
@@ -24,6 +26,13 @@ export default abstract class ContentDirectoryCommandBase extends RolesCommandBa
     options: [...CONTEXTS],
   })
 
+  static ownerContextFlag = flags.enum({
+    name: 'context',
+    required: false,
+    description: `Actor context to execute the command in (${OWNER_CONTEXTS.join('/')})`,
+    options: [...OWNER_CONTEXTS],
+  })
+
   async promptForContext(message = 'Choose in which context you wish to execute the command'): Promise<Context> {
     return this.simplePrompt({
       message,
@@ -32,6 +41,16 @@ export default abstract class ContentDirectoryCommandBase extends RolesCommandBa
     })
   }
 
+  async promptForOwnerContext(
+    message = 'Choose in which context you wish to execute the command'
+  ): Promise<OwnerContext> {
+    return this.simplePrompt({
+      message,
+      type: 'list',
+      choices: OWNER_CONTEXTS.map((c) => ({ name: c, value: c })),
+    })
+  }
+
   // Use when lead access is required in given command
   async requireLead(): Promise<void> {
     await this.getRequiredLead()
@@ -43,7 +62,7 @@ export default abstract class ContentDirectoryCommandBase extends RolesCommandBa
     const groups = await this.getApi().availableCuratorGroups()
     const availableGroupIds = groups
       .filter(
-        ([_, group]) =>
+        ([, group]) =>
           group.active.valueOf() && group.curators.toArray().some((curatorId) => curatorId.eq(curator.workerId))
       )
       .map(([id]) => id)

+ 11 - 11
cli/src/commands/content/channel.ts

@@ -13,29 +13,29 @@ export default class ChannelCommand extends ContentDirectoryCommandBase {
 
   async run() {
     const { channelId } = this.parse(ChannelCommand).args
-    const aChannel = await this.getApi().channelById(channelId)
-    if (aChannel) {
+    const channel = await this.getApi().channelById(channelId)
+    if (channel) {
       displayCollapsedRow({
         'ID': channelId.toString(),
-        'Owner': JSON.stringify(aChannel.owner.toJSON()),
-        'IsCensored': aChannel.is_censored.toString(),
-        'RewardAccount': aChannel.reward_account ? aChannel.reward_account.toString() : 'NONE',
+        'Owner': JSON.stringify(channel.owner.toJSON()),
+        'IsCensored': channel.is_censored.toString(),
+        'RewardAccount': channel.reward_account ? channel.reward_account.toString() : 'NONE',
       })
 
       displayHeader(`Media`)
 
       displayCollapsedRow({
-        'NumberOfVideos': aChannel.videos.length,
-        'NumberOfPlaylists': aChannel.playlists.length,
-        'NumberOfSeries': aChannel.series.length,
+        'NumberOfVideos': channel.videos.length,
+        'NumberOfPlaylists': channel.playlists.length,
+        'NumberOfSeries': channel.series.length,
       })
 
       displayHeader(`MediaData`)
 
       displayCollapsedRow({
-        'Videos': JSON.stringify(aChannel.videos.toJSON()),
-        'Playlists': JSON.stringify(aChannel.playlists.toJSON()),
-        'Series': JSON.stringify(aChannel.series.toJSON()),
+        'Videos': JSON.stringify(channel.videos.toJSON()),
+        'Playlists': JSON.stringify(channel.playlists.toJSON()),
+        'Series': JSON.stringify(channel.series.toJSON()),
       })
     } else {
       this.error(`Channel not found by channel id: "${channelId}"!`)

+ 13 - 15
cli/src/commands/content/createChannel.ts

@@ -1,20 +1,20 @@
 import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
-import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
+import { IOFlags, getInputJson } from '../../helpers/InputOutput'
 import { ChannelCreationParameters, ChannelCreationParametersInput } from '../../Types'
 import { channelMetadataFromInput } from '../../helpers/serialization'
 
 export default class CreateChannelCommand extends ContentDirectoryCommandBase {
   static description = 'Create channel inside content directory.'
   static flags = {
-    context: ContentDirectoryCommandBase.contextFlag,
-    ...IOFlags,
+    context: ContentDirectoryCommandBase.ownerContextFlag,
+    input: IOFlags.input,
   }
 
   async run() {
-    let { context, input, output } = this.parse(CreateChannelCommand).flags
+    let { context, input } = this.parse(CreateChannelCommand).flags
 
     if (!context) {
-      context = await this.promptForContext()
+      context = await this.promptForOwnerContext()
     }
 
     const currentAccount = await this.getRequiredSelectedAccount()
@@ -23,33 +23,31 @@ export default class CreateChannelCommand extends ContentDirectoryCommandBase {
     const actor = await this.getActor(context)
 
     if (input) {
-      let channelCreationParametersInput = await getInputJson<ChannelCreationParametersInput>(input)
+      const channelCreationParametersInput = await getInputJson<ChannelCreationParametersInput>(input)
 
       const api = await this.getOriginalApi()
 
       const meta = channelMetadataFromInput(api, channelCreationParametersInput)
 
-      let channelCreationParameters: ChannelCreationParameters = {
+      const channelCreationParameters: ChannelCreationParameters = {
         assets: channelCreationParametersInput.assets,
         meta,
         reward_account: channelCreationParametersInput.reward_account,
       }
 
-      this.jsonPrettyPrint(JSON.stringify(channelCreationParameters))
+      this.jsonPrettyPrint(JSON.stringify(channelCreationParametersInput))
+
+      this.log('Meta: ' + meta)
+
       const confirmed = await this.simplePrompt({ type: 'confirm', message: 'Do you confirm the provided input?' })
 
-      if (confirmed && channelCreationParametersInput) {
-        saveOutputJson(
-          output,
-          `${channelCreationParametersInput.meta.title}Channel.json`,
-          channelCreationParametersInput
-        )
+      if (confirmed) {
         this.log('Sending the extrinsic...')
 
         await this.sendAndFollowNamedTx(currentAccount, 'content', 'createChannel', [actor, channelCreationParameters])
       }
     } else {
-      this.log('Input invalid or was not provided...')
+      this.error('Input invalid or was not provided...')
     }
   }
 }

+ 2 - 2
cli/src/commands/content/createVideo.ts

@@ -33,13 +33,13 @@ export default class CreateVideoCommand extends ContentDirectoryCommandBase {
     const actor = await this.getActor(context)
 
     if (input) {
-      let videoCreationParametersInput = await getInputJson<VideoCreationParametersInput>(input)
+      const videoCreationParametersInput = await getInputJson<VideoCreationParametersInput>(input)
 
       const api = await this.getOriginalApi()
 
       const meta = videoMetadataFromInput(api, videoCreationParametersInput)
 
-      let videoCreationParameters: VideoCreationParameters = {
+      const videoCreationParameters: VideoCreationParameters = {
         assets: videoCreationParametersInput.assets,
         meta,
       }

+ 11 - 8
cli/src/commands/content/updateChannel.ts

@@ -1,5 +1,5 @@
 import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
-import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
+import { IOFlags, getInputJson } from '../../helpers/InputOutput'
 import { channelMetadataFromInput } from '../../helpers/serialization'
 import { ChannelUpdateParameters, ChannelUpdateParametersInput } from '../../Types'
 
@@ -7,7 +7,7 @@ export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
   static description = 'Update existing content directory channel.'
   static flags = {
     context: ContentDirectoryCommandBase.contextFlag,
-    ...IOFlags,
+    input: IOFlags.input,
   }
 
   static args = [
@@ -19,7 +19,7 @@ export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
   ]
 
   async run() {
-    let { context, input, output } = this.parse(UpdateChannelCommand).flags
+    let { context, input } = this.parse(UpdateChannelCommand).flags
 
     const { channelId } = this.parse(UpdateChannelCommand).args
 
@@ -33,23 +33,26 @@ export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
     const actor = await this.getActor(context)
 
     if (input) {
-      let channelUpdateParametersInput = await getInputJson<ChannelUpdateParametersInput>(input)
+      const channelUpdateParametersInput = await getInputJson<ChannelUpdateParametersInput>(input)
 
       const api = await this.getOriginalApi()
 
       const meta = channelMetadataFromInput(api, channelUpdateParametersInput)
 
-      let channelUpdateParameters: ChannelUpdateParameters = {
+      const channelUpdateParameters: ChannelUpdateParameters = {
         assets: channelUpdateParametersInput.assets,
         new_meta: meta,
         reward_account: channelUpdateParametersInput.reward_account,
       }
 
+      this.jsonPrettyPrint(JSON.stringify(channelUpdateParametersInput))
+
+      this.log('Meta: ' + meta)
+
       this.jsonPrettyPrint(JSON.stringify(channelUpdateParameters))
       const confirmed = await this.simplePrompt({ type: 'confirm', message: 'Do you confirm the provided input?' })
 
-      if (confirmed && channelUpdateParameters) {
-        saveOutputJson(output, `${channelUpdateParametersInput.meta.title}Channel.json`, channelUpdateParametersInput)
+      if (confirmed) {
         this.log('Sending the extrinsic...')
 
         await this.sendAndFollowNamedTx(currentAccount, 'content', 'updateChannel', [
@@ -59,7 +62,7 @@ export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
         ])
       }
     } else {
-      this.log('Input invalid or was not provided...')
+      this.error('Input invalid or was not provided...')
     }
   }
 }

+ 2 - 2
cli/src/commands/content/updateVideo.ts

@@ -33,13 +33,13 @@ export default class UpdateVideoCommand extends ContentDirectoryCommandBase {
     const actor = await this.getActor(context)
 
     if (input) {
-      let videoUpdateParametersInput = await getInputJson<VideoUpdateParametersInput>(input)
+      const videoUpdateParametersInput = await getInputJson<VideoUpdateParametersInput>(input)
 
       const api = await this.getOriginalApi()
 
       const meta = videoMetadataFromInput(api, videoUpdateParametersInput)
 
-      let videoUpdateParameters: VideoUpdateParameters = {
+      const videoUpdateParameters: VideoUpdateParameters = {
         assets: videoUpdateParametersInput.assets,
         meta,
       }

+ 17 - 18
cli/src/helpers/serialization.ts

@@ -14,30 +14,29 @@ import {
 import { ApiPromise } from '@polkadot/api'
 import { Bytes } from '@polkadot/types/primitive'
 
-export function BinaryToMeta(api: ApiPromise, serialized: Uint8Array): Bytes {
-  const metaRaw = api.createType('Raw', serialized)
-  return new Bytes(api.registry, metaRaw)
+export function binaryToMeta(api: ApiPromise, serialized: Uint8Array): Bytes {
+  return api.createType('Bytes', '0x' + Buffer.from(serialized).toString('hex'))
 }
 
 export function videoMetadataFromInput(
   api: ApiPromise,
   videoParametersInput: VideoCreationParametersInput | VideoUpdateParametersInput
 ): Bytes {
-  let mediaType = new MediaType()
-  mediaType.setCodecName(videoParametersInput.meta.mediaType?.codecName!)
-  mediaType.setContainer(videoParametersInput.meta.mediaType?.container!)
-  mediaType.setMimeMediaType(videoParametersInput.meta.mediaType?.mimeMediaType!)
+  const mediaType = new MediaType()
+  mediaType.setCodecName(videoParametersInput.meta.mediaType!.codecName!)
+  mediaType.setContainer(videoParametersInput.meta.mediaType!.container!)
+  mediaType.setMimeMediaType(videoParametersInput.meta.mediaType!.mimeMediaType!)
 
-  let license = new License()
-  license.setCode(videoParametersInput.meta.license?.code!)
-  license.setAttribution(videoParametersInput.meta.license?.attribution!)
-  license.setCustomText(videoParametersInput.meta.license?.customText!)
+  const license = new License()
+  license.setCode(videoParametersInput.meta.license!.code!)
+  license.setAttribution(videoParametersInput.meta.license!.attribution!)
+  license.setCustomText(videoParametersInput.meta.license!.customText!)
 
-  let publishedBeforeJoystream = new PublishedBeforeJoystream()
-  publishedBeforeJoystream.setIsPublished(videoParametersInput.meta.publishedBeforeJoystream?.isPublished!)
-  publishedBeforeJoystream.setDate(videoParametersInput.meta.publishedBeforeJoystream?.date!)
+  const publishedBeforeJoystream = new PublishedBeforeJoystream()
+  publishedBeforeJoystream.setIsPublished(videoParametersInput.meta.publishedBeforeJoystream!.isPublished!)
+  publishedBeforeJoystream.setDate(videoParametersInput.meta.publishedBeforeJoystream!.date!)
 
-  let videoMetadata = new VideoMetadata()
+  const videoMetadata = new VideoMetadata()
   videoMetadata.setTitle(videoParametersInput.meta.title!)
   videoMetadata.setDescription(videoParametersInput.meta.description!)
   videoMetadata.setVideo(videoParametersInput.meta.video!)
@@ -57,14 +56,14 @@ export function videoMetadataFromInput(
   videoMetadata.setPublishedBeforeJoystream(publishedBeforeJoystream)
 
   const serialized = videoMetadata.serializeBinary()
-  return BinaryToMeta(api, serialized)
+  return binaryToMeta(api, serialized)
 }
 
 export function channelMetadataFromInput(
   api: ApiPromise,
   channelParametersInput: ChannelCreationParametersInput | ChannelUpdateParametersInput
 ): Bytes {
-  let channelMetadata = new ChannelMetadata()
+  const channelMetadata = new ChannelMetadata()
   channelMetadata.setTitle(channelParametersInput.meta.title!)
   channelMetadata.setDescription(channelParametersInput.meta.description!)
   channelMetadata.setIsPublic(channelParametersInput.meta.isPublic!)
@@ -74,5 +73,5 @@ export function channelMetadataFromInput(
   channelMetadata.setCategory(channelParametersInput.meta.category!)
 
   const serialized = channelMetadata.serializeBinary()
-  return BinaryToMeta(api, serialized)
+  return binaryToMeta(api, serialized)
 }