Skip to content

Commit

Permalink
fix(report): track assets with preceding forward slash
Browse files Browse the repository at this point in the history
This allows asset reports to include files name `/some_asset.png` which are (currently) supported in frontend code
  • Loading branch information
chrismclarke committed Oct 1, 2024
1 parent 6dfa713 commit e745e3c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ describe("Asset Summary Report", () => {
]);
});

it("Allows authors to specify assets with preceding forward slash", async () => {
const { asset_summary } = await reporter.process({
template: [
{
flow_type: "template",
flow_name: "test_forward_slash",
rows: [{ type: "audio", value: "/mock_audio.mp3" }],
},
],
});
expect(asset_summary.data).toEqual([{ path: "mock_audio.mp3", size_kb: 2048, count: 1 }]);
});

it("Reports missing assets", async () => {
const { assets_missing } = await reporter.process(MOCK_WORKBOOK_DATA);
expect(assets_missing.data).toEqual([{ path: "missing_audio.mp3", count: 1, missing: true }]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FlowTypes, IAssetEntryHashmap, IDeploymentConfigJson } from "data-models";
import { FlowTypes, IAssetEntryHashmap } from "data-models";
import { IReportTable } from "../report.types";
import { isObjectLiteral, kbToMB, sortJsonKeys } from "shared";
import { cleanAssetName, isObjectLiteral, kbToMB, sortJsonKeys } from "shared";
import { IParsedWorkbookData } from "../../convert/types";

interface IReportData {
Expand Down Expand Up @@ -147,6 +147,8 @@ export class AssetsSummaryReport {
}

private markAsset(name: string) {
// NOTE - use same method as frontend asset service to allow assets with preceding `/`
name = cleanAssetName(name);
this.reportSummary[name] ??= 0;
this.reportSummary[name]++;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/src/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ export function parseStringValue(v: string): any {
if (v.match(/^"[a-z0-9.]*"$/gi)) return v.replace(/"/g, "");
return v;
}

/** Remove preceding `/` from any named asset paths */
export function cleanAssetName(value: string) {
if (value.startsWith("/")) value = value.substring(1);
return value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TemplateTranslateService } from "./template-translate.service";
import { IAssetEntry, IAssetContentsEntryMinimal } from "data-models";
import { HttpClient } from "@angular/common/http";
import { BehaviorSubject, lastValueFrom } from "rxjs";
import { cleanAssetName } from "packages/shared/src/utils/string-utils";

/** Synced assets are automatically copied during build to asset subfolder */
const ASSETS_BASE = `assets/app_data/assets`;
Expand Down Expand Up @@ -55,7 +56,7 @@ export class TemplateAssetService extends AsyncServiceBase {
* 4. default theme, default language
*/
getTranslatedAssetPath(value: string) {
let assetName = this.cleanAssetName(value);
let assetName = cleanAssetName(value);
const assetEntry = this.assetsContentsList$.value[assetName];
if (!assetEntry) {
console.error("Asset missing", value, assetName);
Expand Down Expand Up @@ -86,12 +87,6 @@ export class TemplateAssetService extends AsyncServiceBase {
return this.getAssetPath(assetName, assetEntry);
}

private cleanAssetName(value: string) {
// remove prefix slash
if (value.startsWith("/")) value = value.substring(1);
return value;
}

private getAssetPath(
assetName: string,
contentsEntry: IAssetContentsEntryMinimal | Partial<IAssetEntry>
Expand Down

0 comments on commit e745e3c

Please sign in to comment.