diff --git a/package.json b/package.json index b32f64c500..1b7592fd6f 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "lint": "gulp lint", "build": "npm run lint && npm run build:core && npm run build:modules", "build:core": "tsc -p scripts/build/tsconfig-core.json", + "build:utils": "tsc -p scripts/build/tsconfig-utils.json", "build:modules": "node scripts/build/build.js", "shipit": "npm run build && npm run npmpub", "npmpub": "sh scripts/build/publish.sh", diff --git a/scripts/build/build.js b/scripts/build/build.js index 8c42f3eb55..bdc5478102 100644 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -9,12 +9,15 @@ const ROOT = path.resolve(path.join(__dirname, '../../')), // root ionic-native VERSION = require(path.resolve(ROOT, 'package.json')).version, // current ionic-native version PLUGINS_PATH = path.resolve(ROOT, 'src/@ionic-native/plugins'), // path to plugins source files CORE_PACKAGE_JSON = require(path.resolve(__dirname, 'core-package.json')), // core package.json + UTILS_PACKAGE_JSON = require(path.resolve(__dirname, 'utils-package.json')), // utils package.json PLUGIN_PACKAGE_JSON = require(path.resolve(__dirname, 'plugin-package.json')), // plugin package.json template PLUGIN_TS_CONFIG = require(path.resolve(__dirname, 'tsconfig-plugin.json')), // plugin tsconfig template + PLUGIN_CONFIG = require(path.resolve(__dirname, 'plugin-config.json')), // plugin config for @ionic-native/utils BUILD_TMP = path.resolve(ROOT, '.tmp'), // tmp directory path BUILD_DIST_ROOT = path.resolve(ROOT, 'dist/packages-dist/@ionic-native'), // dist directory root path BUILD_PLUGINS_DIST = path.resolve(BUILD_DIST_ROOT, 'plugins'), // plugins dist directory path - BUILD_CORE_DIST = path.resolve(BUILD_DIST_ROOT, 'core'); // core dist directory path + BUILD_CORE_DIST = path.resolve(BUILD_DIST_ROOT, 'core'), // core dist directory path + BUILD_UTILS_DIST = path.resolve(BUILD_DIST_ROOT, 'utils'); // Delete dist directory and any temporary files @@ -33,6 +36,14 @@ console.log('Preparing core module package.json'); CORE_PACKAGE_JSON.version = VERSION; fs.writeJsonSync(path.resolve(BUILD_CORE_DIST, 'package.json'), CORE_PACKAGE_JSON); +console.log('Preparing utils module package.json'); +UTILS_PACKAGE_JSON.version = VERSION; +fs.mkdirpSync(BUILD_UTILS_DIST); +fs.writeJsonSync(path.resolve(BUILD_UTILS_DIST, 'package.json'), UTILS_PACKAGE_JSON); + +console.log('Copying utils module'); +fs.copySync(path.resolve(__dirname, 'utils.js'), path.resolve(BUILD_UTILS_DIST, 'index.js')); + // Fetch a list of the plugins const PLUGINS = fs.readdirSync(PLUGINS_PATH); @@ -72,31 +83,38 @@ const addPluginToQueue = pluginName => { .then(() => fs.readFileAsync(PLUGIN_SRC_PATH, 'utf-8')) // read the plugin definition .then((pluginFile) => { - let postinstall, - regexInstall = /install:\s'(.*)',?\s/g.exec(pluginFile), - regexPlugin = /plugin:\s'(.*)',?\s/g.exec(pluginFile); + let packageLocator, + installVariables, + regexPlugin = /plugin:\s'(.*)',?\s/g.exec(pluginFile), + regexPluginName = /pluginName:\s'(.*)',?\s/g.exec(pluginFile), + regexVars = /installVariables:\s(\[.*]),?\s/g.exec(pluginFile); - if (regexInstall) { - // console.log('install command exists'); - // postinstall = regexInstall[1]; + if (regexPlugin) { + packageLocator = regexPlugin[1]; + } - } else if (regexPlugin) { - postinstall = `ionic plugin add ${ regexPlugin[1] }`; - } else { - console.log('nothing was found'); + if (regexVars) { + installVariables = JSON.parse(regexVars[1].replace(/'/g, '"')); } + if (packageLocator) console.log(packageLocator); + if (installVariables) console.log(installVariables); + + // clone plugin-config.json + const pluginConfig = JSON.parse(JSON.stringify(PLUGIN_CONFIG)); + + pluginConfig.name = regexPluginName[1]; + pluginConfig.variables = installVariables; + pluginConfig.locator = packageLocator; + return fs.writeJsonAsync(path.resolve(BUILD_PLUGINS_DIST, pluginName, 'plugin-config.json'), pluginConfig); + }) + .then(() => { // clone package.json const packageJson = JSON.parse(JSON.stringify(PLUGIN_PACKAGE_JSON)); packageJson.name = `@ionic-native/${pluginName}`; - packageJson.version = packageJson.peerDependencies['@ionic-native/core'] = VERSION; - - if (postinstall) { - // add postinstall script - packageJson.scripts = { postinstall }; - } + packageJson.version = packageJson.peerDependencies['@ionic-native/core'] = packageJson.peerDependencies['@ionic-native/utils'] = VERSION; return fs.writeJsonAsync(path.resolve(BUILD_PLUGINS_DIST, pluginName, 'package.json'), packageJson); }) @@ -107,7 +125,7 @@ const addPluginToQueue = pluginName => { if (err) { // oops! something went wrong. - callback(`Building ${pluginName} failed.`); + callback(`\n\nBuilding ${pluginName} failed.`); console.log(stdout); return; } diff --git a/scripts/build/plugin-config.json b/scripts/build/plugin-config.json new file mode 100644 index 0000000000..d8960d8edd --- /dev/null +++ b/scripts/build/plugin-config.json @@ -0,0 +1,9 @@ +{ + "name": "Facebook", + "variables": [ + "APP_ID", + "SOME_OTHER_VAR", + "AND_ANOTHER_ONE" + ], + "locator": "cordova-plugin-facebook4" +} diff --git a/scripts/build/plugin-package.json b/scripts/build/plugin-package.json index 1ca0d9397e..1af6c474c2 100644 --- a/scripts/build/plugin-package.json +++ b/scripts/build/plugin-package.json @@ -9,10 +9,14 @@ "peerDependencies": { "@angular/core": "2.2.1", "@ionic-native/core": "{{VERSION}}", + "@ionic-native/utils": "{{VERSION}}", "rxjs": "5.0.0-beta.12" }, "repository": { "type": "git", "url": "https://github.com/driftyco/ionic-native.git" + }, + "scripts": { + "postinstall": "node -e \"require('@ionic-native/utils').install(require('./plugin-config.json'))\"" } } diff --git a/scripts/build/publish.sh b/scripts/build/publish.sh index f6597eb4a3..0fe00596fc 100644 --- a/scripts/build/publish.sh +++ b/scripts/build/publish.sh @@ -1,10 +1,15 @@ CORE="./dist/packages-dist/@ionic-native/core/" PLUGINS="./src/@ionic-native/plugins/*" +UTILS="./dist/packages-dist/@ionic-native/utils/" BUILD_PLUGINS_DIST='dist/packages-dist/@ionic-native/plugins' echo "Publishing @ionic-native/core" npm publish "$CORE" --access public + +echo "Publishing @ionic-native/utils" +npm publish "$UTILS" --access public + # For each plugin, replace the values in tsconfig w/ the appropriate ones for this plugin for d in $PLUGINS ; do BASE=`basename $d` diff --git a/scripts/build/utils-package.json b/scripts/build/utils-package.json new file mode 100644 index 0000000000..de630377f4 --- /dev/null +++ b/scripts/build/utils-package.json @@ -0,0 +1,16 @@ +{ + "name": "@ionic-native/utils", + "version": "{{VERSION}}", + "description": "Ionic Native - Native plugins for ionic apps", + "main": "index.js", + "author": "ionic", + "license": "MIT", + "dependencies": { + "prompt": "^1.0.0", + "colors": "^1.1.2" + }, + "repository": { + "type": "git", + "url": "https://github.com/driftyco/ionic-native.git" + } +} diff --git a/scripts/build/utils.js b/scripts/build/utils.js new file mode 100644 index 0000000000..5057ab95d5 --- /dev/null +++ b/scripts/build/utils.js @@ -0,0 +1,94 @@ +var prompt = require('prompt'), + colors = require('colors'), + exec = require('child_process').exec; + +module.exports = (function(){ + + var _self = this; + this.varValues = null; + + this.install = function(pluginConfig) { + this.pluginConfig = pluginConfig; + console.log('\n\n-- Initializing Ionic Native plugin installer --'.gray); + console.log('\nInstalling plugin: '.green + pluginConfig.name); + + if (!pluginConfig || !pluginConfig.name || !pluginConfig.locator) { + console.log('\nInvalid configuration file. Exiting Ionic Native installer.'.red); + return; + } + + if (pluginConfig.variables) { + this.processVariables(); + } else { + this.execInstall(); + } + }; + + this.processVariables = function() { + console.log('\nThis plugin requires variables to install. Please specify values for these variables to complete the process.'.green); + + prompt.message = null; + prompt.delimiter = ': '; + prompt.colors = false; + + prompt.get(this.pluginConfig.variables, function(err, result) { + if (err) { + console.log('\n\nAn error occurred while processing your input. Please try again.'.red); + } else { + _self.confirmValues(result); + } + }); + }; + + this.confirmValues = function(values) { + prompt.message = ''; + prompt.getInput('\nAre you sure you want to continue with the values you provided? (Y/n)'.green, function(err, result) { + if (!result || result.toLowerCase() == 'y') { + _self.varValues = values; + _self.execInstall(); + } else { + _self.processVariables(); + } + }); + }; + + this.parseValues = function() { + if (!this.varValues) return ''; + var args = ''; + for (var prop in this.varValues) { + args += ' --variable '; + args += prop; + args += '='; + + if (typeof this.varValues[prop] === 'string') { + this.varValues[prop] = this.varValues[prop].trim(); + } + + if (/\s/g.test(this.varValues[prop]) && this.varValues[prop].substr(0,1).indexOf(['\'', '"']) === -1) { + // has whitespace, and not quoted + this.varValues[prop] = '"' + this.varValues[prop] + '"'; + } + + args += this.varValues[prop]; + } + return args; + }; + + this.execInstall = function() { + var installCommand = 'ionic plugin add ' + this.pluginConfig.locator; + if (this.varValues) { + installCommand += this.parseValues(); + } + exec(installCommand, function(err, stdout, stderr) { + if (err || stderr) { + console.log('\nAn error occurred while trying to install the plugin. Please install the plugin manually or re-install this package to try again.'.red); + } else { + var message = '\n\n' + _self.pluginConfig.name + ' plugin was successfully installed! Happy coding!'; + console.log(message.green); + } + }); + }; + + return this; + +})(); diff --git a/src/@ionic-native/core/plugin.ts b/src/@ionic-native/core/plugin.ts index a141bdd640..db3543148f 100644 --- a/src/@ionic-native/core/plugin.ts +++ b/src/@ionic-native/core/plugin.ts @@ -34,6 +34,10 @@ export interface PluginConfig { * Custom install command */ install?: string; + /** + * Available installation variables + */ + installVariables?: string[]; /** * Supported platforms */ diff --git a/src/@ionic-native/plugins/deeplinks/index.ts b/src/@ionic-native/plugins/deeplinks/index.ts index bceb5da6a0..1505a26345 100644 --- a/src/@ionic-native/plugins/deeplinks/index.ts +++ b/src/@ionic-native/plugins/deeplinks/index.ts @@ -77,7 +77,8 @@ export interface DeeplinkMatch { pluginRef: 'IonicDeeplink', repo: 'https://github.com/driftyco/ionic-plugin-deeplinks', platforms: ['iOS', 'Android'], - install: 'ionic plugin add ionic-plugin-deeplinks --variable URL_SCHEME=myapp --variable DEEPLINK_SCHEME=https --variable DEEPLINK_HOST=example.com --variable ANDROID_PATH_PREFIX=/' + install: 'ionic plugin add ionic-plugin-deeplinks --variable URL_SCHEME=myapp --variable DEEPLINK_SCHEME=https --variable DEEPLINK_HOST=example.com --variable ANDROID_PATH_PREFIX=/', + installVariables: ['URL_SCHEME', 'DEEPLINK_SCHEME', 'DEEPLINK_HOST', 'ANDROID_PATH_PREFIX'] }) @Injectable() export class Deeplinks { diff --git a/src/@ionic-native/plugins/facebook/index.ts b/src/@ionic-native/plugins/facebook/index.ts index f1a208543d..f96162b394 100644 --- a/src/@ionic-native/plugins/facebook/index.ts +++ b/src/@ionic-native/plugins/facebook/index.ts @@ -105,7 +105,8 @@ export interface FacebookLoginResponse { plugin: 'cordova-plugin-facebook4', pluginRef: 'facebookConnectPlugin', repo: 'https://github.com/jeduan/cordova-plugin-facebook4', - install: 'ionic plugin add cordova-plugin-facebook4 --variable APP_ID="123456789" --variable APP_NAME="myApplication"' + install: 'ionic plugin add cordova-plugin-facebook4 --variable APP_ID="123456789" --variable APP_NAME="myApplication"', + installVariables: ['APP_ID', 'APP_NAME'] }) @Injectable() export class Facebook { diff --git a/src/@ionic-native/plugins/google-plus/index.ts b/src/@ionic-native/plugins/google-plus/index.ts index 07e48f0d9c..9260511b9e 100644 --- a/src/@ionic-native/plugins/google-plus/index.ts +++ b/src/@ionic-native/plugins/google-plus/index.ts @@ -20,7 +20,8 @@ import { Cordova, Plugin } from '@ionic-native/core'; pluginRef: 'window.plugins.googleplus', repo: 'https://github.com/EddyVerbruggen/cordova-plugin-googleplus', platforms: ['Web', 'Android', 'iOS'], - install: 'ionic plugin add cordova-plugin-googleplus --variable REVERSED_CLIENT_ID=myreversedclientid' + install: 'ionic plugin add cordova-plugin-googleplus --variable REVERSED_CLIENT_ID=myreversedclientid', + installVariables: ['REVERSED_CLIENT_ID'] }) @Injectable() export class GooglePlus { diff --git a/src/@ionic-native/plugins/googlemap/index.ts b/src/@ionic-native/plugins/googlemap/index.ts index 7fabec853e..339e429762 100644 --- a/src/@ionic-native/plugins/googlemap/index.ts +++ b/src/@ionic-native/plugins/googlemap/index.ts @@ -481,7 +481,8 @@ export class MapPage { pluginRef: 'plugin.google.maps.Map', plugin: 'cordova-plugin-googlemaps', repo: 'https://github.com/mapsplugin/cordova-plugin-googlemaps', - install: 'ionic plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"' + install: 'ionic plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"', + installVariables: ['API_KEY_FOR_ANDROID', 'API_KEY_FOR_IOS'] }) @Injectable() export class GoogleMap { diff --git a/src/@ionic-native/plugins/pinterest/index.ts b/src/@ionic-native/plugins/pinterest/index.ts index 213e51d6af..96f95639d8 100644 --- a/src/@ionic-native/plugins/pinterest/index.ts +++ b/src/@ionic-native/plugins/pinterest/index.ts @@ -169,6 +169,7 @@ export interface PinterestPin { pluginRef: 'cordova.plugins.Pinterest', repo: 'https://github.com/zyramedia/cordova-plugin-pinterest', install: 'ionic plugin add cordova-plugin-pinterest --variable APP_ID=YOUR_APP_ID', + installVariables: ['APP_ID'], platforms: ['Android', 'iOS'] }) @Injectable() diff --git a/src/@ionic-native/plugins/push/index.ts b/src/@ionic-native/plugins/push/index.ts index 19691143c9..b453a539ee 100644 --- a/src/@ionic-native/plugins/push/index.ts +++ b/src/@ionic-native/plugins/push/index.ts @@ -323,7 +323,8 @@ declare var PushNotification: { plugin: 'phonegap-plugin-push', pluginRef: 'PushNotification', repo: 'https://github.com/phonegap/phonegap-plugin-push', - install: 'ionic plugin add phonegap-plugin-push --variable SENDER_ID=XXXXXXXXX' + install: 'ionic plugin add phonegap-plugin-push --variable SENDER_ID=XXXXXXXXX', + installVariables: ['SENDER_ID'] }) @Injectable() export class Push { diff --git a/src/@ionic-native/plugins/rollbar/index.ts b/src/@ionic-native/plugins/rollbar/index.ts index f738dc3c6b..5ae62dcf34 100644 --- a/src/@ionic-native/plugins/rollbar/index.ts +++ b/src/@ionic-native/plugins/rollbar/index.ts @@ -21,7 +21,8 @@ import { Plugin, Cordova } from '@ionic-native/core'; pluginRef: 'Rollbar', repo: 'https://github.com/Resgrid/cordova-plugins-rollbar', platforms: ['Android', 'iOS'], - install: 'ionic plugin add resgrid-cordova-plugins-rollbar --variable ROLLBAR_ACCESS_TOKEN="YOUR_ROLLBAR_ACCEESS_TOKEN" --variable ROLLBAR_ENVIRONMENT="ROLLBAR_ENVIRONMENT"' + install: 'ionic plugin add resgrid-cordova-plugins-rollbar --variable ROLLBAR_ACCESS_TOKEN="YOUR_ROLLBAR_ACCEESS_TOKEN" --variable ROLLBAR_ENVIRONMENT="ROLLBAR_ENVIRONMENT"', + installVariables: ['ROLLBAR_ACCESS_TOKEN', 'ROLLBAR_ENVIRONMENT'] }) @Injectable() export class Rollbar { diff --git a/src/@ionic-native/plugins/twitter-connect/index.ts b/src/@ionic-native/plugins/twitter-connect/index.ts index 3915cbf442..731063a5ae 100644 --- a/src/@ionic-native/plugins/twitter-connect/index.ts +++ b/src/@ionic-native/plugins/twitter-connect/index.ts @@ -52,7 +52,8 @@ export interface TwitterConnectResponse { plugin: 'twitter-connect-plugin', pluginRef: 'TwitterConnect', repo: 'https://github.com/ManifestWebDesign/twitter-connect-plugin', - install: 'ionic plugin add twitter-connect-plugin --variable FABRIC_KEY=fabric_API_key' + install: 'ionic plugin add twitter-connect-plugin --variable FABRIC_KEY=fabric_API_key', + installVariables: ['FABRIC_KEY'] }) @Injectable() export class TwitterConnect {