Thu Dec 01 2016
Copied to clipboard! Copy reply
  • 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
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
'use strict';
// generated on 2014-08-12 using generator-gulp-webapp 0.1.0
// run with --stage to bring {"stage": true} into templates

//For default task
var configsrc = 'pageconfigs/catalogs/gulpconfig_catalog_eng_DefaultSwap.json';
// 'gulpconfig_catalog_gifting.json';
//'pageconfigs/hubs/gulpconfig_hub_foodtruck.json'
//'pageconfigs/catalogs/gulpconfig_catalog_fra_DefaultSwap.json',
//'pageconfigs/catalogs/gulpconfig_catalog_eng_DefaultSwap.json',
//'pageconfigs/catalogs/gulpconfig_catalog_ita_DefaultSwap.json',
//'pageconfigs/catalogs/gulpconfig_catalog_deu_DefaultSwap.json',
//'pageconfigs/catalogs/gulpconfig_catalog_esp_DefaultSwap.json',
//'pageconfigs/catalogs/gulpconfig_catalog_esc_DefaultSwap.json'

//Full set of configs for full site build
var configs = [

'pageconfigs/catalogs/gulpconfig_catalog_fra_DefaultSwap.json',
'pageconfigs/catalogs/gulpconfig_catalog_eng_DefaultSwap.json',
'pageconfigs/catalogs/gulpconfig_catalog_ita_DefaultSwap.json',
'pageconfigs/catalogs/gulpconfig_catalog_deu_DefaultSwap.json',
'pageconfigs/catalogs/gulpconfig_catalog_esp_DefaultSwap.json',
'pageconfigs/catalogs/gulpconfig_catalog_esc_DefaultSwap.json',
"pageconfigs/catalogs/gulpconfig_catalog_ara_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_kor_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_chi_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_far_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_ned_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_pol_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_gle_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_por_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_ebr_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_grk_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_rus_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_heb_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_sve_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_hin_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_tgl_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_tur_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_vie_DefaultSwap.json",
"pageconfigs/catalogs/gulpconfig_catalog_jpn_DefaultSwap.json"

// 	'pageconfigs/catalogs/gulpconfig_catalog_fra_meclabs.json',
// 	'pageconfigs/catalogs/gulpconfig_catalog_ita_meclabs.json',
// 	'pageconfigs/catalogs/gulpconfig_catalog_deu_meclabs.json',
// 	'pageconfigs/catalogs/gulpconfig_catalog_esp_meclabs.json'
	
//  	'pageconfigs/catalogs/gulpconfig_catalog_fra_devSPO.json',
//  	'pageconfigs/catalogs/gulpconfig_catalog_ita_devSPO.json',
//  	'pageconfigs/catalogs/gulpconfig_catalog_deu_devSPO.json',
//  	'pageconfigs/catalogs/gulpconfig_catalog_esc_devSPO.json',
//  	'pageconfigs/catalogs/gulpconfig_catalog_esp_devSPO.json'
	
	// "pageconfigs/catalogs/gulpconfig_catalog_ara.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_kor.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_chi.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_far.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_ned.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_pol.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_gle.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_por.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_ebr.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_grk.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_rus.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_heb.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_sve.json",
// 	"pageconfigs/catalogs/gulpconfig_catalog_eng.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_hin.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_tgl.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_tur.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_vie.json",
	// "pageconfigs/catalogs/gulpconfig_catalog_jpn.json"

	//'gulpconfig_content_central_french_dev.json',
	//'gulpconfig_content_beginners_french_dev.json',
	//'gulpconfig_content_conversational_french_dev.json',
	//'gulpconfig_content_future_french_dev.json',
	//'gulpconfig_content_history_french_dev.json',
	//'gulpconfig_content_learnfast_french_dev.json',
	//'gulpconfig_content_live_french_dev.json',
	//'gulpconfig_content_phrases_french_dev.json',
	//'gulpconfig_content_pronunciation_french_dev.json',
	//'gulpconfig_content_studystrats_french_dev.json',
	//'gulpconfig_content_whylearn_french_dev.json',
	//'gulpconfig_catalog_albert_english.json',
	//'gulpconfig_catalog_albert_french.json',
	//'gulpconfig_catalog_albert_italian.json',
	//'gulpconfig_catalog_albert_german.json',
	//'gulpconfig_catalog_albert_spanish.json',
	//'gulpconfig_catalog_albert_spanish-spain.json'

	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_ara.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_chi.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_deu.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_ebr.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_eng.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_esc.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_esp.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_far.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_fra.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_gle.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_grk.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_heb.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_hin.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_ita.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_jpn.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_kor.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_lat.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_ned.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_pol.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_por.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_rus.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_sve.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_tgl.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_tur.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_catalog_vie.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_demo.json",
	// "pageconfigs/homeschool/gulpconfig_homeschool_homepage.json"
]

var gulp = require('gulp');
var fs = require('fs');
var path = require('path');
var lo_ = require('lodash');

var args = lo_(process.argv.slice(2))
  .filter(function(v){ return v.match && v.match(/-.+/) })
  .map(function(v){ return v.replace(/^-*/,''); })
  .reduce(function(a,v){ a[v] = true; return a; }, {});

console.log(JSON.stringify(args));


//for binding business logic into the generator.
//Previously we get RSI.json by running: copy(JSON.stringify(RSI()))
//in Chrome console on http://www.rosettastone.com/lp/sbs/sitewide/
//Now update 'RSI_###.json' to correct pricepoint in the following line.
//each pricepoint rsifile exists in this repo.
//TODO: gulp flag implementation for rsi
var businessdata = JSON.parse( fs.readFileSync(__dirname + path.sep + 'RSI_cybermonday.json') );


// load plugins
var $ = require('gulp-load-plugins')();

gulp.task('styles', function () {
	var cfgobj = JSON.parse( fs.readFileSync(__dirname + path.sep + configsrc) );
	if(cfgobj.styles instanceof Array && cfgobj.styles.length <= 0){
		return;
	}
	var destext = cfgobj.dest;
	var stylessrc = cfgobj.styles || 'build/styles/*.less';

	//Styles are templated with a more limited set of data (from config.json#content)
	console.log(stylessrc);
	return gulp.src(stylessrc)
		.pipe($.template( JSON.parse( fs.readFileSync(__dirname + path.sep + configsrc) ).content || {} ))
		.pipe($.concat('main.css'))
		.pipe($.less({
			"options": {
				"no-ie-compat": true
			}
		}))
		.pipe($.minifyCss())
		.pipe($.autoprefixer('last 1 version'))
		.pipe(gulp.dest('deploy' + (destext ? destext : '') + '/styles'))
});


gulp.task('scripts', function () {
	var cfgobj = JSON.parse( fs.readFileSync(__dirname + path.sep + configsrc) );

	var scriptssrc = cfgobj.scripts || 'build/scripts/*.js';
	var baseopts = scriptssrc ? {base: 'build/scripts/'} : null;
	console.log(scriptssrc);
	var destext = cfgobj.dest;

	return gulp.src(scriptssrc)
		.pipe($.size())
		.pipe($.jshint())
		//.pipe( $.jshint.reporter('default') )
		//.pipe( $.jshint.reporter('fail') )
		//.pipe($.sourcemaps.init())
			.pipe($.concat('all.js'))
			.pipe($.uglify())
		//.pipe($.sourcemaps.write())
		.pipe($.size())
		.pipe(gulp.dest('deploy' + (destext ? destext : '') + '/scripts'))
});

gulp.task('assets', function () {
	return gulp.src('build/assets/**/*')
		.pipe(gulp.dest('deploy/assets'))
});

gulp.task('promos', function () {
	return gulp.src('build/promos/*')
		.pipe(gulp.dest('deploy/promos'))
});

gulp.task('pixels', function () {
	return gulp.src('build/pixels/*')
		.pipe(gulp.dest('deploy/pixels'))
});


// moves files from build/assets to deploy/assets
gulp.task('move_assets', function () {
  return gulp.src(['build/assets/*'], { base: 'src' })
    .pipe(gulp.dest('deploy'))
})

function langbranch(_obj, _key) {
    if(!_key || !_obj)
    {
        return {};
    }
    var ret = {};
    var keys = Object.keys(_obj).slice();

    if( ~keys.indexOf(_key) )
    {
        return _obj[_key];
    }
    else
    {
        keys.forEach(function(v){ ret[v] = langbranch(_obj[v], _key); });
    }

    return ret;
}

function RSI(e) {
    function n(e) {
        if (typeof e != "object")
            return [];
        var n = [], r = t.slice(), i = [], s;
        for (var o in e)
            e.hasOwnProperty(o) && i.push([o, e[o]]);
        var u;
        if (!i.length)
            return [];
        while (s = r.pop()) {
            u = !0;
            for (var a = 0, f = i.length; a < f; a++) {
                var l = i[a][0], c = i[a][1];
                if (!s[l] || s[l] !== c)
                    u = !1, a = f
            }
            u && n.push(s)
        }
        return n
    }

	var t = businessdata;

	t = t.concat([
// 		{
// 			family: "fitbrains",
// 			cat: "eng",
// 			lvl: "monthly",
// 			sku: "98392",
// 			media: "subscription",
// 			promokey: "TOSUB_12",
// 			msrp: "19.95",
// 			price: "15.95",
// 			cart: "https://secure.rosettastone.com/us_en_store_view/checkout/cart/add/sku/98392/category_id/eng/?pc=achieve_12M"
// 		},
// 		{
// 			family: "fitbrains",
// 			cat: "eng",
// 			lvl: "yearly",
// 			sku: "98393",
// 			media: "subscription",
// 			promokey: "TOSUB_12",
// 			msrp: "99.95",
// 			price: "79.95",
// 			cart: "https://secure.rosettastone.com/us_en_store_view/checkout/cart/add/sku/98393/category_id/eng/?pc=achieve_12M"
// 		},
// 		{
// 			family: "fitbrains",
// 			cat: "eng",
// 			lvl: "twoyear",
// 			sku: "11111",
// 			media: "subscription",
// 			promokey: "TOSUB_12",
// 			msrp: "299",
// 			price: "6.25",
// 			cart: "https://secure.rosettastone.com/us_en_store_view/checkout/cart/add/sku/11111/category_id/eng/?pc=achieve_12M"
// 		},
// 		{
// 			family: "fitbrains",
// 			cat: "eng",
// 			lvl: "lifetime",
// 			sku: "11111",
// 			media: "subscription",
// 			promokey: "TOSUB_12",
// 			msrp: "299",
// 			price: "2.99",
// 			cart: "https://secure.rosettastone.com/us_en_store_view/checkout/cart/add/sku/11111/category_id/eng/?pc=achieve_12M"
// 		},
// 		{
// 			family: "kids",
// 			cat: "eng",
// 			lvl: "monthly",
// 			sku: "98396",
// 			media: "subscription",
// 			promokey: "TOSUB_12",
// 			msrp: "299",
// 			price: "19.99",
// 			cart: "https://secure.rosettastone.com/us_en_store_view/checkout/cart/add/sku/98396/category_id/eng/?pc=sitewide"
// 		},
// 		{
// 			family: "kids",
// 			cat: "eng",
// 			lvl: "yearly",
// 			sku: "98397",
// 			media: "subscription",
// 			promokey: "TOSUB_12",
// 			msrp: "69.99",
// 			price: "69.99",
// 			cart: "https://secure.rosettastone.com/us_en_store_view/checkout/cart/add/sku/98397/category_id/eng/?pc=sitewide"
// 		},
// 		{
// 			family: "kids",
// 			cat: "eng",
// 			lvl: "threemonth",
// 			sku: "99109",
// 			media: "subscription",
// 			promokey: "TOSUB_12",
// 			msrp: "19.99",
// 			price: "19.99",
// 			cart: "https://secure.rosettastone.com/us_en_store_view/checkout/cart/add/sku/99109/category_id/eng/?pc=sitewide"
// 		}
	]);
	return e ? n(e) : t
}

function getbusinessdata(rqst) {
	var basereq = !!rqst ? {cat: rqst} : {};
	//console.log('Preparing Business Logic For:');
	//console.log(basereq);
	return function(_index) {
		return RSI(lo_.extend(_index, basereq));
	}
}

//prefixing styles for AMP process, which needs inlined styles, so 'styles' must be done prior to 'html''
gulp.task('html', /*['styles'],*/ function gulphtml() {
	var cfgobj = JSON.parse( fs.readFileSync(__dirname + path.sep + configsrc) );
	console.log('processing: ', configsrc);
	var localedata = __dirname + path.sep + 'build' + path.sep + 'assets' + path.sep + 'localedata.json';
	var localeobj = JSON.parse( fs.readFileSync(localedata) );
	var htmlguides = cfgobj.html;
	var contentdata = cfgobj.content;
	var tagsource = langbranch(localeobj, 'en');
	var cfgsource = [];
	var indexbasename = "";
	var destext = cfgobj.dest;

	while (htmlguides.length) {
		(function preparetemplates(_t) {
			tagsource[_t[1]] = fs.readFileSync(__dirname + path.sep + _t[0]);
			cfgsource.push(_t[0]);
			if(_t[1] === "index")
			{
				indexbasename = _t[0].split('/').pop();
			}
		})(htmlguides.pop());
	}

	//apply content variables to tagsource
	!!contentdata && ( tagsource = lo_.defaults(tagsource, contentdata) );

	//apply business logic using the getbusinessdata function (which should use the same source as Landing Pages, to maintain a single point of truth.)
	!!contentdata && ( tagsource = lo_.defaults(tagsource, {"getbusinessdata": getbusinessdata(contentdata && contentdata.content_langcode)}) );
	tagsource = lo_.defaults(tagsource, {"crosssell": getbusinessdata()});
	//SAM: put custom functions you want to expose here.
	tagsource = lo_.defaults(tagsource, {gettoplvl: function(o) {return (getbusinessdata(contentdata && contentdata.content_langcode))(o).sort(function(a,b){return a.lvl === b.lvl ? 0 : a.lvl<b.lvl ? 1 : -1})[0];}});
	//fs.writeFileSync('htmlsafe.json', JSON.stringify(RSI()))

	tagsource = lo_.defaults(tagsource, args);

	//JSON.stringify( crosssell().map(function(v){ var tmp={}, k = Object.keys(v); k.forEach(function(u){ tmp[u] = (v[u] + '').replace(/[<>\/]/g,'') }); return tmp; }) )
	//console.log(langbranch(localeobj, 'de'));
	return gulp.src(cfgsource.length ? cfgsource : 'build/*.html')
		.pipe($.template( tagsource ))
		.pipe($.template( tagsource )) /* 2nd pass to localize template html */
		//.pipe($.htmlhint())
		//.pipe($.htmlhint.reporter())
		.pipe($.size())
		.pipe($.htmlmin({collapseWhitespace: true, removeComments: true, keepClosingSlash:true, minifyJS: true}))
		.pipe($.size())
		.pipe($.rename(function renamehtml(path){
			if ( indexbasename == (path.basename +  path.extname) )
			{
				path.basename = "index";
			}
		}))
		.pipe(gulp.dest('deploy' + (destext ? destext : '')))
});

gulp.task('website', ['styles', 'scripts', 'html', 'promos', 'pixels'], function () {
    return gulp.src('deploy/**/*')
		.pipe($.size());
});

//for gulp full, we only need to move assets and promos once.
gulp.task('websitefull', ['styles', 'scripts', 'html'], function () {
    return gulp.src('deploy/**/*')
		.pipe($.size());
});



gulp.task('clean', function () {
    return gulp.src(['deploy/!(assets)'], { read: false }).pipe($.clean());
});

gulp.task('build', ['website'], function(){ return typeof nxt === "function" && nxt(); });

gulp.task('default', ['clean'], function () {
    gulp.start('build');
});


//NEW FULL: only does assets, promos, and pixels once.
gulp.task('full', ['clean'], function () {
	gulp.start('assets');
	gulp.start('promos');
	gulp.start('pixels');

	function runconfig(cfg) {
		if (!cfg.length)
		{
			nxt = null;
			return 1;
		}
		console.log('Processing: ' + cfg.slice(-1).pop() + ', #remaining: ' + cfg.length);
		configsrc = cfg.pop();
		var nxt = function(){ return runconfig(cfg); };
		gulp.task(configsrc, ['websitefull'], function(){ return nxt(); });
		gulp.start(configsrc);
	}
	runconfig(configs);
});

gulp.task('stage', ['clean'], function () {
	businessdata = lo_.map(businessdata, function(v){
		v.cart = v.cart ? v.cart.replace(/https/, 'http').replace(/secure/, 'secure.stg') : '';
		return v;
	});

	function runconfig(cfg) {
		if (!cfg.length)
		{
			nxt = null;
			return 1;
		}
		console.log('Processing: ' + cfg.slice(-1).pop() + ', #remaining: ' + cfg.length);
		configsrc = cfg.pop();
		var nxt = function(){ return runconfig(cfg); };
		//define a task, with the name of configsrc, which will:
		  //1. run the website with current cfgsource, which is set above
		  //2. recursively run this function with cfg length reduced by 1
		gulp.task(configsrc, ['website'], function(){ return nxt(); });
		//run the task we just defined.
		gulp.start(configsrc);
	}
	runconfig(configs);
});


gulp.task('connect', function () {
    var connect = require('connect');
    var app = connect()
        .use(require('connect-livereload')({ port: 35729 }))
        .use(connect.static('deploy'))
        .use(connect.directory('deploy'));

    require('http').createServer(app)
        .listen(9000)
        .on('listening', function () {
            console.log('Started connect web server on http://localhost:9000');
        });
});

gulp.task('serve', ['connect'], function () {
    require('opn')('http://localhost:9000');
});

gulp.task('watch', ['connect', 'serve'], function () {
    var server = $.livereload();
    // watch for changes

    gulp.watch([
        'deploy/**/*'
    ]).on('change', function (file) {
        server.changed(file.path);
    });

    gulp.watch('build/**/*.css', ['styles']);
    gulp.watch('build/**/*.less', ['styles']);
	gulp.watch('build/**/*.js', ['scripts']);
	gulp.watch('build/**/*.html', ['html']);
	gulp.watch('build/assets/*.json', ['assets']);
	gulp.watch('build/promos/*', ['promos']);
	gulp.watch('build/pixels/*', ['pixels']);
});

gulp.task('fresh', ['connect'], function () {
    var server = $.livereload();
    // watch for changes

    gulp.watch([
        'deploy/**/*'
    ]).on('change', function (file) {
        server.changed(file.path);
    })
	.on('ready', function(){
		server.changed();
	});

	//setTimeout(function(){ server.listen(9000) }, 2000);
    gulp.watch('build/**/*.css', ['styles']);
    gulp.watch('build/**/*.less', ['styles']);
    //to autoreload for AMP - can remove later
    //gulp.watch('build/**/*.less', ['html']);
	gulp.watch('build/**/*.js', ['scripts']);
	gulp.watch('build/**/*.html', ['html']);
    //gulp.watch('build/assets/**/*.*', ['assets']);
	//gulp.watch('build/assets/*.json', ['assets']);
	//gulp.watch('build/images/*', ['html']);
	gulp.watch('build/promos/*', ['promos']);
	gulp.watch('build/pixels/*', ['pixels']);
});

/* gulp pages
 * performs 'gulp full' on a directory in /pageconfigs
 * e.g. 'gulp pages --catalogs'
*/
gulp.task('pages', function () {
	var args = process.argv.slice(3);
	var dir = args[0];
	if (typeof dir==="undefined") {
		console.log("No directory argument given, using default...");
		gulp.start('full');
		return 1;
	}
	dir = dir.replace(/^--/g,"");
	fs.readdir("pageconfigs/" + dir, function(e,f){
		if (e) {
			console.log("Error: Directory '"+dir+"' not found in pageconfigs");
			console.log("Example input: gulp pages --catalogs");
			return false;
		}
		configs = f.filter(function(i){return i.match(/\.json$/)}).map(function(i){return "pageconfigs/"+dir+"/"+i});
		console.log(configs);
		gulp.start('full')
	});
});

module.exports = gulp;