Repository navigation
Expand file tree
/
Copy pathimport.js
More file actions
49 lines (43 loc) · 1.65 KB
/
Copy pathimport.js
File metadata and controls
49 lines (43 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Importer } from "@11ty/import";
import config from "./import.config.js";
// Optional source types to import, e.g. `npm run import -- github npm`
const sources = process.argv.slice(2).filter(arg => !arg.startsWith("--"));
// Only import recent entries, e.g. `npm run import -- --within=*` for everything
const within = process.argv.find(arg => arg.startsWith("--within="))?.slice("--within=".length) || "90d";
async function runImport(type, target, output) {
if(sources.length && !sources.includes(type)) {
return;
}
let importer = new Importer();
importer.setOutputFolder(output);
importer.setCacheDuration("4h");
importer.setVerbose(true);
importer.setSafeMode(!process.argv.includes("--overwrite"));
importer.setDraftsFolder("drafts");
importer.setAssetReferenceType("colocate");
importer.setAvatarReferenceType("colocate");
// Larger assets (mostly video) keep their remote URL
importer.setAssetMaxSize("25MB");
importer.setPruneAssets(process.argv.includes("--prune"));
importer.setOverwriteAllow();
importer.addSource(type, target);
let entries = await importer.getEntries({
contentType: "markdown",
target: "fs",
within,
});
await importer.toFiles(entries);
importer.logResults();
}
let failures = [];
for(let { type, target, output } of config) {
try {
await runImport(type, target, output);
} catch(error) {
failures.push({ type, target, error });
console.error(`[import] ${type} ${JSON.stringify(target)} failed:`, error);
}
}
if(failures.length) {
console.error(`[import] ${failures.length} source(s) failed:\n${failures.map(({ type, target, error }) => `- ${type} ${JSON.stringify(target)}: ${error.message}`).join("\n")}`);
}