Skip to content

Instantly share code, notes, and snippets.

@jonstuebe
Created December 30, 2023 16:58
Show Gist options
  • Save jonstuebe/1b8b2892269935ac87b75731af66cb8b to your computer and use it in GitHub Desktop.
Save jonstuebe/1b8b2892269935ac87b75731af66cb8b to your computer and use it in GitHub Desktop.
Expo plugin for react-native-permissions
const {
withPlugins,
withDangerousMod,
withInfoPlist,
} = require("@expo/config-plugins");
const fs = require("fs");
const path = require("path");
async function readFileAsync(path) {
return fs.promises.readFile(path, "utf8");
}
async function saveFileAsync(path, content) {
return fs.promises.writeFile(path, content, "utf8");
}
const withPodMods = (c) => {
return withDangerousMod(c, [
"ios",
async (config) => {
const file = path.join(config.modRequest.platformProjectRoot, "Podfile");
let contents = await readFileAsync(file);
const lines = contents.split("\n");
const requireMatch = lines.findIndex((line) =>
/scripts\/react_native_pods/.test(line)
);
const prepareProjectMatch = lines.findIndex((line) =>
/prepare_react_native_project!/.test(line)
);
if (requireMatch === -1 || prepareProjectMatch === -1) {
throw new Error(
"Podfile template has changed. This mod will no longer work"
);
}
let podFileContents = [
...lines.slice(0, requireMatch + 1),
`require File.join(File.dirname(\`node --print "require.resolve('react-native-permissions/package.json')"\`), "scripts/setup")`,
...lines.slice(requireMatch + 1, prepareProjectMatch + 1),
``,
`setup_permissions([`,
` 'Bluetooth'`,
`])`,
``,
...lines.slice(prepareProjectMatch + 6),
].join("\n");
await saveFileAsync(file, podFileContents);
return config;
},
]);
};
const withPlistMod = (config) => {
return withInfoPlist(config, (config) => {
config.modResults["NSBluetoothAlwaysUsageDescription"] = "YOUR TEXT";
config.modResults["NSBluetoothPeripheralUsageDescription"] = "YOUR TEXT";
return config;
});
};
module.exports = (config) => withPlugins(config, [withPodMods, withPlistMod]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment