gulpfile.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var gulp = require('gulp'),
  2. rename = require('gulp-rename'),
  3. uglify = require('gulp-uglify'),
  4. header = require('gulp-header'),
  5. concat = require('gulp-concat'),
  6. highland = require('highland'),
  7. fs = require('fs'),
  8. path = require('path'),
  9. File = require('vinyl'),
  10. del = require('del'),
  11. yargs = require('yargs'),
  12. npm = require('npm'),
  13. runningPackagingTask = yargs.argv._.indexOf('createPackages') > -1,
  14. version = yargs.argv.version,
  15. paths = {
  16. components: ['components/**/*.js', '!components/**/*.min.js'],
  17. main: [
  18. 'components/prism-core.js',
  19. 'components/prism-markup.js',
  20. 'components/prism-css.js',
  21. 'components/prism-clike.js',
  22. 'components/prism-javascript.js',
  23. 'plugins/file-highlight/prism-file-highlight.js'
  24. ],
  25. plugins: ['plugins/**/*.js', '!plugins/**/*.min.js']
  26. };
  27. gulp.task('components', function() {
  28. return gulp.src(paths.components)
  29. .pipe(uglify())
  30. .pipe(rename({ suffix: '.min' }))
  31. .pipe(gulp.dest('components'));
  32. });
  33. gulp.task('build', function() {
  34. return gulp.src(paths.main)
  35. .pipe(header('\n/* **********************************************\n' +
  36. ' Begin <%= file.relative %>\n' +
  37. '********************************************** */\n\n'))
  38. .pipe(concat('prism.js'))
  39. .pipe(gulp.dest('./'));
  40. });
  41. gulp.task('plugins', function() {
  42. return gulp.src(paths.plugins)
  43. .pipe(uglify())
  44. .pipe(rename({ suffix: '.min' }))
  45. .pipe(gulp.dest('plugins'));
  46. });
  47. gulp.task('watch', function() {
  48. gulp.watch(paths.components, ['components', 'build']);
  49. gulp.watch(paths.plugins, ['plugins', 'build']);
  50. });
  51. gulp.task('default', ['components', 'plugins', 'build']);
  52. /* tasks for creating and publishing npm packages */
  53. function readme(themeName) {
  54. return [
  55. '# Prism ' + themeName.charAt(0).toUpperCase() + themeName.substring(1) + ' Theme',
  56. 'Prism is a lightweight, robust, elegant syntax highlighting library.',
  57. 'Learn more at [http://prismjs.com/](http://prismjs.com/).'
  58. ].join('\n\n');
  59. }
  60. function packagejson(themeName) {
  61. return JSON.stringify({
  62. name: 'prism-' + themeName + '-theme',
  63. version: version,
  64. description: 'A CSS theme for PrismJS',
  65. repository: {
  66. type: 'git',
  67. url: 'https://github.com/PrismJS/prism.git'
  68. },
  69. keywords: [
  70. 'prism',
  71. 'highlight',
  72. 'theme'
  73. ],
  74. author: 'Lea Verou',
  75. license: 'MIT',
  76. style: 'prism-' + themeName + '.css'
  77. }, null, 2);
  78. }
  79. function license(themeName) {
  80. return fs.readFileSync('LICENSE');
  81. }
  82. var contentStrategy = {
  83. 'package.json': packagejson,
  84. 'README.md': readme,
  85. 'LICENSE': license
  86. };
  87. gulp.task('clean-dist', function(done) {
  88. del('dist', done);
  89. });
  90. gulp.task('copy-css', ['build', 'clean-dist'], function() {
  91. return highland(gulp.src('themes/*.css'))
  92. .map(function(file) {
  93. var name = path.basename(file.path) === 'prism.css' ? 'prism-default' : path.basename(file.path, '.css');
  94. file.path = path.join(path.dirname(file.path), name, name + '.css');
  95. return file;
  96. })
  97. .pipe(gulp.dest('dist'));
  98. });
  99. gulp.task('copy-files', ['copy-css'], function() {
  100. return highland(gulp.src('dist/*'))
  101. .flatMap(function(dir) {
  102. var themeName = /prism-(\w+)/.exec(path.basename(dir.path))[1];
  103. return ['package.json', 'README.md', 'LICENSE']
  104. .map(function(fileName) {
  105. return new File({
  106. base: path.dirname(dir.path),
  107. path: path.join(dir.path, fileName),
  108. contents: new Buffer(contentStrategy[fileName](themeName))
  109. });
  110. });
  111. })
  112. .pipe(gulp.dest('dist'));
  113. });
  114. gulp.task('update-version', function() {
  115. return highland(gulp.src('package.json'))
  116. .map(function(file) {
  117. var packageInfo = JSON.parse(file.contents);
  118. packageInfo.version = version;
  119. file.contents = new Buffer(JSON.stringify(packageInfo, null, 2));
  120. return file;
  121. })
  122. .pipe(gulp.dest('.'));
  123. });
  124. if (runningPackagingTask && !version) {
  125. console.error('Please specify a version number for the release, for example: gulp createPackages --version 1.0.42');
  126. process.exit(1);
  127. }
  128. gulp.task('createPackages', ['copy-css', 'copy-files', 'update-version']);
  129. gulp.task('publishPackages', function(done) {
  130. var packages = fs.readdirSync('dist')
  131. .map(function(packageName) {
  132. return path.join('dist', packageName);
  133. })
  134. .concat('.');
  135. npm.load({}, function() {
  136. function next() {
  137. if (packages.length > 0) {
  138. npm.commands.publish([packages.pop()], next);
  139. } else {
  140. done();
  141. }
  142. }
  143. next();
  144. });
  145. });