Skip to content

Commit

Permalink
chore(build): add auto install scripts (#1059)
Browse files Browse the repository at this point in the history
* feat(core): auto install scripts

* minor fixes

* minor fixes

* remove prompt message
  • Loading branch information
ihadeed authored Feb 13, 2017
1 parent 1dfdbaf commit 6de9697
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
54 changes: 36 additions & 18 deletions scripts/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
})
Expand All @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions scripts/build/plugin-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Facebook",
"variables": [
"APP_ID",
"SOME_OTHER_VAR",
"AND_ANOTHER_ONE"
],
"locator": "cordova-plugin-facebook4"
}
4 changes: 4 additions & 0 deletions scripts/build/plugin-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'))\""
}
}
5 changes: 5 additions & 0 deletions scripts/build/publish.sh
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
16 changes: 16 additions & 0 deletions scripts/build/utils-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
94 changes: 94 additions & 0 deletions scripts/build/utils.js
Original file line number Diff line number Diff line change
@@ -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;

})();
4 changes: 4 additions & 0 deletions src/@ionic-native/core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface PluginConfig {
* Custom install command
*/
install?: string;
/**
* Available installation variables
*/
installVariables?: string[];
/**
* Supported platforms
*/
Expand Down
3 changes: 2 additions & 1 deletion src/@ionic-native/plugins/deeplinks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/@ionic-native/plugins/facebook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/@ionic-native/plugins/google-plus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/@ionic-native/plugins/googlemap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/@ionic-native/plugins/pinterest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/@ionic-native/plugins/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/@ionic-native/plugins/rollbar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/@ionic-native/plugins/twitter-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6de9697

Please sign in to comment.