|
| 1 | +// largely adapted from eslint's plugin system |
| 2 | +const NAMESPACE_REGEX = /^@.*\//iu; |
| 3 | +// In eslint this is a parameter - we don't need to support the extra options |
| 4 | +const prefix = 'commitlint-plugin'; |
| 5 | + |
| 6 | +// Replace Windows with posix style paths |
| 7 | +function convertPathToPosix(filepath) { |
| 8 | + const normalizedFilepath = path.normalize(filepath); |
| 9 | + const posixFilepath = normalizedFilepath.replace(/\\/gu, '/'); |
| 10 | + |
| 11 | + return posixFilepath; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Brings package name to correct format based on prefix |
| 16 | + * @param {string} name The name of the package. |
| 17 | + * @returns {string} Normalized name of the package |
| 18 | + * @private |
| 19 | + */ |
| 20 | +export function normalizePackageName(name) { |
| 21 | + let normalizedName = name; |
| 22 | + |
| 23 | + /** |
| 24 | + * On Windows, name can come in with Windows slashes instead of Unix slashes. |
| 25 | + * Normalize to Unix first to avoid errors later on. |
| 26 | + * https://github.com/eslint/eslint/issues/5644 |
| 27 | + */ |
| 28 | + if (normalizedName.indexOf('\\') > -1) { |
| 29 | + normalizedName = convertPathToPosix(normalizedName); |
| 30 | + } |
| 31 | + |
| 32 | + if (normalizedName.charAt(0) === '@') { |
| 33 | + /** |
| 34 | + * it's a scoped package |
| 35 | + * package name is the prefix, or just a username |
| 36 | + */ |
| 37 | + const scopedPackageShortcutRegex = new RegExp( |
| 38 | + `^(@[^/]+)(?:/(?:${prefix})?)?$`, |
| 39 | + 'u' |
| 40 | + ), |
| 41 | + scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`, 'u'); |
| 42 | + |
| 43 | + if (scopedPackageShortcutRegex.test(normalizedName)) { |
| 44 | + normalizedName = normalizedName.replace( |
| 45 | + scopedPackageShortcutRegex, |
| 46 | + `$1/${prefix}` |
| 47 | + ); |
| 48 | + } else if (!scopedPackageNameRegex.test(normalizedName.split('/')[1])) { |
| 49 | + /** |
| 50 | + * for scoped packages, insert the prefix after the first / unless |
| 51 | + * the path is already @scope/eslint or @scope/eslint-xxx-yyy |
| 52 | + */ |
| 53 | + normalizedName = normalizedName.replace( |
| 54 | + /^@([^/]+)\/(.*)$/u, |
| 55 | + `@$1/${prefix}-$2` |
| 56 | + ); |
| 57 | + } |
| 58 | + } else if (normalizedName.indexOf(`${prefix}-`) !== 0) { |
| 59 | + normalizedName = `${prefix}-${normalizedName}`; |
| 60 | + } |
| 61 | + |
| 62 | + return normalizedName; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Removes the prefix from a fullname. |
| 67 | + * @param {string} fullname The term which may have the prefix. |
| 68 | + * @returns {string} The term without prefix. |
| 69 | + */ |
| 70 | +export function getShorthandName(fullname) { |
| 71 | + if (fullname[0] === '@') { |
| 72 | + let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, 'u').exec(fullname); |
| 73 | + |
| 74 | + if (matchResult) { |
| 75 | + return matchResult[1]; |
| 76 | + } |
| 77 | + |
| 78 | + matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, 'u').exec(fullname); |
| 79 | + if (matchResult) { |
| 80 | + return `${matchResult[1]}/${matchResult[2]}`; |
| 81 | + } |
| 82 | + } else if (fullname.startsWith(`${prefix}-`)) { |
| 83 | + return fullname.slice(prefix.length + 1); |
| 84 | + } |
| 85 | + |
| 86 | + return fullname; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Gets the scope (namespace) of a term. |
| 91 | + * @param {string} term The term which may have the namespace. |
| 92 | + * @returns {string} The namepace of the term if it has one. |
| 93 | + */ |
| 94 | +export function getNamespaceFromTerm(term) { |
| 95 | + const match = term.match(NAMESPACE_REGEX); |
| 96 | + |
| 97 | + return match ? match[0] : ''; |
| 98 | +} |
0 commit comments