forked from iiyo/WebStory-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
157 lines (115 loc) · 3.88 KB
/
build.js
File metadata and controls
157 lines (115 loc) · 3.88 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* global require, console */
/* eslint no-console: off */
var processScriptsFileFn, concatJsFiles, scriptsFilePath;
var browserify = require("browserify");
var fs = require("fs");
var os = require("os");
var normalizePath = require("path").normalize;
var minify = require("minify");
var copy = require("ncp").ncp;
var usingify = require("usingify").usingify;
mkdirSync('./build');
mkdirSync('./export');
mkdirSync('./export/engine');
var usingifyFilePath = normalizePath("WebStoryEngine_usingify.js");
var dependencyFilePath = normalizePath(os.tmpdir() + "/WebStoryEngine_dependencies.js");
fs.writeFileSync(usingifyFilePath, usingify(JSON.parse("" + fs.readFileSync("package.json"))));
var dependencyFile = fs.createWriteStream(dependencyFilePath);
var bundle = browserify(usingifyFilePath).bundle();
var info = JSON.parse("" + fs.readFileSync("package.json"));
scriptsFilePath = 'scripts.json';
dependencyFile.write(
"/*\n" +
" WebStory Engine dependencies (v" + info.version + ")\n" +
" Build time: " + (new Date().toUTCString()) +
"\n*/\n"
);
bundle.pipe(dependencyFile);
function mkdirSync (path) {
try {
fs.mkdirSync(path);
}
catch (e) {
if (e.code != 'EEXIST') {
throw e;
}
}
}
processScriptsFileFn = function (data) {
var json;
try {
json = JSON.parse(data);
}
catch (e) {
console.log('Parsing file ' + scriptsFilePath + ' as JSON failed!');
console.log('Error was:' + e);
return;
}
concatJsFiles(json.files);
};
concatJsFiles = function (files) {
var fn, concatFile, moduleName, moduleString;
moduleString = '';
concatFile = '';
fn = function (path) {
var concFn;
concFn = function (data) {
concatFile += "\n\n" + removeUnwantedSections(data);
};
concFn(fs.readFileSync(normalizePath(path), 'utf-8'));
};
moduleString += "\n\nvar $__WSEScripts = document.getElementsByTagName('script');";
moduleString += "\nWSEPath = $__WSEScripts[$__WSEScripts.length - 1].src;\n";
for (moduleName in files) {
moduleString += "\nusing.modules['" + moduleName + "'] = WSEPath;";
}
fn("libs/MO5/libs/using.js/using.js");
fn(dependencyFilePath);
fn("libs/MO5/js/MO5.js");
concatFile += moduleString;
for (moduleName in files) {
fn(files[moduleName]);
}
writeFileFn(concatFile);
};
function writeFileFn (concatFile) {
function makeErrorFn (successText) {
return function (err) {
if (err) {
console.log(err);
return;
}
console.log(successText);
};
}
fs.writeFileSync('./build/WebStoryEngine.js', concatFile);
makeErrorFn('WebStory Engine file created.')();
minify('./build/WebStoryEngine.js', function(error, data) {
if (error) {
console.log(error);
}
else {
fs.writeFile('./build/WebStoryEngine.min.js', data, makeExport);
}
});
function makeExport (err) {
makeErrorFn("Minified WebStory Engine file created.")(err);
copy("./build", "./export/engine", function () {
copy("./story", "./export", function () {
console.log("Exported WebStory Engine skeleton to export folder.");
});
});
}
}
function removeUnwantedSections (fileContents) {
fileContents = fileContents.replace(
/\/\*<ON_DEPLOY_REMOVE>\*\/[\s\S]*\/\*<\/ON_DEPLOY_REMOVE>\*\//g,
""
);
fileContents = fileContents.replace(/%%%version%%%/g, info.version);
return fileContents;
}
setTimeout(function () {
processScriptsFileFn(fs.readFileSync(scriptsFilePath, 'utf-8'));
fs.unlinkSync(usingifyFilePath);
}, 2000);