diff --git a/.gitignore b/.gitignore index db8d2c4..277dd4b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.iml +/dist ## Directory-based project format: .idea/ # if you remove the above rule, at least ignore the following: diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..7fddfde --- /dev/null +++ b/.jshintrc @@ -0,0 +1,30 @@ +{ + "node": true, + "esnext": true, + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 2, + "latedef": true, + "newcap": true, + "noarg": true, + "quotmark": "single", + "regexp": true, + "undef": true, + "unused": true, + "strict": true, + "trailing": true, + "smarttabs": true, + "white": true, + "validthis": true, + "browser" : true, + "jquery":true, + + "globals": { + + "angular": false + + } +} diff --git a/app/index.html b/app/index.html index 775629e..1328e6e 100644 --- a/app/index.html +++ b/app/index.html @@ -7,8 +7,10 @@ + + @@ -58,6 +60,8 @@ + + \ No newline at end of file diff --git a/app/js/app.js b/app/js/app.js index acd2fb5..f95f90a 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -1,3 +1,4 @@ +'use strict'; /** * Created by Martin on 24/02/2016. */ @@ -7,15 +8,14 @@ $.fn.pressEnter = function (fn) { return this.each(function () { $(this).bind('enterPress', fn); $(this).keyup(function (e) { - if (e.keyCode == 13) { - $(this).trigger("enterPress"); + if (e.keyCode === 13) { + $(this).trigger('enterPress'); } - }) + }); }); }; (function () { - "use strict"; console.log('GO!'); var $list = $('#listContainer'); var displayList = function (obj) { @@ -23,7 +23,7 @@ $.fn.pressEnter = function (fn) { console.log(html); $list.empty(); $list.append(html); - $("#listContainer .entry").not('.emptyMessage').click(function () { + $('#listContainer').find('.entry').not('.emptyMessage').click(function () { console.log('Clicked list. ' + this.id); getRecipe(this.id); }); @@ -58,7 +58,7 @@ $.fn.pressEnter = function (fn) { displayPage(data); }, error: function (xhr, type) { - console.log("ajax error"); + console.log('ajax error'); console.log(xhr); console.log(type); } @@ -89,7 +89,7 @@ $.fn.pressEnter = function (fn) { displayList(data); }, error: function (xhr, type) { - console.log("ajax error"); + console.log('ajax error'); console.log(xhr); console.log(type); } @@ -113,12 +113,12 @@ $.fn.pressEnter = function (fn) { 'Access-Control-Allow-Headers': 'Content-Type' }, - success: function (data) { + success: function () { // console.log(data); // displayList(data); }, error: function (xhr, type) { - console.log("ajax error"); + console.log('ajax error'); console.log(xhr); console.log(type); } @@ -131,14 +131,14 @@ $.fn.pressEnter = function (fn) { $('#newurl').pressEnter(function () { var url = $(this).val(); - if (url != null) { + if (url !== null) { console.log('Adding: ' + url); addNew(url); $('#addstatus').fadeIn(400).delay(1500).fadeOut(400); $(this).val(''); - setTimeout((function () { + setTimeout(function () { getList(); - }), 5000); + }, 5000); } }); diff --git a/gulp/build.js b/gulp/build.js new file mode 100644 index 0000000..8f7a7b3 --- /dev/null +++ b/gulp/build.js @@ -0,0 +1,13 @@ +'use strict'; + +var gulp = require('gulp'); + + +function handleError(err) { + console.error(err.toString()); + this.emit('end'); +} + +gulp.task('clean', function () { + return gulp.src(['.tmp', 'dist'], { read: false }).pipe($.rimraf()); +}); \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..d55bb55 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,73 @@ +"use strict"; +var gulp = require('gulp'), + + autoprefixer = require('gulp-autoprefixer'), + cssnano = require('gulp-cssnano'), + jshint = require('gulp-jshint'), + uglify = require('gulp-uglify'), + + rename = require('gulp-rename'), + concat = require('gulp-concat'), + notify = require('gulp-notify'), + cache = require('gulp-cache'), + livereload = require('gulp-livereload'), + htmlmin = require('gulp-htmlmin'), + inject = require('gulp-inject'), + del = require('del'); + + + +gulp.task('scripts', function() { + return gulp.src('app/js/**/*.js') + .pipe(jshint('.jshintrc')) + .pipe(jshint.reporter('default')) + .pipe(concat('main.js')) + .pipe(gulp.dest('dist/js')) + .pipe(rename({suffix: '.min'})) + .pipe(uglify()) + .pipe(gulp.dest('dist/js')) + .pipe(notify({ message: 'Scripts task complete' })); +}); + +gulp.task('styles', function() { + return gulp.src('app/css/**/*.css') + .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4')) + .pipe(gulp.dest('dist/css')) + .pipe(rename({suffix: '.min'})) + .pipe(cssnano()) + .pipe(gulp.dest('dist/css')) + .pipe(notify({ message: 'Styles task complete' })); +}); + +gulp.task('partials', function() { + + gulp.src(['app/partials/**/*']).pipe(gulp.dest('dist/partials')); + gulp.src(['app/libs/ejs_production.js']).pipe(gulp.dest('dist/libs')); +}); + +gulp.task('minify-html', function () { + var sources = gulp.src(['dist/**/*.min.js', 'dist/**/*.min.css'], {read: false}); + var appstream = gulp.src(['dist/**/*.min.js'], {read:false}); + return gulp.src(['app/*.html']).pipe(inject(gulp.src(['./dist/js/**/*.min.js', './dist/css/**/*.min.css'], {read: false}), { + starttag: '"main": [', + endtag: ']', + transform: function (filepath, file, i, length) { + return ' "' + filepath + '"' + (i + 1 < length ? ',' : ''); + } + })) + .pipe(gulp.dest('dist')); + /*.pipe(gulp.dest('./')); + .pipe(inject(sources),{relative:true}) + /!*.pipe(htmlmin({removeComments: true, collapseWhitespace: true, keepClosingSlash: true}))*!/ + .pipe(gulp.dest('dist'));*/ +}); + + +gulp.task('clean', function() { + return del(['dist']); +}); + + +gulp.task('default', ['clean'], function() { + gulp.start('styles', 'scripts','partials','minify-html'); +}); \ No newline at end of file diff --git a/package.json b/package.json index afa825e..64c5e9a 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,26 @@ "cookie-parser": "^1.4.1", "express": "^4.13.4", "morgan": "^1.7.0", + "require-dir": "^0.3.0", "serve-favicon": "^2.3.0", "sqlite3": "^3.1.1", "string": "^3.3.1" }, "devDependencies": { + "del": "^2.2.0", + "gulp": "^3.9.1", + "gulp-autoprefixer": "^3.1.0", + "gulp-cache": "^0.4.2", + "gulp-concat": "^2.6.0", + "gulp-cssnano": "^2.1.1", + "gulp-htmlmin": "^1.3.0", + "gulp-inject": "^3.0.0", + "gulp-jshint": "^2.0.0", + "gulp-livereload": "^3.8.1", + "gulp-notify": "^2.2.0", + "gulp-rename": "^1.2.2", + "gulp-uglify": "^1.5.3", + "jshint": "^2.9.1", "jsonfile": "^2.2.3", "log4js": "^0.6.31", "simplecrawler": "^0.6.2" diff --git a/recipe-server.js b/recipe-server.js index eb07caa..70f1707 100644 --- a/recipe-server.js +++ b/recipe-server.js @@ -25,7 +25,7 @@ app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); -app.use(express.static(path.join(__dirname, 'app'))); +app.use(express.static(path.join(__dirname, 'dist'))); app.use('/recipes', recipes); //app.use('/users', users); diff --git a/server/recipes.js b/server/recipes.js index 9b813d2..5f60b73 100644 --- a/server/recipes.js +++ b/server/recipes.js @@ -137,13 +137,15 @@ router.get('/list', function(req, res) { db.all('select id, title from `recipes` where title is not null;', function (err, rows) { var out = []; + + if (rows !== null && rows.length > 0) // logger.debug(err); // logger.debug(rows); - rows.forEach(function (row) { - - out.push({"id": row.id, "title": row.title}); - }); + rows.forEach(function (row) { + out.push({"id": row.id, "title": row.title}); + }); + } res.writeHead(200, {"ContentType": "application/json"}); //res.send(JSON.stringify(t)); res.end(JSON.stringify({list:out})); @@ -163,20 +165,20 @@ router.get('/entry/:id', function(req, res) { var out = []; // logger.debug(err); // logger.debug(rows); - rows.forEach(function (row) { + if (rows !== null && rows.length > 0) + rows.forEach(function (row) { - var d = {"id": row.id, "title": row.title}; - - if (row.reduced.length !== 0) { - d.body = row.reduced; - } - else - { - d.body = row.html; - } - out.push(d); - }); + var d = {"id": row.id, "title": row.title}; + if (row.reduced.length !== 0) { + d.body = row.reduced; + } + else { + d.body = row.html; + } + out.push(d); + }); + } res.writeHead(200, {"ContentType": "application/json"}); //res.send(JSON.stringify(t)); res.end(JSON.stringify({list:out}));