react-native-scanbot-sdk
Version:
Scanbot Document and Barcode Scanner SDK React Native Plugin for Android and iOS
104 lines (103 loc) • 4.58 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withAssets = void 0;
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const config_plugins_1 = require("@expo/config-plugins");
async function validateOCRBlobAssetDir(ocrBlobsDirPath) {
return new Promise(async (resolve, reject) => {
const stat = await promises_1.default.stat(ocrBlobsDirPath);
if (stat.isDirectory()) {
const dir = await promises_1.default.readdir(ocrBlobsDirPath);
for (const file of dir) {
if (path_1.default.extname(path_1.default.join(ocrBlobsDirPath, file)) !== '.traineddata') {
reject(new Error(`File ${file} is not a valid train file`));
}
}
resolve();
}
else {
reject(`${ocrBlobsDirPath} is not a valid directory`);
}
});
}
async function copyAndroidAssets(ocrBlobsDirPath, platformProjectRoot) {
return promises_1.default.cp(ocrBlobsDirPath, path_1.default.resolve(platformProjectRoot, 'app', 'src', 'main', 'assets', 'ocr_blobs'), {
recursive: true,
});
}
const withAssetsAndroid = (config, ocrBlobsDirPath) => {
return (0, config_plugins_1.withDangerousMod)(config, [
'android',
async (config) => {
try {
const resolvedOcrBlobsDirPath = path_1.default.resolve(config.modRequest.projectRoot, ocrBlobsDirPath);
await validateOCRBlobAssetDir(resolvedOcrBlobsDirPath);
await copyAndroidAssets(resolvedOcrBlobsDirPath, config.modRequest.platformProjectRoot);
}
catch (error) {
config_plugins_1.WarningAggregator.addWarningAndroid('react-native-scanbot-sdk', `OCR Blobs could not be installed on Android, ${error.message ?? 'Unknown error'}`);
}
return config;
},
]);
};
async function copyIOSAssets(ocrBlobsDirPath, platformProjectRoot) {
const bundlePath = path_1.default.resolve(platformProjectRoot, 'ScanbotSDKOCRData.bundle');
if (!(await promises_1.default.stat(ocrBlobsDirPath)).isDirectory()) {
await promises_1.default.mkdir(bundlePath);
}
const dir = await promises_1.default.readdir(ocrBlobsDirPath);
for (const file of dir) {
await promises_1.default.cp(path_1.default.join(ocrBlobsDirPath, file), path_1.default.join(bundlePath, file));
}
}
const withXcodeProjectAssets = (config) => {
return (0, config_plugins_1.withXcodeProject)(config, (props) => {
try {
const { projectName } = props.modRequest;
const xcodeProject = props.modResults;
props.modResults = config_plugins_1.IOSConfig.XcodeUtils.addResourceFileToGroup({
filepath: 'ScanbotSDKOCRData.bundle',
groupName: `${projectName}/Resources`,
isBuildFile: true,
project: xcodeProject,
verbose: true,
});
return props;
}
catch (error) {
config_plugins_1.WarningAggregator.addWarningIOS('react-native-scanbot-sdk', `OCR Blobs could not be added to XcodeProject, ${error.message ?? 'Unknown error'}`);
}
return props;
});
};
const withAssetsIOS = (config, ocrBlobsDirPath) => {
config = (0, config_plugins_1.withDangerousMod)(config, [
'ios',
async (config) => {
let configWithAssets = config;
try {
const resolvedOcrBlobsDirPath = path_1.default.resolve(config.modRequest.projectRoot, ocrBlobsDirPath);
await validateOCRBlobAssetDir(resolvedOcrBlobsDirPath);
await copyIOSAssets(resolvedOcrBlobsDirPath, config.modRequest.platformProjectRoot);
}
catch (error) {
config_plugins_1.WarningAggregator.addWarningIOS('react-native-scanbot-sdk', `OCR Blobs could not be installed on iOS, ${error.message ?? 'Unknown error'}`);
}
return configWithAssets;
},
]);
return withXcodeProjectAssets(config);
};
const withAssets = (config, { ocrBlobsDirPath } = {}) => {
if (ocrBlobsDirPath) {
config = withAssetsAndroid(config, ocrBlobsDirPath);
config = withAssetsIOS(config, ocrBlobsDirPath);
}
return config;
};
exports.withAssets = withAssets;
;