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.

41 lines
891 B

4 years ago
  1. 'use strict';
  2. const alias = ['stdin', 'stdout', 'stderr'];
  3. const hasAlias = opts => alias.some(x => Boolean(opts[x]));
  4. module.exports = opts => {
  5. if (!opts) {
  6. return null;
  7. }
  8. if (opts.stdio && hasAlias(opts)) {
  9. throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${alias.map(x => `\`${x}\``).join(', ')}`);
  10. }
  11. if (typeof opts.stdio === 'string') {
  12. return opts.stdio;
  13. }
  14. const stdio = opts.stdio || [];
  15. if (!Array.isArray(stdio)) {
  16. throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);
  17. }
  18. const result = [];
  19. const len = Math.max(stdio.length, alias.length);
  20. for (let i = 0; i < len; i++) {
  21. let value = null;
  22. if (stdio[i] !== undefined) {
  23. value = stdio[i];
  24. } else if (opts[alias[i]] !== undefined) {
  25. value = opts[alias[i]];
  26. }
  27. result[i] = value;
  28. }
  29. return result;
  30. };