UNPKG

3.23 kBJavaScriptView Raw
1import fs from 'fs'
2import { getCredentials } from '@commercetools/get-credentials'
3import pino from 'pino'
4import PrettyError from 'pretty-error'
5import yargs from 'yargs'
6
7import CustomerGroupsExporter from './main'
8import { description } from '../package.json'
9
10process.title = 'customer-groups-exporter'
11
12const args = yargs
13 .usage(
14 `
15Usage: $0 [options]
16${description}`
17 )
18 .showHelpOnFail(false)
19 .option('output', {
20 alias: 'o',
21 default: 'stdout',
22 describe: 'Path to output file.',
23 })
24 .coerce('output', arg => {
25 if (arg !== 'stdout') return fs.createWriteStream(String(arg))
26
27 return process.stdout
28 })
29 .option('apiUrl', {
30 default: 'https://api.sphere.io',
31 describe: 'The host URL of the HTTP API service.',
32 })
33 .option('authUrl', {
34 default: 'https://auth.sphere.io',
35 describe: 'The host URL of the OAuth API service.',
36 })
37 .option('accessToken', {
38 describe: 'CTP client access token.',
39 })
40 .option('projectKey', {
41 alias: 'p',
42 describe: 'API project key.',
43 demand: true,
44 })
45 .option('where', {
46 alias: 'w',
47 describe: 'Specify where predicate.',
48 })
49 .option('logLevel', {
50 default: 'info',
51 describe: 'Logging level: error, warn, info or debug.',
52 })
53 .option('prettyLogs ', {
54 describe: 'Pretty print logs to the terminal',
55 type: 'boolean',
56 })
57 .option('logFile', {
58 default: 'customer-groups-export.log',
59 describe: 'Path to where to save logs file.',
60 type: 'string',
61 }).argv
62
63// instantiate logger
64const loggerConfig = {
65 level: args.logLevel,
66 prettyPrint: args.prettyLogs,
67}
68
69// If the stdout is used for a data output, save all logs to a log file.
70// pino writes logs to stdout by default
71let logDestination
72if (args.output === process.stdout)
73 logDestination = fs.createWriteStream(args.logFile)
74
75const logger = pino(loggerConfig, logDestination)
76
77// print errors to stderr if we use stdout for data output
78// if we save data to output file errors are already logged by pino
79const logError = error => {
80 const errorFormatter = new PrettyError()
81
82 if (args.logLevel === 'debug')
83 process.stderr.write(`ERR: ${errorFormatter.render(error)}`)
84 else process.stderr.write(`ERR: ${error.message || error}`)
85}
86
87const errorHandler = errors => {
88 if (Array.isArray(errors)) errors.forEach(logError)
89 else logError(errors)
90
91 process.exitCode = 1
92}
93
94const resolveCredentials = _args => {
95 if (_args.accessToken) return Promise.resolve({})
96 return getCredentials(_args.projectKey)
97}
98
99// Register error listener
100args.output.on('error', errorHandler)
101
102resolveCredentials(args)
103 .then(credentials => {
104 const apiConfig = {
105 host: args.authUrl,
106 apiUrl: args.apiUrl,
107 projectKey: args.projectKey,
108 credentials,
109 }
110 const exporterOptions = {
111 apiConfig,
112 accessToken: args.accessToken,
113 predicate: args.where,
114 logger: {
115 error: logger.error.bind(logger),
116 warn: logger.warn.bind(logger),
117 info: logger.info.bind(logger),
118 debug: logger.debug.bind(logger),
119 },
120 }
121 return new CustomerGroupsExporter(exporterOptions)
122 })
123 .then(customerGroupsExporter => customerGroupsExporter.run(args.output))
124 .catch(errorHandler)