1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
5 | const axios_1 = tslib_1.__importDefault(require("axios"));
|
6 | const command_1 = tslib_1.__importStar(require("@oclif/command"));
|
7 | const update_notifier_1 = tslib_1.__importDefault(require("update-notifier"));
|
8 | require("./interceptors");
|
9 | const constants_1 = require("./constants");
|
10 | update_notifier_1.default({ pkg: require('../package.json') }).notify();
|
11 | class default_1 extends command_1.default {
|
12 | constructor() {
|
13 | super(...arguments);
|
14 | this.axiosConfig = Object.assign({}, axios_1.default.defaults, { baseURL: constants_1.API_BASE_URL });
|
15 | }
|
16 | readGlobalConfig() {
|
17 | let content;
|
18 | try {
|
19 | content = JSON.parse(fs_extra_1.default.readFileSync(constants_1.GLOBAL_CONF_PATH).toString('utf-8')) || {};
|
20 | }
|
21 | catch (_a) {
|
22 | content = {};
|
23 | }
|
24 |
|
25 | if (content.api_token) {
|
26 | content['api-token'] = content.api_token;
|
27 | delete content.api_token;
|
28 | }
|
29 | return content;
|
30 | }
|
31 | async catch(error) {
|
32 | if (error.code === 'ECONNREFUSED' || error.code === 'ECONNRESET') {
|
33 | this.error(`Could not connect to ${(error.config && error.config.baseURL) || 'https://api.liara.ir'}.
|
34 | Please check your network connection.`);
|
35 | }
|
36 | if (error.oclif && error.oclif.exit === 0)
|
37 | return;
|
38 | this.error(error.message);
|
39 | }
|
40 | setAxiosToken(config) {
|
41 | if (!config['api-token']) {
|
42 | return;
|
43 | }
|
44 | this.axiosConfig.headers.Authorization = `Bearer ${config['api-token']}`;
|
45 | }
|
46 | }
|
47 | default_1.flags = {
|
48 | help: command_1.flags.help({ char: 'h' }),
|
49 | dev: command_1.flags.boolean({ description: 'run in dev mode', hidden: true }),
|
50 | debug: command_1.flags.boolean({ char: 'd', description: 'show debug logs' }),
|
51 | 'api-token': command_1.flags.string({ description: 'your api token to use for authentication' }),
|
52 | };
|
53 | exports.default = default_1;
|