OSDN Git Service

[TMP] Before developing command exectuion UI view.
[eos/zephyr.git] / server / api / v1 / execution / index.js
1 'use strict';
2
3 var express    = require('express');
4 var router     = express.Router()
5 var path       = require('path')
6 var formidable = require('formidable')
7 var db = require(__dirname + '/../../../../server/class/DB').instance;
8 var eos = require(__dirname + '/../../../../server/class/Eos').instance;
9 var ws = require(__dirname + '/../../../../server/ws').instance;
10
11 router.post('/', function(req, res) {
12
13     ws.emit();
14
15     var form = new formidable.IncomingForm();
16     var root; 
17     var result;
18
19     var command;
20     var options;
21     var workspace;
22
23     var optionsArray;
24
25     if(process.env.NODE_ENV === 'debug') {
26         root = __dirname + '/../../../../user-specific-files/workspace.debug/';
27     } else {
28         root = __dirname + '/../../../../user-specific-files/workspace/';
29     }
30
31     form.parse(req, function(err, fields, files) {
32         var data = JSON.parse(fields.data);
33         command = data.command;
34         options = data.options;
35         workspace = data.workspace;
36     });
37
38     form.on('end', function() {
39         Promise.all([eos.validate(command,options, workspace), eos.toExecArray(command, options, workspace)])
40         .catch(function(r) {
41             var info = {
42                 status: 'error',
43                 message: r.message
44             };
45             console.log(r.message);
46             res.send(info);
47         })
48         .then(function(r) {
49             var optionsArray = r[1];
50             return eos.execute(command, optionsArray);
51         }).then(function(r) {
52             var info = {
53                 status: 'success',
54             };
55             res.send(r);
56         });
57     });
58 });
59
60 module.exports = router;