@knocklabs/cli 0.1.23 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,16 +4,29 @@
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- Object.defineProperty(exports, "default", {
7
+ Object.defineProperty(exports, "UserConfigStore", {
8
8
  enumerable: true,
9
9
  get: function() {
10
- return _default;
10
+ return UserConfigStore;
11
11
  }
12
12
  });
13
13
  const _nodepath = /*#__PURE__*/ _interop_require_wildcard(require("node:path"));
14
14
  const _fsextra = /*#__PURE__*/ _interop_require_wildcard(require("fs-extra"));
15
- const _yup = /*#__PURE__*/ _interop_require_wildcard(require("yup"));
15
+ const _zod = require("zod");
16
16
  const _const = require("./helpers/const");
17
+ function _define_property(obj, key, value) {
18
+ if (key in obj) {
19
+ Object.defineProperty(obj, key, {
20
+ value: value,
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true
24
+ });
25
+ } else {
26
+ obj[key] = value;
27
+ }
28
+ return obj;
29
+ }
17
30
  function _getRequireWildcardCache(nodeInterop) {
18
31
  if (typeof WeakMap !== "function") return null;
19
32
  var cacheBabelInterop = new WeakMap();
@@ -55,33 +68,58 @@ function _interop_require_wildcard(obj, nodeInterop) {
55
68
  }
56
69
  return newObj;
57
70
  }
58
- const userConfigSchema = _yup.object({
59
- serviceToken: _yup.string(),
60
- apiOrigin: _yup.string()
71
+ // When a user has authenticated via OAuth, we store the session in the user config.
72
+ const userSessionSchema = _zod.z.object({
73
+ accessToken: _zod.z.string(),
74
+ clientId: _zod.z.string(),
75
+ refreshToken: _zod.z.string()
76
+ });
77
+ const userConfigSchema = _zod.z.object({
78
+ serviceToken: _zod.z.string().optional(),
79
+ apiOrigin: _zod.z.string().optional(),
80
+ dashboardOrigin: _zod.z.string().optional(),
81
+ authOrigin: _zod.z.string().optional(),
82
+ userSession: userSessionSchema.optional()
61
83
  });
62
- let USER_CONFIG;
63
- const maybeReadJsonConfig = async (configDir)=>{
64
- // Don't use a user config file in tests.
65
- if (_const.isTestEnv) return null;
66
- const pathToJsonConfig = _nodepath.resolve(configDir, "config.json");
67
- const exists = await _fsextra.pathExists(pathToJsonConfig);
68
- if (!exists) return null;
69
- return _fsextra.readJSON(pathToJsonConfig);
70
- };
71
- const load = async (configDir)=>{
72
- const readConfig = await maybeReadJsonConfig(configDir);
73
- const validConfig = await userConfigSchema.validate(readConfig || {});
74
- // If no valid user config was available, give it an empty map.
75
- USER_CONFIG = validConfig || {};
76
- return USER_CONFIG;
77
- };
78
- const get = ()=>{
79
- if (!USER_CONFIG) {
80
- throw new Error("User config must be loaded first.");
84
+ class UserConfigStore {
85
+ async load() {
86
+ const readConfig = await this.maybeReadJsonConfig();
87
+ const validConfig = userConfigSchema.parse(readConfig || {});
88
+ this.userConfig = validConfig || {};
89
+ return this.userConfig;
90
+ }
91
+ get() {
92
+ return this.userConfig;
93
+ }
94
+ async set(updatedConfig) {
95
+ this.userConfig = {
96
+ ...this.userConfig,
97
+ ...updatedConfig
98
+ };
99
+ await this.maybeWriteJsonConfig();
100
+ return this.userConfig;
81
101
  }
82
- return USER_CONFIG;
83
- };
84
- const _default = {
85
- load,
86
- get
87
- };
102
+ configPath() {
103
+ return _nodepath.resolve(this.configDir, "config.json");
104
+ }
105
+ async maybeReadJsonConfig() {
106
+ if (_const.isTestEnv) return {};
107
+ const path = this.configPath();
108
+ const exists = await _fsextra.pathExists(path);
109
+ if (!exists) return {};
110
+ return _fsextra.readJSON(path);
111
+ }
112
+ async maybeWriteJsonConfig() {
113
+ if (_const.isTestEnv) return;
114
+ const path = this.configPath();
115
+ await _fsextra.outputJson(path, this.userConfig, {
116
+ spaces: 2
117
+ });
118
+ }
119
+ constructor(configDir){
120
+ _define_property(this, "configDir", void 0);
121
+ _define_property(this, "userConfig", void 0);
122
+ this.configDir = configDir;
123
+ this.userConfig = {};
124
+ }
125
+ }