OSDN Git Service

4b870455d25b140345d3c8813151e04e93df824e
[eos/zephyr.git] / server / api / commandExecution / index.js
1 'use strict';
2
3 var express    = require('express'),
4     router     = express.Router(),
5     path       = require('path'),
6     exec       = require('child_process').exec
7
8 router.post('/', function(req, res) {
9     var params     = req.body,
10         command    = params.command,
11         inputPath  = path.normalize('workspace/'+params.inputDir),
12         outputPath = path.normalize('workspace/'+params.outputDir),
13         options    = params.options
14
15     var execLine = command
16     console.log(execLine)
17     if(options) {
18         options.forEach(function(option) {
19             if(option.isFilled) {
20                 var content = option.content.toString().replace(",", " ", "g")
21                 if(option.name === "-i") {
22                     execLine = execLine+' '+option.name+' '+path.normalize(inputPath+'/'+content)
23                 } else if(option.name === "-o") {
24                     execLine = execLine+' '+option.name+' '+path.normalize(outputPath+'/'+content)
25                 } else {
26                     execLine = execLine+' '+option.name+' '+content
27                 }
28             }
29         })
30     }
31     exec(execLine, function(err, stdout, stderr) {
32         if(err) {
33             console.error(err)
34             res.send('error')
35         } else {
36             console.log(stdout)
37             console.log(stderr)
38             res.send('done')
39         }
40     })
41 });
42
43 module.exports = router