33// Bumps @require 'd jsDelivr URLs in userscript
44
55// NOTE: Doesn't git commit to allow script editing from breaking changes
6- // NOTE: Pass --cache to use script. cache.paths.userscripts for faster init
6+ // NOTE: Pass --cache to use cache.paths.userscripts for faster init
77// NOTE: Pass --suffix to override file suffix (default: .user.js)
88
99import fs from 'fs'
1010import path from 'path'
1111
1212const args = process . argv . slice ( 2 )
1313const script = {
14- cache : { paths : { root : '.cache' } , refs : { } } ,
1514 modes : { cache : args . some ( arg => arg . startsWith ( '--cache' ) ) } ,
1615 regex : {
1716 hash : { commit : { full : / ^ [ a - f \d ] { 40 } $ / i, inline : / ( @ | \? v = ) ( [ ^ / # ] + ) / } , sri : / [ ^ # ] + $ / } ,
@@ -31,31 +30,32 @@ const script = {
3130 } ,
3231 userscriptName : args . find ( arg => arg . startsWith ( '--suffix=' ) ) ?. split ( '=' ) [ 1 ] || '.user.js'
3332}
34- script . cache . paths . bumpmjs = path . join ( process . cwd ( ) , `${ script . cache . paths . root } /bump.min.mjs` )
35- script . cache . paths . userscripts = path . join ( process . cwd ( ) , `${ script . cache . paths . root } /userscripts.json` )
36- const { cache : { paths : cachePaths } , regex : re } = script
33+ const { regex : re } = script
34+ const cache = { paths : { root : '.cache' } , refs : { } }
35+ cache . paths . bumpmjs = path . join ( process . cwd ( ) , `${ cache . paths . root } /bump.min.mjs` )
36+ cache . paths . userscripts = path . join ( process . cwd ( ) , `${ cache . paths . root } /userscripts.json` )
3737
3838// Import bump.mjs
39- fs . mkdirSync ( path . dirname ( cachePaths . bumpmjs ) , { recursive : true } )
40- fs . writeFileSync ( cachePaths . bumpmjs , ( await ( await fetch ( script . urls . bumpmjs ) ) . text ( ) ) )
41- const bump = await import ( `file://${ cachePaths . bumpmjs } ` )
42- fs . unlinkSync ( cachePaths . bumpmjs )
39+ fs . mkdirSync ( path . dirname ( cache . paths . bumpmjs ) , { recursive : true } )
40+ fs . writeFileSync ( cache . paths . bumpmjs , ( await ( await fetch ( script . urls . bumpmjs ) ) . text ( ) ) )
41+ const bump = await import ( `file://${ cache . paths . bumpmjs } ` )
42+ fs . unlinkSync ( cache . paths . bumpmjs )
4343const { bumpVersion, findFileBySuffix, generateSRIhash, getLatestCommitHash, isValidResource, log } = bump
4444
4545log . working ( `\n${ script . modes . cache ? 'Collecting' : 'Searching for' } userscripts...\n` )
4646let userscripts
4747if ( script . modes . cache ) {
4848 try { // create missing cache file
49- fs . mkdirSync ( path . dirname ( cachePaths . userscripts ) , { recursive : true } )
50- const fd = fs . openSync ( cachePaths . userscripts ,
49+ fs . mkdirSync ( path . dirname ( cache . paths . userscripts ) , { recursive : true } )
50+ const fd = fs . openSync ( cache . paths . userscripts ,
5151 fs . constants . O_CREAT | fs . constants . O_EXCL | fs . constants . O_RDWR )
52- log . info ( `Cache file missing. Generating ${ cachePaths . userscripts } ...\n` )
52+ log . info ( `Cache file missing. Generating ${ cache . paths . userscripts } ...\n` )
5353 userscripts = await findFileBySuffix ( { suffix : script . userscriptName } )
5454 log . break ( )
5555 fs . writeFileSync ( fd , JSON . stringify ( userscripts , undefined , 2 ) , 'utf-8' )
56- log . success ( `\nCache file created @ ${ cachePaths . userscripts } ` )
56+ log . success ( `\nCache file created @ ${ cache . paths . userscripts } ` )
5757 } catch ( err ) {
58- userscripts = JSON . parse ( fs . readFileSync ( cachePaths . userscripts , 'utf-8' ) )
58+ userscripts = JSON . parse ( fs . readFileSync ( cache . paths . userscripts , 'utf-8' ) )
5959 console . log ( userscripts )
6060 log . break ( )
6161 }
@@ -97,24 +97,24 @@ for (const scriptPath of Object.keys(urlMap)) {
9797 let resLatestRef
9898 if ( re . verTag . test ( currentCommit ) ) { // get latest release tag
9999 const apiURL = `${ script . urls . apis . github . repos } /${ targetRepo } /releases/latest`
100- resLatestRef = script . cache . refs [ targetRepo ] ??= ( await ( await fetch ( apiURL , {
100+ resLatestRef = cache . refs [ targetRepo ] ??= ( await ( await fetch ( apiURL , {
101101 headers : { 'User-Agent' : 'bump-script' } } ) ) . json ( ) ) . tag_name
102102 } else if ( targetRepo . endsWith ( repoName ) && resURL . includes ( 'firefox/extension/' ) ) {
103- if ( ! script . cache . refs . ff ) {
103+ if ( ! cache . refs . ff ) {
104104 console . log ( 'Fetching latest commit hash for firefox/extension...' )
105- script . cache . refs . ff = await getLatestCommitHash ( {
105+ cache . refs . ff = await getLatestCommitHash ( {
106106 repo : targetRepo , path : 'firefox/extension' } )
107107 }
108- resLatestRef = script . cache . refs . ff
108+ resLatestRef = cache . refs . ff
109109 } else if ( targetRepo . endsWith ( repoName ) && resURL . includes ( 'chromium/extension/' ) ) {
110- if ( ! script . cache . refs . chromium ) {
110+ if ( ! cache . refs . chromium ) {
111111 console . log ( 'Fetching latest commit hash for chromium/extension...' )
112- script . cache . refs . chromium = await getLatestCommitHash ( {
112+ cache . refs . chromium = await getLatestCommitHash ( {
113113 repo : targetRepo , path : 'chromium/extension' } )
114114 }
115- resLatestRef = script . cache . refs . chromium
115+ resLatestRef = cache . refs . chromium
116116 } else // get latest commit hash
117- resLatestRef = script . cache . refs [ targetRepo ] ??= await getLatestCommitHash ( { repo : targetRepo } )
117+ resLatestRef = cache . refs [ targetRepo ] ??= await getLatestCommitHash ( { repo : targetRepo } )
118118 if ( resLatestRef . startsWith ( currentCommit ) ) {
119119 console . log ( `${ resName } already up-to-date!` ) ; log . endedWithLineBreak = false
120120 continue
0 commit comments