command.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const os = require("os");
  4. /**
  5. * Commands
  6. *
  7. * Command Format:
  8. * ##[name key=value;key=value]message
  9. *
  10. * Examples:
  11. * ##[warning]This is the user warning message
  12. * ##[set-secret name=mypassword]definitelyNotAPassword!
  13. */
  14. function issueCommand(command, properties, message) {
  15. const cmd = new Command(command, properties, message);
  16. process.stdout.write(cmd.toString() + os.EOL);
  17. }
  18. exports.issueCommand = issueCommand;
  19. function issue(name, message = '') {
  20. issueCommand(name, {}, message);
  21. }
  22. exports.issue = issue;
  23. const CMD_STRING = '::';
  24. class Command {
  25. constructor(command, properties, message) {
  26. if (!command) {
  27. command = 'missing.command';
  28. }
  29. this.command = command;
  30. this.properties = properties;
  31. this.message = message;
  32. }
  33. toString() {
  34. let cmdStr = CMD_STRING + this.command;
  35. if (this.properties && Object.keys(this.properties).length > 0) {
  36. cmdStr += ' ';
  37. for (const key in this.properties) {
  38. if (this.properties.hasOwnProperty(key)) {
  39. const val = this.properties[key];
  40. if (val) {
  41. // safely append the val - avoid blowing up when attempting to
  42. // call .replace() if message is not a string for some reason
  43. cmdStr += `${key}=${escape(`${val || ''}`)},`;
  44. }
  45. }
  46. }
  47. }
  48. cmdStr += CMD_STRING;
  49. // safely append the message - avoid blowing up when attempting to
  50. // call .replace() if message is not a string for some reason
  51. const message = `${this.message || ''}`;
  52. cmdStr += escapeData(message);
  53. return cmdStr;
  54. }
  55. }
  56. function escapeData(s) {
  57. return s.replace(/\r/g, '%0D').replace(/\n/g, '%0A');
  58. }
  59. function escape(s) {
  60. return s
  61. .replace(/\r/g, '%0D')
  62. .replace(/\n/g, '%0A')
  63. .replace(/]/g, '%5D')
  64. .replace(/;/g, '%3B');
  65. }
  66. //# sourceMappingURL=command.js.map