|
22 | 22 | br = '\x1b[1;91m' // bright red |
23 | 23 |
|
24 | 24 | // Init REGEX |
25 | | - const rePatterns = { |
| 25 | + const regEx = { |
26 | 26 | resName: /[^/]+\/(?:css|dist)?\/?[^/]+\.(?:css|js)(?=[?#]|$)/, |
27 | 27 | jsURL: /^\/\/ @require\s+(https:\/\/cdn\.jsdelivr\.net\/gh\/.+)$/, |
28 | 28 | commitHash: /(@|\?v=)([^/#]+)/, sriHash: /[^#]+$/ |
|
101 | 101 | // Collect resourcs |
102 | 102 | log.working('\nCollecting resources...\n') |
103 | 103 | const userJScontent = fs.readFileSync(userJSfilePath, 'utf-8'), |
104 | | - reResURL = new RegExp(rePatterns.jsURL.source, 'gm'), |
| 104 | + reResURL = new RegExp(regEx.jsURL.source, 'gm'), |
105 | 105 | resURLs = [...userJScontent.matchAll(reResURL)].map(match => match[1] || match[2]) |
106 | 106 | log.success(`${resURLs.length} potentially bumpable resource(s) found.`) |
107 | 107 |
|
|
121 | 121 | // Process each resource |
122 | 122 | for (const resURL of resURLs) { |
123 | 123 | if (!await isValidResource(resURL)) continue // to next resource |
124 | | - const resName = rePatterns.resName.exec(resURL)?.[0] || 'resource' // dir/filename for logs |
| 124 | + const resName = regEx.resName.exec(resURL)?.[0] || 'resource' // dir/filename for logs |
125 | 125 |
|
126 | 126 | // Compare/update commit hash |
127 | 127 | let resLatestCommitHash = latestCommitHashes[resURL.includes(repoName) ? 'chromium' : 'aiweb'] |
128 | 128 | if (resLatestCommitHash.startsWith( // compare hashes |
129 | | - rePatterns.commitHash.exec(resURL)?.[2] || '')) { // commit hash didn't change... |
| 129 | + regEx.commitHash.exec(resURL)?.[2] || '')) { // commit hash didn't change... |
130 | 130 | console.log(`${resName} already up-to-date!`) ; log.endedWithLineBreak = false |
131 | 131 | continue // ...so skip resource |
132 | 132 | } |
133 | 133 | resLatestCommitHash = resLatestCommitHash.substring(0, 7) // abbr it |
134 | | - let updatedURL = resURL.replace(rePatterns.commitHash, `$1${resLatestCommitHash}`) // update hash |
| 134 | + let updatedURL = resURL.replace(regEx.commitHash, `$1${resLatestCommitHash}`) // update hash |
135 | 135 | if (!await isValidResource(updatedURL)) continue // to next resource |
136 | 136 |
|
137 | 137 | // Generate/compare/update SRI hash |
138 | 138 | console.log(`${ !log.endedWithLineBreak ? '\n' : '' }Generating SRI (SHA-256) hash for ${resName}...`) |
139 | 139 | const newSRIhash = await generateSRIhash(updatedURL) |
140 | | - if (rePatterns.sriHash.exec(resURL)?.[0] == newSRIhash) { // SRI hash didn't change |
| 140 | + if (regEx.sriHash.exec(resURL)?.[0] == newSRIhash) { // SRI hash didn't change |
141 | 141 | console.log(`${resName} already up-to-date!`) ; log.endedWithLineBreak = false |
142 | 142 | continue // ...so skip resource |
143 | 143 | } |
144 | | - updatedURL = updatedURL.replace(rePatterns.sriHash, newSRIhash) // update hash |
| 144 | + updatedURL = updatedURL.replace(regEx.sriHash, newSRIhash) // update hash |
145 | 145 | if (!await isValidResource(updatedURL)) continue // to next resource |
146 | 146 |
|
147 | 147 | // Write updated URL to userscript |
|
0 commit comments