tm-serve 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mrsekut
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # tm-serve
2
+
3
+ Serve local `.user.js` files for Tampermonkey installation.
4
+
5
+ ## Usage
6
+
7
+ ```bash
8
+ bunx tm-serve ./my-script.user.js
9
+ ```
10
+
11
+ This will:
12
+
13
+ 1. Create a template `.user.js` file if it doesn't exist
14
+ 2. Start an HTTP server serving your userscript
15
+ 3. Open your browser to the script URL
16
+ 4. Tampermonkey automatically shows the install dialog
17
+ 5. Watch for file changes and reload on save
18
+
19
+ ## Options
20
+
21
+ - First argument: path to `.user.js` file (default: `script.user.js`)
22
+ - `PORT` env var: server port (default: `4889`)
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "tm-serve",
3
+ "version": "0.1.0",
4
+ "description": "Serve local .user.js files for Tampermonkey installation",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/mrsekut/tm-serve.git"
9
+ },
10
+ "type": "module",
11
+ "bin": {
12
+ "tm-serve": "src/cli.ts"
13
+ },
14
+ "files": [
15
+ "src",
16
+ "!src/**/*.test.ts"
17
+ ],
18
+ "devDependencies": {
19
+ "@types/bun": "latest"
20
+ },
21
+ "peerDependencies": {
22
+ "typescript": "^5"
23
+ },
24
+ "dependencies": {
25
+ "open": "^11.0.0"
26
+ }
27
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { resolve } from 'path';
4
+ import open from 'open';
5
+ import { generateTemplate } from './template';
6
+ import { startServer } from './server';
7
+ import { watchScript } from './watcher';
8
+
9
+ const DEFAULT_SCRIPT_NAME = 'script.user.js';
10
+ const DEFAULT_PORT = 4889;
11
+
12
+ main();
13
+
14
+ function parseArgs(argv: string[]): { scriptPath: string; port: number } {
15
+ const args = argv.slice(2);
16
+ const scriptPath = resolve(args[0] ?? DEFAULT_SCRIPT_NAME);
17
+ const port = Number(process.env['PORT']) || DEFAULT_PORT;
18
+ return { scriptPath, port };
19
+ }
20
+
21
+ async function main() {
22
+ const { scriptPath, port } = parseArgs(process.argv);
23
+
24
+ await ensureScript(scriptPath);
25
+
26
+ let scriptContent = await Bun.file(scriptPath).text();
27
+
28
+ const server = startServer(scriptPath, port, async () => scriptContent);
29
+
30
+ watchScript(scriptPath, async () => {
31
+ scriptContent = await Bun.file(scriptPath).text();
32
+ });
33
+
34
+ const scriptFileName = scriptPath.split('/').pop() ?? 'script.user.js';
35
+ const scriptUrl = `http://127.0.0.1:${port}/${scriptFileName}`;
36
+
37
+ try {
38
+ await open(scriptUrl);
39
+ } catch {
40
+ console.log(`Open ${scriptUrl} in your browser to install the script.`);
41
+ }
42
+
43
+ process.on('SIGINT', () => {
44
+ console.log('\nShutting down...');
45
+ server.stop();
46
+ process.exit(0);
47
+ });
48
+ }
49
+
50
+ async function ensureScript(scriptPath: string): Promise<void> {
51
+ const file = Bun.file(scriptPath);
52
+ if (!(await file.exists())) {
53
+ await Bun.write(scriptPath, generateTemplate());
54
+ console.log(`Created template: ${scriptPath}`);
55
+ }
56
+ }
package/src/server.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { buildIndexHtml } from './template';
2
+
3
+ export type ScriptProvider = () => Promise<string>;
4
+
5
+ export function startServer(
6
+ scriptPath: string,
7
+ port: number,
8
+ getScript: ScriptProvider,
9
+ ): ReturnType<typeof Bun.serve> {
10
+ const scriptFileName = scriptPath.split('/').pop() ?? 'script.user.js';
11
+ const scriptUrl = `http://127.0.0.1:${port}/${scriptFileName}`;
12
+
13
+ const server = Bun.serve({
14
+ port,
15
+ hostname: '127.0.0.1',
16
+ routes: {
17
+ '/': new Response(buildIndexHtml(scriptUrl, scriptPath), {
18
+ headers: { 'Content-Type': 'text/html; charset=utf-8' },
19
+ }),
20
+ [`/${scriptFileName}`]: async () => {
21
+ const content = await getScript();
22
+ return new Response(content, {
23
+ headers: {
24
+ 'Content-Type': 'text/javascript; charset=utf-8',
25
+ 'Cache-Control': 'no-cache, no-store, must-revalidate',
26
+ },
27
+ });
28
+ },
29
+ },
30
+ });
31
+
32
+ console.log(`Serving ${scriptPath} at ${scriptUrl}`);
33
+ return server;
34
+ }
@@ -0,0 +1,25 @@
1
+ const METADATA_TEMPLATE = `// ==UserScript==
2
+ // @name my-script
3
+ // @namespace http://tampermonkey.net/
4
+ // @version 0.0.1
5
+ // @description A new userscript
6
+ // @match *://*/*
7
+ // @grant none
8
+ // ==/UserScript==
9
+ `;
10
+
11
+ export function generateTemplate(): string {
12
+ return METADATA_TEMPLATE;
13
+ }
14
+
15
+ export function buildIndexHtml(scriptUrl: string, scriptPath: string): string {
16
+ return `<!DOCTYPE html>
17
+ <html>
18
+ <head><meta charset="utf-8"><title>tm-serve</title></head>
19
+ <body>
20
+ <h1>tm-serve</h1>
21
+ <p>Monitoring: <code>${scriptPath}</code></p>
22
+ <p>Install: <a href="${scriptUrl}">${scriptUrl}</a></p>
23
+ </body>
24
+ </html>`;
25
+ }
package/src/watcher.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { watch, type FSWatcher } from 'fs';
2
+
3
+ export function watchScript(
4
+ scriptPath: string,
5
+ onReload: () => void,
6
+ ): FSWatcher {
7
+ const watcher = watch(scriptPath, event => {
8
+ if (event === 'change') {
9
+ const time = new Date().toLocaleTimeString();
10
+ console.log(`[${time}] Reloaded ${scriptPath}`);
11
+ onReload();
12
+ }
13
+ });
14
+
15
+ watcher.on('error', err => {
16
+ console.error(`Watch error: ${err.message}`);
17
+ });
18
+
19
+ return watcher;
20
+ }