Skip to content

Commit c437c12

Browse files
committed
Adds within CLI option (and argument to getEntries)
1 parent 699b4b5 commit c437c12

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ npx @11ty/import [type] [target] --overwrite
4141
# Change local fetch cache duration (default: 24h)
4242
npx @11ty/import [type] [target] --cacheduration=20m
4343

44+
# Only import entries created (or updated) within a duration (default: *)
45+
# Same syntax as --cacheduration
46+
npx @11ty/import [type] [target] --within=7d
47+
4448
# Change output format (default: markdown)
4549
npx @11ty/import [type] [target] --format=html
4650

@@ -138,6 +142,7 @@ importer.addSource("bluesky", "@11ty.dev");
138142

139143
let entries = await importer.getEntries({
140144
contentType: "markdown", // --format
145+
within: "*", // date or last updated date must be within this recent duration (e.g. 24h, 7d, 1y)
141146
});
142147

143148
await importer.toFiles(entries);

cli.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Importer } from "./src/Importer.js";
66
import { Logger } from "./src/Logger.js";
77
import { createRequire } from "node:module";
88

9+
// "string" or "boolean" types are supported by parseArgs
10+
// https://nodejs.org/docs/latest/api/util.html#utilparseargsconfig
911
let { positionals, values } = parseArgs({
1012
allowPositionals: true,
1113
strict: true,
@@ -57,11 +59,15 @@ let { positionals, values } = parseArgs({
5759
type: "string",
5860
default: "relative",
5961
},
62+
within: {
63+
type: "string",
64+
default: "",
65+
},
6066
},
6167
});
6268

6369
let [ type, target ] = positionals;
64-
let { quiet, dryrun, output, help, version, overwrite, cacheduration, format, persist, assetrefs } = values;
70+
let { quiet, dryrun, output, help, version, overwrite, cacheduration, format, persist, assetrefs, within } = values;
6571

6672
if(version) {
6773
const require = createRequire(import.meta.url);
@@ -137,6 +143,7 @@ if(persist) {
137143

138144
let entries = await importer.getEntries({
139145
contentType: format,
146+
within,
140147
});
141148

142149
await importer.toFiles(entries);

package-lock.json

+16-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"license": "MIT",
3636
"dependencies": {
3737
"@11ty/eleventy-fetch": "^5.0.1",
38+
"@11ty/eleventy-utils": "^2.0.1",
3839
"@11ty/posthtml-urls": "^1.0.0",
3940
"@prettier/sync": "^0.5.2",
4041
"@sindresorhus/slugify": "^2.2.1",

src/Importer.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from "node:path";
2-
import { createRequire } from "node:module";
32
import fs from "graceful-fs";
43
import yaml from "js-yaml";
54
import kleur from "kleur";
65
import slugify from '@sindresorhus/slugify';
76
import * as entities from "entities";
7+
import { DateCompare } from "@11ty/eleventy-utils";
88

99
import { Logger } from "./Logger.js";
1010
import { Fetcher } from "./Fetcher.js";
@@ -22,8 +22,7 @@ import { WordPressApi } from "./DataSource/WordPressApi.js";
2222
import { BlueskyUser } from "./DataSource/BlueskyUser.js";
2323
import { FediverseUser } from "./DataSource/FediverseUser.js";
2424

25-
const require = createRequire(import.meta.url);
26-
const pkg = require("../package.json");
25+
import pkg from "../package.json" with { type: "json" };
2726

2827
// For testing
2928
const MAX_IMPORT_SIZE = 0;
@@ -305,6 +304,26 @@ class Importer {
305304
}
306305
}
307306

307+
// If dateUpdated or date within the options.within option time frame, keeps it
308+
// Otherwise, filtered out
309+
if(options.within) {
310+
entries = entries.filter(entry => {
311+
if(entry.dateUpdated) {
312+
if(DateCompare.isTimestampWithinDuration(entry.dateUpdated.getTime(), options.within)) {
313+
return true;
314+
}
315+
}
316+
317+
if(entry.date) {
318+
if(DateCompare.isTimestampWithinDuration(entry.dateUpdated.getTime(), options.within)) {
319+
return true;
320+
}
321+
}
322+
return false;
323+
})
324+
}
325+
326+
// purely for internals testing
308327
if(MAX_IMPORT_SIZE) {
309328
entries = entries.slice(0, MAX_IMPORT_SIZE);
310329
}

0 commit comments

Comments
 (0)