Skip to content

Commit

Permalink
fix: sort mod result keys (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon authored Jun 9, 2021
1 parent 4b15ef3 commit 3a5fd07
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/preview/IntrospectCodeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,36 @@ class IntrospectCodeProvider extends CodeProvider {
platforms: [this.getModPlatform()],
});

return config._internal!.modResults[this.getModPlatform()][this.getModName()];
const results = config._internal!.modResults[this.getModPlatform()][this.getModName()];
return this.sortObject(results);
}

sortObject(obj: Record<string, any>): Record<string, any> {
return sortObjectKeys(obj);
}
}

function sortObjectKeys(
obj: Record<string, any>,
compareFn?: (a: string, b: string) => number
): Record<string, any> {
return Object.keys(obj)
.sort(compareFn)
.reduce(
(acc, key) => ({
...acc,
[key]: obj[key],
}),
{}
);
}

const reverseSort = (a: string, b: string) => {
if (a < b) return 1;
if (a > b) return -1;
return 0;
};

export class AndroidManifestCodeProvider extends IntrospectCodeProvider {
static fileDescription = 'android/app/src/main/AndroidManifest.xml';
readonly defaultLanguage: CodeProviderLanguage = 'xml';
Expand All @@ -49,6 +75,21 @@ export class AndroidManifestCodeProvider extends IntrospectCodeProvider {
getFileName(): string {
return 'android/app/src/main/AndroidManifest.xml';
}

sortObject(obj: Record<string, any>): Record<string, any> {
if (obj.manifest) {
// Reverse sort so application is last and permissions are first
obj.manifest = sortObjectKeys(obj.manifest, reverseSort);

if (Array.isArray(obj.manifest.application)) {
// reverse sort applications so activity is towards the end and meta-data is towards the front.
obj.manifest.application = obj.manifest.application.map((application: any) =>
sortObjectKeys(application, reverseSort)
);
}
}
return obj;
}
}

export class GradlePropertiesCodeProvider extends IntrospectCodeProvider {
Expand Down
4 changes: 2 additions & 2 deletions src/preview/setupPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import {
} from './IntrospectCodeProvider';

const ModProviders: Record<string, typeof CodeProvider> = {
'ios.entitlements': EntitlementsPlistCodeProvider,
'ios.infoPlist': InfoPlistCodeProvider,
'ios.entitlements': EntitlementsPlistCodeProvider,
'ios.expoPlist': ExpoPlistCodeProvider,
'android.manifest': AndroidManifestCodeProvider,
'android.strings': AndroidStringsCodeProvider,
// TODO: Update expo/config-plugins
// 'android.colors': AndroidColorsCodeProvider,
// 'android.styles': AndroidStylesCodeProvider,
'android.manifest': AndroidManifestCodeProvider,
'android.gradleProperties': GradlePropertiesCodeProvider,
};

Expand Down

0 comments on commit 3a5fd07

Please sign in to comment.