index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. "use strict";
  7. var Cpx = require("./cpx");
  8. exports.Cpx = Cpx;
  9. /**
  10. * Copies the files which match with a given glob pattern.
  11. *
  12. * @param {string} source - The glob pattern of source files.
  13. * @param {string} outDir - The path of an output directory.
  14. * @param {object} [options = null] - Omittable. An option object.
  15. * @param {boolean} [options.clean = false] - A flag to remove files that have
  16. * been copied previously before copy.
  17. * @param {boolean} [options.dereference = false] - A flag to follow symbolic
  18. * links.
  19. * @param {function[]} [options.transform = null] - Functions to make transform
  20. * streams for each file.
  21. * @param {function} [cb = null] - A callback function to be called after done.
  22. * @returns {Cpx} A Cpx instance.
  23. */
  24. exports.copy = function copy(source, outDir) {
  25. var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
  26. var cb = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3];
  27. if (typeof options === "function") {
  28. /* eslint-disable no-param-reassign */
  29. cb = options;
  30. options = null;
  31. /* eslint-enable no-param-reassign */
  32. }
  33. var cpx = new Cpx(source, outDir, options);
  34. if (options && options.clean) {
  35. cpx.clean(function (err) {
  36. if (err == null) {
  37. cpx.copy(cb);
  38. } else if (cb != null) {
  39. cb(err);
  40. }
  41. });
  42. } else {
  43. cpx.copy(cb);
  44. }
  45. return cpx;
  46. };
  47. /**
  48. * Copies the files which match with a given glob pattern.
  49. *
  50. * @param {string} source - The glob pattern of source files.
  51. * @param {string} outDir - The path of an output directory.
  52. * @param {object} [options = null] - Omittable. An option object.
  53. * @param {boolean} [options.clean = false] - A flag to remove files that have
  54. * been copied previously before copy.
  55. * @param {boolean} [options.dereference = false] - A flag to follow symbolic
  56. * links.
  57. * @returns {Cpx} A Cpx instance.
  58. */
  59. exports.copySync = function copySync(source, outDir) {
  60. var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
  61. var cpx = new Cpx(source, outDir, options);
  62. if (options && options.clean) {
  63. cpx.cleanSync();
  64. }
  65. cpx.copySync();
  66. };
  67. /**
  68. * Copies the files which match with a given glob pattern.
  69. * Then this observes the files and copies when modified them.
  70. *
  71. * @param {string} source - The glob pattern of source files.
  72. * @param {string} outDir - The path of an output directory.
  73. * @param {object} [options = null] - Omittable. An option object.
  74. * @param {boolean} [options.clean = false] - A flag to remove files that have
  75. * been copied previously before copy.
  76. * @param {boolean} [options.dereference = false] - A flag to follow symbolic
  77. * links.
  78. * @returns {Cpx} A Cpx instance.
  79. */
  80. exports.watch = function watch(source, outDir) {
  81. var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
  82. var cpx = new Cpx(source, outDir, options);
  83. if (options && options.clean) {
  84. cpx.clean(function (err) {
  85. if (err == null) {
  86. cpx.watch();
  87. } else {
  88. cpx.emit("watch-error", err);
  89. }
  90. });
  91. } else {
  92. cpx.watch();
  93. }
  94. return cpx;
  95. };