main.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /* eslint no-console:0, no-process-exit:0, no-process-env:0 */
  7. "use strict";
  8. var _create = require("babel-runtime/core-js/object/create");
  9. var _create2 = _interopRequireDefault(_create);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var _require = require("path");
  12. var resolvePath = _require.resolve;
  13. var _require2 = require("child_process");
  14. var spawn = _require2.spawn;
  15. var _require3 = require("resolve");
  16. var resolveModule = _require3.sync;
  17. var _require4 = require("shell-quote");
  18. var parseShellQuote = _require4.parse;
  19. var duplexer = require("duplexer");
  20. var Cpx = require("../lib/cpx");
  21. module.exports = function main(source, outDir, args) {
  22. //--------------------------------------------------------------------------
  23. // Resolve Command.
  24. var commands = [].concat(args.command).filter(Boolean).map(function (command) {
  25. if (typeof command !== "string") {
  26. console.error("Invalid --command option");
  27. process.exit(1);
  28. }
  29. return function (file) {
  30. var env = (0, _create2.default)(process.env, { FILE: { value: file } });
  31. var parts = parseShellQuote(command, env);
  32. var child = spawn(parts[0], parts.slice(1), { env: env });
  33. var outer = duplexer(child.stdin, child.stdout);
  34. child.on("exit", function (code) {
  35. if (code !== 0) {
  36. var error = new Error("non-zero exit code in command: " + command);
  37. outer.emit("error", error);
  38. }
  39. });
  40. child.stderr.pipe(process.stderr);
  41. return outer;
  42. };
  43. });
  44. //--------------------------------------------------------------------------
  45. // Resolve Transforms.
  46. var ABS_OR_REL = /^[.\/]/;
  47. var transforms = [].concat(args.transform).filter(Boolean).map(function (arg) {
  48. // eslint-disable-line array-callback-return,consistent-return
  49. if (typeof arg === "string") {
  50. return { name: arg, argv: null };
  51. }
  52. if (typeof arg._[0] === "string") {
  53. return { name: arg._.shift(), argv: arg };
  54. }
  55. console.error("Invalid --transform option");
  56. process.exit(1);
  57. }).map(function (item) {
  58. var createStream = ABS_OR_REL.test(item.name) ? require(resolvePath(item.name)) : require(resolveModule(item.name, { basedir: process.cwd() }));
  59. return function (file) {
  60. return createStream(file, item.argv);
  61. };
  62. });
  63. //--------------------------------------------------------------------------
  64. // Merge commands and transforms as same as order of process.argv.
  65. var C_OR_COMMAND = /^(?:-c|--command)$/;
  66. var T_OR_TRANSFORM = /^(?:-t|--transform)$/;
  67. var mergedTransformFactories = process.argv.map(function (part) {
  68. if (C_OR_COMMAND.test(part)) {
  69. return commands.shift();
  70. }
  71. if (T_OR_TRANSFORM.test(part)) {
  72. return transforms.shift();
  73. }
  74. return null;
  75. }).filter(Boolean);
  76. //--------------------------------------------------------------------------
  77. // Main.
  78. var cpx = new Cpx(source, outDir, {
  79. transform: mergedTransformFactories,
  80. dereference: args.dereference,
  81. includeEmptyDirs: args.includeEmptyDirs,
  82. initialCopy: args.initial,
  83. preserve: args.preserve,
  84. update: args.update
  85. });
  86. if (args.verbose) {
  87. cpx.on("copy", function (event) {
  88. console.log("Copied: " + event.srcPath + " --> " + event.dstPath);
  89. });
  90. cpx.on("remove", function (event) {
  91. console.log("Removed: " + event.path);
  92. });
  93. }
  94. if (args.clean) {
  95. if (args.verbose) {
  96. console.log();
  97. console.log("Clean: " + cpx.src2dst(cpx.source));
  98. console.log();
  99. }
  100. try {
  101. cpx.cleanSync();
  102. } catch (err) {
  103. console.error("Failed to clean: " + err.message + ".");
  104. process.exit(1);
  105. }
  106. if (args.verbose) {
  107. console.log();
  108. console.log("Copy: " + source + " --> " + outDir);
  109. console.log();
  110. }
  111. }
  112. if (args.watch) {
  113. if (args.verbose) {
  114. cpx.on("watch-ready", function () {
  115. console.log();
  116. console.log("Be watching in " + cpx.base);
  117. console.log();
  118. });
  119. }
  120. cpx.on("watch-error", function (err) {
  121. console.error(err.message);
  122. });
  123. // In order to kill me by test harness on Windows.
  124. process.stdin.setEncoding("utf8");
  125. process.stdin.on("data", function (chunk) {
  126. if (chunk === "KILL") {
  127. process.exit(0);
  128. }
  129. });
  130. cpx.watch();
  131. } else {
  132. cpx.copy(function (err) {
  133. if (err) {
  134. console.error("Failed to copy: " + err.message + ".");
  135. process.exit(1);
  136. }
  137. });
  138. }
  139. };