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