Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·273 lines (227 loc) · 7.24 KB
/
Copy pathcli.js
File metadata and controls
executable file
·273 lines (227 loc) · 7.24 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/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: {
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,
},
prune: {
type: "boolean",
default: false,
},
"overwrite-allow": {
type: "string",
default: "",
},
cacheduration: {
type: "string",
default: "24h",
},
format: {
type: "string",
default: "markdown",
},
assetrefs: {
type: "string",
default: "relative",
},
avatarrefs: {
type: "string",
},
assetmaxsize: {
type: "string",
default: "",
},
within: {
type: "string",
default: "",
},
preserve: {
type: "string",
default: "",
},
posttypes: {
type: "string",
default: "",
},
path: {
type: "string",
default: "",
},
limit: {
type: "string",
default: "",
},
until: {
type: "string",
default: "",
},
},
});
let [ type, target ] = positionals;
let { quiet, dryrun, output, help, version, overwrite, prune, cacheduration, format, assetrefs, avatarrefs, assetmaxsize, within, preserve, posttypes, path, limit, until } = 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 locally modified files
npx @11ty/import [type] [target] --overwrite
# Allow draft entries to overwrite existing files (bypasses --overwrite)
npx @11ty/import [type] [target] --overwrite-allow=drafts
# Delete unreferenced asset files (e.g. from earlier imports) in imported post folders
npx @11ty/import [type] [target] --prune
# 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
# Only download assets up to a size (bytes, or KB, MB, GB)
npx @11ty/import [type] [target] --assetmaxsize=10MB
# Change avatar reference URLs: absolute (default), relative, colocate, disabled (default when --assetrefs=disabled)
npx @11ty/import [type] [target] --avatarrefs=relative
# WordPress post types to import (default: posts,pages)
npx @11ty/import wordpress [target] --posttypes=posts,pages,product
npx @11ty/import wordpressxml [file] --posttypes=posts,pages,product
# Import git commit history for files, one data file per file
# Reads a local checkout when there is one, otherwise the GitHub API (optional GITHUB_TOKEN)
npx @11ty/import git . --path=README.md
npx @11ty/import git ~/Code/eleventy --path=src/Eleventy.js,README.md
npx @11ty/import git 11ty/eleventy --path=README.md
# A trailing slash imports every file in a folder
npx @11ty/import git 11ty/11ty-website --path=src/blog/
# Only the newest entries (stops fetching once reached)
npx @11ty/import [type] [target] --limit=5
# Only entries created on or before a year, date, or timestamp prefix (wayback, typepad, and hobix also limit captures)
npx @11ty/import [type] [target] --until=2009
# Import webmention.io webmentions into post data files (requires WEBMENTION_IO_TOKEN)
npx @11ty/import webmentions example.com
# Import Instagram posts and reels (requires INSTAGRAM_ACCESS_TOKEN)
npx @11ty/import instagram zachleat
# Import npm packages for a user or organization (one page per package README)
npx @11ty/import npm zachleat
npx @11ty/import npm @11ty
# Import GitHub releases for a repository, repositories for a user or organization, or public gists for a user (optional GITHUB_TOKEN)
npx @11ty/import github 11ty/eleventy
npx @11ty/import github 11ty
npx @11ty/import github-gist zachleat
# Import your public CodePen pens with their source files (requires CODEPEN_API_KEY)
npx @11ty/import codepen zachleat
# Import recent Letterboxd diary entries, reviews, and lists
npx @11ty/import letterboxd zachleat
# Import public Flickr photos (requires FLICKR_API_KEY)
npx @11ty/import flickr zachleat
# Import public Tumblr posts (requires TUMBLR_API_KEY)
npx @11ty/import tumblr staff
# Import Hacker News stories linking to a site
npx @11ty/import hackernews https://www.11ty.dev
# Import Open Collective contributions an account made to others
npx @11ty/import opencollective 11ty
# Import recent Kickstarter project updates
npx @11ty/import kickstarter https://www.kickstarter.com/projects/elanlee/exploding-kittens
`);
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.setPruneAssets(prune);
try {
importer.addSource(type, posttypes || path || limit || until ? {
url: target,
postTypes: posttypes || undefined,
path: path || undefined,
limit: limit || undefined,
until: until || undefined,
} : target);
} catch(e) {
console.error(e.message);
process.exit(1);
}
// TODO wire these up to CLI
importer.setDraftsFolder("drafts");
importer.setAssetsFolder("assets");
importer.setAssetReferenceType(assetrefs);
try {
importer.setAssetMaxSize(assetmaxsize);
} catch(e) {
console.error(e.message);
process.exit(1);
}
if(avatarrefs) {
importer.setAvatarReferenceType(avatarrefs);
}
// CSS selectors to preserve on markdown conversion
if(preserve) {
importer.addPreserved(preserve);
}
importer.setOverwriteAllow(values['overwrite-allow']);
let entries = await importer.getEntries({
contentType: format,
within,
// Pperformance improvement to avoids parsing html and fetching assets for documents outside of --within (and overwrite rules)
target: "fs",
});
await importer.toFiles(entries);
importer.logResults();
if(importer.isIncomplete()) {
process.exitCode = 1;
}