UNPKG

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