diff --git a/gulpfile.js b/gulpfile.js index 214c3e1f7..969bb5aea 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,24 +1,55 @@ -var gulp = require("gulp"); -sourcemaps = require("gulp-sourcemaps"), - babel = require("gulp-babel"), +var gulp = require("gulp"), + source = require('vinyl-source-stream'), + buffer = require('vinyl-buffer'), + sourcemaps = require("gulp-sourcemaps"), concat = require("gulp-concat"), - connect = require('gulp-connect'); + connect = require('gulp-connect'), + browserify = require('browserify'), + watchify = require('watchify'), + babel = require('babelify'); -gulp.task("default", ['build'], function () { -}); + +function compile(watch) { + var bundler = watchify(browserify() + .require('./src/index.js', { + entry: true, + expose: 'ionic-native' + }) + .transform(babel, {presets: ['es2015']})); + + function rebundle() { + bundler.bundle() + .on('error', function(err) { console.error(err); this.emit('end'); }) + .pipe(source('ionic-native.js')) + .pipe(buffer()) + .pipe(sourcemaps.init({ loadMaps: true })) + .pipe(sourcemaps.write('./')) + .pipe(gulp.dest('./dist')); + } + + if (watch) { + bundler.on('update', function() { + console.log('-> bundling...'); + rebundle(); + }); + } + + rebundle(); +} + +function watch() { + return compile(true); +} + +gulp.task("default", ['build'], function () { }); gulp.task('build', function() { - return gulp.src("src/**/*.js") - .pipe(sourcemaps.init()) - .pipe(babel()) - .pipe(concat("ionic-native.js")) - .pipe(sourcemaps.write(".")) - .pipe(gulp.dest("dist")); + return compile(); }); -gulp.task('test', ['build', 'serve'], function() { - -}) +gulp.task('watch', function() { + return watch(); +}); gulp.task('serve', function() { connect.server({ diff --git a/package.json b/package.json new file mode 100644 index 000000000..ad54698c7 --- /dev/null +++ b/package.json @@ -0,0 +1,39 @@ +{ + "name": "ionic-native", + "version": "1.0.0", + "description": "Native plugins to replace ngCordova", + "main": "gulpfile.js", + "directories": { + "test": "test" + }, + "dependencies": { + "babel-preset-es2015": "^6.1.18", + "babelify": "^7.2.0", + "browserify": "^12.0.1", + "gulp": "^3.9.0", + "gulp-concat": "^2.6.0", + "gulp-connect": "^2.2.0", + "gulp-sourcemaps": "^1.6.0", + "vinyl-buffer": "^1.0.0", + "vinyl-source-stream": "^1.1.0", + "watchify": "^3.6.1", + "gulp-babel": "^6.1.0", + "gulp-serve": "^1.2.0" + }, + "devDependencies": { + "babel-preset-stage-0": "^6.1.18" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/driftyco/ionic-native.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/driftyco/ionic-native/issues" + }, + "homepage": "https://github.com/driftyco/ionic-native" +} diff --git a/src/cordova.js b/src/cordova.js new file mode 100644 index 000000000..33958ab48 --- /dev/null +++ b/src/cordova.js @@ -0,0 +1,16 @@ +const promisifyCordova = (pluginName, methodName) => { + return (...args) => { + return new Promise((resolve, reject) => { + if(!window.cordova) { + console.warn('Cordova: tried calling', '"' + pluginName + '.' + methodName + '"', 'but Cordova is not defined. Please make sure you have cordova.js included in your index.html file and you are running in a proper cordova environment'); + reject({ + error: 'cordova_not_available' + }); + return; + } + cordova.exec(resolve, reject, pluginName, methodName, args); + }) + } +} + +export {promisifyCordova}; diff --git a/src/index.js b/src/index.js new file mode 100644 index 000000000..fe5951d04 --- /dev/null +++ b/src/index.js @@ -0,0 +1,35 @@ +import {Plugins} from './plugins'; +import {PluginConfig} from './plugin-config' +import {promisifyCordova} from './cordova'; + +class IonicNative { + constructor() { + let pluginData, promised; + for(let plugin in PluginConfig) { + + pluginData = PluginConfig[plugin]; + console.log('Plugin', plugin, pluginData); + + promised = pluginData.promise; + + for(let method of promised) { + + + let p = promisifyCordova(plugin, method) + p().then((resp) => { + console.log('Thing'); + }) + } + + } + } + +} + + +let Native = new IonicNative; + +export default Native; + +// Because require isn't exporting properly, who the fuck knows +window.IonicNative = Native diff --git a/src/plugin-config.js b/src/plugin-config.js new file mode 100644 index 000000000..25a758aca --- /dev/null +++ b/src/plugin-config.js @@ -0,0 +1,7 @@ +export var PluginConfig = { + "camera": { + 'name': 'Camera', + 'plugin': 'cordova-plugin-camera', + "promise": ["takePicture"], + } +} diff --git a/src/plugins.js b/src/plugins.js index d00d7acc2..6d8a93338 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -1,3 +1,6 @@ -export var Native = { - thing: true, -}; + +export var Plugins = { + getPlugin(name) { + return name; + } +} diff --git a/test.js b/test.js new file mode 100644 index 000000000..29c0e4c2d --- /dev/null +++ b/test.js @@ -0,0 +1,13 @@ +require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o +

Test