Skip to content

Instantly share code, notes, and snippets.

@smowden
Created July 21, 2016 15:45
Show Gist options
  • Save smowden/798fcdd24d53afdb99b27b14020e1969 to your computer and use it in GitHub Desktop.
Save smowden/798fcdd24d53afdb99b27b14020e1969 to your computer and use it in GitHub Desktop.
ES6 version for adding intent filters via cordova hooks on android
module.exports = function (context) {
const fs = require('fs');
const _ = require('lodash');
const scheme = 'flowkey';
const insertIntent = `
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="${scheme}"></data>
</intent-filter>
`;
const manifestPath = context.opts.projectRoot + '/platforms/android/AndroidManifest.xml';
const androidManifest = fs.readFileSync(manifestPath).toString();
if (!androidManifest.includes(`android:scheme="${scheme}"`)) {
const manifestLines = androidManifest.split(/\r?\n/);
const lineNo = _.findIndex(manifestLines, (line) => line.includes('@string/activity_name'));
manifestLines.splice(lineNo + 1, 0, insertIntent);
fs.writeFileSync(manifestPath, manifestLines.join('\n'));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment