-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-header.cjs
More file actions
42 lines (36 loc) · 1.14 KB
/
Copy pathparse-header.cjs
File metadata and controls
42 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const fs = require('fs');
const path = require('path');
/**
* Robust UserScript Header Parser
*/
function parseUserScriptHeader(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
const header = {};
// Match all @key value lines
const regex = /@(\w+)\s+(.+)/g;
let match;
while ((match = regex.exec(content)) !== null) {
const key = match[1].toLowerCase();
const value = match[2].trim();
header[key] = value;
}
// Fallbacks
if (!header.name) {
// Remove .user.js or .js and replace dashes with spaces
header.name = path.basename(filePath)
.replace(/\.user\.js$/i, '')
.replace(/\.js$/i, '')
.replace(/-/g, ' ');
}
if (!header.description) header.description = 'No description provided';
if (!header.author) header.author = 'Unknown';
if (!header.version) header.version = '1.0.0';
return {
name: header.name,
description: header.description,
author: header.author,
version: header.version,
filename: path.basename(filePath)
};
}
module.exports = { parseUserScriptHeader };