mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-26 13:19:49 +07:00
add chrome.power/keep awake as an api sample (#1060)
* add chrome.power/keep awake as an api sample * support localized file info for extension samples Co-authored-by: Oliver Dunk <oliverdunk@google.com>
This commit is contained in:
@@ -10,11 +10,19 @@ export interface ApiItemWithType extends ApiItem {
|
||||
}
|
||||
|
||||
export interface ManifestData {
|
||||
[key: string]: string;
|
||||
name: string;
|
||||
description: string;
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
export interface LocaleData {
|
||||
[key: string]: {
|
||||
message: string;
|
||||
description: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type SampleItem = {
|
||||
type: FolderTypes;
|
||||
name: string;
|
||||
@@ -37,4 +45,4 @@ export type ApiTypeResult =
|
||||
| 'type'
|
||||
| 'unknown';
|
||||
|
||||
export type ExtensionApiMap = Record<string, Record<string, string[]>>
|
||||
export type ExtensionApiMap = Record<string, Record<string, string[]>>
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
import fs from 'fs/promises';
|
||||
import { ManifestData } from '../types';
|
||||
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
|
||||
@@ -7,5 +21,24 @@ export const getManifest = async (
|
||||
const manifest = await fs.readFile(manifestPath, 'utf8');
|
||||
const parsedManifest = JSON.parse(manifest);
|
||||
|
||||
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);
|
||||
|
||||
for (const [key, value] of Object.entries(parsedManifest)) {
|
||||
if (typeof value === 'string' && value.startsWith('__MSG_')) {
|
||||
const localeKey: string | undefined = value.match(localeRegex)?.[1];
|
||||
|
||||
if (localeKey) {
|
||||
const localeKeyData = localeData[localeKey]
|
||||
const localeMessage: string = localeKeyData?.message;
|
||||
|
||||
parsedManifest[key] = localeMessage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parsedManifest;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user