Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merged new js-sdk with appForms
Browse files Browse the repository at this point in the history
  • Loading branch information
nialldonnellyfh committed Mar 25, 2014
1 parent f60e779 commit 77740fc
Show file tree
Hide file tree
Showing 108 changed files with 98,393 additions and 2,432 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ fhc-debug.log
.DS_Store
.idea/
node_modules/
tmp/*
*~
.fhclocal/
src/appforms/dist/
247 changes: 247 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
var path = require('path');
var fs = require("fs");
var exists = fs.existsSync || path.existsSync;
var async = require("async");
var through = require('through');

module.exports = function(grunt) {
var pkg = grunt.file.readJSON('package.json');
grunt.initConfig({
pkg: pkg,
meta: {},
jshint: {
all: ['src/*.js'],
options: {
curly: true,
eqeqeq: true,
eqnull: true,
sub: true,
loopfunc: true
},
globals: {
browser: true
}
},
concat: {
lawnchair: {
src: [
"libs/lawnchair/lawnchair.js",
"libs/lawnchair/lawnchairWindowNameStorageAdapter.js",
"libs/lawnchair/lawnchairLocalStorageAdapter.js",
"libs/lawnchair/lawnchairWebkitSqlAdapter.js"
],
dest: "libs/generated/lawnchair.js"
},
crypto: {
src:[
"libs/cryptojs/cryptojs-core.js",
"libs/cryptojs/cryptojs-enc-base64.js",
"libs/cryptojs/cryptojs-cipher-core.js",
"libs/cryptojs/cryptojs-aes.js",
"libs/cryptojs/cryptojs-md5.js",
"libs/cryptojs/cryptojs-sha1.js",
"libs/cryptojs/cryptojs-x64-core.js",
"libs/cryptojs/cryptojs-sha256.js",
"libs/cryptojs/cryptojs-sha512.js",
"libs/cryptojs/cryptojs-sha3.js"
],
dest: "libs/generated/crypto.js"
},
forms_core: {
"src": "src/appforms/src/core/*.js",
"dest": "libs/generated/appForms/appForms-core.js"
},
forms_backbone: {
"src": ["src/appforms/src/backbone/*.js", "!src/appforms/src/backbone/000-closureStartRequireJS.js", "!src/appforms/src/backbone/999-closureEndRequireJS.js"],
"dest": "libs/generated/appForms/appForms-backbone.js"
},
forms_backboneRequireJS: {
"src": ["src/appforms/src/backbone/*.js", "!src/appforms/src/backbone/000-closureStart.js", "!src/appforms/src/backbone/999-closureEnd.js"],
"dest": "libs/generated/appForms/appForms-backboneRequireJS.js"
},
forms_sdk :{
"src": ["dist/feedhenry.js", "libs/generated/appForms/appForms-core.js"],
"dest": "dist/feedhenry-forms.js"
}
},
'mocha_phantomjs': {
all: {
options: {
urls: [
"http://127.0.0.1:8200/test/browser/index.html",
"http://127.0.0.1:8200/test/browser/index-require.html"
]
}
}
},
connect: {
server: {
options: {
hostname: "*",
port: 8200,
base: '.'
}
}
},
browserify: {
// This browserify build be used by users of the module. It contains a
// UMD (universal module definition) and can be used via an AMD module
// loader like RequireJS or by simply placing a script tag in the page,
// which registers feedhenry as a global var (the module itself registers as $fh as well).
dist:{
//shim is defined inside package.json
src:['src/feedhenry.js'],
dest: 'dist/feedhenry.js',
options: {
standalone: 'feedhenry',
transform: [function(file){
var data = '';

function write (buf) { data += buf }
function end () {
var t = data;
if(file.indexOf("constants.js") >= 0){
t = data.replace("BUILD_VERSION", pkg.version);
}
this.queue(t);
this.queue(null);
}
return through(write, end);
}]
}
},
// This browserify build can be required by other browserify modules that
// have been created with an --external parameter.
require: {
src:['src/feedhenry.js'],
dest: 'test/browser/feedhenry-latest-require.js',
options: {
alias:['./src/feedhenry.js']
}
},
// These are the browserified tests. We need to browserify the tests to be
// able to run the mocha tests while writing the tests as clean, simple
// CommonJS mocha tests (that is, without cross-platform boilerplate
// code). This build will also include the testing libs chai, sinon and
// sinon-chai but must not include the module under test.
test: {
src: [ './test/browser/suite.js' ],
dest: './test/browser/browserified_tests.js',
options: {
external: [ './src/feedhenry.js' ],
// Embed source map for tests
debug: true
}
}
},
watch: {
browserify: {
files: ['src/**/*.js', 'test/tests/*.js'],
tasks: ['browserify'],
options: {
spawn: false
}
}
},
uglify: {
dist: {
"files": {
'dist/feedhenry.min.js': ['dist/feedhenry.js'],
'dist/feedhenry-forms.min.js': ['dist/feedhenry-forms.js']
}
}
},
zip: {
zipall: {
router: function(filepath) {
grunt.log.writeln(filepath);
var filename = path.basename(filepath);
return 'feedhenry-js-sdk/' + filename;
},
dest: 'dist/fh-starter-project-latest.zip',
src: ['src/index.html', 'src/fhconfig.json', 'dist/feedhenry.min.js']
}
}
});

grunt.loadNpmTasks('grunt-zip');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-mocha-phantomjs');
grunt.loadNpmTasks('grunt-contrib-watch');

var spawns = [];
grunt.registerTask('start-local-servers', function () {
var done = this.async();
var spawn = require('child_process').spawn;

var spawnTestCloudServer = function (port, script, cb) {
grunt.log.writeln('Spawning server on port ' + port + ' in cwd ' + __dirname + ' using file ' + __dirname + '/' + script);
var env = {};
env.FH_PORT = port;
var server = spawn('/usr/bin/env', ['node', __dirname + '/' + script], {
cwd: __dirname,
env: env
}).on('exit', function (code) {
grunt.log.writeln('Exiting server on port ' + port + ' with exit code ' + code);
});
server.stdout.on('data', function (data) {
grunt.log.writeln('Spawned Server port ' + port + ' stdout:' + data);
if(data.toString("utf8").indexOf("started") !== -1){
cb(null, null);
}
});
server.stderr.on('data', function (data) {
grunt.log.writeln('Spawned Server port ' + port + ' stderr:' + data);
if(data.toString("utf8").indexOf("Error:") !== -1){
cb(data.toString("utf8"), null);
}
});
grunt.log.writeln('Spawned server on port ' + port);
spawns.push(server);
};

var servers = [{port: 8100, file:"bin/appinit.js"}, {port: 8101, file:"bin/appcloud.js"}];
async.map(servers, function(conf, cb){
spawnTestCloudServer(conf.port, conf.file, cb);
}, function(err){
if(err) {
grunt.log.writeln("Failed to start server. Error: " + err);
return done(false);
}
return done();
});

});

var stopLocalServers = function(){
spawns.forEach(function (server) {
grunt.log.writeln("Killing process " + server.pid);
server.kill();
});
}

process.on('exit', function() {
console.log('killing spawned servers if there are any');
stopLocalServers();
});

grunt.registerTask('stop-local-servers', function(){
stopLocalServers();
});

//use this task for local development. Load example/index.html file in the browser after server started.
//can run grunt watch as well in another terminal to auto generate the combined js file
grunt.registerTask('local', ['start-local-servers', 'connect:server:keepalive']);

//run tests in phatomjs
grunt.registerTask('test', ['jshint', 'browserify', 'connect:server', 'mocha_phantomjs']);

grunt.registerTask('concat-core-sdk', ['concat:lawnchair', 'concat:crypto', 'concat:forms_core', 'concat:forms_backbone', 'concat:forms_backboneRequireJS']);

grunt.registerTask('default', 'jshint concat-core-sdk test concat:forms_sdk uglify:dist zip');
};
64 changes: 59 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FeedHenry JavaScript SDK
========================
[![browser support](https://ci.testling.com/feedhenry/fh-js-sdk.png)
](https://ci.testling.com/feedhenry/fh-js-sdk)

The JavaScript SDK allows developers to integrate the FeedHenry Cloud into any web-based solution - desktop websites, mobile websites or a stand-alone JavaScript client.

The API is provided in the $fh namespace and uses a common convention for most functions, which takes the format:
Expand All @@ -13,11 +16,62 @@ The successFunction callback is called with one argument, a result object, which
Detailed documentation for the JavaScript SDK's API can be found here: http://docs.feedhenry.com/v2/api_js_client_api.html

Building
========
The jssdk is now build using Grunt (install via npm install). To add new files edit the grunt.js file under concat. Thirdparty libraries should go under libs and our modules go under src.
=========
The JS SDK is now built using [Browserify](http://browserify.org/).

### Development

Because of Browserify, you can write any new functions as normal node modules, and use node's "require" to load any other modules, or to be consumed by other modules.

### Testing

Write your tests in test/tests directory. Add the tests to test/browser/suite.js file and run

```
grunt test
```

This will use mocha and phatomjs to run the tests.

In fact, if your module and test don't require a browser environment, you can just run them purely in node. (You may need to add a new grunt task to run them).

To help debugging, you can run

```
grunt local
```

This will start mock servers locally and you can go to http://localhost:8100/example/index.html page to debug. You may want to run

```
grunt watch
```

In another terminal window to auto generate the combined js sdk file.

### Testling

For browser compatibility testing, we use [Testling](https://ci.testling.com/). The project page is here: https://ci.testling.com/feedhenry/fh-js-sdk.

You can also run testling locally:

```
npm install -g testling
testling
```

If testling can not find any browser locally, you either need to add browser paths to your PATH environment variable, or use

```
testling -u
```
and copy the url to a browser.

To build, run:
### Build

grunt
When finish developing and testing, run

There are qunit tests. You will need to install phantomjs to run these tests.
```
grunt
```
To generate the release builds.
7 changes: 5 additions & 2 deletions accept.js → bin/appcloud.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var nodeapp = require("fh-nodeapp");

console.log('Initing hostapp on port ' + process.env.FH_PORT);
nodeapp.HostApp.init();
nodeapp.HostApp.serveApp({
echo: function(params, callback) {
return callback(null, {
echo: params.msg,
echo: params.msg + '-' + process.env.FH_PORT,
hardcoded: 'hardcodedmsg'
});
}
});
});
console.log('Inited hostapp on port ' + process.env.FH_PORT);
41 changes: 41 additions & 0 deletions bin/appinit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var express = require("express");

var app = express();

var hosturl = "http://localhost:8101";
app.use(express.bodyParser());
app.use(app.router);

var initData = {
domain: "testing",
firstTime: false,
hosts: {
"url": hosturl
},
init: {
"trackId": "testtrackid"
}
}

/*app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, X-Request-With, Content-Type");
next();
});*/

app.get("/box/:servlet/:version/app/init", function(req, res){
console.log("Got GET request for app init");
if(req.query._callback){
res.send(req.query._callback + "(" + JSON.stringify(initData) + ")");
} else {
res.send("no callbackId");
}
});

app.post("/box/:servlet/:version/app/init", function(req, res){
console.log("Got POST request for app init");
res.send(initData);
});

app.listen(8100);
console.log('Server started on port ' + 8100);
Loading

0 comments on commit 77740fc

Please sign in to comment.