@knocklabs/cli 0.1.19 → 0.1.21

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.
@@ -0,0 +1,218 @@
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 MessageTypePull;
9
+ }
10
+ });
11
+ const _nodepath = /*#__PURE__*/ _interop_require_wildcard(require("node:path"));
12
+ const _core = require("@oclif/core");
13
+ const _basecommand = /*#__PURE__*/ _interop_require_default(require("../../lib/base-command"));
14
+ const _error = require("../../lib/helpers/error");
15
+ const _flag = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/helpers/flag"));
16
+ const _objectisomorphic = require("../../lib/helpers/object.isomorphic");
17
+ const _page = require("../../lib/helpers/page");
18
+ const _request = require("../../lib/helpers/request");
19
+ const _ux = require("../../lib/helpers/ux");
20
+ const _messagetype = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/marshal/message-type"));
21
+ const _runcontext = require("../../lib/run-context");
22
+ function _define_property(obj, key, value) {
23
+ if (key in obj) {
24
+ Object.defineProperty(obj, key, {
25
+ value: value,
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true
29
+ });
30
+ } else {
31
+ obj[key] = value;
32
+ }
33
+ return obj;
34
+ }
35
+ function _interop_require_default(obj) {
36
+ return obj && obj.__esModule ? obj : {
37
+ default: obj
38
+ };
39
+ }
40
+ function _getRequireWildcardCache(nodeInterop) {
41
+ if (typeof WeakMap !== "function") return null;
42
+ var cacheBabelInterop = new WeakMap();
43
+ var cacheNodeInterop = new WeakMap();
44
+ return (_getRequireWildcardCache = function(nodeInterop) {
45
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
46
+ })(nodeInterop);
47
+ }
48
+ function _interop_require_wildcard(obj, nodeInterop) {
49
+ if (!nodeInterop && obj && obj.__esModule) {
50
+ return obj;
51
+ }
52
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
53
+ return {
54
+ default: obj
55
+ };
56
+ }
57
+ var cache = _getRequireWildcardCache(nodeInterop);
58
+ if (cache && cache.has(obj)) {
59
+ return cache.get(obj);
60
+ }
61
+ var newObj = {
62
+ __proto__: null
63
+ };
64
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
65
+ for(var key in obj){
66
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
67
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
68
+ if (desc && (desc.get || desc.set)) {
69
+ Object.defineProperty(newObj, key, desc);
70
+ } else {
71
+ newObj[key] = obj[key];
72
+ }
73
+ }
74
+ }
75
+ newObj.default = obj;
76
+ if (cache) {
77
+ cache.set(obj, newObj);
78
+ }
79
+ return newObj;
80
+ }
81
+ class MessageTypePull extends _basecommand.default {
82
+ async run() {
83
+ const { args, flags } = this.props;
84
+ if (flags.all && args.messageTypeKey) {
85
+ return this.error(`messageTypeKey arg \`${args.messageTypeKey}\` cannot also be provided when using --all`);
86
+ }
87
+ return flags.all ? this.pullAllMessageTypes() : this.pullOneMessageType();
88
+ }
89
+ /*
90
+ * Pull one message type
91
+ */ async pullOneMessageType() {
92
+ const { flags } = this.props;
93
+ // 1. Retrieve or build a new message type directory context.
94
+ const dirContext = await this.getMessageTypeDirContext();
95
+ if (dirContext.exists) {
96
+ this.log(`‣ Found \`${dirContext.key}\` at ${dirContext.abspath}`);
97
+ } else {
98
+ const prompt = `Create a new message type directory \`${dirContext.key}\` at ${dirContext.abspath}?`;
99
+ const input = flags.force || await (0, _ux.promptToConfirm)(prompt);
100
+ if (!input) return;
101
+ }
102
+ // 2. Fetch the message type with annotations.
103
+ const resp = await (0, _request.withSpinner)(()=>{
104
+ const props = (0, _objectisomorphic.merge)(this.props, {
105
+ args: {
106
+ messageTypeKey: dirContext.key
107
+ },
108
+ flags: {
109
+ annotate: true
110
+ }
111
+ });
112
+ return this.apiV1.getMessageType(props);
113
+ });
114
+ await _messagetype.writeMessageTypeDirFromData(dirContext, resp.data);
115
+ const action = dirContext.exists ? "updated" : "created";
116
+ this.log(`‣ Successfully ${action} \`${dirContext.key}\` at ${dirContext.abspath}`);
117
+ }
118
+ async getMessageTypeDirContext() {
119
+ const { messageTypeKey } = this.props.args;
120
+ const { resourceDir, cwd: runCwd } = this.runContext;
121
+ // Inside an existing resource dir, use it if valid for the target message
122
+ // type.
123
+ if (resourceDir) {
124
+ const target = {
125
+ commandId: _basecommand.default.id,
126
+ type: "message_type",
127
+ key: messageTypeKey
128
+ };
129
+ return (0, _runcontext.ensureResourceDirForTarget)(resourceDir, target);
130
+ }
131
+ // Not inside any existing message type directory, which means either create
132
+ // a new message type directory in the cwd, or update it if there is one
133
+ // already.
134
+ if (messageTypeKey) {
135
+ const dirPath = _nodepath.resolve(runCwd, messageTypeKey);
136
+ const exists = await _messagetype.isMessageTypeDir(dirPath);
137
+ return {
138
+ type: "message_type",
139
+ key: messageTypeKey,
140
+ abspath: dirPath,
141
+ exists
142
+ };
143
+ }
144
+ // Not in any message type directory, nor a message type key arg was given
145
+ // so error.
146
+ return this.error("Missing 1 required arg:\nmessageTypeKey");
147
+ }
148
+ /*
149
+ * Pull all message types
150
+ */ async pullAllMessageTypes() {
151
+ const { flags } = this.props;
152
+ const defaultToCwd = {
153
+ abspath: this.runContext.cwd,
154
+ exists: true
155
+ };
156
+ const targetDirCtx = flags["message-types-dir"] || defaultToCwd;
157
+ const prompt = targetDirCtx.exists ? `Pull latest message types into ${targetDirCtx.abspath}?\n This will overwrite the contents of this directory.` : `Create a new message types directory at ${targetDirCtx.abspath}?`;
158
+ const input = flags.force || await (0, _ux.promptToConfirm)(prompt);
159
+ if (!input) return;
160
+ _ux.spinner.start(`‣ Loading`);
161
+ const messageTypes = await this.listAllMessageTypes();
162
+ await _messagetype.writeMessageTypesIndexDir(targetDirCtx, messageTypes);
163
+ _ux.spinner.stop();
164
+ const action = targetDirCtx.exists ? "updated" : "created";
165
+ this.log(`‣ Successfully ${action} the message types directory at ${targetDirCtx.abspath}`);
166
+ }
167
+ async listAllMessageTypes(pageParams = {}, messageTypesFetchedSoFar = []) {
168
+ const props = (0, _objectisomorphic.merge)(this.props, {
169
+ flags: {
170
+ ...pageParams,
171
+ annotate: true,
172
+ limit: _page.MAX_PAGINATION_LIMIT
173
+ }
174
+ });
175
+ const resp = await this.apiV1.listMessageTypes(props);
176
+ if (!(0, _request.isSuccessResp)(resp)) {
177
+ const message = (0, _request.formatErrorRespMessage)(resp);
178
+ this.error(new _error.ApiError(message));
179
+ }
180
+ const { entries, page_info: pageInfo } = resp.data;
181
+ const messageTypes = [
182
+ ...messageTypesFetchedSoFar,
183
+ ...entries
184
+ ];
185
+ return pageInfo.after ? this.listAllMessageTypes({
186
+ after: pageInfo.after
187
+ }, messageTypes) : messageTypes;
188
+ }
189
+ }
190
+ // Hide until guides are released in GA.
191
+ _define_property(MessageTypePull, "hidden", true);
192
+ _define_property(MessageTypePull, "summary", "Pull one or more in-app message types from an environment into a local file system.");
193
+ _define_property(MessageTypePull, "flags", {
194
+ environment: _core.Flags.string({
195
+ default: "development",
196
+ summary: "The environment to use."
197
+ }),
198
+ all: _core.Flags.boolean({
199
+ summary: "Whether to pull all in-app message types from the specified environment."
200
+ }),
201
+ "message-types-dir": _flag.dirPath({
202
+ summary: "The target directory path to pull all in-app message types into.",
203
+ dependsOn: [
204
+ "all"
205
+ ]
206
+ }),
207
+ "hide-uncommitted-changes": _core.Flags.boolean({
208
+ summary: "Hide any uncommitted changes."
209
+ }),
210
+ force: _core.Flags.boolean({
211
+ summary: "Remove the confirmation prompt."
212
+ })
213
+ });
214
+ _define_property(MessageTypePull, "args", {
215
+ messageTypeKey: _core.Args.string({
216
+ required: false
217
+ })
218
+ });
@@ -0,0 +1,171 @@
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 MessageTypePush;
9
+ }
10
+ });
11
+ const _core = require("@oclif/core");
12
+ const _basecommand = /*#__PURE__*/ _interop_require_default(require("../../lib/base-command"));
13
+ const _const = require("../../lib/helpers/const");
14
+ const _error = require("../../lib/helpers/error");
15
+ const _flag = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/helpers/flag"));
16
+ const _objectisomorphic = require("../../lib/helpers/object.isomorphic");
17
+ const _request = require("../../lib/helpers/request");
18
+ const _string = require("../../lib/helpers/string");
19
+ const _ux = require("../../lib/helpers/ux");
20
+ const _messagetype = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/marshal/message-type"));
21
+ const _validate = /*#__PURE__*/ _interop_require_default(require("./validate"));
22
+ function _define_property(obj, key, value) {
23
+ if (key in obj) {
24
+ Object.defineProperty(obj, key, {
25
+ value: value,
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true
29
+ });
30
+ } else {
31
+ obj[key] = value;
32
+ }
33
+ return obj;
34
+ }
35
+ function _interop_require_default(obj) {
36
+ return obj && obj.__esModule ? obj : {
37
+ default: obj
38
+ };
39
+ }
40
+ function _getRequireWildcardCache(nodeInterop) {
41
+ if (typeof WeakMap !== "function") return null;
42
+ var cacheBabelInterop = new WeakMap();
43
+ var cacheNodeInterop = new WeakMap();
44
+ return (_getRequireWildcardCache = function(nodeInterop) {
45
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
46
+ })(nodeInterop);
47
+ }
48
+ function _interop_require_wildcard(obj, nodeInterop) {
49
+ if (!nodeInterop && obj && obj.__esModule) {
50
+ return obj;
51
+ }
52
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
53
+ return {
54
+ default: obj
55
+ };
56
+ }
57
+ var cache = _getRequireWildcardCache(nodeInterop);
58
+ if (cache && cache.has(obj)) {
59
+ return cache.get(obj);
60
+ }
61
+ var newObj = {
62
+ __proto__: null
63
+ };
64
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
65
+ for(var key in obj){
66
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
67
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
68
+ if (desc && (desc.get || desc.set)) {
69
+ Object.defineProperty(newObj, key, desc);
70
+ } else {
71
+ newObj[key] = obj[key];
72
+ }
73
+ }
74
+ }
75
+ newObj.default = obj;
76
+ if (cache) {
77
+ cache.set(obj, newObj);
78
+ }
79
+ return newObj;
80
+ }
81
+ class MessageTypePush extends _basecommand.default {
82
+ async run() {
83
+ const { flags } = this.props;
84
+ // 1. First read all message type directories found for the given command.
85
+ const target = await _messagetype.ensureValidCommandTarget(this.props, this.runContext);
86
+ const [messageTypes, readErrors] = await _messagetype.readAllForCommandTarget(target, {
87
+ withExtractedFiles: true
88
+ });
89
+ if (readErrors.length > 0) {
90
+ this.error((0, _error.formatErrors)(readErrors, {
91
+ prependBy: "\n\n"
92
+ }));
93
+ }
94
+ if (messageTypes.length === 0) {
95
+ this.error(`No message type directories found in ${target.context.abspath}`);
96
+ }
97
+ // 2. Then validate them all ahead of pushing them.
98
+ _ux.spinner.start(`‣ Validating`);
99
+ const apiErrors = await _validate.default.validateAll(this.apiV1, this.props, messageTypes);
100
+ if (apiErrors.length > 0) {
101
+ this.error((0, _error.formatErrors)(apiErrors, {
102
+ prependBy: "\n\n"
103
+ }));
104
+ }
105
+ _ux.spinner.stop();
106
+ // 3. Finally push up each message type, abort on the first error.
107
+ _ux.spinner.start(`‣ Pushing`);
108
+ for (const messageType of messageTypes){
109
+ const props = (0, _objectisomorphic.merge)(this.props, {
110
+ flags: {
111
+ annotate: true
112
+ }
113
+ });
114
+ // eslint-disable-next-line no-await-in-loop
115
+ const resp = await this.apiV1.upsertMessageType(props, {
116
+ ...messageType.content,
117
+ key: messageType.key
118
+ });
119
+ if ((0, _request.isSuccessResp)(resp)) {
120
+ // Update the message type directory with the successfully pushed message
121
+ // type payload from the server.
122
+ // eslint-disable-next-line no-await-in-loop
123
+ await _messagetype.writeMessageTypeDirFromData(messageType, resp.data.message_type);
124
+ continue;
125
+ }
126
+ const error = new _error.SourceError((0, _request.formatErrorRespMessage)(resp), _messagetype.messageTypeJsonPath(messageType), "ApiError");
127
+ this.error((0, _error.formatError)(error));
128
+ }
129
+ _ux.spinner.stop();
130
+ // 4. Display a success message.
131
+ const messageTypeKeys = messageTypes.map((w)=>w.key);
132
+ const actioned = flags.commit ? "pushed and committed" : "pushed";
133
+ this.log(`‣ Successfully ${actioned} ${messageTypes.length} message type(s):\n` + (0, _string.indentString)(messageTypeKeys.join("\n"), 4));
134
+ }
135
+ }
136
+ // Hide until guides are released in GA.
137
+ _define_property(MessageTypePush, "hidden", true);
138
+ _define_property(MessageTypePush, "summary", "Push one or more message types from a local file system to Knock.");
139
+ _define_property(MessageTypePush, "flags", {
140
+ environment: _core.Flags.string({
141
+ summary: "Pushing a message type is only allowed in the development environment",
142
+ default: _const.KnockEnv.Development,
143
+ options: [
144
+ _const.KnockEnv.Development
145
+ ]
146
+ }),
147
+ all: _core.Flags.boolean({
148
+ summary: "Whether to push all message types from the target directory."
149
+ }),
150
+ "message-types-dir": _flag.dirPath({
151
+ summary: "The target directory path to find all message types to push.",
152
+ dependsOn: [
153
+ "all"
154
+ ]
155
+ }),
156
+ commit: _core.Flags.boolean({
157
+ summary: "Push and commit the message type(s) at the same time"
158
+ }),
159
+ "commit-message": _core.Flags.string({
160
+ summary: "Use the given value as the commit message",
161
+ char: "m",
162
+ dependsOn: [
163
+ "commit"
164
+ ]
165
+ })
166
+ });
167
+ _define_property(MessageTypePush, "args", {
168
+ messageTypeKey: _core.Args.string({
169
+ required: false
170
+ })
171
+ });
@@ -0,0 +1,148 @@
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 MessageTypeValidate;
9
+ }
10
+ });
11
+ const _core = require("@oclif/core");
12
+ const _basecommand = /*#__PURE__*/ _interop_require_default(require("../../lib/base-command"));
13
+ const _const = require("../../lib/helpers/const");
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 _ux = require("../../lib/helpers/ux");
19
+ const _messagetype = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/marshal/message-type"));
20
+ function _define_property(obj, key, value) {
21
+ if (key in obj) {
22
+ Object.defineProperty(obj, key, {
23
+ value: value,
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true
27
+ });
28
+ } else {
29
+ obj[key] = value;
30
+ }
31
+ return obj;
32
+ }
33
+ function _interop_require_default(obj) {
34
+ return obj && obj.__esModule ? obj : {
35
+ default: obj
36
+ };
37
+ }
38
+ function _getRequireWildcardCache(nodeInterop) {
39
+ if (typeof WeakMap !== "function") return null;
40
+ var cacheBabelInterop = new WeakMap();
41
+ var cacheNodeInterop = new WeakMap();
42
+ return (_getRequireWildcardCache = function(nodeInterop) {
43
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
44
+ })(nodeInterop);
45
+ }
46
+ function _interop_require_wildcard(obj, nodeInterop) {
47
+ if (!nodeInterop && obj && obj.__esModule) {
48
+ return obj;
49
+ }
50
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
51
+ return {
52
+ default: obj
53
+ };
54
+ }
55
+ var cache = _getRequireWildcardCache(nodeInterop);
56
+ if (cache && cache.has(obj)) {
57
+ return cache.get(obj);
58
+ }
59
+ var newObj = {
60
+ __proto__: null
61
+ };
62
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
63
+ for(var key in obj){
64
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
65
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
66
+ if (desc && (desc.get || desc.set)) {
67
+ Object.defineProperty(newObj, key, desc);
68
+ } else {
69
+ newObj[key] = obj[key];
70
+ }
71
+ }
72
+ }
73
+ newObj.default = obj;
74
+ if (cache) {
75
+ cache.set(obj, newObj);
76
+ }
77
+ return newObj;
78
+ }
79
+ class MessageTypeValidate extends _basecommand.default {
80
+ async run() {
81
+ // 1. Read all message type directories found for the given command.
82
+ const target = await _messagetype.ensureValidCommandTarget(this.props, this.runContext);
83
+ const [messageTypes, readErrors] = await _messagetype.readAllForCommandTarget(target, {
84
+ withExtractedFiles: true
85
+ });
86
+ if (readErrors.length > 0) {
87
+ this.error((0, _error.formatErrors)(readErrors, {
88
+ prependBy: "\n\n"
89
+ }));
90
+ }
91
+ if (messageTypes.length === 0) {
92
+ this.error(`No message type directories found in ${target.context.abspath}`);
93
+ }
94
+ // 2. Validate each message type data.
95
+ _ux.spinner.start(`‣ Validating`);
96
+ const apiErrors = await MessageTypeValidate.validateAll(this.apiV1, this.props, messageTypes);
97
+ if (apiErrors.length > 0) {
98
+ this.error((0, _error.formatErrors)(apiErrors, {
99
+ prependBy: "\n\n"
100
+ }));
101
+ }
102
+ _ux.spinner.stop();
103
+ // 3. Display a success message.
104
+ const messageTypeKeys = messageTypes.map((w)=>w.key);
105
+ this.log(`‣ Successfully validated ${messageTypes.length} message type(s):\n` + (0, _string.indentString)(messageTypeKeys.join("\n"), 4));
106
+ }
107
+ static async validateAll(api, props, messageTypes) {
108
+ // TODO: Throw an error if a non validation error (e.g. authentication error)
109
+ // instead of printing out same error messages repeatedly.
110
+ const errorPromises = messageTypes.map(async (messageType)=>{
111
+ const resp = await api.validateMessageType(props, {
112
+ ...messageType.content,
113
+ key: messageType.key
114
+ });
115
+ if ((0, _request.isSuccessResp)(resp)) return;
116
+ const error = new _error.SourceError((0, _request.formatErrorRespMessage)(resp), _messagetype.messageTypeJsonPath(messageType), "ApiError");
117
+ return error;
118
+ });
119
+ const errors = (await Promise.all(errorPromises)).filter((e)=>Boolean(e));
120
+ return errors;
121
+ }
122
+ }
123
+ // Hide until guides are released in GA.
124
+ _define_property(MessageTypeValidate, "hidden", true);
125
+ _define_property(MessageTypeValidate, "summary", "Validate one or more message types from a local file system.");
126
+ _define_property(MessageTypeValidate, "flags", {
127
+ environment: _core.Flags.string({
128
+ summary: "Validating a message type is only done in the development environment",
129
+ default: _const.KnockEnv.Development,
130
+ options: [
131
+ _const.KnockEnv.Development
132
+ ]
133
+ }),
134
+ all: _core.Flags.boolean({
135
+ summary: "Whether to validate all message types from the target directory."
136
+ }),
137
+ "message-types-dir": _flag.dirPath({
138
+ summary: "The target directory path to find all message types to validate.",
139
+ dependsOn: [
140
+ "all"
141
+ ]
142
+ })
143
+ });
144
+ _define_property(MessageTypeValidate, "args", {
145
+ messageTypeKey: _core.Args.string({
146
+ required: false
147
+ })
148
+ });
@@ -89,7 +89,7 @@ class PartialPull extends _basecommand.default {
89
89
  // Pull one partial
90
90
  async pullOnePartial() {
91
91
  const { flags } = this.props;
92
- const dirContext = await this.getPartialsDirContext();
92
+ const dirContext = await this.getPartialDirContext();
93
93
  if (dirContext.exists) {
94
94
  this.log(`‣ Found \`${dirContext.key}\` at ${dirContext.abspath}`);
95
95
  } else {
@@ -152,7 +152,7 @@ class PartialPull extends _basecommand.default {
152
152
  after: pageInfo.after
153
153
  }, partials) : partials;
154
154
  }
155
- async getPartialsDirContext() {
155
+ async getPartialDirContext() {
156
156
  const { partialKey } = this.props.args;
157
157
  const { resourceDir, cwd: runCwd } = this.runContext;
158
158
  // Inside an existing resource dir, use it if valid for the target partial.
@@ -0,0 +1,138 @@
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 WorkflowGenerateTypes;
9
+ }
10
+ });
11
+ const _core = require("@oclif/core");
12
+ const _fsextra = /*#__PURE__*/ _interop_require_wildcard(require("fs-extra"));
13
+ const _basecommand = /*#__PURE__*/ _interop_require_default(require("../../lib/base-command"));
14
+ const _const = require("../../lib/helpers/const");
15
+ const _error = require("../../lib/helpers/error");
16
+ const _flag = /*#__PURE__*/ _interop_require_wildcard(require("../../lib/helpers/flag"));
17
+ const _objectisomorphic = require("../../lib/helpers/object.isomorphic");
18
+ const _page = require("../../lib/helpers/page");
19
+ const _request = require("../../lib/helpers/request");
20
+ const _ux = require("../../lib/helpers/ux");
21
+ const _typegenerator = require("../../lib/type-generator");
22
+ function _define_property(obj, key, value) {
23
+ if (key in obj) {
24
+ Object.defineProperty(obj, key, {
25
+ value: value,
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true
29
+ });
30
+ } else {
31
+ obj[key] = value;
32
+ }
33
+ return obj;
34
+ }
35
+ function _interop_require_default(obj) {
36
+ return obj && obj.__esModule ? obj : {
37
+ default: obj
38
+ };
39
+ }
40
+ function _getRequireWildcardCache(nodeInterop) {
41
+ if (typeof WeakMap !== "function") return null;
42
+ var cacheBabelInterop = new WeakMap();
43
+ var cacheNodeInterop = new WeakMap();
44
+ return (_getRequireWildcardCache = function(nodeInterop) {
45
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
46
+ })(nodeInterop);
47
+ }
48
+ function _interop_require_wildcard(obj, nodeInterop) {
49
+ if (!nodeInterop && obj && obj.__esModule) {
50
+ return obj;
51
+ }
52
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
53
+ return {
54
+ default: obj
55
+ };
56
+ }
57
+ var cache = _getRequireWildcardCache(nodeInterop);
58
+ if (cache && cache.has(obj)) {
59
+ return cache.get(obj);
60
+ }
61
+ var newObj = {
62
+ __proto__: null
63
+ };
64
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
65
+ for(var key in obj){
66
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
67
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
68
+ if (desc && (desc.get || desc.set)) {
69
+ Object.defineProperty(newObj, key, desc);
70
+ } else {
71
+ newObj[key] = obj[key];
72
+ }
73
+ }
74
+ }
75
+ newObj.default = obj;
76
+ if (cache) {
77
+ cache.set(obj, newObj);
78
+ }
79
+ return newObj;
80
+ }
81
+ class WorkflowGenerateTypes extends _basecommand.default {
82
+ async run() {
83
+ const { flags } = this.props;
84
+ const fileExtension = flags["output-file"].abspath.split(".").pop();
85
+ const targetLanguage = (0, _typegenerator.getLanguageFromExtension)(fileExtension);
86
+ if (!targetLanguage) {
87
+ this.error(new _error.ApiError(`Unsupported file extension: ${fileExtension}. We currently support .ts, .rb, .go, .py files only.`));
88
+ }
89
+ _ux.spinner.start(`‣ Loading workflows`);
90
+ // 1. List all workflows in the development environment.
91
+ const workflows = await this.listAllWorkflows();
92
+ _ux.spinner.stop();
93
+ // 2. Generate types for all workflows.
94
+ _ux.spinner.start(`‣ Generating types`);
95
+ const { result, workflows: workflowsWithValidTypes } = await (0, _typegenerator.generateWorkflowTypes)(workflows, targetLanguage);
96
+ _ux.spinner.stop();
97
+ if (!result) {
98
+ this.log(`‣ No workflows with valid trigger data JSON schema found, skipping type generation`);
99
+ return;
100
+ }
101
+ // 3. Write the generated types to the output file.
102
+ await _fsextra.writeFile(flags["output-file"].abspath, result.lines.join("\n"));
103
+ this.log(`‣ Successfully generated types for ${workflowsWithValidTypes.length} workflow(s) and wrote them to ${flags["output-file"].abspath}`);
104
+ }
105
+ async listAllWorkflows(pageParams = {}, workflowsFetchedSoFar = []) {
106
+ const props = (0, _objectisomorphic.merge)(this.props, {
107
+ flags: {
108
+ ...pageParams,
109
+ annotate: true,
110
+ limit: _page.MAX_PAGINATION_LIMIT
111
+ }
112
+ });
113
+ const resp = await this.apiV1.listWorkflows(props);
114
+ if (!(0, _request.isSuccessResp)(resp)) {
115
+ const message = (0, _request.formatErrorRespMessage)(resp);
116
+ this.error(new _error.ApiError(message));
117
+ }
118
+ const { entries, page_info: pageInfo } = resp.data;
119
+ const workflows = [
120
+ ...workflowsFetchedSoFar,
121
+ ...entries
122
+ ];
123
+ return pageInfo.after ? this.listAllWorkflows({
124
+ after: pageInfo.after
125
+ }, workflows) : workflows;
126
+ }
127
+ }
128
+ _define_property(WorkflowGenerateTypes, "description", "Generate types for all workflows in the development environment and write them to a file.");
129
+ _define_property(WorkflowGenerateTypes, "flags", {
130
+ environment: _core.Flags.string({
131
+ summary: "Select the environment to generate types for.",
132
+ default: _const.KnockEnv.Development
133
+ }),
134
+ "output-file": _flag.filePath({
135
+ summary: "The output file to write the generated types to. We currently support .ts, .rb, .go, .py files only. Your file extension will determine the target language for the generated types.",
136
+ required: true
137
+ })
138
+ });