node.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. let _ = require('lodash');
  3. let bluebird = require('bluebird');
  4. let path = require('path');
  5. let packageJSON = require('../package');
  6. let registry = require('./tasks');
  7. let utility = require('./utility');
  8. let directory, filterCB,
  9. languages = utility.glob(path.join('src', 'languages', '*.js')),
  10. header = utility.regex.header;
  11. function templateAllFunc(blobs) {
  12. const names = _.map(blobs, blob => path.basename(blob.name, '.js'));
  13. return bluebird.resolve({ names: names });
  14. }
  15. function buildLanguages() {
  16. let input = languages,
  17. output = path.join(directory.build, 'lib', 'languages'),
  18. replaceArgs = utility.replace(header, ''),
  19. template = 'module.exports = <%= content %>;';
  20. return {
  21. logStart: { task: ['log', 'Building language files.'] },
  22. read: { requires: 'logStart', task: ['glob', input] },
  23. filter: { requires: 'read', task: ['filter', filterCB] },
  24. replace: { requires: 'filter', task: ['replace', replaceArgs] },
  25. template: { requires: 'replace', task: ['template', template] },
  26. writeLog: {
  27. requires: 'template',
  28. task: ['log', 'Writing language files.']
  29. },
  30. write: { requires: 'writeLog', task: ['dest', output] }
  31. };
  32. }
  33. function buildCore() {
  34. const input = path.join(directory.root, 'src', 'highlight.js'),
  35. output = path.join(directory.build, 'lib');
  36. return {
  37. startLog: { task: ['log', 'Building core file.'] },
  38. read: { requires: 'startLog', task: ['read', input] },
  39. writeLog: { requires: 'read', task: ['log', 'Writing core file.'] },
  40. write: { requires: 'writeLog', task: ['dest', output] }
  41. };
  42. }
  43. function buildIndex() {
  44. let input = languages,
  45. output = path.join(directory.build, 'lib', 'index.js'),
  46. templateArgs = {
  47. template: [ 'var hljs = require(\'./highlight\');\n'
  48. , '<% _.each(names, function(name) { %>' +
  49. 'hljs.registerLanguage(\'<%= name %>\', ' +
  50. 'require(\'./languages/<%= name %>\'));'
  51. , '<% }); %>'
  52. , 'module.exports = hljs;'
  53. ].join('\n'),
  54. callback: templateAllFunc
  55. };
  56. return {
  57. startLog: { task: ['log', 'Building index file.'] },
  58. read: { requires: 'startLog', task: ['glob', input] },
  59. filter: { requires: 'read', task: ['filter', filterCB] },
  60. reorder: { requires: 'filter', task: 'reorderDeps' },
  61. template: { requires: 'reorder', task: ['templateAll', templateArgs] },
  62. writeLog: { requires: 'template', task: ['log', 'Writing index file.'] },
  63. write: { requires: 'writeLog', task: ['write', output] }
  64. };
  65. }
  66. function copyMetaFiles() {
  67. let docs = path.join('docs', '*.rst'),
  68. glob = `{README.md,LICENSE,${docs}}`,
  69. input = utility.glob(path.join(directory.root, glob)),
  70. output = { dir: directory.build, base: '.' };
  71. return {
  72. startLog: { task: ['log', 'Copying meta files.'] },
  73. read: { requires: 'startLog', task: ['glob', input] },
  74. writeLog: { requires: 'read', task: ['log', 'Writing meta files.'] },
  75. write: { requires: 'writeLog', task: ['dest', output] }
  76. };
  77. }
  78. function buildStyles() {
  79. let input = path.join(directory.root, 'src', 'styles', '*'),
  80. output = path.join(directory.build, 'styles'),
  81. options = { encoding: 'binary', dir: output };
  82. return {
  83. startLog: { task: ['log', 'Building style files.'] },
  84. read: {
  85. requires: 'startLog',
  86. task: ['glob', utility.glob(input, 'binary')]
  87. },
  88. writeLog: { requires: 'read', task: ['log', 'Writing style files.'] },
  89. write: { requires: 'writeLog', task: ['dest', options] }
  90. };
  91. }
  92. function buildPackageFile() {
  93. const input = path.join(directory.root, 'AUTHORS.en.txt'),
  94. output = path.join(directory.build, 'package.json');
  95. return {
  96. startLog: { task: ['log', 'Building package.json file.'] },
  97. read: { requires: 'startLog', task: ['read', input] },
  98. build: { requires: 'read', task: ['buildPackage', packageJSON] },
  99. writeLog: {
  100. requires: 'build',
  101. task: ['log', 'Writing package.json file.']
  102. },
  103. write: { requires: 'writeLog', task: ['write', output] }
  104. };
  105. }
  106. module.exports = function(commander, dir) {
  107. directory = dir;
  108. filterCB = utility.buildFilterCallback(commander.args);
  109. let tasks = [
  110. buildLanguages(),
  111. buildCore(),
  112. buildIndex(),
  113. buildStyles(),
  114. copyMetaFiles(),
  115. buildPackageFile()
  116. ];
  117. return utility.toQueue(tasks, registry);
  118. };