-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetadata_editor.js
81 lines (72 loc) · 1.88 KB
/
metadata_editor.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
// Copyright 2018 Scriptim (see LICENSE.md)
const nodeExiftool = require('node-exiftool')
const exiftool = new nodeExiftool.ExiftoolProcess(require('dist-exiftool'))
const readlineSync = require('readline-sync')
const chalk = require('chalk')
const metatags = [
'FileType',
'Title',
'Author',
'Subject',
'CreateDate',
'FileModifyDate',
'Creator',
'Producer',
'Keywords'
]
const useFile = (filename, args) => {
process.stdout.write(`Editing file ${chalk.bold(filename)}\n`)
exiftool
.open()
.then(() => filename)
.then(readMetadata)
.then(requestMetadata)
.then(data => writeMetadata(data, args))
.then(() => exiftool.close())
.catch(err => {
process.stderr.write(chalk.red.bold(err.message, '\n'))
process.exit(1)
})
}
const readMetadata = filename => {
return metadata = exiftool.readMetadata(filename, metatags)
.then(metadata => {
if (metadata.error !== null) {
throw new Error(metadata.error)
}
if (metadata.data[0].FileType !== 'PDF') {
throw new Error(`File is not a pdf file: ${filename}`)
}
return metadata.data[0]
})
}
const requestMetadata = metadata => {
const newMetadata = {
OutputFile: metadata.SourceFile
}
for (let tag of metatags) {
if (tag === 'FileType') {
continue
}
const options = {}
let question = `${tag}: `
if (metadata[tag]) {
options.defaultInput = metadata[tag]
question = tag + ' ' + chalk.dim(`[${metadata[tag]}]`) + ': '
}
const answer = readlineSync.question(question, options)
newMetadata[tag] = answer
}
return newMetadata
}
const writeMetadata = (data, args) => {
const metadata = {}
for (let tag of metatags) {
if (tag === 'FileType') {
continue
}
metadata[tag] = data[tag]
}
return exiftool.writeMetadata(data.OutputFile, metadata, args)
}
module.exports = { useFile }