From 542e7d24f097fd8bcc44e51f49fad89c950e799a Mon Sep 17 00:00:00 2001
From: Ibby Hadeed <ibby93@gmail.com>
Date: Thu, 28 Dec 2017 22:12:48 -0500
Subject: [PATCH] Extend IonicNativePlugin

---
 package-lock.json                             | 21 ++++++++
 package.json                                  |  4 +-
 scripts/tasks/build-es5.ts                    |  5 +-
 scripts/tasks/publish.ts                      | 53 +++++++++++++------
 src/@ionic-native/plugins/admob-free/index.ts |  6 +--
 5 files changed, 66 insertions(+), 23 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 9d5c3339..63d897f3 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -507,6 +507,27 @@
       "integrity": "sha1-GdOGodntxufByF04iu28xW0zYC0=",
       "dev": true
     },
+    "async-promise-queue": {
+      "version": "1.0.4",
+      "resolved": "https://registry.npmjs.org/async-promise-queue/-/async-promise-queue-1.0.4.tgz",
+      "integrity": "sha512-GQ5X3DT+TefYuFPHdvIPXFTlKnh39U7dwtl+aUBGeKjMea9nBpv3c91DXgeyBQmY07vQ97f3Sr9XHqkamEameQ==",
+      "dev": true,
+      "requires": {
+        "async": "2.6.0",
+        "debug": "2.6.9"
+      },
+      "dependencies": {
+        "async": {
+          "version": "2.6.0",
+          "resolved": "https://registry.npmjs.org/async/-/async-2.6.0.tgz",
+          "integrity": "sha512-xAfGg1/NTLBBKlHFmnd7PlmUW9KhVQIUuSrYem9xzFUZy13ScvtyGGejaae9iAVRiRq9+Cx7DPFaAAhCpyxyPw==",
+          "dev": true,
+          "requires": {
+            "lodash": "4.17.4"
+          }
+        }
+      }
+    },
     "asynckit": {
       "version": "0.4.0",
       "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
diff --git a/package.json b/package.json
index 3483d8e7..09b6627b 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,8 @@
     "lint": "gulp lint",
     "readmes": "gulp readmes",
     "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
-    "postchangelog": "git commit -am \"chore(): update changelog\""
+    "postchangelog": "git commit -am \"chore(): update changelog\"",
+    "shipit": "npm run build && npm run readmes && npm run npmpub"
   },
   "devDependencies": {
     "@angular/common": "latest",
@@ -32,6 +33,7 @@
     "@types/node": "8.0.47",
     "@types/rimraf": "^2.0.2",
     "@types/webpack": "^3.8.1",
+    "async-promise-queue": "^1.0.4",
     "babel-core": "^6.26.0",
     "babel-preset-env": "^1.6.1",
     "babelify": "^8.0.0",
diff --git a/scripts/tasks/build-es5.ts b/scripts/tasks/build-es5.ts
index 0091b06f..43588336 100644
--- a/scripts/tasks/build-es5.ts
+++ b/scripts/tasks/build-es5.ts
@@ -25,7 +25,7 @@ const webpackConfig: webpack.Configuration = {
     modules: ['node_modules'],
     extensions: ['.js'],
     alias: {
-      '@ionic-native/core': path.resolve(DIST, 'core/index.js')
+      '@ionic-native/core': path.resolve(DIST, '@ionic-native/core/index.js')
     }
   },
   module: {
@@ -44,7 +44,8 @@ const webpackConfig: webpack.Configuration = {
     }),
     new uglifyJsPlugin({
       sourceMap: true
-    })
+    }),
+
   ]
 };
 
diff --git a/scripts/tasks/publish.ts b/scripts/tasks/publish.ts
index df76ea7c..22785d68 100644
--- a/scripts/tasks/publish.ts
+++ b/scripts/tasks/publish.ts
@@ -3,20 +3,22 @@ import * as path from 'path';
 import { merge } from 'lodash';
 import { exec } from 'child_process';
 import { PLUGIN_PATHS, ROOT } from '../build/helpers';
+import { cpus } from 'os';
+import * as Queue from 'async-promise-queue';
 
 const MAIN_PACKAGE_JSON = require('../../package.json');
 const VERSION = MAIN_PACKAGE_JSON.version;
 const FLAGS = '--access public --tag beta';
 
 const PACKAGE_JSON_BASE = {
-  "description": "Ionic Native - Native plugins for ionic apps",
-  "module": "index.js",
-  "typings": "index.d.ts",
-  "author": "ionic",
-  "license": "MIT",
-  "repository": {
-    "type": "git",
-    "url": "https://github.com/ionic-team/ionic-native.git"
+  'description': 'Ionic Native - Native plugins for ionic apps',
+  'module': 'index.js',
+  'typings': 'index.d.ts',
+  'author': 'ionic',
+  'license': 'MIT',
+  'repository': {
+    'type': 'git',
+    'url': 'https://github.com/ionic-team/ionic-native.git'
   }
 };
 
@@ -63,17 +65,34 @@ function prepare() {
   });
 }
 
-async function publish() {
-  // TODO apply queue system to process them concurrently
-  for (let pkg of PACKAGES) {
-    // console.log('Going to run the following command: ', `npm publish ${ pkg } ${ FLAGS }`);
-    await new Promise<any>((resolve, reject) => {
-      exec(`npm publish ${ pkg } ${ FLAGS }`, (err, stdout, stderr) => {
-        if (err) reject(err);
-        if (stderr) reject(stderr);
-        if (stdout) resolve(stdout);
+async function publish(ignoreErrors: boolean = false) {
+  // upload 1 package per CPU thread at a time
+  const worker = Queue.async.asyncify((pkg: any) => {
+    new Promise<any>((resolve, reject) => {
+      exec(`npm publish ${ pkg } ${ FLAGS }`, (err, stdout) => {
+        if (stdout) {
+          console.log(stdout);
+          resolve(stdout);
+        }
+        if (err) {
+          if (!ignoreErrors) {
+            if (err.message.includes('You cannot publish over the previously published version')) {
+              console.log('Ignoring duplicate version error.');
+              return resolve();
+            }
+            reject(err);
+          }
+        }
       });
     });
+  });
+
+  try {
+    await Queue(worker, PACKAGES, cpus().length);
+    console.log('Done publishing!');
+  } catch (e) {
+    console.log('Error publishing!');
+    console.log(e);
   }
 }
 
diff --git a/src/@ionic-native/plugins/admob-free/index.ts b/src/@ionic-native/plugins/admob-free/index.ts
index 292eac08..6a26006b 100644
--- a/src/@ionic-native/plugins/admob-free/index.ts
+++ b/src/@ionic-native/plugins/admob-free/index.ts
@@ -72,7 +72,7 @@ export interface AdMobFreeRewardVideoConfig {
   plugin: 'cordova-plugin-admob-free',
   pluginRef: 'admob.banner',
 })
-export class AdMobFreeBanner {
+export class AdMobFreeBanner extends IonicNativePlugin {
 
   /**
    * Update config.
@@ -130,7 +130,7 @@ export class AdMobFreeBanner {
   plugin: 'cordova-plugin-admob-free',
   pluginRef: 'admob.interstitial',
 })
-export class AdMobFreeInterstitial {
+export class AdMobFreeInterstitial extends IonicNativePlugin {
 
   /**
    * Update config.
@@ -179,7 +179,7 @@ export class AdMobFreeInterstitial {
   plugin: 'cordova-plugin-admob-free',
   pluginRef: 'admob.rewardvideo',
 })
-export class AdMobFreeRewardVideo {
+export class AdMobFreeRewardVideo extends IonicNativePlugin {
 
   /**
    * Update config.