@hhkaos/webmentions-widget 0.5.1 → 0.5.3

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/bin/snapshot.js +41 -6
  2. package/package.json +1 -1
package/bin/snapshot.js CHANGED
@@ -15,8 +15,10 @@
15
15
  * WEBMENTION_IO_TOKEN, or --token. Domain queries require it.
16
16
  */
17
17
 
18
+ import {realpathSync} from 'node:fs';
18
19
  import {readFile, writeFile, mkdir} from 'node:fs/promises';
19
20
  import {dirname} from 'node:path';
21
+ import {fileURLToPath} from 'node:url';
20
22
 
21
23
  import {getSnapshotMentions, mergeSnapshot, parseWebmentionJson} from '../src/core.js';
22
24
 
@@ -27,7 +29,7 @@ const RETRIES = 8;
27
29
  const RETRY_DELAY_MS = 3000;
28
30
  const MAX_RETRY_DELAY_MS = 30000;
29
31
 
30
- function parseArgs(argv) {
32
+ export function parseArgs(argv) {
31
33
  const args = {};
32
34
 
33
35
  argv.forEach((arg, index) => {
@@ -36,7 +38,18 @@ function parseArgs(argv) {
36
38
  }
37
39
 
38
40
  const [flag, inline] = arg.slice(2).split('=');
39
- args[flag] = inline ?? (argv[index + 1]?.startsWith('--') ? true : argv[index + 1]);
41
+
42
+ if (inline !== undefined) {
43
+ args[flag] = inline;
44
+ return;
45
+ }
46
+
47
+ const next = argv[index + 1];
48
+
49
+ // A flag is boolean when nothing follows it, or when what follows is
50
+ // another flag. `--full` as the last argument is the common case, and
51
+ // reading it as its (missing) value silently disabled it.
52
+ args[flag] = next === undefined || next.startsWith('--') ? true : next;
40
53
  });
41
54
 
42
55
  return args;
@@ -170,7 +183,29 @@ async function main() {
170
183
  console.log(`Snapshot updated: ${before} → ${snapshot.count} mentions (lastId ${snapshot.lastId}).`);
171
184
  }
172
185
 
173
- main().catch((error) => {
174
- console.error(error);
175
- process.exit(1);
176
- });
186
+ /**
187
+ * True when this file is the process entry point.
188
+ *
189
+ * Compared through realpath on both sides: npm installs a bin as a symlink in
190
+ * node_modules/.bin, so argv[1] is that link rather than this file, and a
191
+ * naive URL comparison makes the command a silent no-op.
192
+ */
193
+ function isEntryPoint() {
194
+ if (!process.argv[1]) {
195
+ return false;
196
+ }
197
+
198
+ try {
199
+ return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
200
+ } catch {
201
+ return false;
202
+ }
203
+ }
204
+
205
+ // Guarded so the helpers above stay importable by tests.
206
+ if (isEntryPoint()) {
207
+ main().catch((error) => {
208
+ console.error(error);
209
+ process.exit(1);
210
+ });
211
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hhkaos/webmentions-widget",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "Framework-agnostic, dependency-free widget to fetch and render webmention.io mentions for the current page.",
5
5
  "type": "module",
6
6
  "license": "MIT",