utils.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import Option from "./Option.ts";
  2. export const removeBrackets = (v: string) => v.replace(/[<[].+/, '').trim();
  3. export const findAllBrackets = (v: string) => {
  4. const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
  5. const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
  6. const res = [];
  7. const parse = (match: string[]) => {
  8. let variadic = false;
  9. let value = match[1];
  10. if (value.startsWith('...')) {
  11. value = value.slice(3);
  12. variadic = true;
  13. }
  14. return {
  15. required: match[0].startsWith('<'),
  16. value,
  17. variadic
  18. };
  19. };
  20. let angledMatch;
  21. while (angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v)) {
  22. res.push(parse(angledMatch));
  23. }
  24. let squareMatch;
  25. while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v)) {
  26. res.push(parse(squareMatch));
  27. }
  28. return res;
  29. };
  30. interface MriOptions {
  31. alias: {
  32. [k: string]: string[];
  33. };
  34. boolean: string[];
  35. }
  36. export const getMriOptions = (options: Option[]) => {
  37. const result: MriOptions = {
  38. alias: {},
  39. boolean: []
  40. };
  41. for (const [index, option] of options.entries()) {
  42. // We do not set default values in mri options
  43. // Since its type (typeof) will be used to cast parsed arguments.
  44. // Which mean `--foo foo` will be parsed as `{foo: true}` if we have `{default:{foo: true}}`
  45. // Set alias
  46. if (option.names.length > 1) {
  47. result.alias[option.names[0]] = option.names.slice(1);
  48. } // Set boolean
  49. if (option.isBoolean) {
  50. if (option.negated) {
  51. // For negated option
  52. // We only set it to `boolean` type when there's no string-type option with the same name
  53. const hasStringTypeOption = options.some((o, i) => {
  54. return i !== index && o.names.some(name => option.names.includes(name)) && typeof o.required === 'boolean';
  55. });
  56. if (!hasStringTypeOption) {
  57. result.boolean.push(option.names[0]);
  58. }
  59. } else {
  60. result.boolean.push(option.names[0]);
  61. }
  62. }
  63. }
  64. return result;
  65. };
  66. export const findLongest = (arr: string[]) => {
  67. return arr.sort((a, b) => {
  68. return a.length > b.length ? -1 : 1;
  69. })[0];
  70. };
  71. export const padRight = (str: string, length: number) => {
  72. return str.length >= length ? str : `${str}${' '.repeat(length - str.length)}`;
  73. };
  74. export const camelcase = (input: string) => {
  75. return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => {
  76. return p1 + p2.toUpperCase();
  77. });
  78. };
  79. export const setDotProp = (obj: {
  80. [k: string]: any;
  81. }, keys: string[], val: any) => {
  82. let i = 0;
  83. let length = keys.length;
  84. let t = obj;
  85. let x;
  86. for (; i < length; ++i) {
  87. x = t[keys[i]];
  88. t = t[keys[i]] = i === length - 1 ? val : x != null ? x : !!~keys[i + 1].indexOf('.') || !(+keys[i + 1] > -1) ? {} : [];
  89. }
  90. };
  91. export const setByType = (obj: {
  92. [k: string]: any;
  93. }, transforms: {
  94. [k: string]: any;
  95. }) => {
  96. for (const key of Object.keys(transforms)) {
  97. const transform = transforms[key];
  98. if (transform.shouldTransform) {
  99. obj[key] = Array.prototype.concat.call([], obj[key]);
  100. if (typeof transform.transformFunction === 'function') {
  101. obj[key] = obj[key].map(transform.transformFunction);
  102. }
  103. }
  104. }
  105. };
  106. export const getFileName = (input: string) => {
  107. const m = /([^\\\/]+)$/.exec(input);
  108. return m ? m[1] : '';
  109. };
  110. export const camelcaseOptionName = (name: string) => {
  111. // Camelcase the option name
  112. // Don't camelcase anything after the dot `.`
  113. return name.split('.').map((v, i) => {
  114. return i === 0 ? camelcase(v) : v;
  115. }).join('.');
  116. };
  117. export class CACError extends Error {
  118. constructor(message: string) {
  119. super(message);
  120. this.name = this.constructor.name;
  121. if (typeof Error.captureStackTrace === 'function') {
  122. Error.captureStackTrace(this, this.constructor);
  123. } else {
  124. this.stack = new Error(message).stack;
  125. }
  126. }
  127. }