@knocklabs/cli 1.1.1 → 1.2.2

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.
Files changed (42) hide show
  1. package/README.md +297 -170
  2. package/dist/commands/audience/new.js +2 -2
  3. package/dist/commands/branch/rebase.js +65 -0
  4. package/dist/commands/commit/index.js +15 -11
  5. package/dist/commands/commit/list.js +2 -8
  6. package/dist/commands/guide/get.js +6 -0
  7. package/dist/commands/guide/list.js +6 -0
  8. package/dist/commands/guide/push.js +1 -0
  9. package/dist/commands/layout/push.js +1 -0
  10. package/dist/commands/message-type/push.js +1 -0
  11. package/dist/commands/partial/push.js +1 -0
  12. package/dist/commands/push.js +4 -0
  13. package/dist/commands/schema/pull.js +197 -0
  14. package/dist/commands/schema/push.js +203 -0
  15. package/dist/commands/source/get.js +180 -0
  16. package/dist/commands/source/list.js +96 -0
  17. package/dist/commands/translation/push.js +1 -0
  18. package/dist/commands/whoami.js +8 -2
  19. package/dist/commands/workflow/get.js +6 -0
  20. package/dist/commands/workflow/list.js +6 -0
  21. package/dist/commands/workflow/push.js +1 -0
  22. package/dist/commands/workflow/run.js +6 -0
  23. package/dist/help.js +74 -0
  24. package/dist/lib/api-v1.js +78 -1
  25. package/dist/lib/auth.js +3 -2
  26. package/dist/lib/helpers/flag.js +15 -0
  27. package/dist/lib/marshal/guide/helpers.js +11 -0
  28. package/dist/lib/marshal/schema/helpers.js +142 -0
  29. package/dist/lib/marshal/schema/index.js +21 -0
  30. package/dist/lib/marshal/schema/reader.js +177 -0
  31. package/dist/lib/marshal/schema/types.js +15 -0
  32. package/dist/lib/marshal/schema/writer.js +154 -0
  33. package/dist/lib/marshal/shared/helpers.js +30 -14
  34. package/dist/lib/marshal/source/helpers.js +28 -0
  35. package/dist/lib/marshal/source/index.js +19 -0
  36. package/dist/lib/marshal/source/types.js +4 -0
  37. package/dist/lib/marshal/workflow/generator.js +11 -8
  38. package/dist/lib/marshal/workflow/helpers.js +10 -6
  39. package/dist/lib/resources.js +11 -0
  40. package/npm-shrinkwrap.json +10698 -0
  41. package/oclif.manifest.json +448 -11
  42. package/package.json +22 -15
@@ -85,7 +85,7 @@ const AUDIENCE_TYPE_CHOICES = [
85
85
  },
86
86
  {
87
87
  name: _audience.AudienceType.Dynamic,
88
- message: "Dynamic (rule-based membership) [Beta - requires access]"
88
+ message: "Dynamic (rule-based membership)"
89
89
  }
90
90
  ];
91
91
  class AudienceNew extends _basecommand.default {
@@ -215,7 +215,7 @@ _define_property(AudienceNew, "flags", {
215
215
  char: "k"
216
216
  }),
217
217
  type: _core.Flags.string({
218
- summary: "The type of the audience (static, dynamic). Note: dynamic is in beta and requires access.",
218
+ summary: "The type of the audience (static, dynamic).",
219
219
  char: "t",
220
220
  options: [
221
221
  _audience.AudienceType.Static,
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return BranchRebase;
9
+ }
10
+ });
11
+ const _core = require("@oclif/core");
12
+ const _basecommand = /*#__PURE__*/ _interop_require_default(require("../../lib/base-command"));
13
+ const _arg = require("../../lib/helpers/arg");
14
+ const _const = require("../../lib/helpers/const");
15
+ const _request = require("../../lib/helpers/request");
16
+ const _ux = require("../../lib/helpers/ux");
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
+ }
30
+ function _interop_require_default(obj) {
31
+ return obj && obj.__esModule ? obj : {
32
+ default: obj
33
+ };
34
+ }
35
+ class BranchRebase extends _basecommand.default {
36
+ async run() {
37
+ const { args, flags } = this.props;
38
+ const prompt = `Rebase branch \`${args.slug}\` with main? This updates your branch with the latest changes from main while preserving your branch's commits.`;
39
+ const input = flags.force || await (0, _ux.promptToConfirm)(prompt);
40
+ if (!input) return;
41
+ const resp = await (0, _request.withSpinner)(()=>this.apiV1.rebaseBranch(args.slug, _const.KnockEnv.Development), {
42
+ action: "‣ Rebasing branch"
43
+ });
44
+ const data = resp.data;
45
+ if (flags.json) return data;
46
+ this.render(data);
47
+ }
48
+ async render(data) {
49
+ this.log(`‣ Successfully rebased branch \`${data.slug}\``);
50
+ this.log(` Updated at: ${data.updated_at}`);
51
+ }
52
+ }
53
+ _define_property(BranchRebase, "summary", "Rebases a branch onto the development environment, bringing in changes from main while preserving branch commits.");
54
+ _define_property(BranchRebase, "enableJsonFlag", true);
55
+ _define_property(BranchRebase, "args", {
56
+ slug: _arg.CustomArgs.slug({
57
+ required: true,
58
+ description: "The slug of the branch to rebase"
59
+ })
60
+ });
61
+ _define_property(BranchRebase, "flags", {
62
+ force: _core.Flags.boolean({
63
+ summary: "Remove the confirmation prompt."
64
+ })
65
+ });
@@ -15,6 +15,7 @@ const _const = require("../../lib/helpers/const");
15
15
  const _flag = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/helpers/flag"));
16
16
  const _request = require("../../lib/helpers/request");
17
17
  const _ux = require("../../lib/helpers/ux");
18
+ const _resources = require("../../lib/resources");
18
19
  function _define_property(obj, key, value) {
19
20
  if (key in obj) {
20
21
  Object.defineProperty(obj, key, {
@@ -74,24 +75,33 @@ function _interop_require_wildcard(obj, nodeInterop) {
74
75
  }
75
76
  return newObj;
76
77
  }
78
+ const ALLOW_EMPTY_RESOURCE_TYPE_SET = new Set(_resources.ALLOW_EMPTY_RESOURCE_TYPES);
77
79
  class Commit extends _basecommand.default {
78
80
  async run() {
79
81
  const { flags } = this.props;
80
82
  if (flags["resource-id"] && !flags["resource-type"]) {
81
83
  this.error("The --resource-id flag must be used together with --resource-type.");
82
84
  }
85
+ if (flags["allow-empty"]) {
86
+ if (!flags["resource-type"] || !flags["resource-id"]) {
87
+ this.error("The --allow-empty flag must be used with a single --resource-type and --resource-id.");
88
+ }
89
+ if (!ALLOW_EMPTY_RESOURCE_TYPE_SET.has(flags["resource-type"])) {
90
+ this.error("Empty commits for `audience` are not yet supported. Other supported resource types: " + _resources.ALLOW_EMPTY_RESOURCE_TYPES.join(", "));
91
+ }
92
+ }
83
93
  const scope = (0, _command.formatCommandScope)(flags);
84
94
  const qualifier = this.formatResourceQualifier(flags);
85
- const prompt = qualifier ? `Commit ${qualifier} in the ${scope}?` : `Commit all changes in the ${scope}?`;
95
+ const prompt = flags["allow-empty"] ? `Create empty commit for ${qualifier} in the ${scope}?` : qualifier ? `Commit ${qualifier} in the ${scope}?` : `Commit all changes in the ${scope}?`;
86
96
  const input = flags.force || await (0, _ux.promptToConfirm)(prompt);
87
97
  if (!input) return;
88
98
  await (0, _request.withSpinner)(()=>this.apiV1.commitAllChanges(this.props));
89
- const successMsg = qualifier ? `‣ Successfully committed ${qualifier} in ${scope}` : `‣ Successfully committed all changes in ${scope}`;
99
+ const successMsg = flags["allow-empty"] ? `‣ Successfully created empty commit for ${qualifier} in ${scope}` : qualifier ? `‣ Successfully committed ${qualifier} in ${scope}` : `‣ Successfully committed all changes in ${scope}`;
90
100
  this.log(successMsg);
91
101
  }
92
102
  formatResourceQualifier(flags) {
93
103
  if (flags["resource-type"] && flags["resource-id"]) {
94
- return `\`${flags["resource-type"]}\` changes for \`${flags["resource-id"]}\``;
104
+ return `\`${flags["resource-type"]}\` \`${flags["resource-id"]}\``;
95
105
  }
96
106
  if (flags["resource-type"]) {
97
107
  return `all \`${flags["resource-type"]}\` changes`;
@@ -116,16 +126,10 @@ _define_property(Commit, "flags", {
116
126
  force: _core.Flags.boolean({
117
127
  summary: "Remove the confirmation prompt."
118
128
  }),
129
+ "allow-empty": _flag.allowEmpty,
119
130
  "resource-type": _core.Flags.string({
120
131
  summary: "Commit only changes for the given resource type. Can be used alone or together with --resource-id.",
121
- options: [
122
- "email_layout",
123
- "guide",
124
- "message_type",
125
- "partial",
126
- "translation",
127
- "workflow"
128
- ]
132
+ options: _resources.ALL_RESOURCE_TYPES
129
133
  }),
130
134
  "resource-id": _core.Flags.string({
131
135
  summary: "Commit only changes for the given resource identifier. Must be used together with --resource-type."
@@ -17,6 +17,7 @@ const _objectisomorphic = require("../../lib/helpers/object.isomorphic");
17
17
  const _page = require("../../lib/helpers/page");
18
18
  const _request = require("../../lib/helpers/request");
19
19
  const _commit = require("../../lib/marshal/commit");
20
+ const _resources = require("../../lib/resources");
20
21
  function _define_property(obj, key, value) {
21
22
  if (key in obj) {
22
23
  Object.defineProperty(obj, key, {
@@ -171,14 +172,7 @@ _define_property(CommitList, "flags", {
171
172
  "resource-type": _core.Flags.string({
172
173
  summary: "Filter commits by resource type. Can be used alone or together with resource-id. Use multiple --resource-type flags for multiple values.",
173
174
  multiple: true,
174
- options: [
175
- "email_layout",
176
- "guide",
177
- "message_type",
178
- "partial",
179
- "translation",
180
- "workflow"
181
- ]
175
+ options: _resources.ALL_RESOURCE_TYPES
182
176
  }),
183
177
  "resource-id": _core.Flags.string({
184
178
  summary: "Filter commits by resource identifier. Must be used together with resource-type. For most resources, this will be the resource key. In the case of translations, this will be the locale code and namespace, separated by a /. For example, en/courses or en."
@@ -125,6 +125,12 @@ class GuideGet extends _basecommand.default {
125
125
  key: "Description",
126
126
  value: guide.description || "-"
127
127
  },
128
+ {
129
+ key: "Tags",
130
+ value: (0, _helpers.formatTags)(guide, {
131
+ emptyDisplay: "-"
132
+ })
133
+ },
128
134
  {
129
135
  key: "Content",
130
136
  value: guide.steps.length > 0 ? (0, _helpers.formatStep)(guide.steps[0]) : "-"
@@ -122,6 +122,12 @@ class GuideList extends _basecommand.default {
122
122
  header: "Description",
123
123
  get: (entry)=>entry.description || "-"
124
124
  },
125
+ tags: {
126
+ header: "Tags",
127
+ get: (entry)=>(0, _helpers.formatTags)(entry, {
128
+ truncateAfter: 3
129
+ })
130
+ },
125
131
  updated_at: {
126
132
  header: "Updated at",
127
133
  get: (entry)=>(0, _date.formatDate)(entry.updated_at)
@@ -163,6 +163,7 @@ _define_property(GuidePush, "flags", {
163
163
  "commit"
164
164
  ]
165
165
  }),
166
+ "allow-empty": _flag.allowEmptyOnPush,
166
167
  force: _flag.force
167
168
  });
168
169
  _define_property(GuidePush, "args", {
@@ -169,6 +169,7 @@ _define_property(EmailLayoutPush, "flags", {
169
169
  "commit"
170
170
  ]
171
171
  }),
172
+ "allow-empty": _flag.allowEmptyOnPush,
172
173
  force: _flag.force
173
174
  });
174
175
  _define_property(EmailLayoutPush, "args", {
@@ -166,6 +166,7 @@ _define_property(MessageTypePush, "flags", {
166
166
  "commit"
167
167
  ]
168
168
  }),
169
+ "allow-empty": _flag.allowEmptyOnPush,
169
170
  force: _flag.force
170
171
  });
171
172
  _define_property(MessageTypePush, "args", {
@@ -166,6 +166,7 @@ _define_property(PartialPush, "flags", {
166
166
  "commit"
167
167
  ]
168
168
  }),
169
+ "allow-empty": _flag.allowEmptyOnPush,
169
170
  force: _flag.force
170
171
  });
171
172
  _define_property(PartialPush, "args", {
@@ -113,6 +113,9 @@ class Push extends _basecommand.default {
113
113
  "--commit-message",
114
114
  flags["commit-message"]
115
115
  ] : [],
116
+ ...flags["allow-empty"] ? [
117
+ "--allow-empty"
118
+ ] : [],
116
119
  ...flags.force ? [
117
120
  "--force"
118
121
  ] : []
@@ -151,6 +154,7 @@ _define_property(Push, "flags", {
151
154
  "commit"
152
155
  ]
153
156
  }),
157
+ "allow-empty": _flag.allowEmptyOnPush,
154
158
  force: _flag.force
155
159
  });
156
160
  const runResourcePushCommand = async (resourceType, targetDirCtx, args)=>{
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return SchemaPull;
9
+ }
10
+ });
11
+ const _core = require("@oclif/core");
12
+ const _basecommand = /*#__PURE__*/ _interop_require_default(require("../../lib/base-command"));
13
+ const _command = require("../../lib/helpers/command");
14
+ const _error = require("../../lib/helpers/error");
15
+ const _flag = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/helpers/flag"));
16
+ const _request = require("../../lib/helpers/request");
17
+ const _ux = require("../../lib/helpers/ux");
18
+ const _schema = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/marshal/schema"));
19
+ function _define_property(obj, key, value) {
20
+ if (key in obj) {
21
+ Object.defineProperty(obj, key, {
22
+ value: value,
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true
26
+ });
27
+ } else {
28
+ obj[key] = value;
29
+ }
30
+ return obj;
31
+ }
32
+ function _interop_require_default(obj) {
33
+ return obj && obj.__esModule ? obj : {
34
+ default: obj
35
+ };
36
+ }
37
+ function _getRequireWildcardCache(nodeInterop) {
38
+ if (typeof WeakMap !== "function") return null;
39
+ var cacheBabelInterop = new WeakMap();
40
+ var cacheNodeInterop = new WeakMap();
41
+ return (_getRequireWildcardCache = function(nodeInterop) {
42
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
43
+ })(nodeInterop);
44
+ }
45
+ function _interop_require_wildcard(obj, nodeInterop) {
46
+ if (!nodeInterop && obj && obj.__esModule) {
47
+ return obj;
48
+ }
49
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
50
+ return {
51
+ default: obj
52
+ };
53
+ }
54
+ var cache = _getRequireWildcardCache(nodeInterop);
55
+ if (cache && cache.has(obj)) {
56
+ return cache.get(obj);
57
+ }
58
+ var newObj = {
59
+ __proto__: null
60
+ };
61
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
62
+ for(var key in obj){
63
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
64
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
65
+ if (desc && (desc.get || desc.set)) {
66
+ Object.defineProperty(newObj, key, desc);
67
+ } else {
68
+ newObj[key] = obj[key];
69
+ }
70
+ }
71
+ }
72
+ newObj.default = obj;
73
+ if (cache) {
74
+ cache.set(obj, newObj);
75
+ }
76
+ return newObj;
77
+ }
78
+ class SchemaPull extends _basecommand.default {
79
+ async run() {
80
+ const { args, flags } = this.props;
81
+ if (flags.all && args.itemType) {
82
+ return this.error(`itemType arg \`${args.itemType}\` cannot also be provided when using --all`);
83
+ }
84
+ if (flags.all && flags.collection) {
85
+ return this.error("Flag --collection cannot be provided when using --all");
86
+ }
87
+ return flags.all ? this.pullAllSchemas() : this.pullOneSchema();
88
+ }
89
+ async pullOneSchema() {
90
+ const { flags } = this.props;
91
+ const itemType = this.parseItemType();
92
+ this.validateCollectionUsage(itemType);
93
+ const schemasDirCtx = await this.getSchemasDirContext();
94
+ const schemaFileCtx = await _schema.schemaFileContext(schemasDirCtx.abspath, itemType, flags.collection);
95
+ if (schemaFileCtx.exists) {
96
+ const prompt = `Overwrite schema file at ${schemaFileCtx.abspath}?`;
97
+ const input = flags.force || await (0, _ux.promptToConfirm)(prompt);
98
+ if (!input) return;
99
+ } else {
100
+ const prompt = `Create a new schema file at ${schemaFileCtx.abspath}?`;
101
+ const input = flags.force || await (0, _ux.promptToConfirm)(prompt);
102
+ if (!input) return;
103
+ }
104
+ const resp = await (0, _request.withSpinner)(()=>this.apiV1.getSchema(this.props, itemType, flags.collection), {
105
+ action: "‣ Loading"
106
+ });
107
+ if (!resp.data.schema) {
108
+ _core.ux.error(new _error.ApiError((0, _request.formatErrorRespMessage)(resp)));
109
+ }
110
+ await _schema.writeSchemaFileFromData(schemaFileCtx, resp.data.schema);
111
+ const action = schemaFileCtx.exists ? "updated" : "created";
112
+ const scope = (0, _command.formatCommandScope)(flags);
113
+ this.log(`‣ Successfully ${action} schema file at ${schemaFileCtx.abspath} using ${scope}`);
114
+ }
115
+ async pullAllSchemas() {
116
+ const { flags } = this.props;
117
+ const schemasDirCtx = await this.getSchemasDirContext();
118
+ const prompt = schemasDirCtx.exists ? `Pull latest schemas into ${schemasDirCtx.abspath}?\n This will overwrite matching schema files in this directory.` : `Create a new schemas directory at ${schemasDirCtx.abspath}?`;
119
+ const input = flags.force || await (0, _ux.promptToConfirm)(prompt);
120
+ if (!input) return;
121
+ // Page through the list endpoint so environments with more schemas than a
122
+ // single page (many object collections) are pulled completely.
123
+ const schemas = [];
124
+ let after;
125
+ do {
126
+ var _resp_data_entries, _ref;
127
+ var _resp_data_page_info;
128
+ // eslint-disable-next-line no-await-in-loop
129
+ const resp = await (0, _request.withSpinner)(()=>this.apiV1.listSchemas(this.props, {
130
+ after
131
+ }), {
132
+ action: "‣ Loading"
133
+ });
134
+ schemas.push(...(_resp_data_entries = resp.data.entries) !== null && _resp_data_entries !== void 0 ? _resp_data_entries : []);
135
+ after = (_ref = (_resp_data_page_info = resp.data.page_info) === null || _resp_data_page_info === void 0 ? void 0 : _resp_data_page_info.after) !== null && _ref !== void 0 ? _ref : undefined;
136
+ }while (after)
137
+ await _schema.writeSchemasIndexDir(schemasDirCtx.abspath, schemas);
138
+ const action = schemasDirCtx.exists ? "updated" : "created";
139
+ const scope = (0, _command.formatCommandScope)(flags);
140
+ this.log(`‣ Successfully ${action} ${schemas.length} schema file(s) at ${schemasDirCtx.abspath} using ${scope}`);
141
+ }
142
+ parseItemType() {
143
+ const { itemType } = this.props.args;
144
+ if (!itemType) {
145
+ return this.error("Missing 1 required arg:\nitemType");
146
+ }
147
+ try {
148
+ return _schema.validateSchemaItemType(itemType);
149
+ } catch (error) {
150
+ return this.error(error.message);
151
+ }
152
+ }
153
+ validateCollectionUsage(itemType) {
154
+ const { collection } = this.props.flags;
155
+ if (itemType === "object" && !collection) {
156
+ this.error("Flag --collection is required when itemType is `object`");
157
+ }
158
+ if (itemType !== "object" && collection) {
159
+ this.error("Flag --collection can only be provided for object schemas");
160
+ }
161
+ if (itemType === "object" && collection) {
162
+ try {
163
+ _schema.validateSchemaCollection(collection);
164
+ } catch (error) {
165
+ this.error(error.message);
166
+ }
167
+ }
168
+ }
169
+ async getSchemasDirContext() {
170
+ return this.props.flags["schemas-dir"] ? this.props.flags["schemas-dir"] : _schema.resolveSchemasDir(this.projectConfig, this.runContext.cwd);
171
+ }
172
+ }
173
+ _define_property(SchemaPull, "summary", "Pull one or more item schemas from an environment into a local file system.");
174
+ _define_property(SchemaPull, "flags", {
175
+ environment: _core.Flags.string({
176
+ default: "development",
177
+ summary: "The environment to use."
178
+ }),
179
+ branch: _flag.branch,
180
+ all: _core.Flags.boolean({
181
+ summary: "Whether to pull all item schemas from the specified environment."
182
+ }),
183
+ collection: _core.Flags.string({
184
+ summary: "The object collection key for object schemas."
185
+ }),
186
+ "schemas-dir": _flag.dirPath({
187
+ summary: "The target schemas directory path."
188
+ }),
189
+ force: _core.Flags.boolean({
190
+ summary: "Remove the confirmation prompt."
191
+ })
192
+ });
193
+ _define_property(SchemaPull, "args", {
194
+ itemType: _core.Args.string({
195
+ required: false
196
+ })
197
+ });
@@ -0,0 +1,203 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return SchemaPush;
9
+ }
10
+ });
11
+ const _core = require("@oclif/core");
12
+ const _basecommand = /*#__PURE__*/ _interop_require_default(require("../../lib/base-command"));
13
+ const _command = require("../../lib/helpers/command");
14
+ const _error = require("../../lib/helpers/error");
15
+ const _flag = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/helpers/flag"));
16
+ const _request = require("../../lib/helpers/request");
17
+ const _string = require("../../lib/helpers/string");
18
+ const _schema = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/marshal/schema"));
19
+ function _define_property(obj, key, value) {
20
+ if (key in obj) {
21
+ Object.defineProperty(obj, key, {
22
+ value: value,
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true
26
+ });
27
+ } else {
28
+ obj[key] = value;
29
+ }
30
+ return obj;
31
+ }
32
+ function _interop_require_default(obj) {
33
+ return obj && obj.__esModule ? obj : {
34
+ default: obj
35
+ };
36
+ }
37
+ function _getRequireWildcardCache(nodeInterop) {
38
+ if (typeof WeakMap !== "function") return null;
39
+ var cacheBabelInterop = new WeakMap();
40
+ var cacheNodeInterop = new WeakMap();
41
+ return (_getRequireWildcardCache = function(nodeInterop) {
42
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
43
+ })(nodeInterop);
44
+ }
45
+ function _interop_require_wildcard(obj, nodeInterop) {
46
+ if (!nodeInterop && obj && obj.__esModule) {
47
+ return obj;
48
+ }
49
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
50
+ return {
51
+ default: obj
52
+ };
53
+ }
54
+ var cache = _getRequireWildcardCache(nodeInterop);
55
+ if (cache && cache.has(obj)) {
56
+ return cache.get(obj);
57
+ }
58
+ var newObj = {
59
+ __proto__: null
60
+ };
61
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
62
+ for(var key in obj){
63
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
64
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
65
+ if (desc && (desc.get || desc.set)) {
66
+ Object.defineProperty(newObj, key, desc);
67
+ } else {
68
+ newObj[key] = obj[key];
69
+ }
70
+ }
71
+ }
72
+ newObj.default = obj;
73
+ if (cache) {
74
+ cache.set(obj, newObj);
75
+ }
76
+ return newObj;
77
+ }
78
+ class SchemaPush extends _basecommand.default {
79
+ async run() {
80
+ const { args, flags } = this.props;
81
+ if (flags.all && args.itemType) {
82
+ return this.error(`itemType arg \`${args.itemType}\` cannot also be provided when using --all`);
83
+ }
84
+ if (flags.all && flags.collection) {
85
+ return this.error("Flag --collection cannot be provided when using --all");
86
+ }
87
+ return flags.all ? this.pushAllSchemas() : this.pushOneSchema();
88
+ }
89
+ async pushOneSchema() {
90
+ const { flags } = this.props;
91
+ const itemType = this.parseItemType();
92
+ this.validateCollectionUsage(itemType);
93
+ const schemasDirCtx = await this.getSchemasDirContext();
94
+ const schemaFileCtx = await _schema.schemaFileContext(schemasDirCtx.abspath, itemType, flags.collection);
95
+ const [schema, readErrors] = await _schema.readSchemaFile(schemaFileCtx);
96
+ if (readErrors.length > 0) {
97
+ this.error((0, _error.formatErrors)(readErrors, {
98
+ prependBy: "\n\n"
99
+ }));
100
+ }
101
+ await this.pushSchema({
102
+ ...schemaFileCtx,
103
+ content: this.normalizeSchemaForPush(schema, itemType, flags.collection)
104
+ });
105
+ const scope = (0, _command.formatCommandScope)(flags);
106
+ this.log(`‣ Successfully pushed 1 schema to ${scope}`);
107
+ }
108
+ async pushAllSchemas() {
109
+ const schemasDirCtx = await this.getSchemasDirContext();
110
+ const [schemas, readErrors] = await _schema.readAllSchemaFiles(schemasDirCtx.abspath);
111
+ if (readErrors.length > 0) {
112
+ this.error((0, _error.formatErrors)(readErrors, {
113
+ prependBy: "\n\n"
114
+ }));
115
+ }
116
+ if (schemas.length === 0) {
117
+ this.error(`No schema files found in ${schemasDirCtx.abspath}`);
118
+ }
119
+ for (const schema of schemas){
120
+ // eslint-disable-next-line no-await-in-loop
121
+ await this.pushSchema({
122
+ ...schema,
123
+ content: this.normalizeSchemaForPush(schema.content, schema.itemType, schema.collection)
124
+ });
125
+ }
126
+ const schemaPaths = schemas.map((schema)=>schema.abspath);
127
+ const scope = (0, _command.formatCommandScope)(this.props.flags);
128
+ this.log(`‣ Successfully pushed ${schemas.length} schema file(s) to ${scope}:\n` + (0, _string.indentString)(schemaPaths.join("\n"), 4));
129
+ }
130
+ async pushSchema(schema) {
131
+ try {
132
+ const resp = await (0, _request.withSpinner)(()=>this.apiV1.upsertSchema(this.props, schema.itemType, schema.content, schema.collection), {
133
+ action: "‣ Pushing"
134
+ });
135
+ if (!resp.data.schema) {
136
+ throw new Error((0, _request.formatErrorRespMessage)(resp));
137
+ }
138
+ await _schema.writeSchemaFileFromData(schema, resp.data.schema);
139
+ } catch (error) {
140
+ const sourceError = new _error.SourceError(error.message, schema.abspath, "ApiError");
141
+ this.error((0, _error.formatError)(sourceError));
142
+ }
143
+ }
144
+ normalizeSchemaForPush(schema, itemType, collection) {
145
+ return {
146
+ ...schema,
147
+ item_type: itemType,
148
+ item_id: itemType === "object" ? collection : null
149
+ };
150
+ }
151
+ parseItemType() {
152
+ const { itemType } = this.props.args;
153
+ if (!itemType) {
154
+ return this.error("Missing 1 required arg:\nitemType");
155
+ }
156
+ try {
157
+ return _schema.validateSchemaItemType(itemType);
158
+ } catch (error) {
159
+ return this.error(error.message);
160
+ }
161
+ }
162
+ validateCollectionUsage(itemType) {
163
+ const { collection } = this.props.flags;
164
+ if (itemType === "object" && !collection) {
165
+ this.error("Flag --collection is required when itemType is `object`");
166
+ }
167
+ if (itemType !== "object" && collection) {
168
+ this.error("Flag --collection can only be provided for object schemas");
169
+ }
170
+ if (itemType === "object" && collection) {
171
+ try {
172
+ _schema.validateSchemaCollection(collection);
173
+ } catch (error) {
174
+ this.error(error.message);
175
+ }
176
+ }
177
+ }
178
+ async getSchemasDirContext() {
179
+ return this.props.flags["schemas-dir"] ? this.props.flags["schemas-dir"] : _schema.resolveSchemasDir(this.projectConfig, this.runContext.cwd);
180
+ }
181
+ }
182
+ _define_property(SchemaPush, "summary", "Push one or more local item schemas to a Knock environment.");
183
+ _define_property(SchemaPush, "flags", {
184
+ environment: _core.Flags.string({
185
+ default: "development",
186
+ summary: "The environment to use."
187
+ }),
188
+ branch: _flag.branch,
189
+ all: _core.Flags.boolean({
190
+ summary: "Whether to push all schemas from the target schemas directory."
191
+ }),
192
+ collection: _core.Flags.string({
193
+ summary: "The object collection key for object schemas."
194
+ }),
195
+ "schemas-dir": _flag.dirPath({
196
+ summary: "The target schemas directory path."
197
+ })
198
+ });
199
+ _define_property(SchemaPush, "args", {
200
+ itemType: _core.Args.string({
201
+ required: false
202
+ })
203
+ });