Skip to content

Commit 774063f

Browse files
authored
Enhance application transporters to handle assets and fonts (#971)
1 parent 3530136 commit 774063f

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

.changeset/afraid-garlics-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ensembleui/js-commons": patch
3+
---
4+
5+
Enhance application transporters to handle assets and fonts

packages/js-commons/src/core/firebase.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
omitBy,
2626
pick,
2727
set,
28+
toNumber,
2829
} from "lodash-es";
2930
import { getFunctions, httpsCallable } from "firebase/functions";
3031
import { ArtifactProps, EnsembleDocumentType } from "./dto";
@@ -99,7 +100,6 @@ export const getFirestoreApplicationTransporter = (
99100
},
100101

101102
put: async (app: ApplicationDTO, userId: string): Promise<ApplicationDTO> => {
102-
// if (!db) return {} as ApplicationDTO;
103103
const timestamp = Timestamp.now();
104104
const userRef = doc(db, CollectionsName.Users, userId);
105105
const appDocRef = doc(db, CollectionsName.Apps, app.id);
@@ -131,7 +131,16 @@ export const getFirestoreApplicationTransporter = (
131131
CollectionsName.InternalArtifacts,
132132
);
133133

134-
const { screens, widgets, scripts, translations, theme, env } = app;
134+
const {
135+
screens,
136+
widgets,
137+
scripts,
138+
translations,
139+
theme,
140+
env,
141+
assets,
142+
fonts,
143+
} = app;
135144

136145
screens.forEach((screen) => {
137146
const screenRef = doc(artifactsRef, screen.id);
@@ -223,6 +232,22 @@ export const getFirestoreApplicationTransporter = (
223232
}
224233

225234
await batch.commit();
235+
236+
const assetPromises = assets?.map((asset) =>
237+
firestoreStoreAsset(db.app, app.id, asset.fileName, asset.content),
238+
);
239+
if (assetPromises) await Promise.all(assetPromises);
240+
241+
const fontPromises = fonts?.map((font) =>
242+
firestoreStoreAsset(db.app, app.id, font.name, font.content, {
243+
fontFamily: font.fontFamily,
244+
fontWeight: toNumber(font.fontWeight),
245+
fontStyle: font.fontStyle,
246+
fontType: font.fontType,
247+
}),
248+
);
249+
if (fontPromises) await Promise.all(fontPromises);
250+
226251
return app;
227252
},
228253
});
@@ -234,7 +259,7 @@ export const firestoreStoreAsset = async (
234259
fileData: string | Buffer,
235260
font?: {
236261
fontFamily?: string;
237-
weight?: number;
262+
fontWeight?: number;
238263
fontStyle?: string;
239264
fontType?: string;
240265
},
@@ -250,7 +275,7 @@ export const firestoreStoreAsset = async (
250275
...(!isEmpty(font)
251276
? {
252277
fontFamily: font.fontFamily,
253-
weight: font.weight,
278+
weight: font.fontWeight,
254279
type: font.fontStyle,
255280
fontType: font.fontType,
256281
}

packages/js-commons/src/core/local-files.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ export const getLocalApplicationTransporter = (
7272
return;
7373
}
7474

75-
const content = await readFile(filePath, { encoding: "utf-8" });
75+
const content = await readFile(filePath, {
76+
encoding: isAssetOrFont(document) ? "base64" : "utf-8",
77+
});
7678

7779
return {
7880
...document,
@@ -212,14 +214,20 @@ export const localStoreAsset = async (
212214
fileData: string | Buffer,
213215
font?: {
214216
fontFamily?: string;
215-
weight?: number;
217+
fontWeight?: number;
216218
fontStyle?: string;
217219
fontType?: string;
218220
},
219-
options?: { existingAppMetadata?: ApplicationLocalMeta },
221+
options?: {
222+
existingAppMetadata?: ApplicationLocalMeta;
223+
},
220224
): Promise<{ relativePath: string; assetDocument: EnsembleDocument }> => {
221225
const appsMetadata = await getGlobalMetadata();
222-
const appMetadata = appsMetadata[appId] || options?.existingAppMetadata;
226+
const existingAppMetadata = appsMetadata[appId];
227+
const appMetadata =
228+
options?.existingAppMetadata ||
229+
(existingAppMetadata &&
230+
(await getAppManifest(existingAppMetadata.projectPath)));
223231
if (!appMetadata) {
224232
throw new Error(`App ${appId} not found in local metadata`);
225233
}
@@ -253,6 +261,7 @@ export const localStoreAsset = async (
253261
const { relativePath } = await saveArtifact(assetDocument, appMetadata, {
254262
relativePath: fileName,
255263
});
264+
256265
return { relativePath, assetDocument };
257266
};
258267

@@ -368,27 +377,29 @@ const setGlobalMetadata = async (
368377
await writeJsonData(filePath, metadata);
369378
};
370379

371-
const getAppManifest = async (projectPath: string): Promise<HasManifest> => {
380+
const getAppManifest = async (
381+
projectPath: string,
382+
): Promise<ApplicationLocalMeta> => {
372383
const filePath = join(projectPath, APP_MANIFEST_FILE);
373384

374385
// Check if the file exists
375386
if (!existsSync(filePath)) {
376-
return {};
387+
return {} as ApplicationLocalMeta;
377388
}
378389

379390
const manifest = readJsonFile<ApplicationLocalMeta>(filePath);
380391
return manifest;
381392
};
382393

383394
const setAppManifest = async (
384-
manifest: HasManifest,
395+
appMetadata: ApplicationLocalMeta,
385396
projectPath: string,
386397
): Promise<void> => {
387398
ensureDir(projectPath);
388399

389400
const filePath = join(projectPath, APP_MANIFEST_FILE);
390401

391-
await writeJsonData(filePath, manifest, true);
402+
await writeJsonData(filePath, appMetadata, true);
392403
};
393404

394405
const readJsonFile = async <T>(filePath: string): Promise<T> => {
@@ -416,7 +427,7 @@ const ensureDir = (path: string): void => {
416427
if (!existsSync(path)) mkdirSync(path, { recursive: true });
417428
};
418429

419-
const isAssetOrFont = (document: EnsembleDocument): boolean => {
430+
const isAssetOrFont = (document: Partial<EnsembleDocument>): boolean => {
420431
return (
421432
document.type === EnsembleDocumentType.Asset ||
422433
document.type === EnsembleDocumentType.Font
@@ -432,7 +443,7 @@ const fetchFileData = async (url: string): Promise<Buffer> => {
432443
const extractFontData = (fontDoc: FontDTO): object => {
433444
return {
434445
fontFamily: fontDoc.fontFamily,
435-
weight: fontDoc.fontWeight,
446+
fontWeight: fontDoc.fontWeight,
436447
fontStyle: fontDoc.fontStyle,
437448
fontType: fontDoc.fontType,
438449
};

packages/js-commons/src/core/service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const bundleApp = (
2727
env: documents.find((d) => d.type === EnsembleDocumentType.Environment),
2828
translations: documents.filter((d) => d.type === EnsembleDocumentType.I18n),
2929
scripts: documents.filter((d) => d.type === EnsembleDocumentType.Script),
30+
assets: documents.filter((d) => d.type === EnsembleDocumentType.Asset),
31+
fonts: documents.filter((d) => d.type === EnsembleDocumentType.Font),
3032
});
3133
};
3234

0 commit comments

Comments
 (0)