plugin-naming.js 3.5 KB

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