From a0f34e3076175a5ef2ad18e0b028210a498ff857 Mon Sep 17 00:00:00 2001 From: Etienne Moureton Date: Tue, 4 Apr 2023 11:12:36 +0200 Subject: [PATCH] full 1st lvl support --- dist/main.js | 34 +++++++++++++++++++++++++++++++--- src/main.ts | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/dist/main.js b/dist/main.js index 1f50c43..22e0a40 100644 --- a/dist/main.js +++ b/dist/main.js @@ -1,4 +1,4 @@ -import { create, emit, DeclarationFlags } from 'dts-dom'; +import { create, emit, DeclarationFlags, type } from 'dts-dom'; import { createWriteStream, existsSync, mkdirSync } from 'fs'; import findType from './utils/findType.js'; import { databasesClient } from './utils/firebase.js'; @@ -28,12 +28,40 @@ const fetchNewTypes = async ({ outDir = './types' } = {}) => { const docData = (await doc.get()).data(); for (const key in docData) { const value = docData[key]; - result.set(key, value); + if (!result.has(key)) { + result.set(key, [findType(value) + ]); + } + else { + const r = result.get(key); + const t = findType(value); + if (!JSON.stringify(r).includes(JSON.stringify(t))) { + result.set(key, r.concat([t])); + } + } } } result.forEach((value, key) => { // Push attribute to interface - intf.members.push(create.property(key, findType(value))); + if (value.length === 1) { + let t = undefined; + if (value[0].kind) { + t = type.array(type[value[0].type]); + } + else { + t = type[value[0]]; + } + intf.members.push(create.property(key, t)); + } + else { + const unionArray = value.map(v => { + if (v.kind) { + return type.array(type[v.type]); + } + return type[v]; + }); + intf.members.push(create.property(key, create.union(unionArray))); + } }); // Write interface to file const writeStream = createWriteStream(`${outDir}/firestore.ts`, { flags: 'a' }); diff --git a/src/main.ts b/src/main.ts index 51f40a2..f0c2280 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { create, emit, DeclarationFlags } from 'dts-dom'; +import { create, emit, DeclarationFlags, type } from 'dts-dom'; import { createWriteStream, existsSync, mkdirSync } from 'fs'; import findType from './utils/findType.js'; import { databasesClient } from './utils/firebase.js'; @@ -40,13 +40,40 @@ const fetchNewTypes = async ({ outDir = './types' }: fetchParameters = {}) => { for (const key in docData) { const value = docData[key]; - result.set(key, value); + + if (!result.has(key)) { + result.set(key, [findType(value) + ]); + } else { + const r = result.get(key); + const t = findType(value); + + if (!JSON.stringify(r).includes(JSON.stringify(t))) { + result.set(key, r.concat([t])); + } + } } } result.forEach((value, key) => { // Push attribute to interface - intf.members.push(create.property(key, findType(value))); + if (value.length === 1) { + let t = undefined; + if (value[0].kind) { + t = type.array(type[value[0].type]); + } else { + t = type[value[0]]; + } + intf.members.push(create.property(key, t)); + } else { + const unionArray = value.map(v => { + if (v.kind) { + return type.array(type[v.type]); + } + return type[v]; + }); + intf.members.push(create.property(key, create.union(unionArray))); + } }); // Write interface to file