forked from dagrejs/dagre-d3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
227 lines (191 loc) · 6.43 KB
/
gulpfile.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
"use strict";
var _ = require("lodash");
var browserSync = require("browser-sync");
var browserify = require("browserify");
var buffer = require("vinyl-buffer");
var changed = require("gulp-changed");
var del = require("del");
var fs = require("fs");
var gulp = require("gulp");
var gutil = require("gulp-util");
var jshint = require("gulp-jshint");
var jshintStylish = require("jshint-stylish");
var karma = require("karma");
var merge = require("merge-stream");
var prettyTime = require("pretty-hrtime");
var rename = require("gulp-rename");
var replace = require("gulp-replace");
var shell = require("gulp-shell");
var source = require("vinyl-source-stream");
var sourcemaps = require("gulp-sourcemaps");
var uglify = require("gulp-uglify");
var watch = require("gulp-watch");
var watchify = require("watchify");
var BUILD_DIR = "build";
var BUILD_DIST_DIR = "build/dist";
var DIST_DIR = "dist";
var DEMO_SRC = "demo/**/*";
var DEMO_BUILD = BUILD_DIST_DIR + "/demo";
gulp.task("demo:build", function() {
return gulp.src(DEMO_SRC)
.pipe(replace("../" + BUILD_DIST_DIR + "/dagre-d3v4.js", "../dagre-d3v4.js"))
.pipe(gulp.dest(DEMO_BUILD));
});
gulp.task("demo:watch", ["demo:build"], function() {
gulp.src(DEMO_SRC)
.pipe(watch(DEMO_SRC), { verbose: true })
.pipe(changed(DEMO_BUILD))
.pipe(replace("../" + BUILD_DIST_DIR + "/dagre-d3v4.js", "../dagre-d3v4.js"))
.pipe(gulp.dest(DEMO_BUILD));
});
gulp.task("demo:test", ["demo:build"], function() {
return gulp.src("test/demo-test.js")
.pipe(shell("phantomjs <%= (file.path) %>"));
});
gulp.task("js:build", function() {
return makeJsBundleTask(false);
});
gulp.task("js:watch", function() {
return makeJsBundleTask(true);
});
gulp.task("js:test", ["js:build"], function(cb) {
karmaSingleRun(__dirname + "/karma.conf.js", cb);
});
gulp.task("js:test:watch", ["js:build"], function(cb) {
new karma.Server({
configFile: __dirname + "/karma.conf.js",
singleRun: false,
browsers: ["PhantomJS"]
}, cb).start();
});
gulp.task("core-js:build", function() {
return makeBundleTask("./index.js", "dagre-d3v4.core.js", false, {
standalone: "dagreD3",
bundleExternal: false,
debug: true
});
});
gulp.task("core-js:test", ["core-js:build"], function(cb) {
karmaSingleRun(__dirname + "/karma.core.conf.js", cb);
});
gulp.task("version:build", function() {
var pkg = readPackageJson();
fs.writeFileSync("lib/version.js", generateVersionJs(pkg));
});
gulp.task("bower:build", function() {
var pkg = readPackageJson();
fs.writeFileSync("bower.json", generateBowerJson(pkg));
});
gulp.task("build", ["demo:build", "js:build", "js:test", "core-js:build", "core-js:test", "demo:test"]);
gulp.task("watch", ["demo:watch", "js:watch", "js:test:watch"]);
gulp.task("serve", ["watch"], function() {
browserSync.init({
files: BUILD_DIST_DIR + "/**/*",
notify: false,
open: false,
reloadOnRestart: true,
server: {
baseDir: BUILD_DIST_DIR,
directory: true
}
});
});
gulp.task("dist", ["version:build", "bower:build", "build"], function() {
return gulp.src(BUILD_DIST_DIR + "/**/*")
.pipe(gulp.dest(DIST_DIR));
});
gulp.task("release", ["dist"], function() {
return gulp.src("src/release/release.sh")
.pipe(shell("<%= (file.path) %>"));
});
gulp.task("clean", function(cb) {
del(BUILD_DIR, cb);
});
gulp.task("default", ["build"]);
function karmaSingleRun(conf, cb) {
var args = {
configFile: conf,
singleRun: true
};
if (process.env.BROWSERS) {
args.browsers = process.env.BROWSERS.split(",");
}
new karma.Server(args, cb).start();
}
function makeJsBundleTask(watch) {
return makeBundleTask("./index.js", "dagre-d3v4.js", watch, {
standalone: "dagreD3",
external: ["node_modules/d3/index.js", "node_modules/d3/build/d3.js"],
debug: true
});
}
function makeBundleTask(src, name, watch, args) {
var bundler = browserify(_.defaults(args, watchify.args))
.add(src);
function bundle(changedFiles) {
gutil.log("Starting '" + gutil.colors.cyan("browserify " + name) + "'...");
var start = process.hrtime();
var compileStream = bundler.bundle()
.on("error", function(err) {
gutil.log(gutil.colors.red("browserify error (" + name + "): " + err.message));
this.emit("end");
})
.on("end", function() {
var end = process.hrtime(start);
gutil.log("Finished '" + gutil.colors.cyan("browserify " + name) + "' after",
gutil.colors.magenta(prettyTime(end)));
})
.pipe(source(name))
.pipe(gulp.dest(BUILD_DIST_DIR))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify({ preserveComments: "some" }))
.pipe(rename({ suffix: ".min" }))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(BUILD_DIST_DIR));
var lintStream;
if (changedFiles) {
lintStream = gulp.src(changedFiles);
} else {
lintStream = gulp.src(["index.js", "lib/**/*.js"]);
}
lintStream = lintStream
.pipe(jshint())
.pipe(jshint.reporter(jshintStylish));
if (!watch) {
lintStream = lintStream.pipe(jshint.reporter("fail"));
}
return merge(lintStream, compileStream);
}
if (watch) {
bundler = watchify(bundler);
bundler.on("update", bundle);
}
return bundle();
}
function generateBowerJson(pkg) {
return prettifyJson(applyTemplate("src/bower.json.tmpl", { pkg: pkg }));
}
function generateVersionJs(pkg) {
return applyTemplate("src/version.js.tmpl", { pkg: pkg });
}
function applyTemplate(templateFile, props) {
var template = fs.readFileSync(templateFile);
var compiled = _.template(template);
return compiled(props);
}
/**
* Read the contents of package.json in as JSON. Do not cache package.json,
* because it may have changed (e.g. when running in watch mode).
*/
function readPackageJson() {
var packageText = fs.readFileSync("package.json");
return JSON.parse(packageText);
}
/**
* Given a JSON string return a prettified version of the string.
*/
function prettifyJson(str) {
var json = JSON.parse(str);
return JSON.stringify(json, null, 2);
}