forked from nical/tde.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.js
78 lines (64 loc) · 2.02 KB
/
project.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var git = require("gift")
var fs = require("fs")
var exec = require("child_process").exec
function validateProjectName(name)
{
return /^[-\w]+$/.test(name)
}
module.exports.init = function(app)
{
app.get("/data/projects", function(req, res, next)
{
fs.readdir(app.get("dataRoot"), function(err, files)
{
if (err) return next(err)
var folders = files.filter(function(file)
{
var stats = fs.statSync(app.get("dataRoot") + file)
return stats.isDirectory()
})
res.json(folders)
})
})
app.post("/data/projects", function(req, res, next)
{
var projectName = req.body.name
if (!validateProjectName(projectName)) return next(new Error("Project names should match /^[-\\w]+$/"))
var projectFolder = app.get("dataRoot") + projectName
fs.mkdir(projectFolder, function(err)
{
if (err) return next(err)
git.init(projectFolder, function(err, repo)
{
if (err) next(err)
res.send(201, "Project created")
})
})
})
app.get("/export/:projectId", function(req, res)
{
var projectId = req.params.projectId
var demoFilename = __dirname + "/export/" + projectId + "/demo.png.html"
fs.unlink(demoFilename, function(err)
{
var command = "cd \"" + __dirname + "\" && ./EXPORT.sh " + projectId
// hack for windows debug (assumes cygwin is installed in c:\cygwin)
if (process.platform == "win32")
command = "c:/cygwin/bin/bash.exe --login -c \"cd '" + __dirname + "' && ./EXPORT.sh " + projectId + "\""
exec(command, function(err, stdout, stderr)
{
fs.exists(demoFilename, function(exists)
{
if (!exists)
{
res.type("text/plain")
res.send(500, "== Export failed ==\n\nstdout:\n" + stdout + "\n\nstderr:\n" + stderr)
return
}
res.type("text/html")
fs.createReadStream(demoFilename).pipe(res)
})
})
})
})
}