UI for Zipcoin Blue

config.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const path = require("path");
  5. const os = require("os");
  6. const chalk_1 = require("chalk");
  7. const backends_1 = require("./backends");
  8. const errors_1 = require("./errors");
  9. const format_1 = require("./utils/format");
  10. const fs_1 = require("@ionic/cli-framework/utils/fs");
  11. class BaseConfig {
  12. constructor(directory, fileName) {
  13. this.fileName = fileName;
  14. // TODO: better way to check if in project
  15. if (directory) {
  16. this.directory = path.resolve(directory);
  17. }
  18. else {
  19. this.directory = '';
  20. }
  21. this.filePath = path.resolve(this.directory, fileName);
  22. }
  23. prepare() {
  24. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  25. try {
  26. const stats = yield fs_1.fsStat(this.directory);
  27. if (!stats.isDirectory()) {
  28. throw new errors_1.FatalException(`${chalk_1.default.bold(this.directory)} appears to be a file, but it must be a directory.`);
  29. }
  30. }
  31. catch (e) {
  32. if (e.code !== 'ENOENT') {
  33. throw e;
  34. }
  35. yield fs_1.fsMkdirp(this.directory);
  36. }
  37. });
  38. }
  39. load(options = {}) {
  40. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  41. if (options.disk || !this.configFile) {
  42. let o;
  43. try {
  44. const stats = yield fs_1.fsStat(this.filePath);
  45. if (stats.size < 5) {
  46. o = {};
  47. }
  48. }
  49. catch (e) {
  50. if (e.code !== 'ENOENT') {
  51. throw e;
  52. }
  53. o = {};
  54. }
  55. if (typeof o === 'undefined') {
  56. try {
  57. o = yield fs_1.fsReadJsonFile(this.filePath);
  58. }
  59. catch (e) {
  60. if (e === fs_1.ERROR_FILE_INVALID_JSON) {
  61. throw new errors_1.FatalException(`The config file (${chalk_1.default.bold(format_1.prettyPath(this.filePath))}) is not valid JSON format.\n\n` +
  62. `Please fix any JSON errors in the file.`);
  63. }
  64. else {
  65. throw e;
  66. }
  67. }
  68. }
  69. const cloneDeep = yield Promise.resolve().then(() => require('lodash/cloneDeep'));
  70. this.originalConfigFile = cloneDeep(o);
  71. o = yield this.provideDefaults(o);
  72. if (this.is(o)) {
  73. this.configFile = o;
  74. }
  75. else {
  76. throw new errors_1.FatalException(`The config file (${chalk_1.default.bold(format_1.prettyPath(this.filePath))}) has an unrecognized JSON format.\n\n` +
  77. `This usually means a key had an unexpected value. Please look through it and fix any issues.\n` +
  78. `If all else fails--the CLI will recreate the file if you delete it.`);
  79. }
  80. }
  81. return this.configFile;
  82. });
  83. }
  84. save(configFile) {
  85. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  86. if (!configFile) {
  87. configFile = this.configFile;
  88. }
  89. if (configFile) {
  90. const [isEqual, cloneDeep] = yield Promise.all([Promise.resolve().then(() => require('lodash/isEqual')), Promise.resolve().then(() => require('lodash/cloneDeep'))]);
  91. if (!isEqual(configFile, this.originalConfigFile)) {
  92. yield fs_1.fsWriteJsonFile(this.filePath, configFile, { encoding: 'utf8' });
  93. this.configFile = configFile;
  94. this.originalConfigFile = cloneDeep(configFile);
  95. }
  96. }
  97. });
  98. }
  99. }
  100. exports.BaseConfig = BaseConfig;
  101. exports.CONFIG_FILE = 'config.json';
  102. exports.DEFAULT_CONFIG_DIRECTORY = path.resolve(os.homedir(), '.ionic');
  103. class Config extends BaseConfig {
  104. provideDefaults(o) {
  105. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  106. const cloneDeep = yield Promise.resolve().then(() => require('lodash/cloneDeep'));
  107. const results = cloneDeep(o);
  108. if (!results.state) {
  109. results.state = {};
  110. }
  111. if (!results.state.doctor) {
  112. results.state.doctor = {};
  113. }
  114. if (typeof results.state.doctor.ignored === 'undefined') {
  115. results.state.doctor.ignored = [];
  116. }
  117. if (!results.state.lastCommand) {
  118. if (results.lastCommand) {
  119. results.state.lastCommand = results.lastCommand;
  120. }
  121. else {
  122. results.state.lastCommand = new Date().toISOString();
  123. }
  124. }
  125. if (!results.created) {
  126. results.created = new Date().toISOString();
  127. }
  128. if (!results.daemon) {
  129. results.daemon = {};
  130. }
  131. if (typeof results.daemon.updates === 'undefined') {
  132. results.daemon.updates = true;
  133. }
  134. if (!results.devapp) {
  135. results.devapp = {};
  136. }
  137. if (!results.devapp.knownInterfaces) {
  138. results.devapp.knownInterfaces = [];
  139. }
  140. if (!results.addresses) {
  141. results.addresses = {};
  142. }
  143. if (!results.git) {
  144. results.git = {};
  145. }
  146. if (!results.user) {
  147. results.user = {};
  148. }
  149. if (typeof results.user.id === 'string') {
  150. results.user.id = Number(results.user.id);
  151. }
  152. if (!results.tokens) {
  153. results.tokens = {};
  154. }
  155. if (!results.tokens.appUser) {
  156. results.tokens.appUser = {};
  157. }
  158. if (typeof results.backend !== 'string') {
  159. results.backend = backends_1.BACKEND_PRO;
  160. }
  161. if (typeof results.telemetry === 'undefined') {
  162. if (results.cliFlags && typeof results.cliFlags.enableTelemetry !== 'undefined') {
  163. results.telemetry = results.cliFlags.enableTelemetry;
  164. }
  165. else if (results.cliFlags && typeof results.cliFlags.telemetry !== 'undefined') {
  166. results.telemetry = results.cliFlags.telemetry;
  167. }
  168. else {
  169. results.telemetry = true;
  170. }
  171. }
  172. if (typeof results.yarn === 'undefined') {
  173. if (results.cliFlags && typeof results.cliFlags.yarn !== 'undefined') {
  174. results.yarn = results.cliFlags.yarn;
  175. }
  176. else {
  177. results.yarn = false;
  178. }
  179. }
  180. delete results.lastCommand;
  181. delete results.lastUpdated;
  182. delete results.cliFlags;
  183. delete results.urls;
  184. delete results.git.host;
  185. delete results.git.port;
  186. return results;
  187. });
  188. }
  189. is(j) {
  190. return j
  191. && typeof j.addresses === 'object'
  192. && typeof j.state === 'object'
  193. && typeof j.state.lastCommand === 'string'
  194. && typeof j.state.doctor === 'object'
  195. && typeof j.daemon === 'object'
  196. && typeof j.devapp === 'object'
  197. && typeof j.user === 'object'
  198. && typeof j.tokens === 'object'
  199. && typeof j.tokens.appUser === 'object'
  200. && typeof j.backend === 'string'
  201. && typeof j.telemetry === 'boolean'
  202. && typeof j.yarn === 'boolean';
  203. }
  204. isUpdatingEnabled() {
  205. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  206. const config = yield this.load();
  207. if (!config.daemon.updates) {
  208. return false;
  209. }
  210. return !config.state.lastNoResponseToUpdate || (new Date().getTime() - new Date(config.state.lastNoResponseToUpdate).getTime() > 86400000);
  211. });
  212. }
  213. getAPIUrl() {
  214. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  215. const config = yield this.load();
  216. if (config.addresses.apiUrl) {
  217. return config.addresses.apiUrl;
  218. }
  219. if (config.backend === 'legacy') {
  220. return 'https://api.ionic.io';
  221. }
  222. return 'https://api.ionicjs.com';
  223. });
  224. }
  225. getDashUrl() {
  226. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  227. const config = yield this.load();
  228. if (config.addresses.dashUrl) {
  229. return config.addresses.dashUrl;
  230. }
  231. if (config.backend === 'legacy') {
  232. return 'https://apps.ionic.io';
  233. }
  234. return 'https://dashboard.ionicjs.com';
  235. });
  236. }
  237. getGitHost() {
  238. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  239. const config = yield this.load();
  240. if (config.addresses.gitHost) {
  241. return config.addresses.gitHost;
  242. }
  243. return 'git.ionicjs.com';
  244. });
  245. }
  246. getGitPort() {
  247. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  248. const config = yield this.load();
  249. if (config.addresses.gitPort) {
  250. return config.addresses.gitPort;
  251. }
  252. return 22;
  253. });
  254. }
  255. }
  256. exports.Config = Config;
  257. function gatherFlags(argv) {
  258. return {
  259. interactive: typeof argv['interactive'] === 'undefined' ? true : argv['interactive'],
  260. confirm: typeof argv['confirm'] === 'undefined' ? false : argv['confirm'],
  261. };
  262. }
  263. exports.gatherFlags = gatherFlags;