You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.8 KiB

  1. /*
  2. * grunt-bower-task
  3. * https://github.com/yatskevich/grunt-bower-task
  4. *
  5. * Copyright (c) 2012 Ivan Yatskevich
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. module.exports = function(grunt) {
  10. var bower = require('bower'),
  11. path = require('path'),
  12. async = require('async'),
  13. colors = require('colors'),
  14. rimraf = require('rimraf').sync,
  15. BowerAssets = require('./lib/bower_assets'),
  16. AssetCopier = require('./lib/asset_copier'),
  17. LayoutsManager = require('./lib/layouts_manager');
  18. function log(message) {
  19. log.logger.writeln(message);
  20. }
  21. function fail(error) {
  22. grunt.fail.fatal(error);
  23. }
  24. function clean(dir, callback) {
  25. rimraf(dir);
  26. callback();
  27. }
  28. function install(callback) {
  29. bower.commands.install()
  30. .on('data', log)
  31. .on('error', fail)
  32. .on('end', callback);
  33. }
  34. function copy(options, callback) {
  35. var bowerAssets = new BowerAssets(bower, options.cwd);
  36. bowerAssets.once('data', function(assets) {
  37. var copier = new AssetCopier(assets, options, function(source, destination, isFile) {
  38. log('grunt-bower ' + 'copying '.cyan + ((isFile ? '' : ' dir ') + source + ' -> ' + destination).grey);
  39. });
  40. copier.once('copied', callback);
  41. copier.copy();
  42. }).get();
  43. }
  44. grunt.registerMultiTask('bower', 'Install Bower packages.', function() {
  45. var tasks = [],
  46. done = this.async(),
  47. options = this.options({
  48. cleanTargetDir: false,
  49. cleanBowerDir: false,
  50. targetDir: './lib',
  51. layout: 'byType',
  52. install: true,
  53. verbose: false,
  54. copy: true
  55. }),
  56. add = function(name, fn) {
  57. tasks.push(function(callback) {
  58. fn(function() {
  59. grunt.log.ok(name);
  60. callback();
  61. });
  62. });
  63. },
  64. bowerDir = path.resolve(bower.config.directory),
  65. targetDir = path.resolve(options.targetDir);
  66. log.logger = options.verbose ? grunt.log : grunt.verbose;
  67. options.layout = LayoutsManager.getLayout(options.layout, fail);
  68. options.cwd = grunt.option('base') || process.cwd();
  69. if (options.cleanup !== undefined) {
  70. options.cleanTargetDir = options.cleanBowerDir = options.cleanup;
  71. }
  72. if (options.cleanTargetDir) {
  73. add('Cleaned target dir ' + targetDir.grey, function(callback) {
  74. clean(targetDir, callback);
  75. });
  76. }
  77. if (options.install) {
  78. add('Installed bower packages', install);
  79. }
  80. if (options.copy) {
  81. add('Copied packages to ' + targetDir.grey, function(callback) {
  82. copy(options, callback);
  83. });
  84. }
  85. if (options.cleanBowerDir) {
  86. add('Cleaned bower dir ' + bowerDir.grey, function(callback) {
  87. clean(bowerDir, callback);
  88. });
  89. }
  90. async.series(tasks, done);
  91. });
  92. };