123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 'use strict';
- let _ = require('lodash');
- let bluebird = require('bluebird');
- let path = require('path');
- let packageJSON = require('../package');
- let registry = require('./tasks');
- let utility = require('./utility');
- let directory, filterCB,
- languages = utility.glob(path.join('src', 'languages', '*.js')),
- header = utility.regex.header;
- function templateAllFunc(blobs) {
- const names = _.map(blobs, blob => path.basename(blob.name, '.js'));
- return bluebird.resolve({ names: names });
- }
- function buildLanguages() {
- let input = languages,
- output = path.join(directory.build, 'lib', 'languages'),
- replaceArgs = utility.replace(header, ''),
- template = 'module.exports = <%= content %>;';
- return {
- logStart: { task: ['log', 'Building language files.'] },
- read: { requires: 'logStart', task: ['glob', input] },
- filter: { requires: 'read', task: ['filter', filterCB] },
- replace: { requires: 'filter', task: ['replace', replaceArgs] },
- template: { requires: 'replace', task: ['template', template] },
- writeLog: {
- requires: 'template',
- task: ['log', 'Writing language files.']
- },
- write: { requires: 'writeLog', task: ['dest', output] }
- };
- }
- function buildCore() {
- const input = path.join(directory.root, 'src', 'highlight.js'),
- output = path.join(directory.build, 'lib');
- return {
- startLog: { task: ['log', 'Building core file.'] },
- read: { requires: 'startLog', task: ['read', input] },
- writeLog: { requires: 'read', task: ['log', 'Writing core file.'] },
- write: { requires: 'writeLog', task: ['dest', output] }
- };
- }
- function buildIndex() {
- let input = languages,
- output = path.join(directory.build, 'lib', 'index.js'),
- templateArgs = {
- template: [ 'var hljs = require(\'./highlight\');\n'
- , '<% _.each(names, function(name) { %>' +
- 'hljs.registerLanguage(\'<%= name %>\', ' +
- 'require(\'./languages/<%= name %>\'));'
- , '<% }); %>'
- , 'module.exports = hljs;'
- ].join('\n'),
- callback: templateAllFunc
- };
- return {
- startLog: { task: ['log', 'Building index file.'] },
- read: { requires: 'startLog', task: ['glob', input] },
- filter: { requires: 'read', task: ['filter', filterCB] },
- reorder: { requires: 'filter', task: 'reorderDeps' },
- template: { requires: 'reorder', task: ['templateAll', templateArgs] },
- writeLog: { requires: 'template', task: ['log', 'Writing index file.'] },
- write: { requires: 'writeLog', task: ['write', output] }
- };
- }
- function copyMetaFiles() {
- let docs = path.join('docs', '*.rst'),
- glob = `{README.md,LICENSE,${docs}}`,
- input = utility.glob(path.join(directory.root, glob)),
- output = { dir: directory.build, base: '.' };
- return {
- startLog: { task: ['log', 'Copying meta files.'] },
- read: { requires: 'startLog', task: ['glob', input] },
- writeLog: { requires: 'read', task: ['log', 'Writing meta files.'] },
- write: { requires: 'writeLog', task: ['dest', output] }
- };
- }
- function buildStyles() {
- let input = path.join(directory.root, 'src', 'styles', '*'),
- output = path.join(directory.build, 'styles'),
- options = { encoding: 'binary', dir: output };
- return {
- startLog: { task: ['log', 'Building style files.'] },
- read: {
- requires: 'startLog',
- task: ['glob', utility.glob(input, 'binary')]
- },
- writeLog: { requires: 'read', task: ['log', 'Writing style files.'] },
- write: { requires: 'writeLog', task: ['dest', options] }
- };
- }
- function buildPackageFile() {
- const input = path.join(directory.root, 'AUTHORS.en.txt'),
- output = path.join(directory.build, 'package.json');
- return {
- startLog: { task: ['log', 'Building package.json file.'] },
- read: { requires: 'startLog', task: ['read', input] },
- build: { requires: 'read', task: ['buildPackage', packageJSON] },
- writeLog: {
- requires: 'build',
- task: ['log', 'Writing package.json file.']
- },
- write: { requires: 'writeLog', task: ['write', output] }
- };
- }
- module.exports = function(commander, dir) {
- directory = dir;
- filterCB = utility.buildFilterCallback(commander.args);
- let tasks = [
- buildLanguages(),
- buildCore(),
- buildIndex(),
- buildStyles(),
- copyMetaFiles(),
- buildPackageFile()
- ];
- return utility.toQueue(tasks, registry);
- };
|