-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·145 lines (120 loc) · 3.09 KB
/
cli.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
import { parseArgs } from "node:util";
import { Importer } from "./src/Importer.js";
import { Logger } from "./src/Logger.js";
import { createRequire } from "node:module";
let { positionals, values } = parseArgs({
allowPositionals: true,
strict: true,
tokens: true,
options: {
type: {
type: "string",
},
target: {
type: "string",
},
output: {
type: "string",
default: ".",
},
quiet: {
type: "boolean",
default: false,
},
dryrun: {
type: "boolean",
default: false,
},
help: {
type: "boolean",
default: false,
},
version: {
type: "boolean",
default: false,
},
overwrite: {
type: "boolean",
default: false,
},
cacheduration: {
type: "string",
default: "24h",
},
format: {
type: "string",
default: "markdown",
},
persist: {
type: "string",
default: "",
},
assetrefs: {
type: "string",
default: "relative",
},
},
});
let [ type, target ] = positionals;
let { quiet, dryrun, output, help, version, overwrite, cacheduration, format, persist, assetrefs } = values;
if(version) {
const require = createRequire(import.meta.url);
let pkg = require("./package.json");
Logger.log(pkg.version);
process.exit();
}
// If you modify this, maybe also add the instructions to README.md too?
if(help) {
Logger.log(`Usage:
npx @11ty/import --help
npx @11ty/import --version
# Import content
npx @11ty/import [type] [target]
# Dry run (don’t write files)
npx @11ty/import [type] [target] --dryrun
# Quietly (limit console output)
npx @11ty/import [type] [target] --quiet
# Change the output folder (default: ".")
npx @11ty/import [type] [target] --output=dist
# Allow overwriting existing files
npx @11ty/import [type] [target] --overwrite
# Change local fetch cache duration (default: 24h)
npx @11ty/import [type] [target] --cacheduration=20m
# Change output format (default: markdown)
npx @11ty/import [type] [target] --format=html
# Change asset reference URLs: relative (default), absolute, colocate, disabled
npx @11ty/import [type] [target] --assetrefs=relative
npx @11ty/import [type] [target] --assetrefs=absolute
npx @11ty/import [type] [target] --assetrefs=colocate
npx @11ty/import [type] [target] --assetrefs=disabled
`);
process.exit();
}
// Input checking
if(!type || !target) {
console.error("Expected usage: npx @11ty/import [type] [target]");
process.exit(1);
} else if(format !== "markdown" && format !== "html") {
console.error("Invalid --format, expected `markdown` or `html`");
process.exit(1);
}
let importer = new Importer();
importer.setOutputFolder(output);
importer.setCacheDuration(cacheduration);
importer.setVerbose(!quiet);
importer.setSafeMode(!overwrite);
importer.setDryRun(dryrun);
importer.addSource(type, target);
// TODO wire these up to CLI
importer.setDraftsFolder("drafts");
importer.setAssetsFolder("assets");
importer.setAssetReferenceType(assetrefs);
if(persist) {
importer.setPersistTarget(persist);
}
let entries = await importer.getEntries({
contentType: format,
});
await importer.toFiles(entries);
importer.logResults();