-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
199 lines (183 loc) · 4.82 KB
/
index.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const fs = require("fs-extra");
const userscriptMeta = require("userscript-meta");
const path = require("path");
function packageToMeta(pkg) {
var target = {};
if (pkg.name) {
target.name = pkg.name;
}
if (pkg.title) {
target.name = pkg.title;
}
if (pkg.version) {
target.version = pkg.version;
}
if (pkg.description) {
target.description = pkg.description;
}
if (pkg.homepage) {
target.homepageURL = pkg.homepage;
}
if (pkg.bugs) {
if (typeof pkg.bugs == "string") {
target.supportURL = pkg.bugs;
} else if (pkg.bugs.url) {
target.supportURL = pkg.bugs.url;
}
}
if (pkg.license) {
target.license = pkg.license;
}
if (pkg.author) {
target.author = personToString(pkg.author);
}
if (pkg.contributors) {
target.contributor = [];
for (var contributor of pkg.contributors) {
target.contributor.push(personToString(contributor));
}
}
if (pkg.repository && !target.homepageURL) {
if (typeof pkg.repository == "string") {
target.homepageURL = repositoryToHomepage(pkg.repository);
} else if (pkg.repository.url) {
target.homepageURL = pkg.repository.url;
}
}
var support;
if (typeof pkg.repository == "string" && !target.supportURL && (support = repositoryToSupport(pkg.repository))) {
target.supportURL = support;
}
if (pkg.engines) {
target.compatible = [];
for (var [key, value] of Object.entries(pkg.engines)) {
if (/firefox|chrome|opera|safari/i.test(key)) {
target.compatible.push(`${key} ${value}`);
}
}
}
if (pkg.userscript) {
Object.assign(target, pkg.userscript);
}
return target;
}
exports.packageToMeta = packageToMeta;
function packageFileToMeta(file) {
const pkg = JSON.parse(fs.readFileSync(file));
return packageToMeta(pkg);
}
function personToString(people) {
if (typeof people == "string") {
return people;
}
var out = people.name;
if (people.email) {
out += ` <${people.email}>`;
}
if (people.url) {
out += ` (${people.url})`;
}
return out;
}
var REPOS = {
github: "https://github.com/",
gist: "https://gist.github.com/",
bitbucket: "https://bitbucket.org/",
gitlab: "https://gitlab.com/"
};
function repositoryToHomepage(repository) {
var match = repository.match(/^(?:(gist|bitbucket|gitlab):)?(.+)/);
return REPOS[match[1] || "github"] + match[2];
}
function repositoryToSupport(repository) {
// no issues for gist
if (repository.startsWith("gist:")) {
return null;
}
return repositoryToHomepage(repository) + "/issues";
}
var METADATA_BLOCK_REGEX = /\/\/ ==UserScript==[^]+?\/\/ ==\/UserScript==/i;
exports.METADATA_BLOCK_REGEX = METADATA_BLOCK_REGEX;
function fileToMeta(file) {
var text = fs.readFileSync(file, "utf8"),
match = text.match(METADATA_BLOCK_REGEX);
if (!match) {
throw new Error(`Can't find metadata block from ${file}`);
}
return userscriptMeta.parse(match[0]);
}
exports.fileToMeta = fileToMeta;
function isFile(file) {
try {
fs.accessSync(file);
} catch (err) {
return false;
}
return true;
}
function findPackagePath() {
let dir = path.resolve();
while (!isFile(path.join(dir, "package.json"))) {
const parentDir = path.dirname(dir);
if (parentDir == dir) {
throw new Error("Cannot find package.json");
}
dir = parentDir;
}
return path.join(dir, "package.json");
}
exports.findPackagePath = findPackagePath;
function init(args) {
const meta = getMeta({
findPackage: !args["--no-package"],
readFiles: args["--read"]
});
if (args["--update"]) {
updateFile(args["--update"], meta);
} else {
var out = args["--json"] ? JSON.stringify(meta, null, 2) : userscriptMeta.stringify(meta);
if (args["--output"]) {
fs.outputFileSync(args["--output"], out);
} else {
process.stdout.write(out);
}
}
function updateFile(file, meta) {
var text = fs.readFileSync(file, "utf8"),
match = text.match(METADATA_BLOCK_REGEX),
out = text.slice(0, match.index) + userscriptMeta.stringify(meta).trim() + text.slice(match.index + match[0].length);
fs.outputFileSync(file, out, "utf8");
}
}
exports.init = init;
function getMeta({findPackage = true, readFiles = []} = {}) {
const meta = {};
if (findPackage) {
Object.assign(meta, packageFileToMeta(findPackagePath()));
}
for (const file of readFiles) {
let newMeta;
if (file.endsWith("package.json")) {
newMeta = packageFileToMeta(file);
} else if (file.endsWith(".json")) {
newMeta = JSON.parse(fs.readFileSync(file));
} else {
newMeta = fileToMeta(file);
}
Object.assign(meta, newMeta);
}
if (!meta.grant) {
meta.grant = "none";
}
if (noMatchPattern(meta)) {
meta.include = "*";
}
return meta;
function noMatchPattern(meta) {
return (!meta.include || Array.isArray(meta.include) && !meta.include.length) &&
(!meta.match || Array.isArray(meta.match) && !meta.match.length)
}
}
exports.getMeta = getMeta;
exports.parse = userscriptMeta.parse;
exports.stringify = userscriptMeta.stringify;