@hhkaos/webmentions-widget 0.5.2 → 0.5.4
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/bin/snapshot.js +39 -6
- package/package.json +1 -1
package/bin/snapshot.js
CHANGED
|
@@ -15,9 +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';
|
|
20
|
-
import {
|
|
21
|
+
import {fileURLToPath} from 'node:url';
|
|
21
22
|
|
|
22
23
|
import {getSnapshotMentions, mergeSnapshot, parseWebmentionJson} from '../src/core.js';
|
|
23
24
|
|
|
@@ -164,12 +165,25 @@ async function main() {
|
|
|
164
165
|
|
|
165
166
|
const before = getSnapshotMentions(existing).length;
|
|
166
167
|
|
|
167
|
-
|
|
168
|
-
|
|
168
|
+
console.log(`Fetched ${collected.length} mention(s) for ${domain}${sinceId ? ` since #${sinceId}` : ''}.`);
|
|
169
|
+
|
|
170
|
+
if (!collected.length) {
|
|
171
|
+
if (args.full) {
|
|
172
|
+
// An authoritative refetch that came back empty is far more likely to be
|
|
173
|
+
// a wrong --domain than a genuinely empty account, so keep what we have.
|
|
174
|
+
console.log('Nothing returned for a full refresh; keeping the current snapshot.');
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
console.log(`Snapshot unchanged (${before}).`);
|
|
169
179
|
return;
|
|
170
180
|
}
|
|
171
181
|
|
|
172
|
-
|
|
182
|
+
// A full refetch is authoritative: replace rather than merge, so mentions
|
|
183
|
+
// collected under a domain we no longer query do not linger forever.
|
|
184
|
+
const snapshot = args.full
|
|
185
|
+
? mergeSnapshot(null, collected)
|
|
186
|
+
: mergeSnapshot(existing, collected);
|
|
173
187
|
|
|
174
188
|
if (snapshot.count === before) {
|
|
175
189
|
console.log(`Snapshot unchanged (${before} mentions).`);
|
|
@@ -182,8 +196,27 @@ async function main() {
|
|
|
182
196
|
console.log(`Snapshot updated: ${before} → ${snapshot.count} mentions (lastId ${snapshot.lastId}).`);
|
|
183
197
|
}
|
|
184
198
|
|
|
185
|
-
|
|
186
|
-
|
|
199
|
+
/**
|
|
200
|
+
* True when this file is the process entry point.
|
|
201
|
+
*
|
|
202
|
+
* Compared through realpath on both sides: npm installs a bin as a symlink in
|
|
203
|
+
* node_modules/.bin, so argv[1] is that link rather than this file, and a
|
|
204
|
+
* naive URL comparison makes the command a silent no-op.
|
|
205
|
+
*/
|
|
206
|
+
function isEntryPoint() {
|
|
207
|
+
if (!process.argv[1]) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
213
|
+
} catch {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Guarded so the helpers above stay importable by tests.
|
|
219
|
+
if (isEntryPoint()) {
|
|
187
220
|
main().catch((error) => {
|
|
188
221
|
console.error(error);
|
|
189
222
|
process.exit(1);
|
package/package.json
CHANGED