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

Commit

Permalink
Extracted sync code
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed May 30, 2017
1 parent 93ce9fe commit ceae5d9
Show file tree
Hide file tree
Showing 15 changed files with 6,002 additions and 2,320 deletions.
282 changes: 13 additions & 269 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ module.exports = function(grunt) {
pkg: pkg,
meta: {},
jshint: {
all: ['src/modules/**/*.js',
'!src/modules/ajax.js',
'src/appforms/src/core/*.js',
'src/appforms/src/backbone/*.js',
'!src/appforms/src/core/000*.js',
'!src/appforms/src/core/060*.js',
'!src/appforms/src/core/999*.js',
'!src/appforms/src/backbone/000*.js',
'!src/appforms/src/backbone/001*.js',
'!src/appforms/src/backbone/999*.js'],
all: ['src/**/*.js'],
options: {
curly: true,
eqeqeq: true,
Expand All @@ -41,17 +32,6 @@ module.exports = function(grunt) {
],
dest: "libs/generated/lawnchair.js"
},
lawnchair_titanium: {
src: [
"libs/generated/lawnchair.js",
"libs/lawnchair/lawnchairTitanium.js",
],
dest: "libs/generated/lawnchair.js"
},
titanium_globals : {
src : ["src/modules/titanium/ti.js", "dist/feedhenry-titanium.js"],
dest : "dist/feedhenry-titanium.js"
},
crypto: {
src:[
"libs/cryptojs/cryptojs-core.js",
Expand All @@ -66,30 +46,6 @@ module.exports = function(grunt) {
"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_core_no_v2: {
"src": ["src/appforms/src/core/*.js", "!src/appforms/src/core/000-api-v2.js"],
"dest": "libs/generated/appForms/appForms-core-no-v2.js"
},
forms_backbone: {
"src": ["src/appforms/src/backbone/*.js", "!src/appforms/src/backbone/000-closureStartRequireJS.js", "!src/appforms/src/backbone/999-closureEndRequireJS.js", "!src/appforms/src/backbone/templates.js"],
"dest": "dist/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", "!src/appforms/src/backbone/templates.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"
},
forms_appFormsTest: {
"src": ["dist/feedhenry.js"],
"dest": "src/appforms/tests/feedhenry.js"
}
},
'mocha_phantomjs': {
Expand Down Expand Up @@ -124,76 +80,22 @@ module.exports = function(grunt) {
// 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).
// which registers fhsync as a global var (the module itself registers as $fh.sync as well).
dist:{
//shim is defined inside package.json
src:['src/feedhenry.js'],
dest: 'dist/feedhenry.js',
src:['src/index.js'],
dest: 'dist/fh-sync.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){
var version = pkg.version;
console.log("found current version = " + version);
if(process.env.TRAVIS_BUILD_NUMBER){
console.log("found BUILD_NUMBER in process.env " + process.env.TRAVIS_BUILD_NUMBER);
version = version + '-' + process.env.TRAVIS_BUILD_NUMBER;
}
console.log("Version to inject is " + version);
t = data.replace("BUILD_VERSION", version);
}
this.queue(t);
this.queue(null);
}
return through(write, end);
}]
standalone: 'fhsync'
}
},
dist_titanium:{
//shim is defined inside package.json
src:['src/feedhenry.js'],
dest: 'dist/feedhenry-titanium.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){
var version = pkg.version;
console.log("found current version = " + version);
if(process.env.TRAVIS_BUILD_NUMBER){
console.log("found BUILD_NUMBER in process.env " + process.env.TRAVIS_BUILD_NUMBER);
version = version + '-' + process.env.TRAVIS_BUILD_NUMBER;
}
console.log("Version to inject is " + version);
t = data.replace("BUILD_VERSION", version);
}
this.queue(t);
this.queue(null);
}
return through(write, end);
}],
alias: ['./src/modules/titanium/cookies.js:./cookies',
'./src/modules/titanium/appProps.js:./appProps',
'./src/modules/titanium/appProps.js:./modules/appProps'
]
}
},
// This browserify build can be required by other browserify modules that
// This browserify build can be required by other browserify that
// have been created with an --external parameter.
require: {
src:['src/feedhenry.js'],
dest: 'test/browser/feedhenry-latest-require.js',
src:['src/index.js'],
dest: 'test/browser/fh-sync-latest-require.js',
options: {
alias:['./src/feedhenry.js']
alias:['./src/sync-client.js']
}
},
// These are the browserified tests. We need to browserify the tests to be
Expand All @@ -205,46 +107,11 @@ module.exports = function(grunt) {
src: [ './test/browser/suite.js' ],
dest: './test/browser/browserified_tests.js',
options: {
external: [ './src/feedhenry.js' ],
ignore: ['../../src-cov/modules/ajax', '../../src-cov/modules/events', '../../src-cov/modules/queryMap', '../../src-cov/modules/sync-cli', '../../src-cov/feedhenry'],
external: [ './src/index.js' ],
// Embed source map for tests
debug: true
}
},
require_cov: {
src:['src-cov/feedhenry.js'],
dest: 'test/browser/feedhenry-latest-require.js',
options: {
alias:['./src-cov/feedhenry.js']
}
},
test_cov: {
src: [ './test/browser/suite.js' ],
dest: './test/browser/browserified_tests.js',
options: {
external: [ './src-cov/feedhenry.js' ],
// Embed source map for tests
debug: true,
add: {
"LIB_COV": 1
}
}
}
},
replace: {
forms_templates: {
src: ["src/appforms/src/backbone/templates.js"],
dest: "src/appforms/src/backbone/040-view00Templates.js",
options: {
processTemplates: false
},
replacements: [{
from: '************TEMPLATES***************', // string replacement
to: function(){
return grunt.file.read("src/appforms/src/backbone/040-view00Templates.html", {encoding: 'utf8'}).replace(/(\r\n|\n|\r)/gm,"");
}
}]
}
},
watch: {
browserify: {
Expand All @@ -258,60 +125,12 @@ module.exports = function(grunt) {
uglify: {
dist: {
"files": {
'dist/feedhenry.min.js': ['dist/feedhenry.js'],
'dist/feedhenry-forms.min.js': ['dist/feedhenry-forms.js'],
'dist/feedhenry-titanium.min.js': ['dist/feedhenry-titanium.js']
'dist/fh-sync.min.js': ['dist/fh-sync.js'],
}
}
},
zip: {
project: {
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']
},
sdk: {
router: function(filepath) {
grunt.log.writeln(filepath);
var filename = path.basename(filepath);
return 'feedhenry-js/' + filename;
},
dest: 'dist/feedhenry-js.zip',
src:['dist/feedhenry.js', 'dist/feedhenry-forms.js', 'dist/feedhenry.min.js', 'dist/feedhenry-forms.min.js', 'dist/appForms-backbone.js']
},
titanium: {
router: function(filepath) {
grunt.log.writeln(filepath);
var filename = path.basename(filepath);
return 'feedhenry-titanium/' + filename;
},
dest: 'dist/feedhenry-titanium.zip',
src:['dist/feedhenry-titanium.js', 'dist/feedhenry-titanium.min.js']
}
},
shell: {
jscov: {
//NOTE: install node-jscoverage first from here: https://github.com/visionmedia/node-jscoverage
command: 'jscoverage src/ src-cov/ --exclude=appforms',
options: {
stdout: true
}
},
htmlcov: {
//NOTE: install jsoncov2htmlcov first from here: https://github.com/plasticine/json2htmlcov
command: 'json2htmlcov rep/coverage.json > rep/coverage.html',
options: {
stdout: true
}
}
}
});

grunt.loadNpmTasks('grunt-zip');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
Expand All @@ -323,86 +142,11 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-text-replace');

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('node', [__dirname + './bin/' + 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:all', 'browserify:dist', 'browserify:require', 'browserify:test', 'connect:server', 'mocha_phantomjs:test']);

grunt.registerTask('concat-forms-backbone', ['jshint', 'replace:forms_templates', 'concat:forms_backbone', 'concat:forms_backboneRequireJS']);

grunt.registerTask('concat-core-sdk', ['jshint', 'concat:lawnchair', 'concat:crypto', 'browserify:dist', 'concat:forms_core', 'concat:forms_sdk','concat:forms_core_no_v2', 'concat-forms-backbone']);


grunt.registerTask('concat-titanium', ['concat:lawnchair', 'concat:lawnchair_titanium', 'concat:crypto']);

// We need to ensure that the Titanium globals (definition of window, document, navigator) are at the very top of the file
grunt.registerTask('concat-titanium-globals', ['concat:titanium_globals']);

grunt.registerTask('titanium', 'concat-titanium browserify:dist_titanium concat-titanium-globals');
grunt.registerTask('concat-core-sdk', ['jshint', 'concat:lawnchair', 'concat:crypto', 'browserify:dist']);

grunt.registerTask('coverage', ['shell:jscov', 'browserify:require_cov', 'browserify:test_cov', 'connect:server', 'mocha_phantomjs:test_coverage', 'shell:htmlcov']);

grunt.registerTask('default', 'jshint concat-core-sdk concat:forms_appFormsTest test titanium uglify:dist zip');
grunt.registerTask('default', ['jshint', 'concat-core-sdk', 'test','uglify:dist']);
};
Loading

0 comments on commit ceae5d9

Please sign in to comment.