Browse Source

storage-node-v2: Add linter.

Shamil Gadelshin 3 years ago
parent
commit
37e8412915

+ 1 - 0
storage-node-v2/.eslintignore

@@ -1 +1,2 @@
 /lib
+.eslintrc.js

+ 0 - 6
storage-node-v2/.eslintrc

@@ -1,6 +0,0 @@
-{
-  "extends": [
-    "oclif",
-    "oclif-typescript"
-  ]
-}

+ 21 - 0
storage-node-v2/.eslintrc.js

@@ -0,0 +1,21 @@
+module.exports = {
+  env: {
+    mocha: true,
+  },
+  parserOptions: {
+    project: './tsconfig.json'
+  },
+  extends: [
+    // The oclif rules have some code-style/formatting rules which may conflict with
+    // our prettier global settings. Disabling for now
+    // I suggest to only add essential rules absolutely required to make the cli work with oclif
+    // at the end of this file.
+    // "oclif",
+    // "oclif-typescript",
+  ],
+  rules: {
+    'no-unused-vars': 'off', // Required by the typescript rule below
+    '@typescript-eslint/no-unused-vars': ['error'],
+    '@typescript-eslint/no-floating-promises': 'error',
+  },
+}

+ 2 - 1
storage-node-v2/package.json

@@ -71,7 +71,8 @@
     "test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
     "version": "oclif-dev readme && git add README.md",
     "build": "tsc --build tsconfig.json",
-    "format": "prettier ./src --write"
+    "format": "prettier ./src --write",
+    "lint": "eslint ./src --ext .ts"
   },
   "types": "lib/index.d.ts"
 }

+ 2 - 2
storage-node-v2/src/commands/wg/leader/create-bucket.ts

@@ -22,7 +22,7 @@ export default class WgLeaderCreateBucket extends Command {
     dev: flags.boolean({ char: 'd', description: 'Use development mode' }),
   }
 
-  async run() {
+  async run(): Promise<void> {
     const { flags } = this.parse(WgLeaderCreateBucket)
 
     const objectSize = flags.size ?? 0
@@ -43,7 +43,7 @@ export default class WgLeaderCreateBucket extends Command {
     )
   }
 
-  async finally(err: any) {
+  async finally(err: Error | undefined): Promise<void> {
     // called after run and catch regardless of whether or not the command errored
     // We'll force exit here, in case there is no error, to prevent console.log from hanging the process
     if (!err) this.exit(0)

+ 2 - 2
storage-node-v2/src/commands/wg/operator/accept-invitation.ts

@@ -21,7 +21,7 @@ export default class WgOperatorAcceptInvitation extends Command {
 
   static args = [{ name: 'file' }]
 
-  async run() {
+  async run(): Promise<void> {
     const { flags } = this.parse(WgOperatorAcceptInvitation)
 
     const worker = flags.worker ?? 0
@@ -35,7 +35,7 @@ export default class WgOperatorAcceptInvitation extends Command {
     await acceptStorageBucketInvitation(worker, bucket)
   }
 
-  async finally(err: any) {
+  async finally(err: Error | undefined): Promise<void> {
     // called after run and catch regardless of whether or not the command errored
     // We'll force exit here, in case there is no error, to prevent console.log from hanging the process
     if (!err) this.exit(0)

+ 19 - 17
storage-node-v2/src/services/api.ts

@@ -1,10 +1,11 @@
 import { ApiPromise, WsProvider } from '@polkadot/api'
-import { RegistryTypes } from '@polkadot/types/types'
+import {
+  RegistryTypes,
+  CodecArg,
+  ISubmittableResult,
+} from '@polkadot/types/types'
 import { types } from '@joystream/types/'
-import { Keyring } from '@polkadot/api'
-
 import { TypeRegistry } from '@polkadot/types'
-import { CodecArg } from '@polkadot/types/types'
 import { KeyringPair } from '@polkadot/keyring/types'
 import chalk from 'chalk'
 import { SubmittableExtrinsic } from '@polkadot/api/types'
@@ -12,15 +13,8 @@ import { DispatchError } from '@polkadot/types/interfaces/system'
 
 export class ExtrinsicFailedError extends Error {}
 
-export async function createApi(): Promise<ApiPromise> {
-  const wsProvider = new WsProvider('ws://localhost:9944')
-  let extendedTypes = createExtendedTypes(types)
-
-  return await ApiPromise.create({ provider: wsProvider, types: extendedTypes })
-}
-
-function createExtendedTypes(defaultTypes: RegistryTypes) {
-  let extendedTypes = types
+function createExtendedTypes(): RegistryTypes {
+  const extendedTypes = types
   extendedTypes.StorageBucketId = 'u64'
   extendedTypes.BagId = {}
   extendedTypes.UploadParameters = {}
@@ -39,11 +33,18 @@ function createExtendedTypes(defaultTypes: RegistryTypes) {
   return extendedTypes
 }
 
+export async function createApi(): Promise<ApiPromise> {
+  const wsProvider = new WsProvider('ws://localhost:9944')
+  const extendedTypes = createExtendedTypes()
+
+  return await ApiPromise.create({ provider: wsProvider, types: extendedTypes })
+}
+
 function sendExtrinsic(
   api: ApiPromise,
   account: KeyringPair,
   tx: SubmittableExtrinsic<'promise'>
-) {
+): Promise<ISubmittableResult> {
   return new Promise((resolve, reject) => {
     let unsubscribe: () => void
     tx.signAndSend(account, {}, (result) => {
@@ -64,9 +65,10 @@ function sendExtrinsic(
                 try {
                   // Need to assert that registry is of TypeRegistry type, since Registry intefrace
                   // seems outdated and doesn't include DispatchErrorModule as possible argument for "findMetaError"
-                  const { name, documentation } = (
-                    api.registry as TypeRegistry
-                  ).findMetaError(dispatchError.asModule)
+                  const typeRegistry = api.registry as TypeRegistry
+                  const { name, documentation } = typeRegistry.findMetaError(
+                    dispatchError.asModule
+                  )
                   errorMsg = `${name} (${documentation})`
                 } catch (e) {
                   // This probably means we don't have this error in the metadata

+ 6 - 9
storage-node-v2/src/services/extrinsics.ts

@@ -3,20 +3,17 @@ import { createApi, sendAndFollowNamedTx } from './api'
 
 export async function createStorageBucket(
   invitedWorker: number | null = null,
-  allowedNewBags: boolean = true,
-  sizeLimit: number = 0,
-  objectsLimit: number = 0
+  allowedNewBags = true,
+  sizeLimit = 0,
+  objectsLimit = 0
 ): Promise<void> {
   try {
-    let api = await createApi()
+    const api = await createApi()
 
     const keyring = new Keyring({ type: 'sr25519' })
     const alice = keyring.addFromUri('//Alice')
 
-    let invitedWorkerValue: any = api.createType(
-      'Option<WorkerId>',
-      invitedWorker
-    )
+    const invitedWorkerValue = api.createType('Option<WorkerId>', invitedWorker)
 
     await sendAndFollowNamedTx(api, alice, 'storage', 'createStorageBucket', [
       invitedWorkerValue,
@@ -34,7 +31,7 @@ export async function acceptStorageBucketInvitation(
   storageBucketId: number
 ): Promise<void> {
   try {
-    let api = await createApi()
+    const api = await createApi()
 
     const keyring = new Keyring({ type: 'sr25519' })
     const alice = keyring.addFromUri('//Alice')