Skip to content

Commit

Permalink
chore: Update the implementation of extractStringsFromApk API (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jan 25, 2024
1 parent d8c67c1 commit ef091f4
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,20 +850,30 @@ apkUtilsMethods.installOrUpgrade = async function installOrUpgrade (appPath, pkg
};
};

/**
* @typedef {Object} ApkStrings
* @property {import('@appium/types').StringRecord} apkStrings parsed resource file
* represented as JSON object
* @property {string} [localPath] the path to the extracted file on the local file system
*/

/**
* Extract string resources from the given package on local file system.
*
* @this {import('../adb.js').ADB}
* @param {string} appPath - The full path to the .apk(s) package.
* @param {string?} language - The name of the language to extract the resources for.
* @param {string?} [language=null] - The name of the language to extract the resources for.
* The default language is used if this equals to `null`
* @param {string} out - The name of the destination folder on the local file system to
* store the extracted file to.
* @return {Promise<import('@appium/types').StringRecord>} A mapping object, where properties are: 'apkStrings', containing
* parsed resource file represented as JSON object, and 'localPath',
* containing the path to the extracted file on the local file system.
* @param {string?} [outRoot=null] - The name of the destination folder on the local file system to
* store the extracted file to. If not provided then the `localPath` property in the returned object
* will be undefined.
* @return {Promise<ApkStrings>}
*/
apkUtilsMethods.extractStringsFromApk = async function extractStringsFromApk (appPath, language, out) {
apkUtilsMethods.extractStringsFromApk = async function extractStringsFromApk (
appPath,
language = null,
outRoot = null
) {
log.debug(`Extracting strings from for language: ${language || 'default'}`);
const originalAppPath = appPath;
if (appPath.endsWith(APKS_EXTENSION)) {
Expand Down Expand Up @@ -918,8 +928,12 @@ apkUtilsMethods.extractStringsFromApk = async function extractStringsFromApk (ap
`'${originalAppPath}' resources for '${configMarker || 'default'}' configuration`);
}

const localPath = path.resolve(out, 'strings.json');
await mkdirp(out);
if (!outRoot) {
return {apkStrings};
}

const localPath = path.resolve(outRoot, 'strings.json');
await mkdirp(outRoot);
await fs.writeFile(localPath, JSON.stringify(apkStrings, null, 2), 'utf-8');
return {apkStrings, localPath};
};
Expand Down

0 comments on commit ef091f4

Please sign in to comment.