forked from harvard-lil/js-wacz
/
cli.js
executable file
·165 lines (144 loc) · 4.81 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
#! /usr/bin/env node
import { createReadStream } from 'fs'
import fs from 'fs/promises'
import { resolve } from 'path'
import * as readline from 'node:readline/promises'
import log from 'loglevel'
import logPrefix from 'loglevel-plugin-prefix'
import { Command } from 'commander'
import { WACZ } from '../index.js'
import { PACKAGE_INFO, LOGGING_COLORS } from '../constants.js'
const program = new Command()
/**
* Program info
* @type {Command}
*/
program
.name(PACKAGE_INFO.name)
.description(PACKAGE_INFO.description)
.version(PACKAGE_INFO.version, '-v, --version')
/**
* `create` command
* @type {Command}
*/
program.command('create')
.description('Creates a .wacz file out of one or multiple .warc or .warc.gz files.')
.option(
'-f --file <string>',
'Path to .warc / .warc.gz file(s) to process. Wrap in quotation marks if glob.')
.option(
'-o --output <string>',
'Path to output .wacz file.', 'archive.wacz')
.option(
'-p --pages <string>',
'Path to a directory of pages JSONL files to copy into WACZ as-is. ' +
'If --pages is not provided, js-wacz will attempt to detect pages.')
.option(
'--url <string>',
'If provided, will be used as the "main page url" in datapackage.json.')
.option(
'--ts <string>',
'If provided, will be used as the "main page date" in datapackage.json.')
.option(
'--title <string>',
'If provided, will be used as the collection title.')
.option(
'--desc <string>',
'If provided, will be used as the collection description.')
.option(
'--signing-url <string>',
'URL of an authsign-compatible server to be used to cryptographically sign the archive. ' +
'See https://github.com/webrecorder/authsign.')
.option(
'--signing-token <string>',
'Required if the server at --signing-url requires an authentication token.')
.option(
'--log-level <string>',
'Can be "silent", "trace", "debug", "info", "warn", "error"', 'info')
.option('--cdxj <string>',
'Path to a directory containing CDXJ indices to merge into final WACZ CDXJ. ' +
'If not provided, js-wacz will reindex from WARCS. Must be used in combination ' +
'with --pages, since using this option will skip reading the WARC files.')
.action(async (name, options, command) => {
/** @type {Object} */
const values = options._optionValues
/** @type {?WACZ} */
let archive = null
// Set log output level and formatting
logPrefix.reg(log)
logPrefix.apply(log, {
format (level, _name, timestamp) {
const timestampColor = LOGGING_COLORS.DEFAULT
const msgColor = LOGGING_COLORS[level.toUpperCase()]
return `${timestampColor(`[${timestamp}]`)} ${msgColor(level)}`
}
})
if (values?.logLevel) {
let level = 'info'
if (['silent', 'trace', 'debug', 'info', 'warn', 'error'].includes(values.logLevel)) {
level = values.logLevel
}
log.setLevel(level)
log.info(`Log output level as been set to ${level}.`)
}
// `--file` is mandatory
if (!values?.file) {
console.error('Error: --file not provided.')
return
}
if (values?.cdxj && !values?.pages) {
console.error('Error: --cdxj option must be used in combination with --pages.')
return
}
// Pass options to WACZ
try {
archive = new WACZ({
input: values.file,
output: values?.output,
url: values?.url,
ts: values?.ts,
title: values?.title,
description: values?.desc,
signingUrl: values?.signingUrl,
signingToken: values?.signingToken,
pages: values?.pages,
log
})
} catch (err) {
log.error(`${err}`) // Show simplified report
return
}
// Ingest user-provided CDX files, if any.
if (values?.cdxj) {
try {
const dirPath = values?.cdxj
const cdxjFiles = await fs.readdir(dirPath)
const allowedExts = ['cdx', 'cdxj']
for (let i = 0; i < cdxjFiles.length; i++) {
const cdxjFile = resolve(dirPath, cdxjFiles[i])
const ext = cdxjFile.split('.').pop()
if (!allowedExts.includes(ext)) {
log.warn(`CDXJ: Skipping file ${cdxjFile}, not a CDXJ file`)
continue
}
log.info(`CDXJ: Reading entries from ${cdxjFile}`)
const rl = readline.createInterface({ input: createReadStream(cdxjFile) })
for await (const line of rl) {
archive.addCDXJ(line + '\n')
}
}
} catch (err) {
log.trace(err)
log.error('An error occurred while processing user-provided CDXJ indices.')
}
}
// Main process
try {
await archive.process()
log.info(`WACZ file ready: ${values.output}`)
} catch (err) {
log.error(err)
log.error('WACZ could not be processed.')
}
})
program.parse()