schemasToTS.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { compileFromFile } from 'json-schema-to-typescript'
  4. // TODO: This will require publishing @joystream/prettier-config if we want to include it in joystream-js
  5. import prettierConfig from '@joystream/prettier-config'
  6. const SCHEMAS_LOCATION = path.join(__dirname, '../schemas')
  7. const OUTPUT_TYPES_LOCATION = path.join(__dirname, '../types')
  8. const SUBDIRS_INCLUDED = ['extrinsics', 'entities'] as const
  9. async function main() {
  10. for (const subdirName of fs.readdirSync(SCHEMAS_LOCATION)) {
  11. if (!SUBDIRS_INCLUDED.includes(subdirName as any)) {
  12. console.log(`Subdir/filename not included: ${subdirName} - skipping...`)
  13. continue
  14. }
  15. const schemaSubdir = subdirName as typeof SUBDIRS_INCLUDED[number]
  16. for (const schemaFilename of fs.readdirSync(path.join(SCHEMAS_LOCATION, schemaSubdir))) {
  17. const schemaFilePath = path.join(SCHEMAS_LOCATION, schemaSubdir, schemaFilename)
  18. const outputFilename = schemaFilename.replace('.schema.json', '.d.ts')
  19. const outputFilePath = path.join(OUTPUT_TYPES_LOCATION, schemaSubdir, outputFilename)
  20. try {
  21. await compileFromFile(schemaFilePath, {
  22. cwd: path.join(SCHEMAS_LOCATION, schemaSubdir),
  23. style: prettierConfig,
  24. }).then((ts) => {
  25. fs.writeFileSync(outputFilePath, ts)
  26. console.log(`${outputFilePath} succesfully generated!`)
  27. })
  28. } catch (e) {
  29. console.log(`${outputFilePath} compilation FAILED!`)
  30. console.error(e)
  31. }
  32. }
  33. }
  34. }
  35. main()
  36. .then(() => process.exit())
  37. .catch((e) => console.error(e))