forked from rowanwins/turf-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (93 loc) · 2.76 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var fs = require("fs");
var path = require("path");
var express = require("express");
var bodyParser = require('body-parser');
var browserify = require('browserify');
var app = express();
app.use(express.static('assets'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.get("/", function(req, res) {
res.render("index", {
turfModules: turfModules,
turfVersion: turfVersion
});
});
app.post("/build", function(req, res) {
var requiredModules = req.body.modules.split(",");
var outputFileString = "module.exports = {";
for (var i = 0; i < requiredModules.length; i++ ) {
var plainModuleName = requiredModules[i].split("-").map(function(elem, ind) {
if (ind > 0) {
return elem.slice(0, 1).toUpperCase()+elem.slice(1);
}
return elem;
}).join("");
outputFileString += plainModuleName + ": require('@turf/"+ requiredModules[i] +"'),";
}
// outputFileString += req.body.modules;
// console.log(req.body.modules);
outputFileString = outputFileString.substring(0, outputFileString.length - 1);
outputFileString += "}";
console.log(outputFileString);
fs.writeFile('tmp.txt', outputFileString, function(err) {
if (err) {
return console.error(err);
}
var b = browserify('tmp.txt', {
standalone: "turf",
paths: ['./node_modules/@turf']
});
b.transform({
global: true
}, 'uglifyify');
b.bundle().pipe(res);
});
});
var turfModules =[];
var turfLocation = 'node_modules/@turf';
var turfVersion;
var startup = (function checkExistance() {
var pjson = require(__dirname+'/node_modules/@turf/turf/package.json');
turfVersion = pjson.version;
fs.lstat(turfLocation, function (err, inodeStatus) {
if (err) {
console.log("Hmmm there was an error and we couldn't find any turf modules... please double check the install instructions");
return;
}
var allModules = getDirectories(turfLocation);
createModuleArray(allModules);
checkCreateOutputDirectory();
startServer();
return "modules could be found";
});
})();
function getDirectories(srcpath) {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
function createModuleArray(allModules) {
for (i = 0; i < allModules.length; i++) {
if (allModules[i].indexOf("turf") === -1) {
turfModules.push(allModules[i]);
}
}
}
function startServer() {
browserify = require('browserify');
var server_port = process.env.PORT || 3000;
app.listen(server_port, function() {
console.log("The Turf build tool has been started at port " + server_port);
});
}
function checkCreateOutputDirectory() {
fs.lstat('./outputs', function (err, inodeStatus) {
if (err) {
fs.mkdir('./outputs');
return;
}
return "output directory already exists";
});
}