inputs.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import path from 'path'
  2. import fs from 'fs'
  3. import { CreateClass, AddClassSchema } from '../../types/extrinsics'
  4. import { EntityBatch } from '../../types/EntityBatch'
  5. export const INPUTS_LOCATION = path.join(__dirname, '../../inputs')
  6. export const INPUT_TYPES = ['classes', 'schemas', 'entityBatches'] as const
  7. export type InputType = typeof INPUT_TYPES[number]
  8. export type FetchedInput<Schema = any> = { fileName: string; data: Schema }
  9. export const getInputsLocation = (inputType: InputType) => path.join(INPUTS_LOCATION, inputType)
  10. export function getInputs<Schema = any>(
  11. inputType: InputType,
  12. rootInputsLocation = INPUTS_LOCATION
  13. ): FetchedInput<Schema>[] {
  14. const inputs: FetchedInput<Schema>[] = []
  15. fs.readdirSync(path.join(rootInputsLocation, inputType)).forEach((fileName) => {
  16. const inputFilePath = path.join(rootInputsLocation, inputType, fileName)
  17. if (path.extname(inputFilePath) !== '.json') {
  18. return
  19. }
  20. const inputJson = fs.readFileSync(inputFilePath).toString()
  21. inputs.push({
  22. fileName,
  23. data: JSON.parse(inputJson) as Schema,
  24. })
  25. })
  26. return inputs
  27. }
  28. export function getInitializationInputs(rootInputsLocation = INPUTS_LOCATION) {
  29. return {
  30. // eslint-disable-next-line @typescript-eslint/no-var-requires
  31. classInputs: require('../../inputs/classes/index.js') as CreateClass[],
  32. schemaInputs: getInputs<AddClassSchema>('schemas').map(({ data }) => data),
  33. entityBatchInputs: getInputs<EntityBatch>('entityBatches').map(({ data }) => data),
  34. }
  35. }