6efeb6778280eb8f08588abb987380e8387c380b96e63e8dcd3f24e0d6350d87482a5e3b59e8d312b8be9d3eccf2ccefc782f18ccbd2bb233441171e64c4da 738 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const SPACES_REGEXP = / +/g;
  3. const joinCommand = (file, args = []) => {
  4. if (!Array.isArray(args)) {
  5. return file;
  6. }
  7. return [file, ...args].join(' ');
  8. };
  9. // Allow spaces to be escaped by a backslash if not meant as a delimiter
  10. const handleEscaping = (tokens, token, index) => {
  11. if (index === 0) {
  12. return [token];
  13. }
  14. const previousToken = tokens[tokens.length - 1];
  15. if (previousToken.endsWith('\\')) {
  16. return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`];
  17. }
  18. return [...tokens, token];
  19. };
  20. // Handle `execa.command()`
  21. const parseCommand = command => {
  22. return command
  23. .trim()
  24. .split(SPACES_REGEXP)
  25. .reduce(handleEscaping, []);
  26. };
  27. module.exports = {
  28. joinCommand,
  29. parseCommand
  30. };