generateAugmentCodec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Generates" augment files in /augment-codec based on already generated ones in /augment via @polkadot/typegen:
  3. * 1. Creates augment-codec/all.ts file that exports all @joystream/types separately
  4. * 2. Copies augment-* files from /augment to /augment-codes. Since those files import types from ./all,
  5. * the imports in /augment-codec will be overriden with our custom codec/class types
  6. */
  7. import path from 'path'
  8. import fs from 'fs'
  9. // Types by module:
  10. import common from '../common'
  11. import members from '../members'
  12. import council from '../council'
  13. import roles from '../roles'
  14. import forum from '../forum'
  15. import stake from '../stake'
  16. import mint from '../mint'
  17. import recurringRewards from '../recurring-rewards'
  18. import hiring from '../hiring'
  19. import workingGroup from '../working-group'
  20. import storage from '../storage'
  21. import proposals from '../proposals'
  22. import content from '../content'
  23. import legacy from '../legacy'
  24. const AUGMENT_INTERFACES_PATH = path.join(__dirname, '../../augment')
  25. const AUGMENT_CODEC_PATH = path.join(__dirname, '../../augment-codec')
  26. const EXPORT_ALL_TYPES_FILE_PATH = path.join(AUGMENT_CODEC_PATH, 'all.ts')
  27. const RELATIVE_TYPES_ROOT_PATH = '..' // @joystream/types/index path relative to AUGMENT_CODEC_PATH
  28. const typesByModule = {
  29. 'legacy': legacy,
  30. 'common': common,
  31. 'members': members,
  32. 'council': council,
  33. 'roles': roles,
  34. 'forum': forum,
  35. 'stake': stake,
  36. 'mint': mint,
  37. 'recurring-rewards': recurringRewards,
  38. 'hiring': hiring,
  39. 'working-group': workingGroup,
  40. 'storage': storage,
  41. 'proposals': proposals,
  42. 'content': content,
  43. }
  44. type Imports = { [moduleName: string]: string[] }
  45. function generateExportAllFile(filePath: string) {
  46. const imports: Imports = {}
  47. const exports: string[] = []
  48. Object.entries(typesByModule).forEach(([moduleName, types]) => {
  49. Object.entries(types).forEach(([typeName, codecOrName]) => {
  50. if (typeof codecOrName === 'function') {
  51. const constructorName = codecOrName.name
  52. if (!constructorName) {
  53. throw new Error(`Codec constructor doesn't have a name: ${typeName}`)
  54. }
  55. const normalizedTypeName = typeName.replace(/[^A-Za-z0-9_]/g, '_')
  56. // Add "as" if necessary
  57. const importStatement =
  58. constructorName === normalizedTypeName ? normalizedTypeName : `${constructorName} as ${normalizedTypeName}`
  59. !imports[moduleName] ? (imports[moduleName] = [importStatement]) : imports[moduleName].push(importStatement)
  60. exports.push(normalizedTypeName)
  61. } else {
  62. throw new Error(
  63. 'All types exposed to registry by a module should have a corresponding class!\n' +
  64. `Class not found for type: ${typeName} in module ${moduleName}`
  65. )
  66. }
  67. })
  68. })
  69. const fileLines: string[] = []
  70. fileLines.push('// This file was automatically generated via generate:augment-codec')
  71. for (const [module, importStatements] of Object.entries(imports)) {
  72. fileLines.push(`import { ${importStatements.join(', ')} } from '${RELATIVE_TYPES_ROOT_PATH}/${module}';`)
  73. }
  74. fileLines.push('')
  75. fileLines.push(`export { ${exports.join(', ')} };`)
  76. fs.writeFileSync(filePath, fileLines.join('\n'))
  77. }
  78. // ACTUAL SCRIPT:
  79. // Generate /augment-codec/all.ts file exporting all types ("augment-*" files will import from it)
  80. generateExportAllFile(EXPORT_ALL_TYPES_FILE_PATH)
  81. console.log(`Generated all types export file in ${EXPORT_ALL_TYPES_FILE_PATH}\n`)
  82. // Copy augment-* files from /augment to /augment-codec
  83. let copiedFilesCounter = 0
  84. fs.readdirSync(AUGMENT_INTERFACES_PATH).forEach((fileName) => {
  85. if (fileName.startsWith('augment-')) {
  86. const src = path.join(AUGMENT_INTERFACES_PATH, fileName)
  87. const dest = path.join(AUGMENT_CODEC_PATH, fileName)
  88. // Copy the file
  89. fs.copyFileSync(src, dest)
  90. console.log(`Copied file!\nFrom: ${src}\nTo: ${dest}\n`)
  91. ++copiedFilesCounter
  92. }
  93. })
  94. if (!copiedFilesCounter) {
  95. console.log('No files were copied! Did you forget to run generate:augment first?')
  96. }