forked from jsplumb/jsplumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
343 lines (307 loc) · 13 KB
/
Gruntfile.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var package = require('./package.json'),
support = require("./build-support.js"),
BUILD = require("./build.json"),
get = function(name) {
if (BUILD.includes[name].p) {
// the new way, with libs from npm
return BUILD.includes[name].p;
} else {
return "lib/" + BUILD.includes[name].f + "-" + BUILD.includes[name].v + ".js";
}
},
objects = {
common:[
'util.js', 'browser-util.js', 'lists.js', 'jsplumb.js', 'overlay-component.js', 'endpoint.js', 'connection.js', 'anchors.js', 'defaults.js', 'group.js'
]
},
optionList = function(grunt, type) {
return grunt.option(type) && grunt.option(type).split(",") || [];
},
getOutputFilename = function(grunt, suffix) {
var suffix2 = grunt.option('outputSuffix') ? ('.' + grunt.option("outputSuffix")) : '';
return 'dist/js/jsplumb' + suffix2 + suffix + '.js';
},
filter = function(l, v, t, o) {
if (l.length === 0 || l.indexOf(v) != -1)
o.push("src/" + t + "-" + v + ".js");
},
getList = function(grunt, type) {
var ol = optionList(grunt, type), l = BUILD[type], out = [];
for (var i = 0; i < l.length; i++)
filter(ol, l[i], type, out);
return out;
},
getSources = function(grunt) {
var sources = BUILD.coreLibs.map(get);
sources.push.apply(sources, BUILD.browserLibs.map(get));
sources.push.apply(sources, objects.common.map(function(v) { return "src/" + v; }));
sources.push.apply(sources, getList(grunt, "connectors"));
//sources.push.apply(sources, ["svg"]);
sources.push.apply(sources, getList(grunt, "renderers"));
sources.push("src/dom.jsplumb.js");
return sources;
},
help = "\nBuilding jsPlumb\n" +
"-----------------\n" +
"To build jsPlumb, execute the 'build' task:\n\n" +
"--> grunt build\n\n" +
"this will, by default, build a version of jsPlumb with all the available connectors.\n\n" +
"You can build a custom version of jsPlumb by specifying a list of connectors and/or renderers on the command line, for example:\n\n" +
"--> grunt build --connectors=flowchart,statemachine\n\n";
module.exports = function(grunt) {
grunt.registerTask('help', 'Help with the jsPlumb build', function(arg1, arg2) {
grunt.log.write(help);
});
grunt.registerTask('info', 'dumps info about what will be built', function(arg1, arg2) {
grunt.log.write('Build jsPlumb');
});
var fileLists = function(suffix) {
suffix = suffix || "";
return {
"dom":{
src:getSources(grunt),
dest:getOutputFilename(grunt, suffix)
}
};
};
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: fileLists(),
uglify: fileLists(".min"),
qunit: {
all: {
options: {
urls:[
'http://localhost:3333/tests/unit/index.html'
]
}
}
},
connect: {
server: {
options: {
port: 3333,
base: '.'
}
}
},
clean:{
options:{
force:true
},
dist:["dist"],
jekyll:[
"jekyll/doc",
"jekyll/docs",
"jekyll/_site"
],
docs:["docs"]
},
jshint: {
options: {
eqnull: true,
loopfunc:true,
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
'-W018':true, // confusing use of '!'
'-W055':true, // a constructor name should start with an uppercase letter.
'-W032':true, // unnecessary semicolon.
'-W068':true, // Wrapping non-IIFE function literals in parens is unnecessary. (rollup does this. it isnt a problem)
'-W030':true, // rollup header issue
'-W103':true, // the __proto__ property is deprecated
globals:{
window:true,
Biltong:true,
exports:true,
console:true,
Image:true,
setTimeout:true,
document:true,
setInterval:true,
jsPlumb:true,
jsPlumbUtil:true,
getComputedStyle:true,
define:true
}
},
files:{
src: [ 'src/anchors.js', 'src/base-library-adapter.js', 'src/browser-util.js', 'src/connection.js', 'src/connectors-bezier.js', 'src/connectors-flowchart.js', 'src/connectors-statemachine.js', 'src/connectors-straight.js', 'src/defaults.js', 'src/dom.jsPlumb.js', 'src/dom-adapter.js', 'src/endpoint.js', 'src/group.js', 'src/jsPlumb.js', 'src/overlay-component.js', 'src/renderers-svg.js', 'src/util.js' ]
}
},
watch: {
scripts: {
files: ['src/*.js'],
tasks: ['build-src']
}
},
jekyll: {
options: {
src:'jekyll'
},
dist:{
options:{
dest:"jekyll/_site",
config:'jekyll/_config.yml'
}
}
},
exec:{
"npmpack":{
command:"npm pack;mv jsplumb-<%= pkg.version%>.tgz jsplumb.tgz"
}
},
copy:{
docs:{
cwd: 'jekyll/_site/docs', // set working folder / root to copy
src: '**/*', // copy all files and subfolders
dest: 'docs', // destination folder
expand: true
},
docs_js:{
cwd: 'jekyll/_site/js', // set working folder / root to copy
src: '**/*', // copy all files and subfolders
dest: 'docs/js', // destination folder
expand: true
},
docs_css:{
cwd: 'jekyll/_site/css', // set working folder / root to copy
src: '**/*', // copy all files and subfolders
dest: 'docs/css', // destination folder
expand: true
},
docs_img:{
cwd: 'jekyll/_site', // set working folder / root to copy
src: 'logo-medium-jsplumb.png', // copy all files and subfolders
dest: 'docs', // destination folder
expand: true
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-qunit-junit');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-jekyll');
grunt.registerTask('insertVersion', function() {
var src = grunt.file.read("dist/js/jsplumb.js");
grunt.file.write("dist/js/jsplumb.js", src.replace("<% pkg.version %>", package.version));
src = grunt.file.read("dist/js/jsplumb.min.js");
grunt.file.write("dist/js/jsplumb.min.js", src.replace("<% pkg.version %>", package.version));
});
grunt.registerTask("build", [ 'build-src', 'exec:npmpack', 'build-docs' ]);
grunt.registerTask('build-src', ['clean', 'jshint', 'concat', 'uglify', 'insertVersion' ]);
grunt.registerTask('default', ['help']);
grunt.registerTask('build-docs', ['clean:docs', 'docs', 'jekyll', 'finaliseDocs', 'copy:docs', 'copy:docs_js','copy:docs_css','copy:docs_img']);
var _replace = function(cwd, pattern, oldV, newV, exclusions) {
exclusions = exclusions || [];
var _one = function(f) {
if (exclusions.indexOf(f) == -1) {
if (!grunt.file.isDir(cwd + "/" + f)) {
var c = grunt.file.read(cwd + "/" + f);
grunt.file.write(cwd + "/" + f, c.replace(oldV, newV));
}
}
};
var sources = grunt.file.expand({ cwd:cwd }, pattern);
for (var i = 0; i < sources.length; i++)
_one(sources[i]);
};
var _prepareDocs = function(inDir, outDir, category, layoutId) {
console.log("PREPARE DOCS ", inDir, outDir, category, layoutId);
// exclusions from input doc dir
var exclusions = ["node_modules", "ff.htl", "dev-", "future-"];
var communityPackage = require('./package.json');
// 1. create directories for docs.
grunt.file.mkdir(outDir);
// 2. copy files from markdown directory into 'doc', and then give each one some front matter.
var sources = grunt.file.expand({ cwd:inDir }, "*");
for (var i = 0; i < sources.length; i++) {
if (exclusions.indexOf(sources[i]) == -1 ) {
if (sources[i].indexOf(".png") == -1) {
support.createMarkdownFile(grunt, inDir, sources[i], {
layout: sources[i] == "contents.md" ? "plain" : layoutId,
docId: sources[i].toUpperCase().replace(".MD", "").replace(/-_/g, " "),
category: category,
communityVersion:communityPackage.version
}, outDir);
}
else {
grunt.file.copy(inDir + "/" + sources[i], outDir + "/" + sources[i], { noProcess:"*.*" });
}
}
}
/*
,
permalink:"/docs/" + category + "/" + sources[i].split(".")[0] + ".html"
*/
};
var _replaceContents = function (dir) {
var c = grunt.file.read(dir + "/contents.html");
grunt.file.recurse(dir, function callback(abspath, rootdir, subdir, filename) {
if (filename !== "contents.html") {
var d = grunt.file.read(abspath);
grunt.file.write(abspath, d.replace(/(<!-- contents.*>.*\n)(.*\n)*(.*\/contents -->)/, c));
}
});
};
var _insertHTMLSuffixIntoDocLinks = function(inDir) {
// for all matches like these in every file inside /public/docs:
// href="anchors"
//href="anchors#something"
// which is to say, an internal link, we need to insert ".html" either at the end of the string or
// before the hash.
//var inDir = "public/docs";
var docs = grunt.file.expand({ cwd:inDir }, "*");
for (var i = 0; i < docs.length; i++) {
var f = grunt.file.read(inDir + "/" + docs[i]);
grunt.file.write(inDir +"/" + docs[i], f.replace(/href=\"([^\"]+)\"/g, function() {
// arguments[1] is the content of the link.
var out;
if (arguments[1] == "#" || arguments[1].indexOf("http") == 0 || arguments[1].indexOf("//") == 0 || arguments[1].indexOf(".") != -1 || arguments[1] === "/")
out = arguments[0];
else {
var a = arguments[1].split("#");
//out = "href=\"" + a[0] + ".html" + (a.length == 2 ? "#" + a[1] : "") + "\"";
out = "href=\"" + (a[0] != null && a[0].length > 0 && a[0] !== "/" ? a[0] + ".html" : "") + (a.length == 2 ? "#" + a[1] : "") + "\"";
}
return out;
}));
}
};
grunt.registerTask("docs", function() {
_prepareDocs("doc/wiki", "jekyll/docs", "community", "doc");
});
grunt.registerTask("finaliseDocs", function() {
_insertHTMLSuffixIntoDocLinks("jekyll/_site/docs");
_replaceContents("jekyll/_site/docs");
grunt.file.write("docs/index.html", "<!doctype html><html><head><meta http-equiv=\"refresh\" content=\"0; URL='home.html'\" /></head></html>");
});
grunt.registerTask('update', function() {
var newV = grunt.option("newver");
if (newV == null) {
grunt.log.error("You must provide the new version: grunt update --newver=X.X.X");
}
else {
var oldV = new RegExp(grunt.config("pkg").version, "g");
// now update version number in all demos and src files etc
_replace("src", "*.js", oldV, newV);
_replace("demo", "**/*.html", oldV, newV);
_replace(".", "package.json", oldV, newV);
_replace(".", "README.md", oldV, newV);
}
});
grunt.registerTask("test", ["connect:server", "qunit_junit", "qunit"]);
};