This commit is contained in:
2022-06-06 21:00:10 -05:00
commit 1e94d1a8ed
23 changed files with 1693 additions and 0 deletions

64
src/main.ts Normal file
View File

@@ -0,0 +1,64 @@
import { build } from 'esbuild'
import {
lstatSync,
readdirSync,
readFileSync,
unlinkSync,
writeFileSync,
} from 'fs'
import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths'
import readMeta from './readmeta'
async function compileProject(name: string) {
//read meta file
let [metaJson, metaString] = readMeta(name)
let outPath = DistPath(name)
await build({
entryPoints: [ScriptPath(name).main],
outfile: outPath,
target: 'esnext',
platform: 'node',
format: 'esm',
bundle: true,
minify: false,
define: {
UserScriptName: `'${metaJson.name}'`,
UserScriptNamespace: `'${metaJson.namespace}'`,
UserScriptVersion: `'${metaJson.version}'`,
UserScriptDownloadURL: `'${metaJson.downloadURL}'`,
UserScriptSupportURL: `'${metaJson.supportURL}'`,
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
},
})
//add UserScript header
let content = readFileSync(outPath).toString()
writeFileSync(outPath, metaString + content)
}
if (!lstatSync('package.json').isFile()) {
console.error('package.json not found, unwilling to run')
process.exit(1)
}
//delete compiled scripts
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
//compile scripts
readdirSync(ScriptBase).forEach(name => {
let path = ScriptPath(name)
if (
!name.endsWith('_') &&
lstatSync(path.dir).isDirectory() &&
lstatSync(path.main).isFile()
) {
compileProject(name)
}
})

16
src/paths.ts Normal file
View File

@@ -0,0 +1,16 @@
export const BaseUrl = `https://git.zomo.dev/zomo/browser-scripts`
export const RemoteBranch = 'main'
export const ScriptBase = 'scripts'
export const DistBase = 'dist'
export const SupportUrl = `${BaseUrl}/issues`
export const FileUrl = (name: string) =>
`${BaseUrl}/raw/branch/${RemoteBranch}/${DistBase}/${name}.user.js`
export const ScriptPath = (name: string) => ({
dir: `${ScriptBase}/${name}`,
main: `${ScriptBase}/${name}/main.ts`,
meta: `${ScriptBase}/${name}/meta.json`,
})
export const DistPath = (name: string) => `${DistBase}/${name}.user.js`

93
src/readmeta.ts Normal file
View File

@@ -0,0 +1,93 @@
import { lstatSync, readFileSync } from 'fs'
import { BaseUrl, FileUrl, ScriptPath, SupportUrl } from './paths'
import { UserDataMeta, UserDataMetaFull, UserDataMetaPartial } from './types'
export default function (name: string): [UserDataMetaFull, string] {
var meta: UserDataMetaFull = {
name: name,
namespace: 'zomo.dev',
match: '',
excludematch: '',
version: '0.0.0',
description: '',
icon: '',
require: '',
resource: '',
runat: '',
noframes: false,
grant: '',
injectinto: '',
downloadURL: FileUrl(name),
supportURL: SupportUrl,
homepageURL: BaseUrl,
unwrap: false,
}
let metaPath = ScriptPath(name).meta
if (lstatSync(metaPath).isFile()) {
try {
let args: UserDataMetaPartial = JSON.parse(
readFileSync(metaPath).toString()
)
let key: keyof UserDataMeta
for (key in meta) {
let val = args[key]
//cases where the value is empty
if (val === undefined) continue
if (val === false) continue
if (val === '')
continue
//:)
;(meta[key] as any) = val
}
} catch (e) {
console.log(e)
}
} else {
console.log(`${metaPath} not found, using default metadata`)
}
const keyConversion = {
injectinto: 'inject-into',
excludematch: 'exclude-match',
}
return [
meta,
`// ==UserScript==
${(Object.keys(meta) as Array<keyof UserDataMetaFull>)
.filter(key => {
let val = meta[key]
if (val === undefined) return false
if (val === false) return false
if (val === '') return false
return true
})
.map(key => {
let val = meta[key]
let key_str =
key in keyConversion
? keyConversion[key as keyof typeof keyConversion]
: key
key_str = key_str.padStart(12, ' ')
if (typeof val === 'boolean') {
if (val) return `// @${key_str}`
} else if (typeof val === 'string') {
return `// @${key_str} ${val}`
} else if (Array.isArray(val)) {
return val.map(v => `// @${key_str} ${v}`).join('\n')
}
return ''
})
.filter(l => l.length)
.join('\n')}
// ==/UserScript==
`,
]
}

42
src/types.ts Normal file
View File

@@ -0,0 +1,42 @@
export interface UserDataMetaPartial {
name?: string
namespace?: string
match?: string | string[]
excludematch?: string | string[]
version?: string
description?: string
icon?: string
require?: string | string[]
resource?: string | string[]
runat?: 'document-start' | 'document-end' | 'document-idle' | ''
noframes?: boolean
grant?: string | string[]
injectinto?: 'page' | 'content' | 'auto' | ''
downloadURL?: string
supportURL?: string
homepageURL?: string
unwrap?: boolean
}
export interface UserDataMeta extends UserDataMetaPartial {
name: string
namespace: string
version: string
downloadURL: string
supportURL: string
homepageURL: string
}
export interface UserDataMetaFull extends UserDataMeta {
match: string | string[]
excludematch: string | string[]
description: string
icon: string
require: string | string[]
resource: string | string[]
runat: 'document-start' | 'document-end' | 'document-idle' | ''
noframes: boolean
grant: string | string[]
injectinto: 'page' | 'content' | 'auto' | ''
unwrap: boolean
}