UNPKG

3.54 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
5const got_1 = tslib_1.__importDefault(require("got"));
6const axios_1 = tslib_1.__importDefault(require("axios"));
7const command_1 = tslib_1.__importStar(require("@oclif/command"));
8const update_notifier_1 = tslib_1.__importDefault(require("update-notifier"));
9const https_proxy_agent_1 = tslib_1.__importDefault(require("https-proxy-agent"));
10require("./interceptors");
11const constants_1 = require("./constants");
12update_notifier_1.default({ pkg: require('../package.json') }).notify();
13axios_1.default.defaults.timeout = 5000;
14class default_1 extends command_1.default {
15 constructor() {
16 super(...arguments);
17 this.axiosConfig = Object.assign({}, axios_1.default.defaults);
18 this.got = got_1.default.extend();
19 }
20 readGlobalConfig() {
21 let content;
22 try {
23 content = JSON.parse(fs_extra_1.default.readFileSync(constants_1.GLOBAL_CONF_PATH).toString('utf-8')) || {};
24 }
25 catch (_a) {
26 content = {};
27 }
28 // For backward compatibility with < 1.0.0 versions
29 if (content.api_token) {
30 content['api-token'] = content.api_token;
31 delete content.api_token;
32 }
33 return content;
34 }
35 async catch(error) {
36 if (error.code === 'ECONNREFUSED' || error.code === 'ECONNRESET') {
37 this.error(`Could not connect to ${(error.config && error.config.baseURL) || 'https://api.liara.ir'}.
38Please check your network connection.`);
39 }
40 if (error.oclif && error.oclif.exit === 0)
41 return;
42 this.error(error.message);
43 }
44 setAxiosConfig(config) {
45 const gotConfig = {};
46 const proxy = process.env.http_proxy || process.env.https_proxy;
47 if (proxy) {
48 this.log(`Using proxy server ${proxy}`);
49 // @ts-ignore
50 const agent = new https_proxy_agent_1.default(proxy);
51 this.axiosConfig.httpsAgent = agent;
52 this.axiosConfig.proxy = false; // Prevents Axios to use proxy envs by itself
53 gotConfig.agent = { https: agent };
54 }
55 if (config['api-token']) {
56 this.axiosConfig.headers.Authorization = `Bearer ${config['api-token']}`;
57 gotConfig.headers = { Authorization: `Bearer ${config['api-token']}` };
58 }
59 config['region'] = config['region'] || constants_1.FALLBACK_REGION;
60 const actualBaseURL = constants_1.REGIONS_API_URL[config['region']];
61 this.axiosConfig.baseURL = constants_1.DEV_MODE ? 'http://localhost:3000' : actualBaseURL;
62 gotConfig.prefixUrl = this.axiosConfig.baseURL;
63 if (constants_1.DEV_MODE) {
64 this.log(`[dev] The actual base url is: ${actualBaseURL}`);
65 this.log(`[dev] but in dev mode we use http://localhost:3000`);
66 }
67 this.got = got_1.default.extend(gotConfig);
68 }
69}
70exports.default = default_1;
71default_1.flags = {
72 help: command_1.flags.help({ char: 'h' }),
73 dev: command_1.flags.boolean({ description: 'run in dev mode', hidden: true }),
74 debug: command_1.flags.boolean({ char: 'd', description: 'show debug logs' }),
75 'api-token': command_1.flags.string({ description: 'your api token to use for authentication' }),
76 region: command_1.flags.string({ description: 'the region you want to deploy your app to', options: ['iran', 'germany'] }),
77};