-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.js
84 lines (82 loc) · 3.07 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
const fs = require('fs')
const path = require('path')
const yargs = require('yargs')
const TRIGGER_DEFAULT_VALUE_FOR_PATH_OPTION = 'non-existing-value-to-know-that-path-was-not-given'
module.exports = yargs
.example('$0 --token XYZ --target src/l10n/{sheet}/{locale}.js --moduleType AMD --sheets my_sheet,other_sheet',
'download sheets "my_sheet" and "other_sheet" from document with token "XYZ" using AMD module system. ' +
'If your sheet "my_sheet" contained "en" translations, you should see them in src/l10n/my_sheet/en.js')
.option('token', {
demandOption: true,
describe: 'Document token (can be found in the URL)',
type: 'string'
})
.option('sheets', {
demandOption: true,
describe: 'Coma-separated list of sheets to download',
type: 'string',
coerce: arg => arg ? arg.split(',') : arg
})
.option('target', {
demandOption: true,
describe: 'Path where to store downloaded table sheets, use {locale} and {sheet} variables',
type: 'string'
})
.option('muteEslintQuotes', {
demandOption: false,
describe: 'Whether to remove "eslint quotes: 0" from the top of generated files',
type: 'boolean'
})
.option('moduleType', {
demandOption: true,
describe: 'Type of module system used in downloaded table sheets',
choices: ['AMD', 'ESM', 'JSON'],
type: 'string'
})
.option('auth', {
default: TRIGGER_DEFAULT_VALUE_FOR_PATH_OPTION,
demandOption: false,
describe: 'Path to auth.json file that has "private_key" and "client_email" fields. This file can be downloaded from Google Console (see docs).',
type: 'string',
coerce: value => {
const authPath = path.join(process.cwd(), value)
try {
fs.accessSync(authPath, fs.constants.R_OK)
return require(authPath)
} catch (_) {
if (value !== TRIGGER_DEFAULT_VALUE_FOR_PATH_OPTION) {
console.log(`Error! File "${value}" is not readable.`)
}
}
}
})
.option('customOptions', {
default: TRIGGER_DEFAULT_VALUE_FOR_PATH_OPTION,
demandOption: false,
describe: 'Path to module with custom options',
type: 'string',
coerce: pathToCustomOptions => {
const defaultCustomOptions = require(path.join(__dirname, 'customOptions.default.js'))
const userCustomFnPath = path.join(process.cwd(), pathToCustomOptions)
try {
fs.accessSync(userCustomFnPath, fs.constants.R_OK)
return {
...defaultCustomOptions,
...require(userCustomFnPath)
}
} catch (_) {
if (pathToCustomOptions !== TRIGGER_DEFAULT_VALUE_FOR_PATH_OPTION) {
console.log(`Error! Custom options file "${userCustomFnPath}" is not readable, using default options.`)
}
return defaultCustomOptions
}
}
})
.option('logLevel', {
default: 'info',
describe: 'Choose the log level.',
choices: ['info', 'debug', 'none (not supported yet)'],
type: 'string'
})
.help()
.argv