-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
107 lines (88 loc) · 2.77 KB
/
mod.ts
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
import {bundler, path} from './deps.ts';
import {
exitWithMessage,
findMetablockPath,
requestPermission,
statsIfExists,
} from './utils.ts';
// @deno-types="./metablock/index.d.ts"
import {getMetablockEntries} from './metablock/index.js';
export {getMetablockEntries};
export type BundleInfo = {
bundle: string;
/**
* Calling `metablockEntries.toString()` will return a formatted metablock
* string.
*/
metablockEntries: ReturnType<typeof getMetablockEntries>;
path: string;
};
export async function bundleUserscript (
entrypointPath: string,
options?: Pick<bundler.BundleOptions, 'logDiagnostics'>,
): Promise<BundleInfo> {
const parsedPath = path.parse(path.normalize(entrypointPath));
const sourceDir = parsedPath.dir || '.';
await requestPermission(
{name: 'read', path: sourceDir},
'Read entrypoint file and find metablock file',
);
const stats = await statsIfExists(entrypointPath);
if (!(stats.exists && stats.isFile)) {
throw new Error(`The entrypoint argument provided is not a file:\n${entrypointPath}`);
}
let bundleName = '';
for (const suffix of [
'.user.ts',
'.user.js',
'.ts',
'.js',
]) {
if (parsedPath.base.endsWith(suffix)) {
const base = parsedPath.base.slice(0, suffix.length * -1);
bundleName = `${base}.bundle.user.js`;
break;
}
}
if (!bundleName) bundleName = `${parsedPath.base}.bundle.user.js`;
const bundlePath = path.join(sourceDir, bundleName);
const defaultMetablockValues: NonNullable<Parameters<typeof getMetablockEntries>[0]>['override'] = {
grant: 'none',
match: 'https://*/*',
name: 'Untitled userscript',
namespace: 'none',
noframes: true,
'run-at': 'document-idle',
version: '0.1.0',
};
const metablockFilePath = await findMetablockPath(sourceDir);
const metablockOptions: Parameters<typeof getMetablockEntries>[0] = {
file: metablockFilePath || '',
manager: 'tampermonkey',
override: metablockFilePath ? undefined : defaultMetablockValues,
};
const metablockEntries = getMetablockEntries(metablockOptions);
const metablock = metablockEntries.toString();
const {bundle} = await bundler.bundleModule(entrypointPath, {
bundleType: 'classic',
header: metablock,
logDiagnostics: options?.logDiagnostics ?? true,
});
await requestPermission(
{name: 'write', path: bundlePath},
'Write userscript file',
);
await Deno.writeTextFile(bundlePath, bundle);
return {
bundle,
metablockEntries,
path: bundlePath,
};
}
async function main () {
const [entrypointPath] = Deno.args;
if (!entrypointPath) exitWithMessage(1, 'No entrypoint argument provided');
const info = await bundleUserscript(entrypointPath);
console.log(`"${info.path}" written`);
}
if (import.meta.main) main();