@url-sanitize/cli 2.0.3 → 2.0.5

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.
Files changed (2) hide show
  1. package/dist/bin.js +135 -165
  2. package/package.json +5 -5
package/dist/bin.js CHANGED
@@ -1,182 +1,152 @@
1
1
  #!/usr/bin/env node
2
-
3
- // src/bin.ts
4
- import { readFileSync } from "fs";
5
- import { createRequire } from "module";
2
+ import { createRequire } from "node:module";
3
+ import { readFileSync } from "node:fs";
6
4
  import { compileSanitizer } from "@url-sanitize/core";
7
5
  import { mergedCatalog, mergedMetadata } from "@url-sanitize/merged";
8
- var args = process.argv.slice(2);
9
- runTypeScriptCli(args);
6
+ //#region src/bin.ts
7
+ runTypeScriptCli(process.argv.slice(2));
10
8
  function runTypeScriptCli(rawArgs) {
11
- if (rawArgs.includes("-h") || rawArgs.includes("--help")) {
12
- printHelp(process.stdout);
13
- process.exit(0);
14
- }
15
- if (rawArgs.includes("--version")) {
16
- process.stdout.write(
17
- `url-sanitize ${packageVersion()} (catalog ${catalogHash()} ${mergedMetadata.generatedAt})
18
- `
19
- );
20
- process.exit(0);
21
- }
22
- const parsed = parseArgs(rawArgs);
23
- if (typeof parsed === "string") {
24
- process.stderr.write(`error: ${parsed}
25
- `);
26
- process.stderr.write("run `url-sanitize --help` for usage\n");
27
- process.exit(1);
28
- }
29
- const sanitize = compileSanitizer(mergedCatalog, parsed.options);
30
- let anyBlocked = false;
31
- for (const url of parsed.urls) {
32
- anyBlocked = emit(sanitize(url), parsed.json, parsed.explain) || anyBlocked;
33
- }
34
- if (parsed.readStdin || parsed.urls.length === 0 && !process.stdin.isTTY) {
35
- const input = readAllStdin();
36
- for (const line of input.split(/\r?\n/)) {
37
- if (line.length === 0) continue;
38
- anyBlocked = emit(sanitize(line), parsed.json, parsed.explain) || anyBlocked;
39
- }
40
- }
41
- if (parsed.urls.length === 0 && !parsed.readStdin && process.stdin.isTTY) {
42
- printHelp(process.stderr);
43
- process.exit(1);
44
- }
45
- process.exit(anyBlocked ? 2 : 0);
9
+ if (rawArgs.includes("-h") || rawArgs.includes("--help")) {
10
+ printHelp(process.stdout);
11
+ process.exit(0);
12
+ }
13
+ if (rawArgs.includes("--version")) {
14
+ process.stdout.write(`url-sanitize ${packageVersion()} (catalog ${catalogHash()} ${mergedMetadata.generatedAt})\n`);
15
+ process.exit(0);
16
+ }
17
+ const parsed = parseArgs(rawArgs);
18
+ if (typeof parsed === "string") {
19
+ process.stderr.write(`error: ${parsed}\n`);
20
+ process.stderr.write("run `url-sanitize --help` for usage\n");
21
+ process.exit(1);
22
+ }
23
+ const sanitize = compileSanitizer(mergedCatalog, parsed.options);
24
+ let anyBlocked = false;
25
+ for (const url of parsed.urls) anyBlocked = emit(sanitize(url), parsed.json, parsed.explain) || anyBlocked;
26
+ if (parsed.readStdin || parsed.urls.length === 0 && !process.stdin.isTTY) {
27
+ const input = readAllStdin();
28
+ for (const line of input.split(/\r?\n/)) {
29
+ if (line.length === 0) continue;
30
+ anyBlocked = emit(sanitize(line), parsed.json, parsed.explain) || anyBlocked;
31
+ }
32
+ }
33
+ if (parsed.urls.length === 0 && !parsed.readStdin && process.stdin.isTTY) {
34
+ printHelp(process.stderr);
35
+ process.exit(1);
36
+ }
37
+ process.exit(anyBlocked ? 2 : 0);
46
38
  }
47
39
  function parseArgs(rawArgs) {
48
- let json = false;
49
- let explain = false;
50
- let stripReferral = false;
51
- let keepReferralExplicit = false;
52
- let unwrapRedirects;
53
- let domainBlocking = false;
54
- let readStdin = false;
55
- const urls = [];
56
- for (const arg of rawArgs) {
57
- switch (arg) {
58
- case "--json":
59
- json = true;
60
- break;
61
- case "--explain":
62
- explain = true;
63
- break;
64
- case "--strip-referral":
65
- stripReferral = true;
66
- break;
67
- case "--keep-referral":
68
- keepReferralExplicit = true;
69
- break;
70
- case "--unwrap-redirects":
71
- unwrapRedirects = true;
72
- break;
73
- case "--no-unwrap-redirects":
74
- unwrapRedirects = false;
75
- break;
76
- case "--block-domains":
77
- domainBlocking = true;
78
- break;
79
- case "-":
80
- readStdin = true;
81
- break;
82
- default:
83
- if (arg.startsWith("--") || arg.startsWith("-") && arg.length > 1) {
84
- return `unknown flag: ${arg}`;
85
- }
86
- urls.push(arg);
87
- }
88
- }
89
- if (keepReferralExplicit) {
90
- stripReferral = false;
91
- }
92
- const options = {
93
- stripReferralMarketing: stripReferral,
94
- domainBlocking
95
- };
96
- if (unwrapRedirects !== void 0) {
97
- options.unwrapRedirects = unwrapRedirects;
98
- }
99
- return {
100
- json,
101
- explain,
102
- options,
103
- readStdin,
104
- urls
105
- };
40
+ let json = false;
41
+ let explain = false;
42
+ let stripReferral = false;
43
+ let keepReferralExplicit = false;
44
+ let unwrapRedirects;
45
+ let domainBlocking = false;
46
+ let readStdin = false;
47
+ const urls = [];
48
+ for (const arg of rawArgs) switch (arg) {
49
+ case "--json":
50
+ json = true;
51
+ break;
52
+ case "--explain":
53
+ explain = true;
54
+ break;
55
+ case "--strip-referral":
56
+ stripReferral = true;
57
+ break;
58
+ case "--keep-referral":
59
+ keepReferralExplicit = true;
60
+ break;
61
+ case "--unwrap-redirects":
62
+ unwrapRedirects = true;
63
+ break;
64
+ case "--no-unwrap-redirects":
65
+ unwrapRedirects = false;
66
+ break;
67
+ case "--block-domains":
68
+ domainBlocking = true;
69
+ break;
70
+ case "-":
71
+ readStdin = true;
72
+ break;
73
+ default:
74
+ if (arg.startsWith("--") || arg.startsWith("-") && arg.length > 1) return `unknown flag: ${arg}`;
75
+ urls.push(arg);
76
+ }
77
+ if (keepReferralExplicit) stripReferral = false;
78
+ const options = {
79
+ stripReferralMarketing: stripReferral,
80
+ domainBlocking
81
+ };
82
+ if (unwrapRedirects !== void 0) options.unwrapRedirects = unwrapRedirects;
83
+ return {
84
+ json,
85
+ explain,
86
+ options,
87
+ readStdin,
88
+ urls
89
+ };
106
90
  }
107
91
  function emit(result, json, explain) {
108
- if (json) {
109
- process.stdout.write(`${JSON.stringify(result)}
110
- `);
111
- return result.kind === "blocked";
112
- }
113
- switch (result.kind) {
114
- case "unchanged":
115
- process.stdout.write(`${result.url}
116
- `);
117
- return false;
118
- case "cleaned":
119
- process.stdout.write(`${result.url}
120
- `);
121
- if (explain) {
122
- const providers = result.matchedRules.map((rule) => rule.provider).join(",");
123
- process.stderr.write(`# cleaned: ${result.original} -> ${result.url} [${providers}]
124
- `);
125
- }
126
- return false;
127
- case "redirected":
128
- process.stdout.write(`${result.url}
129
- `);
130
- if (explain) {
131
- process.stderr.write(
132
- `# redirected: ${result.original} -> ${result.url} [${result.via.provider}]
133
- `
134
- );
135
- }
136
- return false;
137
- case "blocked":
138
- process.stderr.write(`blocked: ${result.original} (via ${result.via.provider})
139
- `);
140
- return true;
141
- }
92
+ if (json) {
93
+ process.stdout.write(`${JSON.stringify(result)}\n`);
94
+ return result.kind === "blocked";
95
+ }
96
+ switch (result.kind) {
97
+ case "unchanged":
98
+ process.stdout.write(`${result.url}\n`);
99
+ return false;
100
+ case "cleaned":
101
+ process.stdout.write(`${result.url}\n`);
102
+ if (explain) {
103
+ const providers = result.matchedRules.map((rule) => rule.provider).join(",");
104
+ process.stderr.write(`# cleaned: ${result.original} -> ${result.url} [${providers}]\n`);
105
+ }
106
+ return false;
107
+ case "redirected":
108
+ process.stdout.write(`${result.url}\n`);
109
+ if (explain) process.stderr.write(`# redirected: ${result.original} -> ${result.url} [${result.via.provider}]\n`);
110
+ return false;
111
+ case "blocked":
112
+ process.stderr.write(`blocked: ${result.original} (via ${result.via.provider})\n`);
113
+ return true;
114
+ }
142
115
  }
143
116
  function readAllStdin() {
144
- return readFileSync(0, "utf8");
117
+ return readFileSync(0, "utf8");
145
118
  }
146
119
  function packageVersion() {
147
- const require2 = createRequire(import.meta.url);
148
- const pkg = require2("../package.json");
149
- return pkg.version ?? "0.0.0";
120
+ return createRequire(import.meta.url)("../package.json").version ?? "0.0.0";
150
121
  }
151
122
  function catalogHash() {
152
- const hashes = mergedCatalog.sources.map((source) => source.hash).filter(Boolean).join(":");
153
- return hashes || mergedCatalog.version;
123
+ return mergedCatalog.sources.map((source) => source.hash).filter(Boolean).join(":") || mergedCatalog.version;
154
124
  }
155
125
  function printHelp(stream) {
156
- stream.write(
157
- [
158
- `url-sanitize ${packageVersion()} (catalog ${catalogHash()} ${mergedMetadata.generatedAt})`,
159
- "",
160
- "Strip tracking parameters from URLs with the merged multi-source catalog.",
161
- "",
162
- "USAGE:",
163
- " url-sanitize [OPTIONS] [URL]...",
164
- " url-sanitize [OPTIONS] - read URLs from stdin (one per line)",
165
- " url-sanitize [OPTIONS] < file same",
166
- "",
167
- "OPTIONS:",
168
- " --json Emit one JSON SanitizeResult per input line",
169
- " --explain Default mode, plus a matched-rule line on stderr",
170
- " --strip-referral Strip affiliate / referral marketing params",
171
- " --keep-referral Explicitly keep referral params (default)",
172
- " --unwrap-redirects Unwrap redirector URLs (default)",
173
- " --no-unwrap-redirects Leave redirector URLs untouched",
174
- " --block-domains Emit blocked for domain-blocked URLs",
175
- " --version Print version + catalog hash/date",
176
- " -h, --help Show this help",
177
- "",
178
- "Runtime: TypeScript",
179
- ""
180
- ].join("\n")
181
- );
126
+ stream.write([
127
+ `url-sanitize ${packageVersion()} (catalog ${catalogHash()} ${mergedMetadata.generatedAt})`,
128
+ "",
129
+ "Strip tracking parameters from URLs with the merged multi-source catalog.",
130
+ "",
131
+ "USAGE:",
132
+ " url-sanitize [OPTIONS] [URL]...",
133
+ " url-sanitize [OPTIONS] - read URLs from stdin (one per line)",
134
+ " url-sanitize [OPTIONS] < file same",
135
+ "",
136
+ "OPTIONS:",
137
+ " --json Emit one JSON SanitizeResult per input line",
138
+ " --explain Default mode, plus a matched-rule line on stderr",
139
+ " --strip-referral Strip affiliate / referral marketing params",
140
+ " --keep-referral Explicitly keep referral params (default)",
141
+ " --unwrap-redirects Unwrap redirector URLs (default)",
142
+ " --no-unwrap-redirects Leave redirector URLs untouched",
143
+ " --block-domains Emit blocked for domain-blocked URLs",
144
+ " --version Print version + catalog hash/date",
145
+ " -h, --help Show this help",
146
+ "",
147
+ "Runtime: TypeScript",
148
+ ""
149
+ ].join("\n"));
182
150
  }
151
+ //#endregion
152
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@url-sanitize/cli",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Command-line tool to remove tracking parameters and unwrap tracking redirects from URLs.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -11,8 +11,8 @@
11
11
  "dist"
12
12
  ],
13
13
  "dependencies": {
14
- "@url-sanitize/core": "2.0.3",
15
- "@url-sanitize/merged": "2.0.3"
14
+ "@url-sanitize/core": "2.0.5",
15
+ "@url-sanitize/merged": "2.0.5"
16
16
  },
17
17
  "keywords": [
18
18
  "cli",
@@ -30,7 +30,7 @@
30
30
  "directory": "packages/cli"
31
31
  },
32
32
  "scripts": {
33
- "build": "tsup src/bin.ts --format esm --clean",
34
- "dev": "tsup src/bin.ts --format esm --watch"
33
+ "build": "vp pack",
34
+ "dev": "vp pack --watch"
35
35
  }
36
36
  }