|
1 | 1 | import path from 'path';
|
2 | 2 |
|
3 |
| -// largely adapted from eslint's plugin system |
4 |
| -const NAMESPACE_REGEX = /^@.*\//iu; |
5 | 3 | // In eslint this is a parameter - we don't need to support the extra options
|
6 | 4 | const prefix = 'commitlint-plugin';
|
7 | 5 |
|
8 |
| -// Replace Windows with posix style paths |
9 |
| -function convertPathToPosix(filepath: string) { |
| 6 | +/** |
| 7 | + * Replace Windows with posix style paths |
| 8 | + */ |
| 9 | +function convertPathToPosix(filepath: string): string { |
10 | 10 | const normalizedFilepath = path.normalize(filepath);
|
11 |
| - const posixFilepath = normalizedFilepath.replace(/\\/gu, '/'); |
12 |
| - |
13 |
| - return posixFilepath; |
| 11 | + return normalizedFilepath.replace(/\\/gu, '/'); |
14 | 12 | }
|
15 | 13 |
|
16 | 14 | /**
|
17 | 15 | * Brings package name to correct format based on prefix
|
18 |
| - * @param {string} name The name of the package. |
19 |
| - * @returns {string} Normalized name of the package |
20 |
| - * @private |
| 16 | + * @param name The name of the package. |
| 17 | + * @returns Normalized name of the package |
| 18 | + * @internal |
21 | 19 | */
|
22 |
| -export function normalizePackageName(name: string) { |
| 20 | +export function normalizePackageName( |
| 21 | + name: string |
| 22 | +): {longName: string; shortName: string} { |
23 | 23 | let normalizedName = name;
|
24 | 24 |
|
| 25 | + if ( |
| 26 | + path.isAbsolute(name) || |
| 27 | + name.startsWith('./') || |
| 28 | + name.startsWith('../') || |
| 29 | + name.startsWith('.\\') || |
| 30 | + name.startsWith('..\\') |
| 31 | + ) { |
| 32 | + return { |
| 33 | + longName: name, |
| 34 | + shortName: path.basename(name) || name, |
| 35 | + }; |
| 36 | + } |
| 37 | + |
25 | 38 | /**
|
26 | 39 | * On Windows, name can come in with Windows slashes instead of Unix slashes.
|
27 | 40 | * Normalize to Unix first to avoid errors later on.
|
@@ -61,40 +74,33 @@ export function normalizePackageName(name: string) {
|
61 | 74 | normalizedName = `${prefix}-${normalizedName}`;
|
62 | 75 | }
|
63 | 76 |
|
64 |
| - return normalizedName; |
| 77 | + return { |
| 78 | + longName: normalizedName, |
| 79 | + shortName: getShorthandName(normalizedName), |
| 80 | + }; |
65 | 81 | }
|
66 | 82 |
|
67 | 83 | /**
|
68 |
| - * Removes the prefix from a fullname. |
69 |
| - * @param {string} fullname The term which may have the prefix. |
70 |
| - * @returns {string} The term without prefix. |
| 84 | + * Removes the prefix from a fullName. |
| 85 | + * @param fullName The term which may have the prefix. |
| 86 | + * @returns The term without prefix. |
| 87 | + * @internal |
71 | 88 | */
|
72 |
| -export function getShorthandName(fullname: string) { |
73 |
| - if (fullname[0] === '@') { |
74 |
| - let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, 'u').exec(fullname); |
| 89 | +export function getShorthandName(fullName: string): string { |
| 90 | + if (fullName[0] === '@') { |
| 91 | + let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, 'u').exec(fullName); |
75 | 92 |
|
76 | 93 | if (matchResult) {
|
77 | 94 | return matchResult[1];
|
78 | 95 | }
|
79 | 96 |
|
80 |
| - matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, 'u').exec(fullname); |
| 97 | + matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, 'u').exec(fullName); |
81 | 98 | if (matchResult) {
|
82 | 99 | return `${matchResult[1]}/${matchResult[2]}`;
|
83 | 100 | }
|
84 |
| - } else if (fullname.startsWith(`${prefix}-`)) { |
85 |
| - return fullname.slice(prefix.length + 1); |
| 101 | + } else if (fullName.startsWith(`${prefix}-`)) { |
| 102 | + return fullName.slice(prefix.length + 1); |
86 | 103 | }
|
87 | 104 |
|
88 |
| - return fullname; |
89 |
| -} |
90 |
| - |
91 |
| -/** |
92 |
| - * Gets the scope (namespace) of a term. |
93 |
| - * @param {string} term The term which may have the namespace. |
94 |
| - * @returns {string} The namepace of the term if it has one. |
95 |
| - */ |
96 |
| -export function getNamespaceFromTerm(term: string) { |
97 |
| - const match = term.match(NAMESPACE_REGEX); |
98 |
| - |
99 |
| - return match ? match[0] : ''; |
| 105 | + return fullName; |
100 | 106 | }
|
0 commit comments