Skip to content

Commit

Permalink
recursivly search the object rather than searching the stringified ve…
Browse files Browse the repository at this point in the history
…rsion
  • Loading branch information
patrickkettner committed Jan 12, 2024
1 parent cdde058 commit bd6bf32
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion .repo/sample-list-generator/src/utils/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import { dirname } from 'path';
import { ManifestData, LocaleData } from '../types';
const localeRegex = /__MSG_([^_]*)__/

function usesLocaleFiles(obj: object): boolean {
// recursively check if any value in a supplied object
// is a string that starts with __MSG_. If found, it
// means that the extension uses locale files.
return Object.values(obj).some((value) => {
if (Object.prototype.toString.call(value) === '[object Object]') {
return usesLocaleFiles(value);
}
return typeof value === 'string' && value.startsWith('__MSG_')
});
}

export const getManifest = async (
manifestPath: string
): Promise<ManifestData> => {
const manifest = await fs.readFile(manifestPath, 'utf8');
const parsedManifest = JSON.parse(manifest);
const localeKeyUsed = false;

if (manifest.includes('__MSG_')) {
if (usesLocaleFiles(parsedManifest)) {
const directory = dirname(manifestPath);
const localeFile: string = await fs.readFile(`${directory}/_locales/en/messages.json`, 'utf8')
const localeData: LocaleData = JSON.parse(localeFile);
Expand Down

0 comments on commit bd6bf32

Please sign in to comment.