From 427536debb19810672eb52c91e862491724b7da0 Mon Sep 17 00:00:00 2001 From: Justin Cooper Date: Fri, 2 Feb 2018 15:06:34 -0600 Subject: [PATCH] more refactoring of webide --- combined.log | 0 controllers/editor.js | 1 - controllers/user.js | 3 +- error.log | 0 helpers/editor_setup.js | 56 ++++++++++++++++++------- helpers/git_helper.js | 81 ++++++++++++++++++++----------------- helpers/scheduler.js | 3 +- helpers/websocket_helper.js | 3 ++ models/webideModel.js | 11 +++++ output.log | 0 package.json | 4 +- server.js | 13 ++---- 12 files changed, 105 insertions(+), 70 deletions(-) create mode 100644 combined.log create mode 100644 error.log create mode 100644 helpers/websocket_helper.js create mode 100644 models/webideModel.js create mode 100644 output.log diff --git a/combined.log b/combined.log new file mode 100644 index 0000000..e69de29 diff --git a/controllers/editor.js b/controllers/editor.js index df68ae8..61c2730 100644 --- a/controllers/editor.js +++ b/controllers/editor.js @@ -43,7 +43,6 @@ exports.editor = function(ws, req) { case 'self-check-request': winston.debug('self-check-request'); editor_setup.health_check(ws); - winston.debug('here'); break; case 'git-delete': git_helper.remove_commit_push(data.file, ws.request.session, function(err, status) { diff --git a/controllers/user.js b/controllers/user.js index 36c95d4..cb37c9c 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -1,6 +1,5 @@ var path = require('path'), - Datastore = require('nedb'), - db = new Datastore({ filename: path.resolve(process.env.PWD, 'db/webide_data_store'), autoload: true }), + db = require('../models/webideModel'), scripts_helper = require('../helpers/scripts_helper'), config = require('../config/config'), check = require('validator').check, diff --git a/error.log b/error.log new file mode 100644 index 0000000..e69de29 diff --git a/helpers/editor_setup.js b/helpers/editor_setup.js index 56c7ed0..1d185ea 100644 --- a/helpers/editor_setup.js +++ b/helpers/editor_setup.js @@ -1,29 +1,23 @@ var path = require('path'), - Datastore = require('nedb'), winston = require('winston'), - db = new Datastore({ filename: path.resolve(process.env.PWD, 'db/webide_data_store'), autoload: true }), + db = require('../models/webideModel'), exec = require('child_process').exec, fs = require ('fs'), - git_helper = require('./git_helper'), + git_helper = require('./git_helper.js'), fs_helper = require('./fs_helper'), request_helper = require('./request_helper'), + ws_helper = require('./websocket_helper'), config = require('../config/config'); fs.exists || (fs.exists = path.exists); - exports.setup_github = function(socket) { - git_helper.set_config(function() { - this.setup_adafruit_libraries(socket); - }); - }; - - exports.setup_adafruit_libraries = function(socket) { + exports.setup_adafruit_libraries = function(ws) { git_helper.clone_adafruit_libraries(config.adafruit.repository, config.adafruit.remote, function(cloned_libraries) { - socket.emit("self-check-message", "Cloning remote Adafruit repository"); + ws_helper.send_message(ws, "self-check-message", "Cloning remote Adafruit repository"); //cloned_libraries is false if they already existed...if false, let's pull the latest version of the adafruit libraries if (!cloned_libraries) { git_helper.pull(config.adafruit.repository, "origin", "master", function() { - socket.emit("self-check-message", "Adafruit repository updated"); + ws_helper.send_message(ws, "self-check-message", "Adafruit repository updated"); }); } }); @@ -38,8 +32,40 @@ var path = require('path'), settings = {offline: true}; } winston.debug("getting settings", settings); - ws.send(JSON.stringify({type: "self-check-settings", data: settings})); + ws_helper.send_message(ws, "self-check-settings", settings); }) - winston.debug('self-check-complete'); - ws.send(JSON.stringify({type: 'self-check-complete', data: true})); + + this.setup_adafruit_libraries(ws); + + git_helper.set_config(function() { + var my_repository = "my-pi-projects"; + fs_helper.check_for_repository(my_repository, function(err, exists) { + ws_helper.send_message(ws, "self-check-message", "Validated my-pi-projects"); + winston.debug(exists); + if (exists) { + git_helper.pull(my_repository, "origin", "master", function(err, status) { + ws_helper.send_message(ws, "self-check-message", "Updated my-pi-projects"); + ws_helper.send_message(ws, 'self-check-complete'); + }); + } else { + git_helper.create_local_repository(my_repository, function(err, response) { + ws_helper.send_message(ws, "self-check-message", "Cloned my-pi-projects on local system"); + fs_helper.create_project_gitignore(function(err, file) { + ws_helper.send_message(ws, "self-check-message", "Added .gitignore in my-pi-projects"); + fs_helper.create_project_readme(function(err, file) { + ws_helper.send_message(ws, "self-check-message", "Added README.md in my-pi-projects"); + winston.debug(file); + if (err) winston.error(err); + + git_helper.commit_push_and_save(file, "Added README.md and .gitignore", null, function(err, response) { + ws_helper.send_message(ws, "self-check-message", "Pushed changes to my-pi-projects"); + ws_helper.send_message(ws, 'self-check-complete'); + }); + }); + }); + }); + } + winston.debug("after everything"); + }); + }); }; diff --git a/helpers/git_helper.js b/helpers/git_helper.js index 089f963..1245253 100644 --- a/helpers/git_helper.js +++ b/helpers/git_helper.js @@ -1,6 +1,5 @@ var path = require('path'), - Datastore = require('nedb'), - db = new Datastore({ filename: path.resolve(process.env.PWD, 'db/webide_data_store'), autoload: true }), + db = require('../models/webideModel'), git = require('gitty'), url = require('url'), winston = require('winston'), @@ -16,7 +15,7 @@ var push_queue = [], pushInterval, PUSH_TIMER = 30000; * The queue is currently set using the PUSH_TIMER to delay the remote pushes. */ function push_queue_interval() { - console.log('git push queue init'); + winston.debug('git push queue init'); function push(repository_path, remote, branch, username) { git.push(repository_path, remote, branch, function(obj) { require('../server').get_socket(username, function(socket) { @@ -24,7 +23,7 @@ function push_queue_interval() { winston.error(obj); socket.emit('git-push-error', {err: "Error: Failure pushing code to remote repository"}); } - //console.log(obj); + //winston.debug(obj); }); }); } @@ -35,7 +34,7 @@ function push_queue_interval() { } while(push_queue.length > 0) { - console.log('pushing code to remote repository'); + winston.debug('pushing code to remote repository'); var element = push_queue.shift(); push(element.repository_path, element.remote, element.branch, element.username); } @@ -51,7 +50,7 @@ exports.clone_adafruit_libraries = function(adafruit_repository, remote, cb) { fs_helper.check_for_repository(adafruit_repository, function(err, status) { if (!err && !status) { git.clone(REPOSITORY_PATH, remote, function(output) { - console.log(output); + winston.debug(output); cb(true); }); } else { @@ -75,26 +74,32 @@ exports.clone_update_remote_push = function(profile, repository_url, retain_remo var repository_name = path.basename(repository_url, '.git'); self.clone_repository(repository_url, function(err, results) { - console.log("clone repository locally: " + repository_name); + winston.debug("clone repository locally: " + repository_name); cb(err, true); }); }; exports.clone_repository = function(repository_path, cb) { - console.log(repository_path); + winston.debug("clone_repository", repository_path); var repository_url = url.parse(repository_path); - console.log("cloning", repository_path); + winston.debug("cloning", repository_path); git.clone(REPOSITORY_PATH, repository_url.href, function(output) { cb(output.error, output.message); }); }; +exports.create_local_repository = function(name, cb) { + winston.debug("create_repository", name); + git.create(name, "", REPOSITORY_PATH, function(output) { + cb(output.error, output.message); + }); +}; + exports.validate_config = function validate_config(cb) { git.config("user.email", null, function(err, email) { git.config("user.name", null, function(err, name) { if (err) winston.error("git_helper.validate_config err", err); - if (name && email) { cb(true); } else { @@ -108,15 +113,15 @@ exports.set_config = function(cb) { var self = this; self.validate_config(function(is_valid) { if (is_valid) { - console.log('git config is valid'); + winston.debug('git config is valid'); cb(); } else { winston.error('git config is invalid'); db.findOne({type: "user"}, function (err, user) { - console.log("set_config user", user); + winston.debug("set_config user", user); git.config("user.email", user.email, function(err, email) { git.config("user.name", user.name, function(err, name) { - console.log("git config set", email, name); + winston.debug("git config set", email, name); cb(); }); }); @@ -132,7 +137,7 @@ exports.set_config = function(cb) { exports.update_remote = function(profile, repository, cb) { var remote_url = "ssh://git@bitbucket.org/" + profile.username + "/" + repository.toLowerCase() + ".git"; git.remote.update(REPOSITORY_PATH + repository, "origin", remote_url, function(output) { - //console.log(output); + //winston.debug(output); cb(output.error, output.message); }); }; @@ -142,7 +147,7 @@ exports.update_remote = function(profile, repository, cb) { */ exports.add_remote = function(repository, remote_name, remote_url, cb) { git.remote.add(REPOSITORY_PATH + repository, remote_name, remote_url, function(output) { - //console.log(output); + //winston.debug(output); cb(output.error, output.message); }); }; @@ -158,8 +163,8 @@ exports.add = function add(repository, files, cb) { } var repository_path = REPOSITORY_PATH + repository; git.add(repository_path, files, function(output) { - //console.log(output.errors); - //console.log(output); + //winston.debug(output.errors); + //winston.debug(output); cb(output.errors, output.added); }); }; @@ -175,7 +180,7 @@ exports.remove = function remove(repository, files, cb) { } var repository_path = REPOSITORY_PATH + repository; git.remove(repository_path, files, function(output) { - //console.log(output.errors); + //winston.debug(output.errors); cb(output.errors, output.added); }); }; @@ -189,7 +194,7 @@ exports.remove_recursive = function remove_recursive(repository, path, cb) { var repository_path = REPOSITORY_PATH + repository; git.remove_recursive(repository_path, path, function(output) { - console.log(output); + winston.debug(output); cb(output.errors, output.added); }); }; @@ -201,7 +206,7 @@ exports.remove_recursive = function remove_recursive(repository, path, cb) { exports.move = function move(repository, source, destination, cb) { var repository_path = REPOSITORY_PATH + repository; git.move(repository_path, source, destination, function(obj) { - //console.log(obj); + //winston.debug(obj); cb(obj.error, obj.message); }); }; @@ -213,9 +218,9 @@ exports.move = function move(repository, source, destination, cb) { */ exports.commit = function commit(repository, message, cb) { var repository_path = REPOSITORY_PATH + repository; - console.log(repository_path); + winston.debug(repository_path); git.commit(repository_path, message, function(obj) { - //console.log(obj); + //winston.debug(obj); cb(obj.error, obj.message); }); }; @@ -233,7 +238,7 @@ exports.is_modified = function (file, cb) { var is_modified = false; git.status(repository_path, function(output) { - console.log(output); + winston.debug(output); if (output.not_staged.length > 0) { output.not_staged.forEach(function(item, index) { @@ -261,12 +266,12 @@ exports.is_untracked = function (file, cb) { var repository_path = path.resolve(REPOSITORY_PATH, repository); var item_path = path_array.slice(3).join('/'); - console.log(item_path); + winston.debug(item_path); var is_untracked = false; git.status(repository_path, function(output) { - console.log(output); + winston.debug(output); if (output.untracked.indexOf(item_path) !== -1) { is_untracked = true; @@ -283,7 +288,7 @@ exports.is_untracked = function (file, cb) { exports.push = function push(repository, remote, branch, profile, cb) { var repository_path = REPOSITORY_PATH + repository; var key = repository + remote + branch; - console.log('called push ' + key); + winston.debug('called push ' + key); //if the repository, remote and branch are already on the queue, just skip it...otherwise add it to the end if (push_queue.length > 0) { @@ -291,7 +296,7 @@ exports.push = function push(repository, remote, branch, profile, cb) { if (push_queue[i].key === key) { break; } else { - console.log('added to queue ' + key); + winston.debug('added to queue ' + key); push_queue.push({ key: key, repository_path: repository_path, @@ -322,7 +327,7 @@ exports.push = function push(repository, remote, branch, profile, cb) { exports.pull = function pull(repository, remote, branch, cb) { var repository_path = REPOSITORY_PATH + repository; git.pull(repository_path, remote, branch, function(obj) { - //console.log(obj); + //winston.debug(obj); if (obj.error) { winston.error(obj.error); cb("Error: Failure updating from remote repository", obj.message); @@ -338,12 +343,12 @@ exports.pull = function pull(repository, remote, branch, cb) { */ exports.remove_commit_push = function(item, profile, cb) { var self = this; - console.log(item); + winston.debug(item); var path_array = item.path.split('/'); var repository = path_array[2]; var item_path = path_array.slice(3).join('/'); - console.log(item_path); - console.log(repository); + winston.debug(item_path); + winston.debug(repository); if (item.type === 'directory') { @@ -405,18 +410,18 @@ exports.move_commit_push = function(item, profile, cb) { self.move(repository, item_path, destination_path, function(err, status) { var commit_message = "Moved " + item.name; if (err) { - console.log ("has error returning"); + winston.debug ("has error returning"); cb("Error: Failure moving file (renaming)"); return; } self.commit(repository, commit_message, function(err, status) { - console.log("Committed Moved File"); + winston.debug("Committed Moved File"); if (err) { cb("Error: Failure comitting file into git"); return; } self.push(repository, "origin", "master", profile, function() { - console.log("Pushed latest changes"); + winston.debug("Pushed latest changes"); cb(); }); }); @@ -441,24 +446,24 @@ exports.commit_push_and_save = function(file, commit_message, profile, cb) { file_path = file.path; } - console.log(commit_message); + winston.debug(commit_message); self.add(repository, file_path, function(err, status) { - console.log("added", err, status); + winston.debug("added", err, status); if (err && err.length > 0) { cb("Error: Failure adding file to git", status); return; } self.commit(repository, commit_message, function(err, status) { - console.log("committed", err, status); + winston.debug("committed", err, status); if (err && err.length > 0) { cb("Error: Failure comitting file into git", status); return; } self.push(repository, "origin", "master", profile, function() { - console.log("added to push queue"); + winston.debug("added to push queue"); cb(); }); }); diff --git a/helpers/scheduler.js b/helpers/scheduler.js index da6b743..9dea4b0 100644 --- a/helpers/scheduler.js +++ b/helpers/scheduler.js @@ -1,6 +1,5 @@ var path = require('path'), - Datastore = require('nedb'), - db = new Datastore({ filename: path.resolve(process.env.PWD, 'db/webide_data_store'), autoload: true }), + db = require('../models/webideModel'), exec = require('child_process').exec, fs = require ('fs'), winston = require('winston'), diff --git a/helpers/websocket_helper.js b/helpers/websocket_helper.js new file mode 100644 index 0000000..41b3488 --- /dev/null +++ b/helpers/websocket_helper.js @@ -0,0 +1,3 @@ +exports.send_message = function send_message(ws, type, data) { + ws.send(JSON.stringify({type: type, data: data})); +} diff --git a/models/webideModel.js b/models/webideModel.js new file mode 100644 index 0000000..87486aa --- /dev/null +++ b/models/webideModel.js @@ -0,0 +1,11 @@ +'use strict'; + +var Datastore = require('nedb'), + path = require('path'); + +var webideDb = new Datastore({ + filename: path.resolve(process.env.PWD, 'db/webide_data_store'), + autoload: true +}); + +module.exports = webideDb; diff --git a/output.log b/output.log new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index ce876ac..27b4c36 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "pug": "^2.0.0-rc.4", "request": "2.14.0", "serve-static": "^1.13.1", - "validator": "0.4.25", - "winston": "0.6.2", + "validator": "9.4.0", + "winston": "^2.4.0", "xterm": "3.0.2" } } diff --git a/server.js b/server.js index 8e6bdde..8d5d408 100644 --- a/server.js +++ b/server.js @@ -17,7 +17,6 @@ var express = require('express'), path = require('path'), updater = require('./helpers/updater'), scheduler = require('./helpers/scheduler'), - git_helper = require('./helpers/git_helper'), exec_helper = require('./helpers/exec_helper'), fs_helper = require('./helpers/fs_helper'), exec_helper = require('./helpers/exec_helper'), @@ -25,14 +24,14 @@ var express = require('express'), debug_helper = require('./helpers/python/debug_helper'), config = require('./config/config'), winston = require('winston'), - Datastore = require('nedb'), + db = require('./models/webideModel'), pty = require('node-pty'); var davServer, HOSTNAME, REPOSITORY_PATH = path.resolve(__dirname + "/repositories"); -winston.info("REPOSITORY_PATH", REPOSITORY_PATH); + var terminals = {}, logs = {}; @@ -51,7 +50,7 @@ if (!exists) { //winston.remove(winston.transports.Console); -var db = new Datastore({ filename: './database/webide_data_store', autoload: true }); +winston.info("REPOSITORY_PATH", REPOSITORY_PATH); //redirect anything with /filesystem in the url to the WebDav server. app.use(function(req, res, next) { @@ -252,9 +251,3 @@ process.on('SIGINT', function() { }); process.exit(); }); - -process.on('uncaughtException', function(err) { - debug_helper.kill_debug(false, function() { - //no need to wait for this - }); -});