init
This commit is contained in:
93
src/readmeta.ts
Normal file
93
src/readmeta.ts
Normal 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==
|
||||
`,
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user