-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·170 lines (142 loc) · 3.7 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/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";
// "string" or "boolean" types are supported by parseArgs
// https://nodejs.org/docs/latest/api/util.html#utilparseargsconfig
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,
},
"overwrite-allow": {
type: "string",
default: "",
},
cacheduration: {
type: "string",
default: "24h",
},
format: {
type: "string",
default: "markdown",
},
persist: {
type: "string",
default: "",
},
assetrefs: {
type: "string",
default: "relative",
},
within: {
type: "string",
default: "",
},
preserve: {
type: "string",
default: "",
}
},
});
let [ type, target ] = positionals;
let { quiet, dryrun, output, help, version, overwrite, cacheduration, format, persist, assetrefs, within, preserve } = 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
# Allow draft entries to overwrite existing files (bypasses --overwrite)
npx @11ty/import [type] [target] --overwrite-allow=drafts
# 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);
// CSS selectors to preserve on markdown conversion
if(preserve) {
importer.addPreserved(preserve);
}
if(persist) {
importer.setPersistTarget(persist);
}
importer.setOverwriteAllow(values['overwrite-allow']);
let entries = await importer.getEntries({
contentType: format,
within,
});
await importer.toFiles(entries);
importer.logResults();