gulpfile.js 11 KB

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