@knocklabs/cli 0.1.18 → 0.1.20

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 (34) hide show
  1. package/README.md +132 -24
  2. package/dist/commands/message-type/get.js +124 -0
  3. package/dist/commands/message-type/list.js +101 -0
  4. package/dist/commands/message-type/pull.js +218 -0
  5. package/dist/commands/message-type/push.js +171 -0
  6. package/dist/commands/message-type/validate.js +148 -0
  7. package/dist/commands/partial/get.js +124 -0
  8. package/dist/commands/partial/list.js +103 -0
  9. package/dist/commands/partial/pull.js +209 -0
  10. package/dist/commands/partial/push.js +169 -0
  11. package/dist/commands/partial/validate.js +146 -0
  12. package/dist/lib/api-v1.js +94 -0
  13. package/dist/lib/base-command.js +1 -4
  14. package/dist/lib/helpers/const.js +3 -3
  15. package/dist/lib/helpers/error.js +3 -6
  16. package/dist/lib/helpers/liquid.js +0 -2
  17. package/dist/lib/helpers/page.js +3 -3
  18. package/dist/lib/marshal/guide/index.js +19 -0
  19. package/dist/lib/marshal/guide/processor.isomorphic.js +85 -0
  20. package/dist/lib/marshal/guide/types.js +4 -0
  21. package/dist/lib/marshal/index.isomorphic.js +12 -8
  22. package/dist/lib/marshal/message-type/helpers.js +135 -0
  23. package/dist/lib/marshal/message-type/index.js +3 -0
  24. package/dist/lib/marshal/message-type/reader.js +198 -0
  25. package/dist/lib/marshal/message-type/writer.js +175 -0
  26. package/dist/lib/marshal/partial/helpers.js +134 -0
  27. package/dist/lib/marshal/partial/index.js +3 -0
  28. package/dist/lib/marshal/partial/reader.js +198 -0
  29. package/dist/lib/marshal/partial/types.js +3 -3
  30. package/dist/lib/marshal/partial/writer.js +175 -0
  31. package/dist/lib/marshal/workflow/types.js +3 -3
  32. package/dist/lib/run-context/loader.js +35 -31
  33. package/oclif.manifest.json +776 -1
  34. package/package.json +13 -13
@@ -239,6 +239,100 @@ class ApiV1 {
239
239
  params
240
240
  });
241
241
  }
242
+ // By resources: Partials
243
+ async listPartials({ flags }) {
244
+ const params = (0, _objectisomorphic.prune)({
245
+ environment: flags.environment,
246
+ hide_uncommitted_changes: flags["hide-uncommitted-changes"],
247
+ annotate: flags.annotate,
248
+ ...(0, _page.toPageParams)(flags)
249
+ });
250
+ return this.get("/partials", {
251
+ params
252
+ });
253
+ }
254
+ async getPartial({ args, flags }) {
255
+ const params = (0, _objectisomorphic.prune)({
256
+ environment: flags.environment,
257
+ annotate: flags.annotate,
258
+ hide_uncommitted_changes: flags["hide-uncommitted-changes"]
259
+ });
260
+ return this.get(`/partials/${args.partialKey}`, {
261
+ params
262
+ });
263
+ }
264
+ async upsertPartial({ flags }, partial) {
265
+ const params = (0, _objectisomorphic.prune)({
266
+ environment: flags.environment,
267
+ annotate: flags.annotate,
268
+ commit: flags.commit,
269
+ commit_message: flags["commit-message"]
270
+ });
271
+ const data = {
272
+ partial
273
+ };
274
+ return this.put(`/partials/${partial.key}`, data, {
275
+ params
276
+ });
277
+ }
278
+ async validatePartial({ flags }, partial) {
279
+ const params = (0, _objectisomorphic.prune)({
280
+ environment: flags.environment
281
+ });
282
+ const data = {
283
+ partial
284
+ };
285
+ return this.put(`/partials/${partial.key}/validate`, data, {
286
+ params
287
+ });
288
+ }
289
+ // By resources: Message types
290
+ async listMessageTypes({ flags }) {
291
+ const params = (0, _objectisomorphic.prune)({
292
+ environment: flags.environment,
293
+ hide_uncommitted_changes: flags["hide-uncommitted-changes"],
294
+ annotate: flags.annotate,
295
+ ...(0, _page.toPageParams)(flags)
296
+ });
297
+ return this.get("/message_types", {
298
+ params
299
+ });
300
+ }
301
+ async getMessageType({ args, flags }) {
302
+ const params = (0, _objectisomorphic.prune)({
303
+ environment: flags.environment,
304
+ annotate: flags.annotate,
305
+ hide_uncommitted_changes: flags["hide-uncommitted-changes"]
306
+ });
307
+ return this.get(`/message_types/${args.messageTypeKey}`, {
308
+ params
309
+ });
310
+ }
311
+ async upsertMessageType({ flags }, messageType) {
312
+ const params = (0, _objectisomorphic.prune)({
313
+ environment: flags.environment,
314
+ annotate: flags.annotate,
315
+ commit: flags.commit,
316
+ commit_message: flags["commit-message"]
317
+ });
318
+ const data = {
319
+ message_type: messageType
320
+ };
321
+ return this.put(`/message_types/${messageType.key}`, data, {
322
+ params
323
+ });
324
+ }
325
+ async validateMessageType({ flags }, messageType) {
326
+ const params = (0, _objectisomorphic.prune)({
327
+ environment: flags.environment
328
+ });
329
+ const data = {
330
+ message_type: messageType
331
+ };
332
+ return this.put(`/message_types/${messageType.key}/validate`, data, {
333
+ params
334
+ });
335
+ }
242
336
  // By methods:
243
337
  async get(subpath, config) {
244
338
  return this.client.get(`/${API_VERSION}` + subpath, config);
@@ -88,10 +88,7 @@ class BaseCommand extends _core.Command {
88
88
  this.runContext = await _runcontext.load(this.id);
89
89
  }
90
90
  constructor(...args){
91
- super(...args);
92
- _define_property(this, "props", void 0);
93
- _define_property(this, "apiV1", void 0);
94
- _define_property(this, "runContext", void 0);
91
+ super(...args), _define_property(this, "props", void 0), _define_property(this, "apiV1", void 0), _define_property(this, "runContext", void 0);
95
92
  }
96
93
  }
97
94
  // Base flags are inherited by any command that extends BaseCommand.
@@ -68,10 +68,10 @@ function _interop_require_wildcard(obj, nodeInterop) {
68
68
  }
69
69
  return newObj;
70
70
  }
71
- var KnockEnv;
72
- (function(KnockEnv) {
71
+ var KnockEnv = /*#__PURE__*/ function(KnockEnv) {
73
72
  KnockEnv["Development"] = "development";
74
73
  KnockEnv["Production"] = "production";
75
- })(KnockEnv || (KnockEnv = {}));
74
+ return KnockEnv;
75
+ }({});
76
76
  const isTestEnv = process.env.NODE_ENV === "test";
77
77
  const sandboxDir = _nodepath.resolve(_fsextra.realpathSync(_nodeos.default.tmpdir()), ".knock");
@@ -63,16 +63,14 @@ class JsonSyntaxError extends CustomError {
63
63
  }
64
64
  class JsonDataError extends CustomError {
65
65
  constructor(message, objPath){
66
- super(message);
67
- // For example: `foo.bar[2].baz`
66
+ super(message), // For example: `foo.bar[2].baz`
68
67
  _define_property(this, "objPath", void 0);
69
68
  this.objPath = objPath;
70
69
  }
71
70
  }
72
71
  class LiquidParseError extends CustomError {
73
72
  constructor(message, context){
74
- super(message);
75
- // Shows the erroneous liquid content with line numbers, should be taken
73
+ super(message), // Shows the erroneous liquid content with line numbers, should be taken
76
74
  // directly from a LiquidError.
77
75
  _define_property(this, "context", void 0);
78
76
  this.context = context;
@@ -80,8 +78,7 @@ class LiquidParseError extends CustomError {
80
78
  }
81
79
  class SourceError extends CustomError {
82
80
  constructor(message, source, tag){
83
- super(tag ? `${tag}: ${message}` : message);
84
- // Arbitrary string to describe the identifying source of the error message.
81
+ super(tag ? `${tag}: ${message}` : message), // Arbitrary string to describe the identifying source of the error message.
85
82
  _define_property(this, "source", void 0);
86
83
  this.source = source;
87
84
  }
@@ -19,8 +19,6 @@ const disabled = {
19
19
  throw new Error(`"${token.name}" tag is not supported in Knock`);
20
20
  }
21
21
  };
22
- // TODO(KNO-6058) - Enable render tag for partials
23
- engine.registerTag("render", disabled);
24
22
  engine.registerTag("include", disabled);
25
23
  function validateLiquidSyntax(input) {
26
24
  try {
@@ -58,11 +58,11 @@ const pageFlags = {
58
58
  const toPageParams = (flags)=>{
59
59
  return (0, _lodash.pick)(flags, Object.keys(pageFlags));
60
60
  };
61
- var PageAction;
62
- (function(PageAction) {
61
+ var PageAction = /*#__PURE__*/ function(PageAction) {
63
62
  PageAction["Previous"] = "p";
64
63
  PageAction["Next"] = "n";
65
- })(PageAction || (PageAction = {}));
64
+ return PageAction;
65
+ }({});
66
66
  /*
67
67
  * Format a prompt text to show available page actions.
68
68
  * e.g. [p: preview, n: next]
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ _export_star(require("./processor.isomorphic"), exports);
6
+ _export_star(require("./types"), exports);
7
+ function _export_star(from, to) {
8
+ Object.keys(from).forEach(function(k) {
9
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
10
+ Object.defineProperty(to, k, {
11
+ enumerable: true,
12
+ get: function() {
13
+ return from[k];
14
+ }
15
+ });
16
+ }
17
+ });
18
+ return from;
19
+ }
@@ -0,0 +1,85 @@
1
+ /*
2
+ * IMPORTANT:
3
+ *
4
+ * This file is suffixed with `.isomorphic` because the code in this file is
5
+ * meant to run not just in a nodejs environment but also in a browser. For this
6
+ * reason there are some restrictions for which nodejs imports are allowed in
7
+ * this module. See `.eslintrc.json` for more details.
8
+ */ "use strict";
9
+ Object.defineProperty(exports, "__esModule", {
10
+ value: true
11
+ });
12
+ function _export(target, all) {
13
+ for(var name in all)Object.defineProperty(target, name, {
14
+ enumerable: true,
15
+ get: all[name]
16
+ });
17
+ }
18
+ _export(exports, {
19
+ GUIDE_JSON: function() {
20
+ return GUIDE_JSON;
21
+ },
22
+ buildGuideDirBundle: function() {
23
+ return buildGuideDirBundle;
24
+ }
25
+ });
26
+ const _lodash = require("lodash");
27
+ const _objectisomorphic = require("../../helpers/object.isomorphic");
28
+ const _constisomorphic = require("../shared/const.isomorphic");
29
+ const _helpersisomorphic = require("../shared/helpers.isomorphic");
30
+ const GUIDE_JSON = "guide.json";
31
+ const compileExtractionSettings = (guide)=>{
32
+ const extractableFields = (0, _lodash.get)(guide, [
33
+ "__annotation",
34
+ "extractable_fields"
35
+ ], {});
36
+ const map = new Map();
37
+ for (const [key] of Object.entries(guide)){
38
+ // If the field we are on is extractable, then add its extraction settings
39
+ // to the map with the current object path.
40
+ if (key in extractableFields) {
41
+ map.set([
42
+ key
43
+ ], extractableFields[key]);
44
+ }
45
+ }
46
+ return map;
47
+ };
48
+ const buildGuideDirBundle = (remoteGuide, localGuide = {})=>{
49
+ const bundle = {};
50
+ const mutRemoteGuide = (0, _lodash.cloneDeep)(remoteGuide);
51
+ // A map of extraction settings of every field in the guide.
52
+ const compiledExtractionSettings = compileExtractionSettings(mutRemoteGuide);
53
+ // Iterate through each extractable field, determine whether we need to
54
+ // extract the field content, and if so, perform the extraction.
55
+ for (const [objPathParts, extractionSettings] of compiledExtractionSettings){
56
+ // If this field path does not exist, then we don't extract.
57
+ if (!(0, _lodash.has)(mutRemoteGuide, objPathParts)) continue;
58
+ // If the field at this path is extracted in the local guide, then always
59
+ // extract; otherwise extract based on the field settings default.
60
+ const objPathStr = _objectisomorphic.ObjPath.stringify(objPathParts);
61
+ const extractedFilePath = (0, _lodash.get)(localGuide, `${objPathStr}${_constisomorphic.FILEPATH_MARKER}`);
62
+ const { default: extractByDefault, file_ext: fileExt } = extractionSettings;
63
+ if (!extractedFilePath && !extractByDefault) continue;
64
+ // By this point, we have a field where we need to extract its content.
65
+ const data = (0, _lodash.get)(mutRemoteGuide, objPathParts);
66
+ const fileName = objPathParts.pop();
67
+ // If we have an extracted file path from the local guide, we use that;
68
+ // otherwise use the default path.
69
+ const relpath = typeof extractedFilePath === "string" ? extractedFilePath : `${fileName}.${fileExt}`;
70
+ // Perform the extraction by adding the content and its file path to the
71
+ // bundle for writing to the file system later. Then replace the field
72
+ // content with the extracted file path and mark the field as extracted
73
+ // with @ suffix.
74
+ (0, _lodash.set)(bundle, [
75
+ relpath
76
+ ], data);
77
+ (0, _lodash.set)(mutRemoteGuide, `${objPathStr}${_constisomorphic.FILEPATH_MARKER}`, relpath);
78
+ (0, _lodash.unset)(mutRemoteGuide, objPathStr);
79
+ }
80
+ // At this point the bundle contains all extractable files, so we finally add
81
+ // the guide JSON relative path + the file content.
82
+ return (0, _lodash.set)(bundle, [
83
+ GUIDE_JSON
84
+ ], (0, _helpersisomorphic.prepareResourceJson)(mutRemoteGuide));
85
+ };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
@@ -14,21 +14,25 @@ _export(exports, {
14
14
  buildEmailLayoutDirBundle: function() {
15
15
  return _processorisomorphic.buildEmailLayoutDirBundle;
16
16
  },
17
+ buildGuideDirBundle: function() {
18
+ return _processorisomorphic1.buildGuideDirBundle;
19
+ },
17
20
  buildMessageTypeDirBundle: function() {
18
- return _processorisomorphic1.buildMessageTypeDirBundle;
21
+ return _processorisomorphic2.buildMessageTypeDirBundle;
19
22
  },
20
23
  buildPartialDirBundle: function() {
21
- return _processorisomorphic2.buildPartialDirBundle;
24
+ return _processorisomorphic3.buildPartialDirBundle;
22
25
  },
23
26
  buildTranslationDirBundle: function() {
24
- return _processorisomorphic3.buildTranslationDirBundle;
27
+ return _processorisomorphic4.buildTranslationDirBundle;
25
28
  },
26
29
  buildWorkflowDirBundle: function() {
27
- return _processorisomorphic4.buildWorkflowDirBundle;
30
+ return _processorisomorphic5.buildWorkflowDirBundle;
28
31
  }
29
32
  });
30
33
  const _processorisomorphic = require("./email-layout/processor.isomorphic");
31
- const _processorisomorphic1 = require("./message-type/processor.isomorphic");
32
- const _processorisomorphic2 = require("./partial/processor.isomorphic");
33
- const _processorisomorphic3 = require("./translation/processor.isomorphic");
34
- const _processorisomorphic4 = require("./workflow/processor.isomorphic");
34
+ const _processorisomorphic1 = require("./guide/processor.isomorphic");
35
+ const _processorisomorphic2 = require("./message-type/processor.isomorphic");
36
+ const _processorisomorphic3 = require("./partial/processor.isomorphic");
37
+ const _processorisomorphic4 = require("./translation/processor.isomorphic");
38
+ const _processorisomorphic5 = require("./workflow/processor.isomorphic");
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ ensureValidCommandTarget: function() {
13
+ return ensureValidCommandTarget;
14
+ },
15
+ isMessageTypeDir: function() {
16
+ return isMessageTypeDir;
17
+ },
18
+ lsMessageTypeJson: function() {
19
+ return lsMessageTypeJson;
20
+ },
21
+ messageTypeJsonPath: function() {
22
+ return messageTypeJsonPath;
23
+ }
24
+ });
25
+ const _nodepath = /*#__PURE__*/ _interop_require_wildcard(require("node:path"));
26
+ const _core = require("@oclif/core");
27
+ const _fsextra = /*#__PURE__*/ _interop_require_wildcard(require("fs-extra"));
28
+ const _processorisomorphic = require("./processor.isomorphic");
29
+ function _getRequireWildcardCache(nodeInterop) {
30
+ if (typeof WeakMap !== "function") return null;
31
+ var cacheBabelInterop = new WeakMap();
32
+ var cacheNodeInterop = new WeakMap();
33
+ return (_getRequireWildcardCache = function(nodeInterop) {
34
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
35
+ })(nodeInterop);
36
+ }
37
+ function _interop_require_wildcard(obj, nodeInterop) {
38
+ if (!nodeInterop && obj && obj.__esModule) {
39
+ return obj;
40
+ }
41
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
42
+ return {
43
+ default: obj
44
+ };
45
+ }
46
+ var cache = _getRequireWildcardCache(nodeInterop);
47
+ if (cache && cache.has(obj)) {
48
+ return cache.get(obj);
49
+ }
50
+ var newObj = {
51
+ __proto__: null
52
+ };
53
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
54
+ for(var key in obj){
55
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
56
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
57
+ if (desc && (desc.get || desc.set)) {
58
+ Object.defineProperty(newObj, key, desc);
59
+ } else {
60
+ newObj[key] = obj[key];
61
+ }
62
+ }
63
+ }
64
+ newObj.default = obj;
65
+ if (cache) {
66
+ cache.set(obj, newObj);
67
+ }
68
+ return newObj;
69
+ }
70
+ const messageTypeJsonPath = (messageTypeDirCtx)=>_nodepath.resolve(messageTypeDirCtx.abspath, _processorisomorphic.MESSAGE_TYPE_JSON);
71
+ const lsMessageTypeJson = async (dirPath)=>{
72
+ const messageTypeJsonPath = _nodepath.resolve(dirPath, _processorisomorphic.MESSAGE_TYPE_JSON);
73
+ const exists = await _fsextra.pathExists(messageTypeJsonPath);
74
+ return exists ? messageTypeJsonPath : undefined;
75
+ };
76
+ const isMessageTypeDir = async (dirPath)=>Boolean(await lsMessageTypeJson(dirPath));
77
+ const ensureValidCommandTarget = async (props, runContext)=>{
78
+ const { args, flags } = props;
79
+ const { commandId, resourceDir: resourceDirCtx, cwd: runCwd } = runContext;
80
+ // If the target resource is a different type than the current resource dir
81
+ // type, error out.
82
+ if (resourceDirCtx && resourceDirCtx.type !== "message_type") {
83
+ return _core.ux.error(`Cannot run ${commandId} inside a ${resourceDirCtx.type} directory`);
84
+ }
85
+ // Cannot accept both message type key arg and --all flag.
86
+ if (flags.all && args.messageTypeKey) {
87
+ return _core.ux.error(`messageTypeKey arg \`${args.messageTypeKey}\` cannot also be provided when using --all`);
88
+ }
89
+ // --all flag is given, which means no message type key arg.
90
+ if (flags.all) {
91
+ // If --all flag used inside a message type directory, then require a message
92
+ // types dir path.
93
+ if (resourceDirCtx && !flags["message-types-dir"]) {
94
+ return _core.ux.error("Missing required flag message-types-dir");
95
+ }
96
+ // Targeting all message type dirs in the message types index dir.
97
+ // TODO: Default to the knock project config first if present before cwd.
98
+ const defaultToCwd = {
99
+ abspath: runCwd,
100
+ exists: true
101
+ };
102
+ const indexDirCtx = flags["message-types-dir"] || defaultToCwd;
103
+ return {
104
+ type: "messageTypesIndexDir",
105
+ context: indexDirCtx
106
+ };
107
+ }
108
+ // Message type key arg is given, which means no --all flag.
109
+ if (args.messageTypeKey) {
110
+ if (resourceDirCtx && resourceDirCtx.key !== args.messageTypeKey) {
111
+ return _core.ux.error(`Cannot run ${commandId} \`${args.messageTypeKey}\` inside another message type directory:\n${resourceDirCtx.key}`);
112
+ }
113
+ const targetDirPath = resourceDirCtx ? resourceDirCtx.abspath : _nodepath.resolve(runCwd, args.messageTypeKey);
114
+ const messageTypeDirCtx = {
115
+ type: "message_type",
116
+ key: args.messageTypeKey,
117
+ abspath: targetDirPath,
118
+ exists: await isMessageTypeDir(targetDirPath)
119
+ };
120
+ return {
121
+ type: "messageTypeDir",
122
+ context: messageTypeDirCtx
123
+ };
124
+ }
125
+ // From this point on, we have neither a message type key arg nor --all flag.
126
+ // If running inside a message type directory, then use that message type
127
+ // directory.
128
+ if (resourceDirCtx) {
129
+ return {
130
+ type: "messageTypeDir",
131
+ context: resourceDirCtx
132
+ };
133
+ }
134
+ return _core.ux.error("Missing 1 required arg:\nmessageTypeKey");
135
+ };
@@ -2,8 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
+ _export_star(require("./helpers"), exports);
5
6
  _export_star(require("./processor.isomorphic"), exports);
7
+ _export_star(require("./reader"), exports);
6
8
  _export_star(require("./types"), exports);
9
+ _export_star(require("./writer"), exports);
7
10
  function _export_star(from, to) {
8
11
  Object.keys(from).forEach(function(k) {
9
12
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ readAllForCommandTarget: function() {
13
+ return readAllForCommandTarget;
14
+ },
15
+ readMessageTypeDir: function() {
16
+ return readMessageTypeDir;
17
+ }
18
+ });
19
+ const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
20
+ const _core = require("@oclif/core");
21
+ const _fsextra = /*#__PURE__*/ _interop_require_wildcard(require("fs-extra"));
22
+ const _lodash = require("lodash");
23
+ const _error = require("../../helpers/error");
24
+ const _json = require("../../helpers/json");
25
+ const _objectisomorphic = require("../../helpers/object.isomorphic");
26
+ const _helpers = require("../shared/helpers");
27
+ const _constisomorphic = require("../shared/const.isomorphic");
28
+ const _helpers1 = require("./helpers");
29
+ const _processorisomorphic = require("./processor.isomorphic");
30
+ function _interop_require_default(obj) {
31
+ return obj && obj.__esModule ? obj : {
32
+ default: obj
33
+ };
34
+ }
35
+ function _getRequireWildcardCache(nodeInterop) {
36
+ if (typeof WeakMap !== "function") return null;
37
+ var cacheBabelInterop = new WeakMap();
38
+ var cacheNodeInterop = new WeakMap();
39
+ return (_getRequireWildcardCache = function(nodeInterop) {
40
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
41
+ })(nodeInterop);
42
+ }
43
+ function _interop_require_wildcard(obj, nodeInterop) {
44
+ if (!nodeInterop && obj && obj.__esModule) {
45
+ return obj;
46
+ }
47
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
48
+ return {
49
+ default: obj
50
+ };
51
+ }
52
+ var cache = _getRequireWildcardCache(nodeInterop);
53
+ if (cache && cache.has(obj)) {
54
+ return cache.get(obj);
55
+ }
56
+ var newObj = {
57
+ __proto__: null
58
+ };
59
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
60
+ for(var key in obj){
61
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
62
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
63
+ if (desc && (desc.get || desc.set)) {
64
+ Object.defineProperty(newObj, key, desc);
65
+ } else {
66
+ newObj[key] = obj[key];
67
+ }
68
+ }
69
+ }
70
+ newObj.default = obj;
71
+ if (cache) {
72
+ cache.set(obj, newObj);
73
+ }
74
+ return newObj;
75
+ }
76
+ /*
77
+ * For the given list of message type directory contexts, read each message type dir and
78
+ * return message type directory data.
79
+ */ const readMessageTypeDirs = async (messageTypeDirCtxs, opts = {})=>{
80
+ const messageTypes = [];
81
+ const errors = [];
82
+ for (const messageTypeDirCtx of messageTypeDirCtxs){
83
+ // eslint-disable-next-line no-await-in-loop
84
+ const [messageType, readErrors] = await readMessageTypeDir(messageTypeDirCtx, opts);
85
+ if (readErrors.length > 0) {
86
+ const messageTypeJsonPath = _nodepath.default.resolve(messageTypeDirCtx.abspath, _processorisomorphic.MESSAGE_TYPE_JSON);
87
+ const e = new _error.SourceError((0, _error.formatErrors)(readErrors), messageTypeJsonPath);
88
+ errors.push(e);
89
+ continue;
90
+ }
91
+ messageTypes.push({
92
+ ...messageTypeDirCtx,
93
+ content: messageType
94
+ });
95
+ }
96
+ return [
97
+ messageTypes,
98
+ errors
99
+ ];
100
+ };
101
+ const readMessageTypeDir = async (messageTypeDirCtx, opts = {})=>{
102
+ const { abspath } = messageTypeDirCtx;
103
+ const { withExtractedFiles = false, withReadonlyField = false } = opts;
104
+ const dirExists = await _fsextra.pathExists(abspath);
105
+ if (!dirExists) throw new Error(`${abspath} does not exist`);
106
+ const messageTypeJsonPath = await (0, _helpers1.lsMessageTypeJson)(abspath);
107
+ if (!messageTypeJsonPath) throw new Error(`${abspath} is not a message type directory`);
108
+ const result = await (0, _json.readJson)(messageTypeJsonPath);
109
+ if (!result[0]) return result;
110
+ let [messageTypeJson] = result;
111
+ messageTypeJson = withReadonlyField ? messageTypeJson : (0, _objectisomorphic.omitDeep)(messageTypeJson, [
112
+ "__readonly"
113
+ ]);
114
+ return withExtractedFiles ? joinExtractedFiles(messageTypeDirCtx, messageTypeJson) : [
115
+ messageTypeJson,
116
+ []
117
+ ];
118
+ };
119
+ const joinExtractedFiles = async (messageTypeDirCtx, messageTypeJson)=>{
120
+ // Tracks any errors encountered during traversal. Mutated in place.
121
+ const errors = [];
122
+ // Tracks each new valid extracted file path seen (rebased to be relative to
123
+ // message_type.json) in the message type json node. Mutated in place, and used
124
+ // to validate the uniqueness of an extracted path encountered.
125
+ const uniqueFilePaths = {};
126
+ (0, _objectisomorphic.mapValuesDeep)(messageTypeJson, (relpath, key, parts)=>{
127
+ // If not marked with the @suffix, there's nothing to do.
128
+ if (!_constisomorphic.FILEPATH_MARKED_RE.test(key)) return;
129
+ const objPathToFieldStr = _objectisomorphic.ObjPath.stringify(parts);
130
+ const inlinObjPathStr = objPathToFieldStr.replace(_constisomorphic.FILEPATH_MARKED_RE, "");
131
+ // If there is inlined content present already, then nothing more to do.
132
+ if ((0, _lodash.hasIn)(messageTypeJson, inlinObjPathStr)) return;
133
+ // Check if the extracted path found at the current field path is valid
134
+ const invalidFilePathError = (0, _helpers.validateExtractedFilePath)(relpath, _nodepath.default.resolve(messageTypeDirCtx.abspath, _processorisomorphic.MESSAGE_TYPE_JSON), uniqueFilePaths, objPathToFieldStr);
135
+ if (invalidFilePathError) {
136
+ errors.push(invalidFilePathError);
137
+ // Wipe the invalid file path in the node so the final message type json
138
+ // object ends up with only valid file paths, this way message type writer
139
+ // can see only valid file paths and use those when pulling.
140
+ (0, _lodash.set)(messageTypeJson, inlinObjPathStr, undefined);
141
+ (0, _lodash.set)(messageTypeJson, objPathToFieldStr, undefined);
142
+ return;
143
+ }
144
+ // By this point we have a valid extracted file path, so attempt to read the file.
145
+ const [content, readExtractedFileError] = (0, _helpers.readExtractedFileSync)(relpath, messageTypeDirCtx, key);
146
+ if (readExtractedFileError) {
147
+ errors.push(readExtractedFileError);
148
+ // If there's an error, replace the extracted file path with the original one, and set the
149
+ // inlined field path in message type object with empty content, so we know
150
+ // we do not need to try inlining again.
151
+ (0, _lodash.set)(messageTypeJson, objPathToFieldStr, relpath);
152
+ (0, _lodash.set)(messageTypeJson, inlinObjPathStr, undefined);
153
+ return;
154
+ }
155
+ // Inline the file content and remove the extracted file path.
156
+ (0, _lodash.set)(messageTypeJson, objPathToFieldStr, relpath);
157
+ (0, _lodash.set)(messageTypeJson, inlinObjPathStr, content);
158
+ });
159
+ return [
160
+ messageTypeJson,
161
+ errors
162
+ ];
163
+ };
164
+ const readAllForCommandTarget = async (target, opts = {})=>{
165
+ const { type: targetType, context: targetCtx } = target;
166
+ if (!targetCtx.exists) {
167
+ const subject = targetType === "messageTypeDir" ? "a message type directory at" : "message type directories in";
168
+ return _core.ux.error(`Cannot locate ${subject} \`${targetCtx.abspath}\``);
169
+ }
170
+ switch(targetType){
171
+ case "messageTypeDir":
172
+ {
173
+ return readMessageTypeDirs([
174
+ targetCtx
175
+ ], opts);
176
+ }
177
+ case "messageTypesIndexDir":
178
+ {
179
+ const dirents = await _fsextra.readdir(targetCtx.abspath, {
180
+ withFileTypes: true
181
+ });
182
+ const promises = dirents.map(async (dirent)=>{
183
+ const abspath = _nodepath.default.resolve(targetCtx.abspath, dirent.name);
184
+ const messageTypeDirCtx = {
185
+ type: "message_type",
186
+ key: dirent.name,
187
+ abspath,
188
+ exists: await (0, _helpers1.isMessageTypeDir)(abspath)
189
+ };
190
+ return messageTypeDirCtx;
191
+ });
192
+ const messageTypeDirCtxs = (await Promise.all(promises)).filter((messageTypeDirCtx)=>messageTypeDirCtx.exists);
193
+ return readMessageTypeDirs(messageTypeDirCtxs, opts);
194
+ }
195
+ default:
196
+ throw new Error(`Invalid message type command target: ${target}`);
197
+ }
198
+ };