init
This commit is contained in:
89
lib/readmeta.js
Normal file
89
lib/readmeta.js
Normal file
@@ -0,0 +1,89 @@
|
||||
import { lstatSync, readFileSync } from 'fs';
|
||||
import { BaseUrl, FileUrl, ScriptPath, SupportUrl } from './paths';
|
||||
export default function (name) {
|
||||
var meta = {
|
||||
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 = JSON.parse(readFileSync(metaPath).toString());
|
||||
let key;
|
||||
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] = 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)
|
||||
.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]
|
||||
: 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==
|
||||
`,
|
||||
];
|
||||
}
|
||||
//# sourceMappingURL=readmeta.js.map
|
||||
Reference in New Issue
Block a user