gulpfile.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Utlimate Jay Mega Gulpfile */
  2. /* global require:true, process:true */
  3. /* jshint laxbreak:true */
  4. const babel = require('gulp-babel');
  5. const terser = require('gulp-terser-js');
  6. (function() {
  7. 'use strict';
  8. var pkg = require('./package.json'),
  9. del = require('del'),
  10. yargs = require('yargs'),
  11. exec = require('exec'),
  12. fs = require('fs'),
  13. dateFormat = require('date-format'),
  14. spawn = require('child_process').spawn,
  15. gulp = require('gulp'),
  16. plugins = require('gulp-load-plugins')();
  17. var bumpVersion = yargs.argv.type || 'patch';
  18. var settings = {
  19. name: 'vegas',
  20. banner: {
  21. content: [
  22. '/*!-----------------------------------------------------------------------------',
  23. ' * <%= pkg.description %>',
  24. ' * v<%= pkg.version %> - built <%= datetime %>',
  25. ' * Licensed under the MIT License.',
  26. ' * http://vegas.jaysalvat.com/',
  27. ' * ----------------------------------------------------------------------------',
  28. ' * Copyright (C) 2010-<%= year %> Jay Salvat',
  29. ' * http://jaysalvat.com/',
  30. ' * --------------------------------------------------------------------------*/',
  31. ''
  32. ].join('\n'),
  33. vars: {
  34. pkg: pkg,
  35. datetime: dateFormat.asString('yyyy-MM-dd'),
  36. year: dateFormat.asString('yyyy')
  37. }
  38. }
  39. };
  40. const getPackageJson = function() {
  41. return JSON.parse(fs.readFileSync('./package.json'));
  42. };
  43. function clean(cb) {
  44. return del([ './dist' ], cb);
  45. }
  46. exports.clean = clean;
  47. function tmpClean(cb) {
  48. return del([ './tmp' ], cb);
  49. }
  50. exports.tmpClean = tmpClean;
  51. function tmpCreate(cb) {
  52. return exec('mkdir -p ./tmp', cb);
  53. }
  54. exports.tmpCreate = tmpCreate;
  55. function tmpCopy() {
  56. return gulp.src('./dist/**/*')
  57. .pipe(gulp.dest('./tmp'));
  58. }
  59. exports.tmpCopy = gulp.series(tmpCreate, tmpCopy);
  60. function zip() {
  61. const filename = settings.name + '.zip';
  62. return gulp.src('./dist/**/*')
  63. .pipe(plugins.zip(filename))
  64. .pipe(gulp.dest('./tmp'));
  65. }
  66. exports.zip = gulp.series(tmpCreate, zip);
  67. function failIfDirty(cb) {
  68. return exec('git diff-index HEAD --', function(err, output) {
  69. if (err) {
  70. return cb(err);
  71. }
  72. if (output) {
  73. return cb('Repository is dirty');
  74. }
  75. return cb();
  76. });
  77. }
  78. exports.zip = failIfDirty;
  79. function failIfNotMaster(cb) {
  80. exec('git symbolic-ref -q HEAD', function(err, output) {
  81. if (err) {
  82. return cb(err);
  83. }
  84. if (!/refs\/heads\/master/.test(output)) {
  85. return cb('Branch is not Master');
  86. }
  87. return cb();
  88. });
  89. }
  90. exports.failIfNotMaster = failIfNotMaster;
  91. function gitTag(cb) {
  92. const message = 'v' + getPackageJson().version;
  93. return exec('git tag ' + message, cb);
  94. }
  95. exports.gitTag = gitTag;
  96. function gitAdd(cb) {
  97. return exec('git add -A', cb);
  98. }
  99. exports.gitAdd = gitAdd;
  100. function gitCommit(cb) {
  101. const message = 'Build v' + getPackageJson().version;
  102. return exec('git commit -m "' + message + '"', cb);
  103. }
  104. exports.gitCommit = gulp.series(gitAdd, gitCommit);
  105. function gitPull(cb) {
  106. return exec('git pull origin master', function(err, output, code) {
  107. if (code !== 0) {
  108. return cb(err + output);
  109. }
  110. return cb();
  111. });
  112. }
  113. exports.gitPull = gitPull;
  114. function gitPush(cb) {
  115. return exec('git push origin master --tags', function(err, output, code) {
  116. if (code !== 0) {
  117. return cb(err + output);
  118. }
  119. return cb();
  120. });
  121. }
  122. exports.gitCommit = gulp.series(gitAdd, gitCommit, gitPush);
  123. function npmPublish(cb) {
  124. exec('npm publish', function(err, output, code) {
  125. if (code !== 0) {
  126. return cb(err + output);
  127. }
  128. return cb();
  129. });
  130. }
  131. exports.npmPublish = npmPublish;
  132. function meta(cb) {
  133. const metadata = {
  134. date: dateFormat.asString('yyyy-MM-dd HH:MM'),
  135. version: 'v' + getPackageJson().version
  136. },
  137. json = JSON.stringify(metadata, null, 4);
  138. fs.writeFileSync('tmp/metadata.json', json);
  139. fs.writeFileSync('tmp/metadata.js', '__metadata(' + json + ');');
  140. return cb();
  141. }
  142. exports.npmPublish = gulp.series(tmpCreate, meta);
  143. function bump() {
  144. return gulp.src([ 'package.json', 'bower.json', 'component.json' ])
  145. .pipe(plugins.bump(
  146. /^[a-z]+$/.test(bumpVersion)
  147. ? { type: bumpVersion }
  148. : { version: bumpVersion }
  149. ))
  150. .pipe(gulp.dest('.'));
  151. }
  152. exports.npmPublish = npmPublish;
  153. function year() {
  154. return gulp.src([ './README.md' ])
  155. .pipe(plugins.replace(/(Copyright )(\d{4})/g, '$1' + dateFormat.asString('yyyy')))
  156. .pipe(gulp.dest('.'));
  157. }
  158. exports.year = year;
  159. function lint() {
  160. return gulp.src('./src/**.js')
  161. .pipe(plugins.jshint())
  162. .pipe(plugins.jshint.reporter('default'));
  163. }
  164. exports.lint = lint;
  165. function copy() {
  166. return gulp.src([ './src/**/*', '!./src/sass', '!./src/sass/**' ])
  167. .pipe(gulp.dest('./dist'));
  168. }
  169. exports.copy = copy;
  170. function uglify() {
  171. return gulp.src('./dist/**/!(*.min.js).js')
  172. .pipe(babel({
  173. presets: ['@babel/env']
  174. }))
  175. .pipe(plugins.rename({ suffix: '.min' }))
  176. .pipe(plugins.sourcemaps.init())
  177. .pipe(plugins.uglify({
  178. compress: {
  179. },
  180. mangle: true,
  181. output: {
  182. comments: /^!/
  183. }
  184. }))
  185. .on('error', function(err) { console.log(err) })
  186. .pipe(plugins.sourcemaps.write('.'))
  187. .pipe(gulp.dest('./dist/'));
  188. }
  189. exports.uglify = uglify;
  190. function terserMinify() {
  191. return gulp.src('./dist/**/!(*.min).js')
  192. .pipe(plugins.rename({ suffix: '.terser.min' }))
  193. .pipe(plugins.sourcemaps.init())
  194. .pipe(terser({
  195. mangle: {
  196. toplevel: true,
  197. }
  198. }))
  199. .on('error', function(err) { console.log(err) })
  200. .pipe(plugins.sourcemaps.write('.'))
  201. .pipe(gulp.dest('./dist/'));
  202. }
  203. exports.terserMinify = terserMinify;
  204. function cssmin() {
  205. return gulp.src('./dist/**/!(*.min.css).css')
  206. .pipe(plugins.sourcemaps.init())
  207. .pipe(plugins.rename({ suffix: '.min' }))
  208. .pipe(plugins.cssmin())
  209. .pipe(plugins.sourcemaps.write('.'))
  210. .pipe(gulp.dest('./dist/'));
  211. }
  212. exports.cssmin = cssmin;
  213. function sass() {
  214. return gulp.src("./src/sass/vegas.sass")
  215. // .pipe(plugins.sourcemaps.init())
  216. .pipe(plugins.sass({
  217. outputStyle: 'expanded',
  218. indentWidth: 4
  219. }).on('error', plugins.sass.logError))
  220. .pipe(plugins.autoprefixer())
  221. // .pipe(plugins.sourcemaps.write('.'))
  222. .pipe(gulp.dest("./dist/"));
  223. }
  224. exports.sass = sass;
  225. function header() {
  226. settings.banner.vars.pkg = getPackageJson();
  227. return gulp.src('./dist/*.js')
  228. .pipe(plugins.header(settings.banner.content, settings.banner.vars ))
  229. .pipe(gulp.dest('./dist/'));
  230. }
  231. exports.header = header;
  232. function ghPages(cb) {
  233. var version = getPackageJson().version;
  234. exec([ 'git checkout gh-pages',
  235. 'rm -rf releases/' + version,
  236. 'mkdir -p releases/' + version,
  237. 'cp -r tmp/* releases/' + version,
  238. 'git add -A releases/' + version,
  239. 'rm -rf releases/latest',
  240. 'mkdir -p releases/latest',
  241. 'cp -r tmp/* releases/latest',
  242. 'git add -A releases/latest',
  243. 'git commit -m "Publish release v' + version + '."',
  244. 'git push origin gh-pages',
  245. 'git checkout -'
  246. ].join(' && '),
  247. function(err, output, code) {
  248. if (code !== 0) {
  249. return cb(err + output);
  250. }
  251. return cb();
  252. }
  253. );
  254. }
  255. exports.ghPages = ghPages;
  256. function changelog(cb) {
  257. var filename = 'CHANGELOG.md',
  258. editor = process.env.EDITOR || 'vim',
  259. version = getPackageJson().version,
  260. date = dateFormat.asString('yyyy-MM-dd'),
  261. changelog = fs.readFileSync(filename).toString(),
  262. lastDate = /\d{4}-\d{2}-\d{2}/.exec(changelog)[0];
  263. exec('git log --since="' + lastDate + ' 00:00:00" --oneline --pretty=format:"%s"', function(err, stdout) {
  264. if (err) {
  265. return cb(err);
  266. }
  267. if (!stdout) {
  268. return cb();
  269. }
  270. const updates = [
  271. '### Vegas ' + version + ' ' + date,
  272. '',
  273. '* ' + stdout.replace(/\n/g, '\n* ')
  274. ].join('\n');
  275. changelog = changelog.replace(/(## CHANGE LOG)/, '$1\n\n' + updates);
  276. fs.writeFileSync(filename, changelog);
  277. const vim = spawn(editor, [ filename, '-n', '+7' ], {
  278. stdio: 'inherit'
  279. });
  280. vim.on('close', function() {
  281. return cb();
  282. });
  283. });
  284. }
  285. exports.changelog = changelog;
  286. function watch() {
  287. return gulp.watch("./src/**/*", build);
  288. }
  289. exports.watch = watch;
  290. exports.default = watch;
  291. const build = gulp.series(
  292. lint,
  293. clean,
  294. copy,
  295. sass,
  296. header,
  297. cssmin,
  298. uglify,
  299. );
  300. exports.build = build;
  301. const publish = gulp.series(
  302. failIfNotMaster,
  303. failIfDirty,
  304. tmpCreate,
  305. tmpCopy,
  306. meta,
  307. zip,
  308. ghPages,
  309. tmpClean
  310. );
  311. exports.publish = publish;
  312. const release = gulp.series(
  313. failIfNotMaster,
  314. failIfDirty,
  315. gitPull,
  316. bump,
  317. changelog,
  318. year,
  319. clean,
  320. copy,
  321. sass,
  322. header,
  323. uglify,
  324. cssmin,
  325. gitAdd,
  326. gitCommit,
  327. gitTag,
  328. gitPush,
  329. publish,
  330. npmPublish
  331. );
  332. exports.release = release;
  333. exports['build:hugh'] = gulp.series(
  334. lint,
  335. clean,
  336. copy,
  337. sass,
  338. uglify,
  339. terserMinify,
  340. cssmin,
  341. );
  342. })();
  343. /*
  344. NPM Installation
  345. ----------------
  346. npm install --save-dev del
  347. npm install --save-dev yargs
  348. npm install --save-dev exec
  349. npm install --save-dev jshint
  350. npm install --save-dev gulp
  351. npm install --save-dev gulp-sass
  352. npm install --save-dev gulp-load-plugins
  353. npm install --save-dev gulp-bump
  354. npm install --save-dev gulp-header
  355. npm install --save-dev gulp-cssmin
  356. npm install --save-dev gulp-autoprefixer
  357. npm install --save-dev gulp-uglify
  358. npm install --save-dev gulp-sourcemaps
  359. npm install --save-dev gulp-jshint
  360. npm install --save-dev gulp-util
  361. npm install --save-dev gulp-zip
  362. npm install --save-dev gulp-rename
  363. npm install --save-dev gulp-replace
  364. Gh-pages creation
  365. -----------------
  366. git checkout --orphan gh-pages
  367. git rm -rf .
  368. rm -fr
  369. echo 'Welcome' > index.html
  370. git add index.html
  371. git commit -a -m 'First commit'
  372. git push origin gh-pages
  373. git checkout -
  374. */