Browse Source

Fix npm pack, add types index files

Leszek Wiesner 4 years ago
parent
commit
d7c8099b89

+ 1 - 0
content-directory-schemas/.npmignore

@@ -0,0 +1 @@
+operations.json

+ 12 - 1
content-directory-schemas/scripts/schemasToTS.ts

@@ -10,15 +10,19 @@ const OUTPUT_TYPES_LOCATION = path.join(__dirname, '../types')
 const SUBDIRS_INCLUDED = ['extrinsics', 'entities'] as const
 
 async function main() {
+  // Create typescript files
   for (const subdirName of fs.readdirSync(SCHEMAS_LOCATION)) {
     if (!SUBDIRS_INCLUDED.includes(subdirName as any)) {
       console.log(`Subdir/filename not included: ${subdirName} - skipping...`)
       continue
     }
     const schemaSubdir = subdirName as typeof SUBDIRS_INCLUDED[number]
+    const indexExportedTypes: string[] = []
     for (const schemaFilename of fs.readdirSync(path.join(SCHEMAS_LOCATION, schemaSubdir))) {
       const schemaFilePath = path.join(SCHEMAS_LOCATION, schemaSubdir, schemaFilename)
-      const outputFilename = schemaFilename.replace('.schema.json', '.d.ts')
+      const mainTypeName = schemaFilename.replace('.schema.json', '')
+      const outputFilename = mainTypeName + '.d.ts'
+      indexExportedTypes.push(mainTypeName)
       const outputDir = path.join(OUTPUT_TYPES_LOCATION, schemaSubdir)
       if (!fs.existsSync(outputDir)) {
         fs.mkdirSync(outputDir)
@@ -37,6 +41,13 @@ async function main() {
         console.error(e)
       }
     }
+    // Generate main index.d.ts export file for entities
+    const indexFilePath = path.join(OUTPUT_TYPES_LOCATION, schemaSubdir, 'index.d.ts')
+    fs.writeFileSync(
+      indexFilePath,
+      indexExportedTypes.reduce((content, typeName) => (content += `export { ${typeName} } from './${typeName}'\n`), '')
+    )
+    console.log(`${indexFilePath} succesfully generated!`)
   }
 }