Skip to content

Commit 1bcb82b

Browse files
committed
Moved script.cache to top level
1 parent 1900540 commit 1bcb82b

2 files changed

Lines changed: 26 additions & 27 deletions

File tree

utils/bump/chatbots.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Bumps @version in chatbot userscripts
44

5-
// NOTE: Pass --cache to use script.cache.paths.chatbots for faster init
5+
// NOTE: Pass --cache to use cache.paths.chatbots for faster init
66
// NOTE: Pass <--commit-msg|-m> "msg" to commit w/ msg
77
// NOTE: Pass <--no-push|-np> to skip git push
88

@@ -11,9 +11,7 @@ import fs from 'fs'
1111
import path from 'path'
1212

1313
const args = process.argv.slice(2)
14-
1514
const script = {
16-
cache: { paths: { root: '.cache' }, refs: {} },
1715
chatbots: ['amazongpt', 'bravegpt', 'duckduckgpt', 'googlegpt'],
1816
config: {
1917
cache: args.some(arg => arg.startsWith('--cache')),
@@ -25,8 +23,9 @@ const script = {
2523
},
2624
urls: { bumpmjs: 'https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c664f09/utils/bump/lib/bump.min.mjs' }
2725
}
28-
script.cache.paths.bumpmjs = path.join(process.cwd(), `${script.cache.paths.root}/bump.min.mjs`)
29-
script.cache.paths.chatbots = path.join(process.cwd(), `${script.cache.paths.root}/chatbots.json`)
26+
const cache = { paths: { root: '.cache' }}
27+
cache.paths.bumpmjs = path.join(process.cwd(), `${cache.paths.root}/bump.min.mjs`)
28+
cache.paths.chatbots = path.join(process.cwd(), `${cache.paths.root}/chatbots.json`)
3029
const { cache: { paths: cachePaths }} = script
3130
const gitCmd = process.platform == 'win32' ? 'git' : '/usr/bin/git' // same as your ext script
3231

utils/bump/userscript-requires.mjs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
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

99
import fs from 'fs'
1010
import path from 'path'
1111

1212
const args = process.argv.slice(2)
1313
const 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)
4343
const { bumpVersion, findFileBySuffix, generateSRIhash, getLatestCommitHash, isValidResource, log } = bump
4444

4545
log.working(`\n${ script.modes.cache ? 'Collecting' : 'Searching for' } userscripts...\n`)
4646
let userscripts
4747
if (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

Comments
 (0)