Guest User

Untitled

a guest
Jun 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.89 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /*!
  4. * Module dependencies.
  5. */
  6.  
  7. var templateCache = require('gulp-angular-templatecache');
  8. var sourcemaps = require('gulp-sourcemaps');
  9. var minifyCSS = require('gulp-minify-css');
  10. var aws = require('gulp-awspublish');
  11. var parallelize = require("concurrent-transform");
  12. var concat = require('gulp-concat');
  13. var uglify = require('gulp-uglify');
  14. var rename = require('gulp-rename');
  15. var header = require('gulp-header');
  16. var footer = require('gulp-footer');
  17. var replace = require('gulp-replace');
  18. var karma = require('karma').server;
  19. var less = require('less-stream');
  20. var gulpif = require('gulp-if');
  21. var gulp = require('gulp');
  22. var fs = require('fs');
  23. var closureCompiler = require('gulp-closure-compiler');
  24.  
  25.  
  26. // conventions
  27. var fonts = ['.eot', '.svg', '.ttf', '.woff', '.otf'];
  28. var images = ['.png', '.jpeg', '.jpg', '.gif'];
  29. var envs = ['dev', 'staging', 'production'];
  30. var apps = fs.readdirSync(__dirname + '/public/apps');
  31. var locales = fs.readdirSync(__dirname + '/public/locales');
  32.  
  33. // make sure apps only contains dirs
  34. apps = apps.filter(dir(__dirname + '/public/apps'));
  35.  
  36. // banner
  37. var banner = [
  38. '/**',
  39. ' * <%= pkg.name %>',
  40. ' * <%= pkg.description %>',
  41. ' * @version <%= pkg.version %>',
  42. ' */',
  43. '', ''
  44. ].join('\n');
  45.  
  46. // create tasks for release
  47. releases(prepare());
  48.  
  49. // create tasks for test
  50. tests();
  51.  
  52. // create tasks for publishing to s3
  53. publish();
  54.  
  55. // put all the locales and translations per app in one object
  56. // allLocales[app1][en],
  57. // allLocales[app1][nl]
  58. // this makes it easier to inject to window.locales
  59. var allLocales = {};
  60. buildLocales();
  61.  
  62. // create all release tasks
  63. apps.forEach(function (app) {
  64. envs.forEach(function (env) {
  65. gulp.task('release:' + app + ':' + env, function () {
  66. return release.call(this, app, env);
  67. });
  68. });
  69. });
  70.  
  71. // used to make releases and tests
  72. var pkg; // app.json file of the app
  73. var dist; // dist directory
  74. var path; // path to the app
  75. var files = []; // js files required by the app
  76.  
  77. /**
  78. * Bump
  79. */
  80.  
  81. gulp.task('bump', function () {
  82. return gulp.src(path + '/app.json')
  83. .pipe(gulp.dest(path + '/'));
  84. });
  85.  
  86. /**
  87. * Concat js
  88. */
  89.  
  90. gulp.task('concat', ['bump', 'templates'], function () {
  91. // don't use require here as it will be cached and doesn't get the
  92. // appropriate result
  93. pkg = JSON.parse(fs.readFileSync(path + '/app.json'));
  94. var appLocale = JSON.stringify(allLocales[pkg.name]) + ';\n';
  95. return gulp.src(files)
  96. .pipe(sourcemaps.init())
  97. .pipe(concat('app.js', {newLine: ';'}))
  98. .pipe(header('window.locales = ' + appLocale))
  99. .pipe(header(banner, {pkg: pkg}))
  100. .pipe(sourcemaps.write('./'))
  101. .pipe(gulp.dest(dist + '/'));
  102. });
  103.  
  104. /**
  105. * Minify js
  106. *
  107. */
  108.  
  109. gulp.task('minify', ['concat'], function () {
  110. return gulp.src(dist + '/app.js')
  111. .pipe(uglify({mangle: false}))
  112. .pipe(closureCompiler({
  113. compilerPath: 'public/bower_components/closure-compiler/compiler.jar',
  114. fileName: 'app.' + pkg.version + '.min.js',
  115. continueWithWarnings: true,
  116. compilerFlags: {
  117. warning_level: 'QUIET',
  118. }
  119. }))
  120. .pipe(gulp.dest(dist))
  121. .pipe(header(banner, {pkg: pkg}))
  122. .pipe(footer('//# sourceMappingURL=app.js.map'))
  123. .pipe(gulp.dest(dist + '/'));
  124. });
  125.  
  126. /**
  127. * Less to css
  128. *
  129. * concat is a dependency coz we need pkg to be populated with bumped version.
  130. * we can also just read the file and parse it here. but its ok!
  131. */
  132.  
  133. gulp.task('less', ['bump', 'concat'], function () {
  134. // modify vars for releases and production
  135. var options = {
  136. modifyVariables: {
  137. 'fa-font-path' : '"fonts"',
  138. 'icon-font-path': '"fonts/"',
  139. 'cs-font-path' : '"fonts"',
  140. 'img-path' : '""',
  141. 'component-root': ''
  142. },
  143. paths : ['./components']
  144. };
  145.  
  146. return gulp.src(path + '/app.less')
  147. .pipe(sourcemaps.init())
  148. .pipe(concat('app.css'))
  149. .pipe(less(options))
  150. .pipe(header(banner, {pkg: pkg}))
  151. .pipe(sourcemaps.write('./'))
  152. .pipe(gulp.dest(dist));
  153. });
  154.  
  155. /**
  156. * Minify css
  157. *
  158. */
  159.  
  160. gulp.task('minify-css', ['less'], function () {
  161. return gulp.src(dist + '/app.css')
  162. .pipe(minifyCSS())
  163. .pipe(rename('app.' + pkg.version + '.min.css'))
  164. .pipe(header(banner, {pkg: pkg}))
  165. .pipe(gulp.dest(dist));
  166. });
  167.  
  168. /**
  169. * Templates
  170. */
  171.  
  172. gulp.task('templates', ['bump'], function () {
  173. files.push(dist + '/templates.js');
  174. // ignore coverage folder
  175. return gulp.src([
  176. path + '/**/*.html',
  177. '!' + path + '/development.html',
  178. '!' + path + '/production.html'
  179. ])
  180. .pipe(templateCache({module: pkg.name, root: '/apps/' + pkg.name}))
  181. .pipe(gulp.dest(dist));
  182. });
  183.  
  184. /**
  185. * component-templates
  186. */
  187.  
  188. gulp.task('component-templates', ['templates'], function () {
  189. files.push(dist + '/component-tpls-' + pkg.version +'/templates.js');
  190.  
  191. var _tpls = [];
  192.  
  193. pkg.components.forEach(function (com) {
  194. _tpls.push('./components/' + com.split('/')[0] + '/**/*.html');
  195. });
  196.  
  197. _tpls.push('!./components/**/*_test.html');
  198. _tpls.push('!./components/**/*_example*.html');
  199.  
  200.  
  201. // ignore coverage folder
  202. return gulp.src(_tpls)
  203. .pipe(templateCache({
  204. module: pkg.name,
  205. base : function (file) {
  206. var arr = file.path.split('/components/');
  207. return '/' + arr[1]
  208. }
  209. }))
  210. .pipe(gulp.dest(dist + '/component-tpls-' + pkg.version));
  211. });
  212.  
  213. /**
  214. * test components
  215. */
  216.  
  217. gulp.task('test:components', function (done) {
  218. karma.start({
  219. configFile: __dirname + '/components/karma.conf.js'
  220. }, done);
  221. });
  222.  
  223. /**
  224. * copy fonts
  225. */
  226.  
  227. gulp.task('copy:fonts', function () {
  228. return gulp.src([__dirname + '/public/assets/fonts/**'])
  229. .pipe(gulp.dest(dist + '/assets/fonts'))
  230. .pipe(gulp.dest(dist + '/fonts'));
  231. });
  232.  
  233. /**
  234. * copy images
  235. */
  236.  
  237. gulp.task('copy:img', function () {
  238. return gulp.src([__dirname + '/public/assets/img/**'])
  239. .pipe(gulp.dest(dist + '/assets/img'))
  240. .pipe(gulp.dest(dist + '/img'));
  241. });
  242.  
  243.  
  244. /**
  245. * copy favicons
  246. */
  247.  
  248. gulp.task('copy:favicons', function () {
  249. return gulp.src([__dirname + '/public/assets/favicons/**'])
  250. .pipe(gulp.dest(dist + '/assets/favicons'))
  251. .pipe(gulp.dest(dist + '/favicons'));
  252. });
  253.  
  254.  
  255. /**
  256. * Default
  257. */
  258.  
  259. gulp.task('default', [
  260. 'test',
  261. 'release'
  262. ]);
  263.  
  264. /**
  265. * test an app
  266. */
  267.  
  268. function test(app, done) {
  269. karma.start({
  270. configFile: __dirname + '/public/apps/' + app + '/karma.conf.js'
  271. }, done);
  272. }
  273.  
  274. /**
  275. * test all apps
  276. */
  277.  
  278. function tests() {
  279. var tasks = ['test:components'];
  280. apps.forEach(function (app) {
  281. tasks.push('test:' + app);
  282. gulp.task('test:' + app, function (done) {
  283. return test.call(this, app, done);
  284. });
  285. });
  286. // gulp test
  287. gulp.task('test', tasks);
  288. }
  289.  
  290. /**
  291. * prepare
  292. *
  293. * Prepare tasks
  294. *
  295. * Parse the following to produce an array of tasks
  296. *
  297. * Examples:
  298. *
  299. * release:app1:env => release app1 with env settings
  300. * release:env => release all with env settings
  301. * release:app1 => release app1 app with dev env
  302. *
  303. * test:app1 => test app1 app
  304. */
  305.  
  306. function prepare() {
  307. var releases = [];
  308.  
  309. // release:env
  310. envs.forEach(function (env) {
  311. var _tasks = [];
  312. // all apps with `env` settings
  313. apps.forEach(function (app) {
  314. _tasks.push('release:' + app + ':' + env);
  315. });
  316. releases.push({
  317. task: 'release:' + env,
  318. arr : _tasks
  319. });
  320. });
  321.  
  322. // release:app1
  323. apps.forEach(function (app) {
  324. // `app` with dev settings
  325. releases.push({
  326. task: 'release:' + app,
  327. arr : ['release:' + app + ':dev']
  328. });
  329. });
  330.  
  331. // release:app1:env
  332. apps.forEach(function (app) {
  333. envs.forEach(function (env) {
  334. releases.push({
  335. task: 'release:' + app + ':' + env,
  336. arr : ['release:' + app + ':' + env]
  337. });
  338. });
  339. });
  340.  
  341. var _tasks = [];
  342. apps.forEach(function (app) {
  343. _tasks.push('release:' + app + ':dev');
  344. });
  345.  
  346. releases.push({
  347. task: 'release',
  348. arr : _tasks
  349. });
  350.  
  351. return releases;
  352. }
  353.  
  354. /**
  355. * releases
  356. */
  357.  
  358. function releases(tasks) {
  359. // for gulp release:app|env
  360. tasks.forEach(function (o) {
  361. gulp.task(o.task, o.arr);
  362. });
  363. }
  364.  
  365. /**
  366. * release
  367. */
  368.  
  369. function release(name, env) {
  370. path = './public/apps/' + name;
  371. pkg = require(path + '/app.json');
  372. dist = './public/dist/' + pkg.name;
  373. env = env;
  374.  
  375. files = pkg.dependencies.map(function (dep) {
  376. return 'public/bower_components/' + dep;
  377. });
  378. files = files.concat(pkg.components.map(function (com) {
  379. return 'components/' + com;
  380. }));
  381. files = files.concat(pkg.files.map(function (file) {
  382. return 'public/apps/' + name + '/' + file;
  383. }));
  384.  
  385. var cssJsIncludes = [
  386. '<link rel="stylesheet" href="app.' + pkg.version + '.min.css"/>',
  387. '<script src="app.' + pkg.version + '.min.js" type="text/javascript"></script>',
  388. '<script src="component-tpls-' + pkg.version +'/templates.js" type="text/javascript"></script>'
  389. ].join('')
  390.  
  391. var config = '<script>' + 'window.CONFIG = window.CONFIG || ' + JSON.stringify(pkg.config[env]) + ';</script>';
  392. gulp.src([__dirname + '/public/apps/' + pkg.name + '/production.html'])
  393. .pipe(rename('index.html'))
  394. .pipe(replace('<!-- INSERT_CONFIG -->', config))
  395. .pipe(replace('<!-- INSERT_CSS_JS -->', cssJsIncludes))
  396. .pipe(gulp.dest(dist));
  397.  
  398. return gulp.start(
  399. 'bump',
  400. 'templates',
  401. 'component-templates',
  402. 'concat',
  403. 'minify',
  404. 'less',
  405. 'minify-css',
  406. 'copy:fonts',
  407. 'copy:img',
  408. 'copy:favicons'
  409. );
  410. }
  411.  
  412. /**
  413. * Publish
  414. *
  415. * gulp publish:[env]:app1
  416. */
  417.  
  418. function publish() {
  419. apps.forEach(function (app) {
  420. // This is mostly used by wercker
  421. // $ gulp publish:app1
  422. if (process.env.NODE_ENV) {
  423. gulp.task('publish:' + app, function () {
  424. return upload.call(this, process.env.NODE_ENV, app);
  425. });
  426. }
  427.  
  428. // $ gulp publish:[env]:app1
  429. envs.forEach(function (env) {
  430. gulp.task('publish:' + env + ':' + app, function () {
  431. return upload.call(this, env, app);
  432. });
  433. });
  434. });
  435. }
  436.  
  437. /**
  438. * upload
  439. */
  440.  
  441. function upload(env, app) {
  442. var pkg = JSON.parse(fs.readFileSync('public/apps/' + app + '/app.json'));
  443.  
  444. if(process.env.S3_KEY && process.env.S3_SECRET && process.env.S3_BUCKET) {
  445. console.log("Environments variables preset.")
  446. } else {
  447. try {
  448. var config = JSON.parse(fs.readFileSync(__dirname + '/config/s3.json'));
  449. Object.keys(config[env]).forEach(function (key) {
  450. process.env['S3_' + key.toUpperCase()] = config[env][key];
  451. });
  452. } catch (err) {
  453. console.log('Make sure you copy config/s3.example.json to config/s3.json or declare environment variables');
  454. console.log(err);
  455. }
  456. }
  457.  
  458. // require it again so that the process.env's are replaced
  459. var s3creds = require('./config/s3');
  460. var creds = s3creds[env];
  461.  
  462. creds.params = { Bucket: creds.bucket };
  463. console.log('creds are ', creds);
  464. var s3 = aws.create(creds);
  465.  
  466. // Set max-age depending on env
  467. var maxAge = require('./config/max-age.json')[process.env.NODE_ENV || 'dev'];
  468.  
  469. // custom headers
  470. var headers = {
  471. 'Cache-Control': 'max-age=' + maxAge + ', no-transform, public'
  472. };
  473.  
  474. var opts = {
  475. force: true // skip cache
  476. };
  477.  
  478. // pseudo dir on s3
  479. function pseudoDir(prefix) {
  480. return function (path) {
  481. path.dirname = '/' + pkg.name + '/' + prefix;
  482. if (~fonts.indexOf(path.extname)) {
  483. path.basename = 'fonts/' + path.basename;
  484. }
  485. if (~images.indexOf(path.extname)) {
  486. path.basename = 'img/' + path.basename;
  487. }
  488. };
  489. }
  490.  
  491. function cssOrJs(file) {
  492. // .css (4 chars) and .js (3 chars)
  493. var ext = file.path.slice(-4);
  494. return ~ext.indexOf('.js') || ~ext.indexOf('.css');
  495. }
  496.  
  497. gulp.src('./public/dist/' + pkg.name + '/**/**')
  498. .pipe(gulpif(cssOrJs, aws.gzip()))
  499. .pipe(parallelize(s3.publish(headers, opts)))
  500. .pipe(aws.reporter());
  501. }
  502.  
  503. /**
  504. * Build locales object
  505. */
  506.  
  507. function buildLocales() {
  508. apps.forEach(function (app) {
  509. allLocales[app] = {};
  510. locales.forEach(function (locale) {
  511. var file = __dirname + '/public/locales/' + locale + '/' + app + '.json';
  512. allLocales[app][locale] = JSON.parse(fs.readFileSync(file));
  513. });
  514. });
  515. }
  516.  
  517. /**
  518. * dir
  519. */
  520.  
  521. function dir(path) {
  522. return function (app) {
  523. return fs.statSync(path + '/' + app).isDirectory();
  524. };
  525. }
Add Comment
Please, Sign In to add comment