Explorar el Código

Query node build: replace "sed" with custom postCodegen script

Leszek Wiesner hace 3 años
padre
commit
b4effd27e0
Se han modificado 2 ficheros con 22 adiciones y 8 borrados
  1. 2 8
      query-node/build.sh
  2. 20 0
      query-node/mappings/scripts/postCodegen.ts

+ 2 - 8
query-node/build.sh

@@ -16,14 +16,8 @@ yarn clean
 yarn codegen:noinstall
 yarn typegen # if this fails try to run this command outside of yarn workspaces
 
-# TS4 useUnknownInCatchVariables is not compatible with auto-generated code
-# i.bak is used for Mac compatibility
-sed -i.bak '/\"compilerOptions\"\:\ {/a \ \ \ \ "useUnknownInCatchVariables": false,' ./generated/graphql-server/tsconfig.json
-rm ./generated/graphql-server/tsconfig.json.bak
-# Type assertions are no longer needed for createTypeUnsafe in @polkadot/api 5.9.1 (and they break the build)
-# Here we're relpacing createTypeUnsafe<Assertions>(...params) to createTypeUnsafe(...params) in all generated types:
-find ./mappings/generated -type f -print0 | xargs -0 sed -i.bak -r 's/createTypeUnsafe<[^(]+[(]/createTypeUnsafe(/g'
-find ./mappings/generated -type f -iname "*.bak" -print0 | xargs -0 rm -f
+# Post-codegen - fixes in autogenerated files
+yarn ts-node --project ./mappings/tsconfig.json ./mappings/scripts/postCodegen.ts
 
 # We run yarn again to ensure graphql-server dependencies are installed
 # and are inline with root workspace resolutions

+ 20 - 0
query-node/mappings/scripts/postCodegen.ts

@@ -0,0 +1,20 @@
+// A script to be executed post hydra codegen, that may include modifications to autogenerated files
+import fs from 'fs'
+import path from 'path'
+
+// TS4 useUnknownInCatchVariables is not compatible with auto-generated code inside generated/graphql-server
+const serverTsConfigPath = path.resolve(__dirname, '../../generated/graphql-server/tsconfig.json')
+const serverTsConfig = JSON.parse(fs.readFileSync(serverTsConfigPath).toString())
+serverTsConfig.compilerOptions.useUnknownInCatchVariables = false
+fs.writeFileSync(serverTsConfigPath, JSON.stringify(serverTsConfig, undefined, 2))
+
+// Type assertions are no longer needed for createTypeUnsafe in @polkadot/api 5.9.1 (and they break the build)
+// Here we're relpacing createTypeUnsafe<Assertions>(...params) to createTypeUnsafe(...params) in all generated types:
+const generatedTypesPaths = path.resolve(__dirname, '../generated/types')
+fs.readdirSync(generatedTypesPaths).map((fileName) => {
+  if (path.extname(fileName) === '.ts') {
+    const filePath = path.join(generatedTypesPaths, fileName)
+    const fileContent = fs.readFileSync(filePath).toString()
+    fs.writeFileSync(filePath, fileContent.replace(/createTypeUnsafe<[^(]+[(]/g, 'createTypeUnsafe('))
+  }
+})