|
| 1 | +const packNamePartRegex = /[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/; |
| 2 | +const packNameRegex = new RegExp( |
| 3 | + `^(?<scope>${packNamePartRegex.source})/(?<name>${packNamePartRegex.source})$`, |
| 4 | +); |
| 5 | +const packNameLength = 128; |
| 6 | + |
| 7 | +export interface ExtensionPackName { |
| 8 | + scope: string; |
| 9 | + name: string; |
| 10 | +} |
| 11 | + |
| 12 | +export function formatPackName(packName: ExtensionPackName): string { |
| 13 | + return `${packName.scope}/${packName.name}`; |
| 14 | +} |
| 15 | + |
| 16 | +export function autoNameExtensionPack( |
| 17 | + name: string, |
| 18 | + language: string, |
| 19 | +): ExtensionPackName | undefined { |
| 20 | + let packName = `${name}-${language}`; |
| 21 | + if (!packName.includes("/")) { |
| 22 | + packName = `pack/${packName}`; |
| 23 | + } |
| 24 | + |
| 25 | + const parts = packName.split("/"); |
| 26 | + const sanitizedParts = parts.map((part) => sanitizeExtensionPackName(part)); |
| 27 | + |
| 28 | + // If the scope is empty (e.g. if the given name is "-/b"), then we need to still set a scope |
| 29 | + if (sanitizedParts[0].length === 0) { |
| 30 | + sanitizedParts[0] = "pack"; |
| 31 | + } |
| 32 | + |
| 33 | + return { |
| 34 | + scope: sanitizedParts[0], |
| 35 | + // This will ensure there's only 1 slash |
| 36 | + name: sanitizedParts.slice(1).join("-"), |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function sanitizeExtensionPackName(name: string) { |
| 41 | + // Lowercase everything |
| 42 | + name = name.toLowerCase(); |
| 43 | + |
| 44 | + // Replace all spaces, dots, and underscores with hyphens |
| 45 | + name = name.replaceAll(/[\s._]+/g, "-"); |
| 46 | + |
| 47 | + // Replace all characters which are not allowed by empty strings |
| 48 | + name = name.replaceAll(/[^a-z0-9-]/g, ""); |
| 49 | + |
| 50 | + // Remove any leading or trailing hyphens |
| 51 | + name = name.replaceAll(/^-|-$/g, ""); |
| 52 | + |
| 53 | + // Remove any duplicate hyphens |
| 54 | + name = name.replaceAll(/-{2,}/g, "-"); |
| 55 | + |
| 56 | + return name; |
| 57 | +} |
| 58 | + |
| 59 | +export function parsePackName(packName: string): ExtensionPackName | undefined { |
| 60 | + const matches = packNameRegex.exec(packName); |
| 61 | + if (!matches?.groups) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const scope = matches.groups.scope; |
| 66 | + const name = matches.groups.name; |
| 67 | + |
| 68 | + return { |
| 69 | + scope, |
| 70 | + name, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +export function validatePackName(name: string): string | undefined { |
| 75 | + if (!name) { |
| 76 | + return "Pack name must not be empty"; |
| 77 | + } |
| 78 | + |
| 79 | + if (name.length > packNameLength) { |
| 80 | + return `Pack name must be no longer than ${packNameLength} characters`; |
| 81 | + } |
| 82 | + |
| 83 | + const matches = packNameRegex.exec(name); |
| 84 | + if (!matches?.groups) { |
| 85 | + if (!name.includes("/")) { |
| 86 | + return "Invalid package name: a pack name must contain a slash to separate the scope from the pack name"; |
| 87 | + } |
| 88 | + |
| 89 | + return "Invalid package name: a pack name must contain only lowercase ASCII letters, ASCII digits, and hyphens"; |
| 90 | + } |
| 91 | + |
| 92 | + return undefined; |
| 93 | +} |
0 commit comments