|
27 | 27 |
|
28 | 28 | // Init REGEX |
29 | 29 | const rePatterns = { |
30 | | - resourceName: /[^/]+\/(?:css|dist)?\/?[^/]+\.(?:css|js)(?=[?#]|$)/, |
| 30 | + resName: /[^/]+\/(?:css|dist)?\/?[^/]+\.(?:css|js)(?=[?#]|$)/, |
31 | 31 | cssURL: /^\/\/ @resource.+(https:\/\/assets.+\.css.+)$/, |
32 | 32 | jsURL: /^\/\/ @require\s+(https:\/\/cdn\.jsdelivr\.net\/gh\/.+)$/, |
33 | 33 | commitHash: /(@|\?v=)([^/#]+)/, sriHash: /[^#]+$/ |
|
59 | 59 | return fetch(url) |
60 | 60 | } |
61 | 61 |
|
62 | | - async function isValidResource(resourceURL) { |
| 62 | + async function isValidResource(resURL) { |
63 | 63 | try { |
64 | | - const resourceIsValid = !(await (await fetchData(resourceURL)).text()).startsWith('Package size exceeded') |
65 | | - if (!resourceIsValid) log.error(`\nInvalid resource: ${resourceURL}\n`) |
66 | | - return resourceIsValid |
| 64 | + const resIsValid = !(await (await fetchData(resURL)).text()).startsWith('Package size exceeded') |
| 65 | + if (!resIsValid) log.error(`\nInvalid resource: ${resURL}\n`) |
| 66 | + return resIsValid |
67 | 67 | } catch (err) { |
68 | | - log.error(`\nCannot validate resource: ${resourceURL}\n`) |
| 68 | + log.error(`\nCannot validate resource: ${resURL}\n`) |
69 | 69 | return null |
70 | 70 | } |
71 | 71 | } |
|
106 | 106 | // Collect resourcs |
107 | 107 | log.working('\nCollecting resources...\n') |
108 | 108 | const userJScontent = fs.readFileSync(userJSfilePath, 'utf-8') |
109 | | - const reResourceURL = new RegExp( // eslint-disable-next-line |
| 109 | + const reResURL = new RegExp( // eslint-disable-next-line |
110 | 110 | `(?:${rePatterns.cssURL.source})|(?:${rePatterns.jsURL.source})`, 'gm') |
111 | | - const resourceURLs = [...userJScontent.matchAll(reResourceURL)].map(match => match[1] || match[2]) |
112 | | - log.success(`${resourceURLs.length} potentially bumpable resource(s) found.`) |
| 111 | + const resURLs = [...userJScontent.matchAll(reResURL)].map(match => match[1] || match[2]) |
| 112 | + log.success(`${resURLs.length} potentially bumpable resource(s) found.`) |
113 | 113 |
|
114 | 114 | // Fetch latest commit hash for adamlui/ai-web-extensions/assets/styles/rising-stars |
115 | 115 | const risingStarsPath = 'assets/styles/rising-stars' |
|
120 | 120 | let urlsUpdatedCnt = 0 |
121 | 121 |
|
122 | 122 | // Fetch latest commit hash for repo/chrom<e|ium>/extension |
123 | | - if (resourceURLs.some(url => url.includes(repoName))) { |
| 123 | + if (resURLs.some(url => url.includes(repoName))) { |
124 | 124 | console.log('Fetching latest commit hash for Chromium extension...') |
125 | 125 | for (const chrDirName of ['chromium', 'chrome']) { |
126 | | - latestCommitHashes.repoResources = await getLatestCommitHash( |
| 126 | + latestCommitHashes.repoRes = await getLatestCommitHash( |
127 | 127 | `adamlui/${repoName}`, `${chrDirName}/extension`) |
128 | | - if (latestCommitHashes.repoResources) break |
| 128 | + if (latestCommitHashes.repoRes) break |
129 | 129 | } |
130 | 130 | } |
131 | 131 |
|
132 | 132 | // Process each resource |
133 | | - for (const resourceURL of resourceURLs) { |
134 | | - if (!await isValidResource(resourceURL)) continue |
135 | | - const resourceName = rePatterns.resourceName.exec(resourceURL)?.[0] || 'resource' // dir/filename for logs |
| 133 | + for (const resURL of resURLs) { |
| 134 | + if (!await isValidResource(resURL)) continue |
| 135 | + const resName = rePatterns.resName.exec(resURL)?.[0] || 'resource' // dir/filename for logs |
136 | 136 |
|
137 | 137 | // Compare commit hashes |
138 | | - const resourceLatestCommitHash = latestCommitHashes[ |
139 | | - resourceURL.includes(repoName) ? 'repoResources' : 'risingStars'] |
140 | | - if (resourceLatestCommitHash.startsWith( |
141 | | - rePatterns.commitHash.exec(resourceURL)?.[2] || '')) { // commit hash didn't change... |
142 | | - console.log(`${resourceName} already up-to-date!`) ; log.endedWithLineBreak = false |
| 138 | + const resLatestCommitHash = latestCommitHashes[ |
| 139 | + resURL.includes(repoName) ? 'repoRes' : 'risingStars'] |
| 140 | + if (resLatestCommitHash.startsWith( |
| 141 | + rePatterns.commitHash.exec(resURL)?.[2] || '')) { // commit hash didn't change... |
| 142 | + console.log(`${resName} already up-to-date!`) ; log.endedWithLineBreak = false |
143 | 143 | continue // ...so skip resource |
144 | 144 | } |
145 | | - let updatedURL = resourceURL.replace(rePatterns.commitHash, `$1${resourceLatestCommitHash}`) // otherwise update commit hash |
| 145 | + let updatedURL = resURL.replace(rePatterns.commitHash, `$1${resLatestCommitHash}`) // otherwise update commit hash |
146 | 146 | if (!await isValidResource(updatedURL)) continue |
147 | 147 |
|
148 | 148 | // Generate/compare SRI hash |
149 | | - console.log(`${ !log.endedWithLineBreak ? '\n' : '' }Generating SHA-256 hash for ${resourceName}...`) |
| 149 | + console.log(`${ !log.endedWithLineBreak ? '\n' : '' }Generating SHA-256 hash for ${resName}...`) |
150 | 150 | const newSRIhash = await getSRIhash(updatedURL) |
151 | | - if (rePatterns.sriHash.exec(resourceURL)?.[0] == newSRIhash) { // SRI hash didn't change |
152 | | - console.log(`${resourceName} already up-to-date!`) ; log.endedWithLineBreak = false |
| 151 | + if (rePatterns.sriHash.exec(resURL)?.[0] == newSRIhash) { // SRI hash didn't change |
| 152 | + console.log(`${resName} already up-to-date!`) ; log.endedWithLineBreak = false |
153 | 153 | continue // ...so skip resource |
154 | 154 | } |
155 | 155 | updatedURL = updatedURL.replace(rePatterns.sriHash, newSRIhash) // otherwise update SRI hash |
156 | 156 | if (!await isValidResource(updatedURL)) continue |
157 | 157 |
|
158 | 158 | // Write updated URL to userscript |
159 | | - console.log(`Writing updated URL for ${resourceName}...`) |
| 159 | + console.log(`Writing updated URL for ${resName}...`) |
160 | 160 | const userJScontent = fs.readFileSync(userJSfilePath, 'utf-8') |
161 | | - fs.writeFileSync(userJSfilePath, userJScontent.replace(resourceURL, updatedURL), 'utf-8') |
162 | | - log.success(`${resourceName} bumped!\n`) |
| 161 | + fs.writeFileSync(userJSfilePath, userJScontent.replace(resURL, updatedURL), 'utf-8') |
| 162 | + log.success(`${resName} bumped!\n`) |
163 | 163 | urlsUpdatedCnt++ |
164 | 164 | } |
165 | 165 | if (urlsUpdatedCnt > 0) { |
|
0 commit comments