|
25 | 25 |
|
26 | 26 | // Init REGEX
|
27 | 27 | const rePatterns = {
|
28 |
| - resourceName: /\w+\/\w+\.js(?=#|$)/, |
| 28 | + resourceName: /[^/]+\/(?:css|dist)?\/?[^/]+\.(?:css|js)(?=[?#]|$)/, |
| 29 | + cssURL: /^\/\/ @resource.+(https:\/\/assets.+\.css.+)$/gm, |
29 | 30 | jsURL: /^\/\/ @require\s+(https:\/\/cdn\.jsdelivr\.net\/gh\/.+$)/gm,
|
30 |
| - commitHash: /@([^/]+)/, sriHash: /[^#]+$/ |
| 31 | + commitHash: /(@|\?v=)([^/#]+)/, sriHash: /[^#]+$/ |
31 | 32 | }
|
32 | 33 |
|
33 | 34 | // Define FUNCTIONS
|
|
76 | 77 | } else // bump to today
|
77 | 78 | newVer = today
|
78 | 79 | fs.writeFileSync(userJSfilePath, userJScontent.replace(re_version, `$1${newVer}`), 'utf-8')
|
79 |
| - console.log(`Updated: ${bw}v${currentVer}${nc} → ${bg}v${newVer}${nc}\n`) |
| 80 | + console.log(`Updated: ${bw}v${currentVer}${nc} → ${bg}v${newVer}${nc}`) |
80 | 81 | }
|
81 | 82 |
|
82 | 83 | // Run MAIN routine
|
83 | 84 |
|
84 |
| - log.working('\nCollecting JS resources...\n') |
| 85 | + // Collect resourcs |
| 86 | + log.working('\nCollecting resources...\n') |
85 | 87 | const userJScontent = fs.readFileSync(userJSfilePath, 'utf-8'),
|
86 |
| - resourceURLs = [...userJScontent.matchAll(rePatterns.jsURL)].map(match => match[1]) |
| 88 | + reResourceURL = new RegExp(`(?:${rePatterns.cssURL.source})|(?:${rePatterns.jsURL.source})`, 'gm'), |
| 89 | + resourceURLs = [...userJScontent.matchAll(reResourceURL)].map(match => match[1] || match[2]) |
87 | 90 | log.success(`${resourceURLs.length} potentially bumpable resource(s) found.`)
|
88 | 91 |
|
| 92 | + // Fetch latest commit hash for adamlui/ai-web-extensions/assets/styles/rising-stars |
| 93 | + const ghEndpoint = 'https://api.github.com/repos/adamlui/ai-web-extensions/commits', |
| 94 | + risingStarsPath = 'assets/styles/rising-stars' |
| 95 | + log.working(`\nFetching latest commit hash for ${risingStarsPath}...\n`) |
| 96 | + const latestCommitHashes = { |
| 97 | + risingStars: (await (await fetch(`${ghEndpoint}?path=${risingStarsPath}`)).json())[0]?.sha } |
| 98 | + console.log(`${latestCommitHashes.risingStars}`) |
| 99 | + |
89 | 100 | log.working('\nProcessing resource(s)...\n')
|
90 | 101 | let urlsUpdatedCnt = 0
|
91 | 102 |
|
92 | 103 | // Fetch latest commit hash
|
93 |
| - console.log('Fetching latest commit hash...') |
94 |
| - const latestCommitHash = require('child_process').execFileSync( |
95 |
| - 'git', ['ls-remote', `https://github.com/adamlui/${repoName}.git`, 'HEAD']).toString().split('\t')[0] |
96 |
| - console.log(latestCommitHash + '\n') |
| 104 | + if (resourceURLs.some(url => url.includes(repoName))) { |
| 105 | + console.log('Fetching latest commit hash for repo...') |
| 106 | + latestCommitHashes.repoResources = require('child_process').execFileSync( |
| 107 | + 'git', ['ls-remote', `https://github.com/adamlui/${repoName}.git`, 'HEAD']).toString().split('\t')[0] |
| 108 | + console.log(`${latestCommitHashes.repoResources}\n`) |
| 109 | + } |
97 | 110 |
|
98 | 111 | // Process each resource
|
99 | 112 | for (const resourceURL of resourceURLs) {
|
100 |
| - const resourceName = rePatterns.resourceName.exec(resourceURL)?.[0] || 'resource' // dir/filename.js for logs |
| 113 | + const resourceName = rePatterns.resourceName.exec(resourceURL)?.[0] || 'resource' // dir/filename for logs |
101 | 114 |
|
102 | 115 | // Compare commit hashes
|
103 |
| - if (latestCommitHash.startsWith(rePatterns.commitHash.exec(resourceURL)?.[1] || '')) { // commit hash didn't change... |
104 |
| - console.log(`${resourceName} already up-to-date!\n`) ; continue } // ...so skip resource |
105 |
| - let updatedURL = resourceURL.replace(rePatterns.commitHash, `@${latestCommitHash}`) // othrwise update commit hash |
| 116 | + const resourceLatestCommitHash = latestCommitHashes[ |
| 117 | + resourceURL.includes(repoName) ? 'repoResources' : 'risingStars'] |
| 118 | + if (resourceLatestCommitHash.startsWith( |
| 119 | + rePatterns.commitHash.exec(resourceURL)?.[2] || '')) { // commit hash didn't change... |
| 120 | + console.log(`${resourceName} already up-to-date!`) ; log.hadLineBreak = false |
| 121 | + continue // ...so skip resource |
| 122 | + } |
| 123 | + let updatedURL = resourceURL.replace(rePatterns.commitHash, `$1${resourceLatestCommitHash}`) // otherwise update commit hash |
106 | 124 |
|
107 | 125 | // Generate/compare SRI hash
|
108 |
| - console.log(`Generating SHA-256 hash for ${resourceName}...`) |
| 126 | + console.log(`${ !log.hadLineBreak ? '\n' : '' }Generating SHA-256 hash for ${resourceName}...`) |
109 | 127 | const newSRIhash = await getSRIhash(updatedURL)
|
110 | 128 | if (rePatterns.sriHash.exec(resourceURL)?.[0] == newSRIhash) { // SRI hash didn't change
|
111 |
| - console.log(`${resourceName} already up-to-date!\n`) ; continue } // ...so skip resource |
| 129 | + console.log(`${resourceName} already up-to-date!`) ; log.hadLineBreak = false |
| 130 | + continue // ...so skip resource |
| 131 | + } |
112 | 132 | updatedURL = updatedURL.replace(rePatterns.sriHash, newSRIhash) // otherwise update SRI hash
|
113 | 133 |
|
114 | 134 | // Write updated URL to userscript
|
|
119 | 139 | urlsUpdatedCnt++
|
120 | 140 | }
|
121 | 141 | if (urlsUpdatedCnt > 0) {
|
122 |
| - console.log('Bumping userscript version...') |
| 142 | + console.log(`${ !log.hadLineBreak ? '\n' : '' }Bumping userscript version...`) |
123 | 143 | bumpUserJSver(userJSfilePath)
|
124 | 144 | }
|
125 | 145 |
|
126 | 146 | // Log final summary
|
127 | 147 | log[urlsUpdatedCnt > 0 ? 'success' : 'info'](
|
128 |
| - `${ urlsUpdatedCnt > 0 ? 'Success! ' : '' }${urlsUpdatedCnt} resource(s) bumped.`) |
| 148 | + `\n${ urlsUpdatedCnt > 0 ? 'Success! ' : '' }${urlsUpdatedCnt} resource(s) bumped.`) |
129 | 149 |
|
130 | 150 | })()
|
0 commit comments