UI for Zipcoin Blue

set.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const chalk_1 = require("chalk");
  5. const errors_1 = require("../../lib/errors");
  6. function set(env, inputs, options) {
  7. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  8. const { prettyPath } = yield Promise.resolve().then(() => require('../../lib/utils/format'));
  9. let [p, v] = inputs;
  10. const { global, json, force } = options;
  11. if (!global && !env.project.directory) {
  12. throw new errors_1.FatalException(`Sorry--this won't work outside an Ionic project directory. Did you mean to set global config using ${chalk_1.default.green('--global')}?`);
  13. }
  14. const file = global ? env.config : env.project;
  15. const config = yield file.load();
  16. const [get, set] = yield Promise.all([Promise.resolve().then(() => require('lodash/get')), Promise.resolve().then(() => require('lodash/set'))]);
  17. const oldValue = get(config, p);
  18. if (!v.match(/^\d+e\d+$/)) {
  19. try {
  20. v = JSON.parse(v);
  21. }
  22. catch (e) {
  23. if (!(e instanceof SyntaxError)) {
  24. throw e;
  25. }
  26. if (json) {
  27. throw new errors_1.FatalException(`${chalk_1.default.green('--json')}: ${chalk_1.default.green(v)} is invalid JSON: ${chalk_1.default.red(String(e))}`);
  28. }
  29. }
  30. }
  31. let newValue = v;
  32. if (oldValue && typeof oldValue === 'object' && !force) {
  33. throw new errors_1.FatalException(`Sorry--will not override objects or arrays without ${chalk_1.default.green('--force')}.\n` +
  34. `Value of ${chalk_1.default.green(p)} is: ${chalk_1.default.bold(JSON.stringify(oldValue))}`);
  35. }
  36. const valueChanged = oldValue !== newValue;
  37. set(config, p, newValue);
  38. yield file.save();
  39. if (global && p === 'backend' && valueChanged) {
  40. yield env.hooks.fire('backend:changed', { env });
  41. }
  42. if (valueChanged) {
  43. env.log.ok(`${chalk_1.default.green(p)} set to ${chalk_1.default.green(JSON.stringify(v))} in ${chalk_1.default.bold(prettyPath(file.filePath))}!`);
  44. }
  45. else {
  46. env.log.info(`${chalk_1.default.green(p)} is already set to ${chalk_1.default.bold(JSON.stringify(v))}.`);
  47. }
  48. });
  49. }
  50. exports.set = set;