Skip to content

Commit

Permalink
fix(manifest): filter out duplicate names
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Apr 30, 2024
1 parent 255bf19 commit 9592d08
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
39 changes: 33 additions & 6 deletions packages/manifest/src/pixiManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import {
stripTags
} from '@play-co/assetpack-core';

export interface PixiManifest
export interface PixiBundle
{
name: string;
assets: PixiManifestEntry[];
}

export interface PixiManifest
{
bundles: PixiBundle[];
}

export interface PixiManifestEntry
{
alias: string | string[];
Expand Down Expand Up @@ -79,29 +84,51 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
const newFileName = path.dirname(options.output) === '.'
? path.joinSafe(pipeSystem.outputPath, options.output) : options.output;

const defaultBundle = {
const defaultBundle: PixiBundle = {
name: 'default',
assets: []
};

const manifest = {
const manifest: PixiManifest = {
bundles: [defaultBundle]
};

collectAssets(asset, options, pipeSystem.outputPath, pipeSystem.entryPath, manifest.bundles, defaultBundle);

filterUniqueNames(manifest);
await fs.writeJSON(newFileName, manifest, { spaces: 2 });
}
};
}

function filterUniqueNames(manifest: PixiManifest)
{
const nameMap = new Map<PixiManifestEntry, string[]>();

manifest.bundles.forEach((bundle) =>
bundle.assets.forEach((asset) => nameMap.set(asset, asset.alias as string[])));

const arrays = Array.from(nameMap.values());
const sets = arrays.map((arr) => new Set(arr));
const uniqueArrays = arrays.map((arr, i) => arr.filter((x) => sets.every((set, j) => j === i || !set.has(x))));

manifest.bundles.forEach((bundle) =>
{
bundle.assets.forEach((asset) =>
{
const names = nameMap.get(asset) as string[];

asset.alias = uniqueArrays.find((arr) => arr.every((x) => names.includes(x))) as string[];
});
});
}

function collectAssets(
asset: Asset,
options: PixiManifestOptions,
outputPath = '',
entryPath = '',
bundles: PixiManifest[],
bundle: PixiManifest,
bundles: PixiBundle[],
bundle: PixiBundle,
)
{
if (asset.skip) return;
Expand Down
12 changes: 0 additions & 12 deletions packages/manifest/test/Manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,7 @@ describe('Manifest', () =>
{
alias: [
'folder/json.json',
'folder/json',
'json.json',
'json'
],
src: [
'folder/json.json'
Expand All @@ -368,9 +366,7 @@ describe('Manifest', () =>
{
alias: [
'folder/json.json5',
'folder/json',
'json.json5',
'json'
],
src: [
'folder/json.json5'
Expand All @@ -394,8 +390,6 @@ describe('Manifest', () =>
alias: [
'folder2/1.mp3',
'folder2/1',
'1.mp3',
'1'
],
src: [
'folder2/1.ogg',
Expand All @@ -406,8 +400,6 @@ describe('Manifest', () =>
alias: [
'folder2/folder3/1.mp3',
'folder2/folder3/1',
'1.mp3',
'1'
],
src: [
'folder2/folder3/1.ogg',
Expand All @@ -417,9 +409,7 @@ describe('Manifest', () =>
{
alias: [
'spine/dragon.json',
'spine/dragon',
'dragon.json',
'dragon'
],
src: [
'spine/dragon.json'
Expand All @@ -428,9 +418,7 @@ describe('Manifest', () =>
{
alias: [
'spine/dragon.atlas',
'spine/dragon',
'dragon.atlas',
'dragon'
],
src: [
'spine/[email protected]',
Expand Down

0 comments on commit 9592d08

Please sign in to comment.