Skip to content

Commit 030d9b9

Browse files
authored
update folder structure when an app is pulled (#1023)
1 parent 9e9ef24 commit 030d9b9

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed
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+
update folder structure when an app is pulled

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export const getFirestoreApplicationTransporter = (
204204
});
205205

206206
translations?.forEach((translation) => {
207-
const translationRef = doc(internalArtifactsRef, translation.id);
207+
const translationRef = doc(artifactsRef, translation.id);
208208
const history = doc(collection(translationRef, CollectionsName.History));
209209
const updatedTranslation = {
210210
...translation,

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

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isArray,
1111
isEmpty,
1212
isNil,
13+
map,
1314
omit,
1415
pick,
1516
values,
@@ -60,7 +61,10 @@ export const getLocalApplicationTransporter = (
6061
if (!document.type) {
6162
return;
6263
}
63-
const subDir = join(existingAppMetadata.projectPath, document.type);
64+
const subDir = getSubDir(
65+
existingAppMetadata.projectPath,
66+
document.type,
67+
);
6468
let filePath;
6569
if (document.relativePath) {
6670
filePath = join(subDir, document.relativePath);
@@ -72,9 +76,11 @@ export const getLocalApplicationTransporter = (
7276
return;
7377
}
7478

75-
const content = await readFile(filePath, {
76-
encoding: isAssetOrFont(document) ? "base64" : "utf-8",
77-
});
79+
const content = isConfigOrSecret(document)
80+
? undefined
81+
: await readFile(filePath, {
82+
encoding: isAssetOrFont(document) ? "base64" : "utf-8",
83+
});
7884

7985
return {
8086
...document,
@@ -105,7 +111,9 @@ export const getLocalApplicationTransporter = (
105111
path?: string,
106112
): Promise<ApplicationDTO> => {
107113
ensureDir(ensembleDir);
108-
const updatedAppData = omit(appData, ["assets", "fonts"]);
114+
const updatedAppData = excludeContentField(
115+
omit(appData, ["assets", "fonts"]),
116+
);
109117
const appsMetaData = await getGlobalMetadata();
110118
const existingAppMetadata = (
111119
appsMetaData[appData.id]
@@ -167,7 +175,7 @@ export const getLocalApplicationTransporter = (
167175
}
168176

169177
return {
170-
...docToWrite,
178+
...omitContent(docToWrite),
171179
relativePath,
172180
};
173181
});
@@ -232,7 +240,7 @@ export const localStoreAsset = async (
232240
throw new Error(`App ${appId} not found in local metadata`);
233241
}
234242

235-
const assetDir = join(
243+
const assetDir = getSubDir(
236244
appMetadata.projectPath,
237245
!isEmpty(font) ? EnsembleDocumentType.Font : EnsembleDocumentType.Asset,
238246
);
@@ -277,7 +285,7 @@ export const localRemoveAsset = async (
277285
throw new Error(`App ${appId} not found in local metadata`);
278286
}
279287

280-
const assetDir = join(
288+
const assetDir = getSubDir(
281289
appMetadata.projectPath,
282290
isFont ? EnsembleDocumentType.Font : EnsembleDocumentType.Asset,
283291
);
@@ -318,37 +326,34 @@ export const saveArtifact = async (
318326
const existingPath = app.manifest[artifact.id]?.relativePath;
319327

320328
// TODO: allow custom project structures
321-
const artifactSubDir = join(app.projectPath, artifact.type);
329+
const artifactSubDir = getSubDir(app.projectPath, artifact.type);
322330
ensureDir(artifactSubDir);
323331

324332
let pathToWrite = existingPath;
325333
if (options.relativePath) {
326334
pathToWrite = options.relativePath;
327335
}
328-
if (
329-
artifact.type === EnsembleDocumentType.Environment ||
330-
artifact.type === EnsembleDocumentType.Secrets
331-
) {
336+
if (isConfigOrSecret(artifact)) {
332337
pathToWrite = `${artifact.id}.json`; // appConfig.json or secrets.json
333338
}
339+
if (artifact.type === EnsembleDocumentType.Theme) {
340+
pathToWrite = `${EnsembleDocumentType.Theme}.yaml`;
341+
}
334342
if (!pathToWrite) {
335343
pathToWrite = `${artifact.name || artifact.id}.yaml`;
336344
}
337345

338346
if (!isAssetOrFont(artifact)) {
339347
await writeFile(
340348
join(artifactSubDir, pathToWrite),
341-
artifact.type === EnsembleDocumentType.Environment ||
342-
artifact.type === EnsembleDocumentType.Secrets
343-
? JSON.stringify(artifact)
344-
: artifact.content,
349+
isConfigOrSecret(artifact) ? JSON.stringify(artifact) : artifact.content,
345350
"utf-8",
346351
);
347352
}
348353

349354
if (!options.skipMetadata) {
350355
app.manifest[artifact.id] = {
351-
...artifact,
356+
...omitContent(artifact),
352357
relativePath: pathToWrite,
353358
};
354359
await setAppManifest(app, app.projectPath);
@@ -434,6 +439,13 @@ const isAssetOrFont = (document: Partial<EnsembleDocument>): boolean => {
434439
);
435440
};
436441

442+
const isConfigOrSecret = (document: Partial<EnsembleDocument>): boolean => {
443+
return (
444+
document.type === EnsembleDocumentType.Environment ||
445+
document.type === EnsembleDocumentType.Secrets
446+
);
447+
};
448+
437449
const fetchFileData = async (url: string): Promise<Buffer> => {
438450
const response = await fetch(url);
439451
const arrayBuffer = await response.arrayBuffer();
@@ -448,3 +460,35 @@ const extractFontData = (fontDoc: FontDTO): object => {
448460
fontType: fontDoc.fontType,
449461
};
450462
};
463+
464+
const getSubDir = (
465+
projectPath: string,
466+
docType: EnsembleDocumentType,
467+
): string => join(projectPath, FOLDER_MAP[docType]);
468+
469+
const FOLDER_MAP = {
470+
[EnsembleDocumentType.Screen]: "screens",
471+
[EnsembleDocumentType.Widget]: "widgets",
472+
[EnsembleDocumentType.Script]: "scripts",
473+
[EnsembleDocumentType.Asset]: "assets",
474+
[EnsembleDocumentType.Font]: "fonts",
475+
[EnsembleDocumentType.I18n]: "translations",
476+
[EnsembleDocumentType.Label]: "labels",
477+
[EnsembleDocumentType.Environment]: "config",
478+
[EnsembleDocumentType.Secrets]: "config",
479+
[EnsembleDocumentType.Theme]: "",
480+
} as const;
481+
482+
const omitContent = (
483+
doc: EnsembleDocument,
484+
): Omit<EnsembleDocument, "content"> => omit(doc, "content");
485+
486+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
487+
const excludeContentField = (appData: ApplicationDTO) => ({
488+
...appData,
489+
screens: map(appData.screens, omitContent),
490+
widgets: map(appData.widgets, omitContent),
491+
scripts: map(appData.scripts, omitContent),
492+
translations: map(appData.translations, omitContent),
493+
theme: appData.theme ? omitContent(appData.theme) : undefined,
494+
});

0 commit comments

Comments
 (0)