|
| 1 | +import { glob } from 'tinyglobby'; |
| 2 | +import { exitWithError } from './logger.js'; |
| 3 | +import { FileMatch } from '../schema.js'; |
| 4 | + |
| 5 | +const GLOB_EXISTING_DOUBLE_STAR = /(\*\*)/g; |
| 6 | +const GLOB_EXISTING_STAR = /(\*)/g; |
| 7 | +const GLOB_EXISTING_ENUM = /\{([^}]*?,[^}]*?)\}/g; |
| 8 | + |
| 9 | +const PLACEHOLDER_DOUBLE_ASTERISK = '__double_asterisk'; |
| 10 | +const PLACEHOLDER_ASTERISK = '__asterisk'; |
| 11 | +const PLACEHOLDER_ENUM_PREFIX = '__enum:'; |
| 12 | + |
| 13 | +export class FileMatcherException extends Error { |
| 14 | + constructor(message: string) { |
| 15 | + super(message); |
| 16 | + this.name = this.constructor.name; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function splitToParts(template: string) { |
| 21 | + return template.split(/(\{.*?\})/g).filter(Boolean); |
| 22 | +} |
| 23 | + |
| 24 | +function getVariableName(part: string) { |
| 25 | + if (part.startsWith('{') && part.endsWith('}')) { |
| 26 | + return part.substring(1, part.length - 1).trim(); |
| 27 | + } |
| 28 | + return false; |
| 29 | +} |
| 30 | + |
| 31 | +export function sanitizeTemplate(template: string) { |
| 32 | + let value = template; |
| 33 | + const matchedEnums = [...(value.match(GLOB_EXISTING_ENUM)?.values() || [])]; |
| 34 | + matchedEnums.forEach((val) => { |
| 35 | + value = value.replace( |
| 36 | + val, |
| 37 | + `{${PLACEHOLDER_ENUM_PREFIX}${getVariableName(val)}}` |
| 38 | + ); |
| 39 | + }); |
| 40 | + value = value.replaceAll( |
| 41 | + GLOB_EXISTING_DOUBLE_STAR, |
| 42 | + '{' + PLACEHOLDER_DOUBLE_ASTERISK + '}' |
| 43 | + ); |
| 44 | + value = value.replaceAll( |
| 45 | + GLOB_EXISTING_STAR, |
| 46 | + '{' + PLACEHOLDER_ASTERISK + '}' |
| 47 | + ); |
| 48 | + return value; |
| 49 | +} |
| 50 | + |
| 51 | +export function getFileMatcher(file: string, template: string) { |
| 52 | + let fileName = file; |
| 53 | + const allVariables: Record<string, string> = {}; |
| 54 | + const templateParts = splitToParts(template); |
| 55 | + for (const [i, part] of templateParts.entries()) { |
| 56 | + const variable = getVariableName(part); |
| 57 | + if (!variable) { |
| 58 | + if (fileName.startsWith(part)) { |
| 59 | + fileName = fileName.substring(part.length); |
| 60 | + } else { |
| 61 | + throw new FileMatcherException(`Unexpected part "${part}"`); |
| 62 | + } |
| 63 | + } else { |
| 64 | + const next = templateParts[i + 1]; |
| 65 | + if (next) { |
| 66 | + const variableEnd = fileName.indexOf(next); |
| 67 | + if (getVariableName(next) || variableEnd === -1) { |
| 68 | + throw new FileMatcherException( |
| 69 | + `Can't have two variables without separator (${part} + ${next})` |
| 70 | + ); |
| 71 | + } else { |
| 72 | + allVariables[variable] = fileName.substring(0, variableEnd); |
| 73 | + fileName = fileName.substring(variableEnd); |
| 74 | + } |
| 75 | + } else { |
| 76 | + allVariables[variable] = fileName; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + const result: FileMatch = { path: file }; |
| 82 | + for (const [variable, value] of Object.entries(allVariables)) { |
| 83 | + if (variable === 'languageTag') { |
| 84 | + result.language = value; |
| 85 | + } else if (variable === 'snakeLanguageTag') { |
| 86 | + result.language = value.replaceAll('_', '-'); |
| 87 | + } else if (variable === 'androidLanguageTag') { |
| 88 | + if (value[3] === 'r') { |
| 89 | + result.language = |
| 90 | + value.substring(0, 3) + value.substring(4, value.length); |
| 91 | + } else { |
| 92 | + result.language = value; |
| 93 | + } |
| 94 | + } else if (variable === 'namespace') { |
| 95 | + result.namespace = value; |
| 96 | + } else if ( |
| 97 | + variable !== 'extension' && |
| 98 | + ![PLACEHOLDER_ASTERISK, PLACEHOLDER_DOUBLE_ASTERISK].includes(variable) && |
| 99 | + !variable.startsWith(PLACEHOLDER_ENUM_PREFIX) |
| 100 | + ) { |
| 101 | + throw new FileMatcherException(`Unknown variable "${variable}"`); |
| 102 | + } |
| 103 | + } |
| 104 | + return result; |
| 105 | +} |
| 106 | + |
| 107 | +export function getGlobPattern(template: string) { |
| 108 | + let value = template.replaceAll( |
| 109 | + GLOB_EXISTING_DOUBLE_STAR, |
| 110 | + '{__double_asterisk}' |
| 111 | + ); |
| 112 | + value = value.replaceAll(GLOB_EXISTING_STAR, '{__asterisk}'); |
| 113 | + const parts = splitToParts(value); |
| 114 | + const globPattern = parts |
| 115 | + .map((part) => { |
| 116 | + const variableName = getVariableName(part); |
| 117 | + if (variableName) { |
| 118 | + if (variableName === PLACEHOLDER_DOUBLE_ASTERISK) { |
| 119 | + return '**'; |
| 120 | + } else if (variableName.startsWith(PLACEHOLDER_ENUM_PREFIX)) { |
| 121 | + return ( |
| 122 | + '{' + variableName.substring(PLACEHOLDER_ENUM_PREFIX.length) + '}' |
| 123 | + ); |
| 124 | + } else { |
| 125 | + return '*'; |
| 126 | + } |
| 127 | + } else { |
| 128 | + return part; |
| 129 | + } |
| 130 | + }) |
| 131 | + .join(''); |
| 132 | + |
| 133 | + return globPattern; |
| 134 | +} |
| 135 | + |
| 136 | +export async function findFilesByTemplate( |
| 137 | + template: string |
| 138 | +): Promise<FileMatch[]> { |
| 139 | + try { |
| 140 | + const sanitized = sanitizeTemplate(template); |
| 141 | + const globPattern = getGlobPattern(sanitized); |
| 142 | + const files = await glob(globPattern, { onlyFiles: true, absolute: true }); |
| 143 | + return files.map((file) => { |
| 144 | + return getFileMatcher(file, sanitized); |
| 145 | + }); |
| 146 | + } catch (e) { |
| 147 | + if (e instanceof FileMatcherException) { |
| 148 | + exitWithError(e.message + ` in template ${template}`); |
| 149 | + } else { |
| 150 | + throw e; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments