Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

command.js 738B

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. };