1 | import fs from 'fs'
|
2 | import { getCredentials } from '@commercetools/get-credentials'
|
3 | import pino from 'pino'
|
4 | import PrettyError from 'pretty-error'
|
5 | import yargs from 'yargs'
|
6 |
|
7 | import CustomerGroupsExporter from './main'
|
8 | import { description } from '../package.json'
|
9 |
|
10 | process.title = 'customer-groups-exporter'
|
11 |
|
12 | const args = yargs
|
13 | .usage(
|
14 | `
|
15 | Usage: $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 |
|
64 | const loggerConfig = {
|
65 | level: args.logLevel,
|
66 | prettyPrint: args.prettyLogs,
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 | let logDestination
|
72 | if (args.output === process.stdout)
|
73 | logDestination = fs.createWriteStream(args.logFile)
|
74 |
|
75 | const logger = pino(loggerConfig, logDestination)
|
76 |
|
77 |
|
78 |
|
79 | const 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 |
|
87 | const errorHandler = errors => {
|
88 | if (Array.isArray(errors)) errors.forEach(logError)
|
89 | else logError(errors)
|
90 |
|
91 | process.exitCode = 1
|
92 | }
|
93 |
|
94 | const resolveCredentials = _args => {
|
95 | if (_args.accessToken) return Promise.resolve({})
|
96 | return getCredentials(_args.projectKey)
|
97 | }
|
98 |
|
99 |
|
100 | args.output.on('error', errorHandler)
|
101 |
|
102 | resolveCredentials(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)
|