Skip to content

Commit

Permalink
always great fun these surprises
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed Apr 10, 2024
1 parent 8690ea6 commit a59337c
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 86 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
*.d.ts
headless
submodules
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@
path = site/themes/relearn
url = https://github.com/McShelby/hugo-theme-relearn.git
branch = main
[submodule "submodules/zotero"]
path = submodules/zotero
url = https://github.com/zotero/zotero.git
branch = main
13 changes: 8 additions & 5 deletions content/ErrorReport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Components.utils.import('resource://gre/modules/Services.jsm')

import { Shim } from './os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

import { PromptService } from './prompt'

import { Preference } from './prefs'
Expand Down Expand Up @@ -66,7 +69,7 @@ export class ErrorReport {
const version = require('../gen/version.js')

try {
await Zotero.HTTP.request('PUT', `${this.bucket}/${OS.Path.basename(this.tarball)}`, {
await Zotero.HTTP.request('PUT', `${this.bucket}/${$OS.Path.basename(this.tarball)}`, {
noCache: true,
// followRedirects: true,
// noCache: true,
Expand Down Expand Up @@ -155,7 +158,7 @@ export class ErrorReport {

public async save(): Promise<void> {
const filename = await pick('Logs', 'save', [['Tape Archive (*.tgz)', '*.tgz']], `${this.key}.tgz`)
if (filename) await OS.File.writeAtomic(filename, this.tar(), { tmpPath: filename + '.tmp' })
if (filename) await $OS.File.writeAtomic(filename, this.tar(), { tmpPath: filename + '.tmp' })
}

private async ping(region: string) {
Expand Down Expand Up @@ -208,8 +211,8 @@ export class ErrorReport {
items: win.arguments[0].wrappedJSObject.items,
}

const acronyms = OS.Path.join(Zotero.BetterBibTeX.dir, 'acronyms.csv')
if (await OS.File.exists(acronyms)) this.errorlog.acronyms = await OS.File.read(acronyms, { encoding: 'utf-8' }) as unknown as string
const acronyms = $OS.Path.join(Zotero.BetterBibTeX.dir, 'acronyms.csv')
if (await $OS.File.exists(acronyms)) this.errorlog.acronyms = await $OS.File.read(acronyms, { encoding: 'utf-8' }) as unknown as string

this.setValue('better-bibtex-error-context', this.errorlog.info)
this.setValue('better-bibtex-error-errors', this.errorlog.errors)
Expand Down Expand Up @@ -288,7 +291,7 @@ export class ErrorReport {
if (autoExports.length) {
info += 'Auto-exports:\n'
for (const ae of autoExports) {
info += ` path: ...${JSON.stringify(OS.Path.split(ae.path).components.pop())}`
info += ` path: ...${JSON.stringify($OS.Path.split(ae.path).components.pop())}`
switch (ae.type) {
case 'collection':
info += ` (${Zotero.Collections.get(ae.id)?.name || '<collection>'})`
Expand Down
5 changes: 4 additions & 1 deletion content/Preferences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Components.utils.import('resource://gre/modules/Services.jsm')

import { Shim } from './os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

import type { XUL } from '../typings/xul'

import { log } from './logger'
Expand Down Expand Up @@ -124,7 +127,7 @@ class AutoExportPane {
let label: string = { library: icons.computer, collection: icons.folder }[ae.type]
label += ` ${this.name(ae, 'short')}`
label += ` (${Translators.byId[ae.translatorID].label})`
const path = ae.path.startsWith(OS.Constants.Path.homeDir) ? ae.path.replace(OS.Constants.Path.homeDir, '~') : ae.path
const path = ae.path.startsWith($OS.Constants.Path.homeDir) ? ae.path.replace($OS.Constants.Path.homeDir, '~') : ae.path
label += ` ${path}`
return label
}
Expand Down
22 changes: 12 additions & 10 deletions content/auto-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Components.utils.import('resource://gre/modules/FileUtils.jsm')
declare const FileUtils: any

import { log } from './logger'
import { Shim } from './os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

import { Events } from './events'
import { DB as Cache } from './db/cache'
Expand Down Expand Up @@ -146,7 +148,7 @@ class Git {

case 'always':
try {
repo.path = OS.Path.dirname(bib)
repo.path = $OS.Path.dirname(bib)
}
catch (err) {
log.error('git.repo:', err)
Expand All @@ -157,16 +159,16 @@ class Git {
case 'config':
// eslint-disable-next-line no-case-declarations
let config = null
for (let root = OS.Path.dirname(bib); (await OS.File.exists(root)) && (await OS.File.stat(root)).isDir && root !== OS.Path.dirname(root); root = OS.Path.dirname(root)) {
config = OS.Path.join(root, '.git')
if ((await OS.File.exists(config)) && (await OS.File.stat(config)).isDir) break
for (let root = $OS.Path.dirname(bib); (await $OS.File.exists(root)) && (await $OS.File.stat(root)).isDir && root !== $OS.Path.dirname(root); root = $OS.Path.dirname(root)) {
config = $OS.Path.join(root, '.git')
if ((await $OS.File.exists(config)) && (await $OS.File.stat(config)).isDir) break
config = null
}
if (!config) return repo
repo.path = OS.Path.dirname(config)
repo.path = $OS.Path.dirname(config)

config = OS.Path.join(config, 'config')
if (!(await OS.File.exists(config)) || (await OS.File.stat(config)).isDir) {
config = $OS.Path.join(config, 'config')
if (!(await $OS.File.exists(config)) || (await $OS.File.stat(config)).isDir) {
return repo
}

Expand Down Expand Up @@ -341,8 +343,8 @@ const queue = new class TaskQueue {

const root = scope.type === 'collection' ? scope.collection : false

const dir = OS.Path.dirname(ae.path)
const base = OS.Path.basename(ae.path).replace(new RegExp(`${ext.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')}$`), '')
const dir = $OS.Path.dirname(ae.path)
const base = $OS.Path.basename(ae.path).replace(new RegExp(`${ext.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')}$`), '')

const autoExportPathReplace = {
diacritics: Preference.autoExportPathReplaceDiacritics,
Expand All @@ -351,7 +353,7 @@ const queue = new class TaskQueue {
}

for (const collection of collections) {
const output = OS.Path.join(dir, [base]
const output = $OS.Path.join(dir, [base]
.concat(this.getCollectionPath(collection, root))
// eslint-disable-next-line no-control-regex
.map((p: string) => p.replace(/[<>:'"/\\|?*\u0000-\u001F]/g, ''))
Expand Down
29 changes: 17 additions & 12 deletions content/aux-scanner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Components.utils.import('resource://gre/modules/osfile.jsm')
import { is7 } from './client'

import { Shim } from './os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

if (!is7) Components.utils.import('resource://gre/modules/osfile.jsm')

import { Translators } from './translators'
import { Preference } from './prefs'
Expand Down Expand Up @@ -61,7 +66,7 @@ export const AUXScanner = new class { // eslint-disable-line @typescript-eslint/
}
}

const basename = OS.Path.basename(path).replace(/\.[^.]*$/, '')
const basename = $OS.Path.basename(path).replace(/\.[^.]*$/, '')
if (options.tag) {
await this.saveToTag(itemIDs, options.tag, libraryID)
}
Expand All @@ -80,7 +85,7 @@ export const AUXScanner = new class { // eslint-disable-line @typescript-eslint/

private async read(path) {
const decoder: TextDecoder = new TextDecoder
return decoder.decode(await OS.File.read(path) as BufferSource)
return decoder.decode(await $OS.File.read(path) as BufferSource)
}

private async parse(path: string, citekeys: string[], bibfiles: Record<string, string>): Promise<Source> {
Expand Down Expand Up @@ -109,7 +114,7 @@ export const AUXScanner = new class { // eslint-disable-line @typescript-eslint/
const lua = `list-citekeys-${version}.lua`

const filters: string[] = []
const iterator = new OS.File.DirectoryIterator(Zotero.BetterBibTeX.dir)
const iterator = new $OS.File.DirectoryIterator(Zotero.BetterBibTeX.dir)
try {
await iterator.forEach(entry => {
if (entry.isFile && entry.name !== lua && entry.name.match(/^list-citekeys.*\.lua$/)) filters.push(entry.name)
Expand All @@ -119,22 +124,22 @@ export const AUXScanner = new class { // eslint-disable-line @typescript-eslint/
iterator.close()
}
for (const old of filters) {
await OS.File.remove(OS.Path.join(Zotero.BetterBibTeX.dir, old))
await $OS.File.remove($OS.Path.join(Zotero.BetterBibTeX.dir, old))
}

const filter = OS.Path.join(Zotero.BetterBibTeX.dir, lua)
if (!(await OS.File.exists(filter))) {
const filter = $OS.Path.join(Zotero.BetterBibTeX.dir, lua)
if (!(await $OS.File.exists(filter))) {
const url = 'chrome://zotero-better-bibtex/content/resource/list-citekeys.lua'
const file = Zotero.File.pathToFile(filter)
const contents = Zotero.File.getContentsFromURL(url)
Zotero.File.putContents(file, contents)
}
return filter
return <string>filter
}

private async parseMD(path: string, citekeys: string[]) {
const filter = await this.luaFilter()
const output: string = OS.Path.join(Zotero.getTempDirectory().path, `citekeys_${Zotero.Utilities.randomString()}.txt`)
const output: string = $OS.Path.join(Zotero.getTempDirectory().path, `citekeys_${Zotero.Utilities.randomString()}.txt`)
try {
await Zotero.Utilities.Internal.exec(this.pandoc, [ '--lua-filter', filter, '-t', 'markdown', '-o', output, path ])
for (const citekey of (await Zotero.File.getContentsAsync(output)).split(/\s+/)) {
Expand All @@ -155,14 +160,14 @@ export const AUXScanner = new class { // eslint-disable-line @typescript-eslint/
let m, re

const contents = await this.read(path)
const parent = OS.Path.dirname(path)
const parent = $OS.Path.dirname(path)

if (bibfiles) {
// bib files used
re = /\\bibdata\{([^}]+)\}/g
while (m = re.exec(contents)) {
for (const bib of [m[1], `${m[1]}.bib`]) {
if (!bibfiles[bib] && await OS.File.exists(bib)) {
if (!bibfiles[bib] && await $OS.File.exists(bib)) {
bibfiles[bib] = await this.read(bib)
break
}
Expand All @@ -180,7 +185,7 @@ export const AUXScanner = new class { // eslint-disable-line @typescript-eslint/
// include files
re = /\\@input\{([^}]+)\}/g
while (m = re.exec(contents)) {
await this.parseAUX(OS.Path.join(parent, m[1]), citekeys, bibfiles)
await this.parseAUX($OS.Path.join(parent, m[1]), citekeys, bibfiles)
}
}

Expand Down
14 changes: 8 additions & 6 deletions content/better-bibtex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable prefer-rest-params */

import { is7 } from './client'
import { Shim } from './os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

if (is7) Components.utils.importGlobalProperties(['FormData'])

Expand Down Expand Up @@ -454,7 +456,7 @@ $patch$(Zotero.Translate.Export.prototype, 'translate', original => function Zot
if (this.location) {
if (displayOptions.exportFileData) { // when exporting file data, the user was asked to pick a directory rather than a file
displayOptions.exportDir = this.location.path
displayOptions.exportPath = OS.Path.join(this.location.path, `${this.location.leafName}.${translator.target}`)
displayOptions.exportPath = $OS.Path.join(this.location.path, `${this.location.leafName}.${translator.target}`)
displayOptions.cache = false
}
else {
Expand Down Expand Up @@ -609,7 +611,7 @@ export class BetterBibTeX {

case 'tag':
// eslint-disable-next-line no-case-declarations
let name = OS.Path.basename(aux)
let name = $OS.Path.basename(aux)
name = name.lastIndexOf('.') > 0 ? name.substr(0, name.lastIndexOf('.')) : name
// eslint-disable-next-line no-case-declarations
const tag = prompt({
Expand Down Expand Up @@ -694,16 +696,16 @@ export class BetterBibTeX {
// this is what really takes long
await Zotero.initializationPromise

this.dir = OS.Path.join(Zotero.DataDirectory.dir, 'better-bibtex')
await OS.File.makeDir(this.dir, { ignoreExisting: true })
this.dir = $OS.Path.join(Zotero.DataDirectory.dir, 'better-bibtex')
await $OS.File.makeDir(this.dir, { ignoreExisting: true })
await Preference.startup(this.dir)
Events.startup()
},
})

orchestrator.add('sqlite', {
startup: async () => {
await Zotero.DB.queryAsync('ATTACH DATABASE ? AS betterbibtex', [OS.Path.join(Zotero.DataDirectory.dir, 'better-bibtex.sqlite')])
await Zotero.DB.queryAsync('ATTACH DATABASE ? AS betterbibtex', [$OS.Path.join(Zotero.DataDirectory.dir, 'better-bibtex.sqlite')])

const tables: Record<string, boolean> = {}
for (const table of await Zotero.DB.columnQueryAsync("SELECT LOWER(REPLACE(name, '-', '')) FROM betterbibtex.sqlite_master where type='table'")) {
Expand Down Expand Up @@ -868,7 +870,7 @@ export class BetterBibTeX {
}

const file = new FileUtils.File(path)
// cannot use await OS.File.exists here because we may be invoked in noWait mod
// cannot use await $OS.File.exists here because we may be invoked in noWait mod
if (!file.exists()) {
log.error('BetterBibTeX.getContents:', path, 'does not exist')
return null
Expand Down
20 changes: 11 additions & 9 deletions content/db/store/file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/member-ordering */

Components.utils.import('resource://gre/modules/osfile.jsm')

import { log } from '../../logger'
import { is7 } from '../../client'
import { Shim } from '../../os'
if (!is7) Components.utils.import('resource://gre/modules/osfile.jsm')
const $OS = typeof OS !== 'undefined' ? OS : Shim

// Components.utils.import('resource://gre/modules/Sqlite.jsm')
// declare const Sqlite: any
Expand Down Expand Up @@ -30,12 +32,12 @@ export class File {
}

private async save(name: string, data, dirty: boolean) {
const path = OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
const save = dirty || !(await OS.File.exists(path))
const path = $OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
const save = dirty || !(await $OS.File.exists(path))

if (!save) return null

await OS.File.writeAtomic(path, JSON.stringify(data), { encoding: 'utf-8', tmpPath: `${path}.tmp`})
await $OS.File.writeAtomic(path, JSON.stringify(data), { encoding: 'utf-8', tmpPath: `${path}.tmp`})
}

public async loadDatabase(name: string, callback: ((v: null) => void)): Promise<void> {
Expand Down Expand Up @@ -68,16 +70,16 @@ export class File {
}

private async load(name) {
const path = OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
const exists = await OS.File.exists(path)
const path = $OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
const exists = await $OS.File.exists(path)

if (!exists) return null

const data = JSON.parse(await OS.File.read(path, { encoding: 'utf-8' }) as unknown as string)
const data = JSON.parse(await $OS.File.read(path, { encoding: 'utf-8' }) as unknown as string)

// this is intentional. If all is well, the database will be retained in memory until it's saved at
// shutdown. If all is not well, this will make sure the caches are rebuilt from scratch on next start
await OS.File.move(path, `${path}.bak`)
await $OS.File.move(path, `${path}.bak`)

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return data
Expand Down
5 changes: 4 additions & 1 deletion content/key-manager/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Tag, RegularItem as SerializedRegularItem, Item as SerializedItem } from '../../gen/typings/serialized-item'

import { Shim } from '../os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

import { client } from '../client'

import { Events } from '../events'
Expand Down Expand Up @@ -1149,7 +1152,7 @@ class PatternFormatter {
const acronyms: Record<string, string> = {}

try {
for (const row of csv2list(OS.Path.join(Zotero.BetterBibTeX.dir, `${list}.csv`))) {
for (const row of csv2list($OS.Path.join(Zotero.BetterBibTeX.dir, `${list}.csv`))) {
if (row.length !== 2) {
log.error('unexpected row in', `${list}.csv`, ':', row)
continue
Expand Down
5 changes: 4 additions & 1 deletion content/load-csv.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Shim } from './os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

import csv from 'papaparse'
import { log } from './logger'

async function read(path: string): Promise<string> {
try {
return (await OS.File.exists(path)) ? (await OS.File.read(path, { encoding: 'utf-8' }) as unknown as string) : ''
return (await $OS.File.exists(path)) ? (await $OS.File.read(path, { encoding: 'utf-8' }) as unknown as string) : ''
}
catch (err) {
log.error('csv.read', path, 'error:', err)
Expand Down
5 changes: 4 additions & 1 deletion content/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { Reason } from './bootstrap'
import { log } from './logger'
import { Preference } from './prefs'

import { Shim } from './os'
const $OS = typeof OS !== 'undefined' ? OS : Shim

type Handler = (reason: Reason, task?: Task) => void | string | Promise<void | string>

import { print } from './logger'
Expand Down Expand Up @@ -298,7 +301,7 @@ export class Orchestrator {
</project>
`

Zotero.File.putContents(Zotero.File.pathToFile(OS.Path.join(Zotero.BetterBibTeX.dir, `${phase}.gan`)), gantt)
Zotero.File.putContents(Zotero.File.pathToFile($OS.Path.join(Zotero.BetterBibTeX.dir, `${phase}.gan`)), gantt)
}

public async startup(reason: Reason, progress?: Progress): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions content/os.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { is7 } from './client'

import { $OS } from '../gen/osfile-shim'
export const Shim: any = is7 ? $OS : null
Loading

0 comments on commit a59337c

Please sign in to comment.