This repository has been archived by the owner on Oct 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
121 lines (103 loc) · 2.49 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
#!/usr/bin/env node
'use strict'
const path = require('path')
const fs = require('fs')
const meow = require('meow')
const logSymbols = require('log-symbols')
const GoProMedia = require('./gopro-media')
const MediaFile = require('./media-file')
const cli = meow(
`
Add videos to the GoPro Quik Desktop for Mac app.
Usage
$ npx add-quik-video <videofile>
$ npx add-quik-video <videofile> <videofile>…
Examples
$ npx add-quik-video videos/IMG_1337.mp4
$ npx add-quik-video *.mov
`,
{
flags: {}
}
)
function checkFilesExist(filenamesFiltered) {
filenamesFiltered
.filter(filename => !fs.existsSync(filename))
.forEach(filename => {
console.error(logSymbols.error, 'file not found:', filename)
process.exit(3)
})
}
function ignoreDirectories(filenames) {
const filenamesFiltered = filenames.filter(filename =>
fs.statSync(filename).isFile()
)
return filenamesFiltered
}
async function getAndVerifyFiles(filenames) {
// Stop if file does not exist
checkFilesExist(filenames)
const promises = ignoreDirectories(filenames)
.map(filename => {
// Make path absolute
return path.resolve(filename)
})
.map(async filename => MediaFile.load(filename))
return Promise.all(promises)
}
async function getMetadataAndLogErrors(filenames) {
const files = await getAndVerifyFiles(filenames)
files
.filter(file => !file.isValid())
.forEach(file =>
console.error(
logSymbols.error,
`WARN: File ${file.filename} does not look like a video file`
)
)
return files.filter(file => file.isValid())
}
async function init(args) {
if (args.length === 0) {
cli.showHelp(1)
}
const cleanFiles = await getMetadataAndLogErrors(args)
let media = null
try {
media = new GoProMedia()
await media.init()
await Promise.all(
cleanFiles.map(async file => {
const containsMedia = await media.contains(file)
if (containsMedia) {
console.log(logSymbols.warning, `already in media: ${file.filename}`)
return Promise.resolve()
}
return media
.add(file)
.then(() => console.log(logSymbols.success, 'added', file.filename))
.catch(error => {
console.error(
logSymbols.error,
`WARN: Can not import ${file.filename}.`,
error
)
})
})
)
console.log(
'\n',
logSymbols.info,
'Remember to restart GoPro Quik to see the new media.'
)
} finally {
if (media) {
media.end()
}
MediaFile.end()
}
}
init(cli.input).catch(error => {
console.error(logSymbols.error, error)
process.exit(1)
})