Skip to content

Commit 03de131

Browse files
Dan LaskyShuwen Qian
authored andcommitted
migrate config to single obj (prep for multifile/multibuild)
1 parent ff011bf commit 03de131

1 file changed

Lines changed: 65 additions & 58 deletions

File tree

Gulpfile.js

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,22 @@ var base64 = require('gulp-base64');
4747
var minimist = require('minimist');
4848
var open = require('gulp-open');
4949

50-
var SRC = 'src/';
51-
var BUILD = 'build/';
52-
var BUILD_DOCS = 'build_docs/';
53-
var DOCS = 'docs/';
54-
var DIST = 'dist/';
55-
var TEMPLATES = 'gulp/templates/'; //TODO swap to gulp
50+
const C = {
51+
SRC:'src/',
52+
BUILD: 'build/',
53+
BUILD_DOCS: 'build_docs/',
54+
DOCS: 'docs/',
55+
DIST: 'dist/',
56+
TEMPLATES: 'gulp/templates/',
57+
MODULE_MASK: 'mm-*',
58+
MODULE_HTML: 'mm-*.html',
59+
SHARED: 'shared/',
60+
BOWER: 'bower_components/',
61+
LIVE_PORT: 8000,
62+
DOCS_PORT: 8001
63+
};
64+
5665

57-
var LIVE_PORT = 8000;
58-
var DOCS_PORT = 8001;
5966

6067
var PATCH_LIST = [
6168
'bower_components/moment/min/moment.min.js'
@@ -71,53 +78,53 @@ function dbg(t) {
7178
/** BUILD **/
7279

7380
gulp.task('patch-lib', function() {
74-
gulp.src(PATCH_LIST, {base: j(__dirname, 'bower_components')})
81+
gulp.src(C.PATCH_LIST, {base: j(__dirname, 'bower_components')})
7582
.pipe(dbg('patch-lib'))
7683
.pipe(wrap("(function(define, require) { {{{contents}}} })();",{},{engine:"hogan"}).on('error',console.error))
7784
.pipe(gulp.dest( j(__dirname, 'bower_components') ));
7885
});
7986

8087
gulp.task('clean', function() {
81-
return del([j(BUILD,'**'), j(BUILD_DOCS,'**')]);
88+
return del([j(C.BUILD,'**'), j(C.BUILD_DOCS,'**')]);
8289
});
8390

8491
gulp.task('clean:dist', function() {
85-
return del([j(DIST,'**')]);
92+
return del([j(C.DIST,'**')]);
8693
});
8794

8895
gulp.task('copy', function() {
89-
return gulp.src([j(SRC,'**/*.+(html|js|woff)'), j('!',SRC,'**/example.html')])
96+
return gulp.src([j(C.SRC,'**/*.+(html|js|woff)'), j('!',C.SRC,'**/example.html')])
9097
.pipe(cache('copy'))
9198
.pipe(dbg('copy'))
92-
.pipe(gulp.dest(BUILD));
99+
.pipe(gulp.dest(C.BUILD));
93100
});
94101

95102
gulp.task('sass', function() {
96-
var wrapper = fs.readFileSync(j(TEMPLATES,"style_module_template.html"),'utf8');
97-
return gulp.src(SRC + 'mm-*/*.scss')
103+
var wrapper = fs.readFileSync(j(C.TEMPLATES,"style_module_template.html"),'utf8');
104+
return gulp.src(j(C.SRC, C.MODULE_MASK,'*.scss'))
98105
.pipe(cache('scss'))
99106
.pipe(sass({includePaths: ['./bower_components/bourbon/app/assets/stylesheets/', './src/shared/sass/']}).on('error', sass.logError))
100107
.pipe(postcss([autoprefixer({browsers: ['last 2 versions']})]))
101108
.pipe(dbg('sass'))
102-
.pipe(gulp.dest(BUILD))
109+
.pipe(gulp.dest(C.BUILD))
103110
.pipe(wrap(function(data) {
104111
data.fname = path.basename(data.file.relative,'.css');
105112
return wrapper;
106113
},{},{engine:"hogan"}))
107114
.pipe(rename({basename:"style", extname: ".html"}))
108115
.pipe(dbg('sass-html'))
109-
.pipe(gulp.dest(BUILD));
116+
.pipe(gulp.dest(C.BUILD));
110117
});
111118

112119
gulp.task('font', function() {
113-
return gulp.src(SRC + 'shared/fonts/fonts.scss')
120+
return gulp.src(C.SRC + 'shared/fonts/fonts.scss')
114121
.pipe(sass({includePaths: ['./bower_components/bourbon/app/assets/stylesheets/', './src/shared/sass/']}).on('error', sass.logError))
115122
.pipe(dbg('font'))
116-
.pipe(gulp.dest(BUILD + 'shared/fonts/'))
123+
.pipe(gulp.dest(C.BUILD + 'shared/fonts/'))
117124
.pipe(wrap("<style>{{{contents}}}</style>",{},{engine:"hogan"}).on('error',console.log))
118125
.pipe(rename("fonts.html").on('error',console.log))
119126
.pipe(dbg('font-output'))
120-
.pipe(gulp.dest(j(BUILD,'/shared/fonts/')));
127+
.pipe(gulp.dest(j(C.BUILD,'/shared/fonts/')));
121128
});
122129

123130
function vulcanizeSingle(opts, baseList, basePath) {
@@ -152,7 +159,7 @@ function vulcanizeSingle(opts, baseList, basePath) {
152159
}
153160

154161
gulp.task('vulcanize', function() {
155-
var moduleGlob = j(BUILD,'mm-*/mm-*.html');
162+
var moduleGlob = j(C.BUILD,'mm-*/mm-*.html');
156163
var excludes = glob.sync(moduleGlob);
157164
excludes.push('bower_components/polymer/polymer.html');
158165

@@ -162,18 +169,18 @@ gulp.task('vulcanize', function() {
162169
inlineScripts: true,
163170
inlineCss: true,
164171
implicitStrip: false
165-
}, excludes, BUILD))
172+
}, excludes, C.BUILD))
166173
.pipe(dbg('vulcanize-modules'))
167174
.pipe(htmlmin())
168-
.pipe(gulp.dest(BUILD));
175+
.pipe(gulp.dest(C.BUILD));
169176

170-
var lib = gulp.src(j(BUILD,"strand.html"))
177+
var lib = gulp.src(j(C.BUILD,"strand.html"))
171178
.pipe(vulcanize({
172179
inlineScripts: true,
173180
inlineCss: true
174181
}))
175182
.pipe(dbg('vulcanize-lib'))
176-
.pipe(gulp.dest(BUILD));
183+
.pipe(gulp.dest(C.BUILD));
177184
return merge(modules, lib);
178185
});
179186

@@ -186,15 +193,15 @@ gulp.task('build', function(cb) {
186193
});
187194

188195
gulp.task('build:prod', ['patch-lib', 'build'], function() {
189-
var excludes = glob.sync(j(BUILD,'mm-*/mm-*.html'));
196+
var excludes = glob.sync(j(C.BUILD,'mm-*/mm-*.html'));
190197
excludes.push('bower_components/polymer/polymer.html');
191198

192-
var modules = gulp.src(j(BUILD,'mm-*/mm-*.html'))
199+
var modules = gulp.src(j(C.BUILD,'mm-*/mm-*.html'))
193200
.pipe(vulcanizeSingle({
194201
inlineScripts: true,
195202
inlineCss: true,
196203
implicitStrip: false
197-
}, excludes, BUILD))
204+
}, excludes, C.BUILD))
198205
.pipe(base64(['.woff']))
199206
.pipe(htmlmin({
200207
quotes: true,
@@ -204,9 +211,9 @@ gulp.task('build:prod', ['patch-lib', 'build'], function() {
204211
.pipe(inlinemin())
205212
.pipe(header('<!--\n' + fs.readFileSync('BANNER.txt','utf8') + ' -->'))
206213
.pipe(dbg('vulcanize-modules'))
207-
.pipe(gulp.dest(DIST));
214+
.pipe(gulp.dest(C.DIST));
208215

209-
var lib = gulp.src(j(BUILD,'strand.html'))
216+
var lib = gulp.src(j(C.BUILD,'strand.html'))
210217
.pipe(vulcanize({
211218
inlineScripts: true,
212219
inlineCss: true,
@@ -221,7 +228,7 @@ gulp.task('build:prod', ['patch-lib', 'build'], function() {
221228
.pipe(inlinemin())
222229
.pipe(header('<!--\n' + fs.readFileSync('BANNER.txt','utf8') + ' -->'))
223230
.pipe(dbg('vulcanize-lib'))
224-
.pipe(gulp.dest(DIST));
231+
.pipe(gulp.dest(C.DIST));
225232

226233
return merge(modules, lib);
227234
});
@@ -230,7 +237,7 @@ gulp.task('build:prod', ['patch-lib', 'build'], function() {
230237
gulp.task('docs', ['copy:docs', 'sass:docs', 'docs:templates']);
231238

232239
gulp.task('clean:docs', function() {
233-
return del([BUILD_DOCS+'**']);
240+
return del([C.BUILD_DOCS+'**']);
234241
});
235242

236243
gulp.task('copy:docs', function() {
@@ -239,15 +246,15 @@ gulp.task('copy:docs', function() {
239246
var license = gulp.src('LICENSE.txt');
240247

241248
var merged_static = merge(assets, cname, license)
242-
.pipe(gulp.dest(BUILD_DOCS));
249+
.pipe(gulp.dest(C.BUILD_DOCS));
243250

244251
var bower_components = gulp.src(['bower_components/webcomponentsjs/**/*', 'bower_components/polymer/**/*'], {base:'bower_components'})
245252
.pipe(dbg('copy-bower'))
246-
.pipe(gulp.dest(BUILD_DOCS+'/bower_components/'));
253+
.pipe(gulp.dest(C.BUILD_DOCS+'/bower_components/'));
247254

248-
var lib = gulp.src(j(BUILD,'**'))
255+
var lib = gulp.src(j(C.BUILD,'**'))
249256
.pipe(dbg('copy-lib'))
250-
.pipe(gulp.dest(j(BUILD_DOCS,'/bower_components/strand/dist')));
257+
.pipe(gulp.dest(j(C.BUILD_DOCS,'/bower_components/strand/dist')));
251258

252259
return merge(bower_components, merged_static, lib);
253260
});
@@ -257,7 +264,7 @@ gulp.task('sass:docs', function() {
257264
.pipe(dbg('sass-docs'))
258265
.pipe(sass({includePaths: ['./bower_components/bourbon/app/assets/stylesheets/', './src/shared/sass/']}).on('error', sass.logError))
259266
.pipe(postcss([autoprefixer({browsers: ['last 2 versions']})]))
260-
.pipe(gulp.dest(BUILD_DOCS));
267+
.pipe(gulp.dest(C.BUILD_DOCS));
261268
});
262269

263270
gulp.task('docs:templates', function() {
@@ -319,7 +326,7 @@ gulp.task('docs:templates', function() {
319326
function injectModuleData(pkg, moduleMap, articleList, articleMap) {
320327
return through.obj(function(file, enc, cb) {
321328
var moduleDoc = JSON.parse(file.contents);
322-
var examplePath = path.join(SRC, moduleDoc.name, 'example.html');
329+
var examplePath = path.join(C.SRC, moduleDoc.name, 'example.html');
323330
var example;
324331
try {
325332
example = fs.readFileSync(examplePath).toString('utf8');
@@ -364,15 +371,15 @@ gulp.task('docs:templates', function() {
364371
var pkg = getPkgInfo();
365372

366373
// Create moduleList from directory listing
367-
var modules = glob.sync("mm-*/doc.json", {cwd:SRC});
374+
var modules = glob.sync("mm-*/doc.json", {cwd:C.SRC});
368375
var moduleList = modules.map(function(name) { return name.replace('/doc.json',''); });
369376
var moduleMap = moduleList.map(function(name) { return {name: name}; });
370377

371378
// Create behaviorsMap
372-
var behaviors = glob.sync(j(SRC,'shared/behaviors/*.json'));
379+
var behaviors = glob.sync(j(C.SRC,'shared/behaviors/*.json'));
373380
var behaviorsMap = {};
374381
behaviors.forEach(function(behavior) {
375-
var behaviorKey = behavior.replace(j(SRC,'shared/behaviors/'),'')
382+
var behaviorKey = behavior.replace(j(C.SRC,'shared/behaviors/'),'')
376383
.replace('.json','')
377384
.toLowerCase();
378385
behaviorsMap[behaviorKey] = JSON.parse(fs.readFileSync(behavior));
@@ -426,9 +433,9 @@ gulp.task('docs:templates', function() {
426433
this.push(file);
427434
cb();
428435
}))
429-
.pipe(gulp.dest(BUILD_DOCS));
436+
.pipe(gulp.dest(C.BUILD_DOCS));
430437

431-
var moduleStream = gulp.src(j(SRC,'mm-*/doc.json'))
438+
var moduleStream = gulp.src(j(C.SRC,'mm-*/doc.json'))
432439
.pipe(cache('docs_module'))
433440
.pipe(injectBehaviorDocs(behaviorsMap))
434441
.pipe(injectModuleData(pkg, moduleMap, articleList, articleMap))
@@ -448,7 +455,7 @@ gulp.task('docs:templates', function() {
448455
path.dirname = '';
449456
path.extname = '.html';
450457
}))
451-
.pipe(gulp.dest(BUILD_DOCS))
458+
.pipe(gulp.dest(C.BUILD_DOCS))
452459
.on('error',console.log);
453460

454461
var articleStream = gulp.src('./docs/articles/*.md')
@@ -457,14 +464,14 @@ gulp.task('docs:templates', function() {
457464
.pipe(injectArticleData(pkg, moduleMap, articleList, articleMap))
458465
.pipe(rename({prefix: 'article_'}))
459466
.pipe(dbg('docs-articles'))
460-
.pipe(gulp.dest(BUILD_DOCS));
467+
.pipe(gulp.dest(C.BUILD_DOCS));
461468

462469
return merge(indexStream, moduleStream, articleStream);
463470
});
464471

465472
gulp.task('gh-pages', function() {
466473
var pkg = getPkgInfo();
467-
return gulp.src(BUILD_DOCS+'**/*')
474+
return gulp.src(C.BUILD_DOCS+'**/*')
468475
.pipe(dbg('gh-pages'))
469476
.pipe(ghPages({
470477
message: 'docs updates v'+pkg.version
@@ -474,47 +481,47 @@ gulp.task('gh-pages', function() {
474481
/** LIVE **/
475482

476483
gulp.task('watch', function () {
477-
gulp.watch(j(SRC,'mm-*/*.scss'), ['sass']);
478-
gulp.watch(j(SRC,'mm-*/*.html'), ['copy', 'vulcanize']);
484+
gulp.watch(j(C.SRC,'mm-*/*.scss'), ['sass']);
485+
gulp.watch(j(C.SRC,'mm-*/*.html'), ['copy', 'vulcanize']);
479486
});
480487

481488
gulp.task('index', function() {
482-
var modules = glob.sync("mm-*/index.html", {cwd:SRC});
489+
var modules = glob.sync("mm-*/index.html", {cwd:C.SRC});
483490
var moduleList = modules.map(function(name) { return name.replace('/index.html',''); });
484491
var moduleMap = {modules: moduleList};
485492
var templatePath = path.join(__dirname,'gulp/templates/index_template.html');
486493
var templateString = fs.readFileSync(templatePath).toString('utf8');
487494
var template = hogan.compile(templateString);
488495
var index = template.render(moduleMap);
489-
fs.writeFileSync(j(BUILD,'index.html'), index);
496+
fs.writeFileSync(j(C.BUILD,'index.html'), index);
490497
});
491498

492499
gulp.task('server', function() {
493500
var server = connect()
494501
.use('/bower_components', serveStatic('./bower_components'))
495-
.use(serveStatic(BUILD))
496-
.listen(LIVE_PORT);
502+
.use(serveStatic(C.BUILD))
503+
.listen(C.LIVE_PORT);
497504

498505
gulp.src(__filename)
499506
.pipe(open({
500-
uri: 'http://localhost:'+LIVE_PORT
507+
uri: 'http://localhost:'+C.LIVE_PORT
501508
}));
502509
});
503510

504511
gulp.task('live', ['index', 'watch', 'server']);
505512

506513
gulp.task('watch:docs', function() {
507-
gulp.watch(['docs/**/*.md', SRC + '**/doc.json'], ['docs:templates']);
514+
gulp.watch(['docs/**/*.md', C.SRC + '**/doc.json'], ['docs:templates']);
508515
});
509516

510517
gulp.task('server:docs', function() {
511518
var server = connect()
512-
.use(serveStatic(BUILD_DOCS))
513-
.listen(DOCS_PORT);
519+
.use(serveStatic(C.BUILD_DOCS))
520+
.listen(C.DOCS_PORT);
514521

515522
gulp.src(__filename)
516523
.pipe(open({
517-
uri: 'http://localhost:'+DOCS_PORT
524+
uri: 'http://localhost:'+C.DOCS_PORT
518525
}));
519526
});
520527

@@ -569,7 +576,7 @@ function getPkgInfo() {
569576

570577
gulp.task('stage-release', function() {
571578
var pkg = getPkgInfo();
572-
return gulp.src([DIST, 'package.json', 'bower.json', 'CHANGELOG.md'])
579+
return gulp.src([C.DIST, 'package.json', 'bower.json', 'CHANGELOG.md'])
573580
.pipe(git.add())
574581
.pipe(git.commit('Release v'+pkg.version));
575582
});

0 commit comments

Comments
 (0)