Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expo user agent #49

Merged
merged 30 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"prepare": "bob build",
"example": "yarn --cwd example",
"pods": "cd example && pod-install --quiet",
"bootstrap": "yarn example && yarn && yarn pods"
"bootstrap": "yarn example && yarn && yarn pods",
"postinstall": "node src/postInstall.js"
},
"keywords": [
"react-native",
Expand Down
43 changes: 43 additions & 0 deletions src/postInstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs');
xtreem88 marked this conversation as resolved.
Show resolved Hide resolved

// set package.json file paths
const expoPJsonFile = `${__dirname}/../../customerio-expo-plugin/package.json`;
const rnPJsonFile = `${__dirname}/../package.json`;

// check if expo plugin is installed
if (fs.existsSync(expoPJsonFile)) {
// try and catch to revent errors
try {
// read package.json file for current package
const rnPJson = fs.readFileSync(rnPJsonFile, 'utf8');

// import expo plugin's package.json
const expoPJson = require(expoPJsonFile);

// set regular expression for expo version
const expoVersionSnippet = `"expoVersion": "${expoPJson.version}"`;
const versionRegEx = new RegExp(expoVersionSnippet);

// split package.json lines into array
const lines = rnPJson.split('\n');

const missingMmatch = rnPJson.match(versionRegEx);
const expoVersionRegex = /"expoVersion": ".*"/;
const existatch = rnPJson.match(expoVersionRegex);
xtreem88 marked this conversation as resolved.
Show resolved Hide resolved

// check if expoVersion key exists in current package and it has not been set already
if (existatch && !missingMmatch) {
xtreem88 marked this conversation as resolved.
Show resolved Hide resolved
const index = lines.findIndex((line) => expoVersionRegex.test(line));

// set react native SDK expoVersion to current version in expo plugin package
const content = [
...lines.slice(0, index),
` ${expoVersionSnippet},`,
...lines.slice(index + 1),
];

// save react native SDK package.json file
fs.writeFileSync(rnPJsonFile, content.join('\n'), 'utf8');
}
} catch (error) {} // do nothing if this operation fails
}