-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.js
295 lines (271 loc) · 8.72 KB
/
options.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import Path from 'path';
import os from 'os';
import Package from '../package.json';
import yargs from 'yargs';
import gitVer from 'git-rev-sync';
import fs from 'fs-extra';
import requireg from 'requireg';
import chalk from 'chalk';
import {
Module,
Logger
} from 'nmmes-backend';
const cliSpecificOptions = {
'version': {
describe: 'Displays version information.',
group: 'General:',
type: 'boolean',
default: false
},
'help': {
describe: 'Displays help page.',
group: 'General:',
type: 'boolean',
default: false
},
// 'log-file': {
// default: '',
// describe: 'Sets the log file location for all output from h265ize.',
// type: 'string',
// normalize: true,
// group: 'General:'
// },
'profile': {
default: 'none',
describe: 'Url or name of an encoding preset.',
type: 'string',
group: 'General:'
},
'g': {
alias: 'temp-directory',
default: Path.resolve(os.tmpdir(), Package.name),
describe: 'Folder where files are stored during encoding.',
type: 'string',
normalize: true,
group: 'General:'
},
'd': {
alias: 'destination',
default: Path.resolve(process.cwd(), 'nmmes-out'),
describe: 'Folder where encoded files are output.',
type: 'string',
normalize: true,
group: 'General:'
},
// 'debug': {
// default: false,
// describe: 'Enables debug mode. Prints extra debugging information.',
// type: 'boolean',
// group: 'Advanced:'
// },
'delete': {
default: false,
describe: 'Delete source after encoding is complete and replaces it with new encode. [DANGER]',
type: 'boolean',
group: 'Advanced:'
},
'modules': {
describe: 'List of modules to enable',
group: 'General:',
type: 'array',
default: ['normalize', 'he-audio', 'encoder']
},
'f': {
alias: 'output-format',
default: 'mkv',
describe: 'Output container format.',
choices: ['mkv', 'mp4', 'm4v'],
type: 'string',
group: 'General:'
},
's': {
alias: 'skip-video-codec',
default: [],
describe: 'Skips videos already encoded with a specific video codecs.',
type: 'array',
group: 'General:'
},
'watch': {
default: false,
describe: 'Watches a directory for new video files to be converted.',
type: 'boolean',
group: 'Advanced:'
},
};
export function getVersion() {
if (fs.existsSync('node_modules')) {
let path = Path.resolve(fs.realpathSync('node_modules'), '../');
let version = gitVer.branch(path) + '#' + gitVer.short(path);
return {
version: Package.version,
formatted: `(Development Build) ${version}`
};
}
return {
version: Package.version,
formatted: Package.version
};
}
export default async function load() {
let cliArgs = yargs
.version(false)
.help(false)
.usage('Usage: $0 [options] file|directory')
.options(cliSpecificOptions).argv;
if (cliArgs.version) {
console.log(`${Package.name} ${getVersion().formatted}`);
process.exit();
}
if (cliArgs.debug) {
Logger.setLevel('trace');
}
let modules = await loadModules(cliArgs.modules);
let options = await extractModuleOptions(modules);
if (cliArgs.profile) {
let profile = await getProfile(cliArgs.profile);
// QUESTION: Why doesn't Object.entries(profile.options) work when profile.options = {}?
for (let [key, value] of Object.entries(profile.options || {})) {
if (!options[key]) {
Logger.warn(`Option ${key} is not associated with an activated module.`);
continue;
}
options[key].default = value;
options[key].defaultSetBy = 'profile';
}
}
let moduleArgs = yargs
.version(false)
.help(false)
.strict()
.usage('Usage: $0 file|directory [options]')
.options(options).argv;
if (moduleArgs.help || moduleArgs._.length < 1) {
console.log('Package:', Package.name, '\t', 'Version:', getVersion().formatted);
console.log('Description:', Package.description);
yargs.showHelp();
process.exit();
}
return {
args: moduleArgs,
modules
};
}
async function loadModules(modules) {
let loadedModules = {};
for (let module of modules) {
let mName = `nmmes-module-${module}`;
loadedModules[module] = await requireModule(mName);
}
return loadedModules;
}
async function requireModule(name) {
if (process.env.NODE_ENV === 'development') {
try {
let mod = requireg(Path.join(os.homedir(), '.config/yarn/link', name));
Logger.trace(`Module ${name} loaded from link directory.`);
return mod;
} catch (e) {
Logger.trace("Could not load module from link directory.", e);
}
}
try {
let mod = requireg(name);
Logger.trace(`Loaded module ${name}.`);
return mod;
} catch (e) {
Logger.trace(`Unable to require ${name}:`, e);
throw new Error(`Could not load ${name}@${Module.MODULE_VERSION}. Try installing it via "npm install --global ${name}@${Module.MODULE_VERSION}".`);
}
}
async function extractModuleOptions(modules) {
let options = {};
for (let [mName, mod] of Object.entries(modules)) {
let ops = mod.options();
for (let name of Object.keys(ops)) {
Object.defineProperty(ops, `${mName}-${name}`,
Object.getOwnPropertyDescriptor(ops, name));
delete ops[name];
}
Object.assign(options, ops);
}
return options;
}
import isUrl from 'is-url';
import rp from 'request-promise-native';
export function localProfiles() {
let profiles = require.context('./profiles/', true, /\.json$/).keys();
for (let idx in profiles) {
profiles[idx] = Path.basename(profiles[idx], '.json');
}
return profiles;
}
async function getProfile(profileLocation) {
if (profileLocation === 'none')
return {};
if (~localProfiles().indexOf(profileLocation)) {
Logger.debug(`Built in profile "${profileLocation}" found!`);
let profile = require('./profiles/' + profileLocation + '.json');
return profile;
}
if (isUrl(profileLocation))
try {
return await rp({
uri: profileLocation,
json: true
});
} catch (e) {
throw new Error(`Error retreiving profile: ${e.message}`);
}
if (fs.existsSync(profileLocation)) {
return fs.readFile(profileLocation, (err, data) => {
if (err)
throw err;
return JSON.parse(data);
});
}
Logger.info('Availiable local profiles are: [', chalk.bold(localProfiles().join(', ')), ']');
Logger.info(`You may activate a profile via it's local name or a url to a profile json file.`);
Logger.info(`Examples:
\t${Package.name} --profile anime my/movies/folder
\t${Package.name} --profile https://raw.githubusercontent.com/NMMES/nmmes-cli/master/src/profiles/anime.json`)
throw new Error(`Profile ${profileLocation} not found.`);
}
// const options = {
// // 'x': {
// // alias: 'extra-options',
// // default: '',
// // describe: 'Options provided directly to the encoder.',
// // type: 'string',
// // group: 'Advanced:'
// // },
// // 'video-bitrate': {
// // default: 0,
// // describe: 'Sets the video bitrate, set to 0 to use qp rate control instead of a target bitrate.',
// // type: 'number',
// // group: 'Video:'
// // },
// // 'accurate-timestamps': {
// // default: false,
// // describe: 'Become blu-ray complient and reduce the max keyInt to the average frame rate.',
// // type: 'boolean',
// // group: 'Video:'
// // },
// // 'multi-pass': {
// // default: 0,
// // describe: 'Enable multiple passes by the encoder. Must be greater than 1.',
// // type: 'number',
// // group: 'Video:'
// // },
// 'screenshots': {
// default: 0,
// describe: 'Take n screenshots at regular intervals throughout the finished encode.',
// type: 'number',
// group: 'Video:'
// },
// 'stats': {
// default: false,
// describe: 'Output a stats file containing stats to this destination.',
// type: 'string',
// group: 'Advanced:'
// },
// };