massarg 1.0.7-pre.1 → 2.0.0-pre.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.
package/command.js ADDED
@@ -0,0 +1,319 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MassargHelpCommand = exports.MassargCommand = exports.CommandConfig = void 0;
4
+ const zod_1 = require("zod");
5
+ const error_1 = require("./error");
6
+ const help_1 = require("./help");
7
+ const option_1 = require("./option");
8
+ const utils_1 = require("./utils");
9
+ const example_1 = require("./example");
10
+ const CommandConfig = (args) => zod_1.z.object({
11
+ /** Command name */
12
+ name: zod_1.z.string(),
13
+ /** Command description, displayed in the help output */
14
+ description: zod_1.z.string(),
15
+ /** Command aliases */
16
+ aliases: zod_1.z.string().array().optional(),
17
+ /**
18
+ * Function used when invoking this command. It receives the parsed options and the primary
19
+ * instance of Massarg used to invoke this command (the top-level instance)
20
+ */
21
+ run: zod_1.z
22
+ .function()
23
+ .args(args, zod_1.z.any())
24
+ .returns(zod_1.z.union([zod_1.z.promise(zod_1.z.void()), zod_1.z.void()])),
25
+ });
26
+ exports.CommandConfig = CommandConfig;
27
+ /**
28
+ * A command is a named function that can be invoked with a set of options.
29
+ *
30
+ * Commands can have sub-commands, which can have their own sub-commands, and so on.
31
+ *
32
+ * Options are not inherited by sub-commands, but their parsed values are passed down when
33
+ * invoking a sub-command. This works recursively.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * massarg(options).command({
38
+ * name: 'foo',
39
+ * description: 'foo command',
40
+ * run: (options, instance) => {
41
+ * console.log(options, instance)
42
+ * },
43
+ * })
44
+ * ```
45
+ */
46
+ class MassargCommand {
47
+ constructor(options, parent) {
48
+ this.commands = [];
49
+ this.options = [];
50
+ this.examples = [];
51
+ this.args = {};
52
+ (0, exports.CommandConfig)(zod_1.z.any()).parse(options);
53
+ this.name = options.name;
54
+ this.description = options.description;
55
+ this.aliases = options.aliases ?? [];
56
+ this._run = options.run;
57
+ this._helpConfig = {};
58
+ this.parent = parent;
59
+ }
60
+ get helpConfig() {
61
+ if (this.parent) {
62
+ return (0, utils_1.deepMerge)(this.parent.helpConfig, this._helpConfig);
63
+ }
64
+ return (0, utils_1.deepMerge)(help_1.defaultHelpConfig, this._helpConfig);
65
+ }
66
+ command(config) {
67
+ try {
68
+ const command = config instanceof MassargCommand ? config : new MassargCommand(config);
69
+ const existing = this.commands.find((c) => c.name === command.name);
70
+ if (existing) {
71
+ throw new error_1.ValidationError({
72
+ code: 'duplicate_command',
73
+ message: `Command "${command.name}" already exists`,
74
+ path: [this.name, command.name],
75
+ });
76
+ }
77
+ command.parent = this;
78
+ this.commands.push(command);
79
+ return this;
80
+ }
81
+ catch (e) {
82
+ if ((0, error_1.isZodError)(e)) {
83
+ throw new error_1.ValidationError({
84
+ path: [this.name, config.name, ...e.issues[0].path.map((p) => p.toString())],
85
+ code: e.issues[0].code,
86
+ message: e.issues[0].message,
87
+ });
88
+ }
89
+ throw e;
90
+ }
91
+ }
92
+ flag(config) {
93
+ try {
94
+ const flag = config instanceof option_1.MassargFlag ? config : new option_1.MassargFlag(config);
95
+ const existing = this.options.find((c) => c.name === flag.name);
96
+ if (existing) {
97
+ throw new error_1.ValidationError({
98
+ code: 'duplicate_flag',
99
+ message: `Flag "${flag.name}" already exists`,
100
+ path: [this.name, flag.name],
101
+ });
102
+ }
103
+ this.options.push(flag);
104
+ return this;
105
+ }
106
+ catch (e) {
107
+ if ((0, error_1.isZodError)(e)) {
108
+ throw new error_1.ValidationError({
109
+ path: [this.name, config.name, ...e.issues[0].path.map((p) => p.toString())],
110
+ code: e.issues[0].code,
111
+ message: e.issues[0].message,
112
+ });
113
+ }
114
+ throw e;
115
+ }
116
+ }
117
+ option(config) {
118
+ try {
119
+ const option = config instanceof option_1.MassargOption
120
+ ? config
121
+ : option_1.MassargOption.fromTypedConfig(config);
122
+ const existing = this.options.find((c) => c.name === option.name);
123
+ if (existing) {
124
+ throw new error_1.ValidationError({
125
+ code: 'duplicate_option',
126
+ message: `Option "${option.name}" already exists`,
127
+ path: [this.name, option.name],
128
+ });
129
+ }
130
+ if (option.isDefault) {
131
+ const defaultOption = this.options.find((o) => o.isDefault);
132
+ if (defaultOption) {
133
+ throw new error_1.ValidationError({
134
+ code: 'duplicate_default_option',
135
+ message: `Option "${option.name}" cannot be set as default because option "${defaultOption.name}" is already set as default`,
136
+ path: [this.name, option.name],
137
+ });
138
+ }
139
+ }
140
+ this.options.push(option);
141
+ return this;
142
+ }
143
+ catch (e) {
144
+ if ((0, error_1.isZodError)(e)) {
145
+ throw new error_1.ValidationError({
146
+ path: [this.name, config.name, ...e.issues[0].path.map((p) => p.toString())],
147
+ code: e.issues[0].code,
148
+ message: e.issues[0].message,
149
+ });
150
+ }
151
+ throw e;
152
+ }
153
+ }
154
+ /**
155
+ * Adds an example to this command.
156
+ *
157
+ * An example is a description of how to use the command, with an example input and output.
158
+ *
159
+ * At least one of `description`, `input` or `output` must be provided, but neither alone is
160
+ * required.
161
+ */
162
+ example(config) {
163
+ this.examples.push(new example_1.MassargExample(config));
164
+ return this;
165
+ }
166
+ /**
167
+ * Configure the help output for this (and all child) commands.
168
+ *
169
+ * You can automatically bind the help command to this command, and/or bind the help option
170
+ * to this command.
171
+ *
172
+ * If you don't opt-in to this behavior with `bindCommand` or `bindOption`, you can still
173
+ * access the help output via `this.helpString()` and `this.printHelp()`.
174
+ */
175
+ help(config) {
176
+ this._helpConfig = help_1.HelpConfig.parse(config);
177
+ if (this.helpConfig.bindCommand) {
178
+ this.command(new MassargHelpCommand());
179
+ }
180
+ if (this.helpConfig.bindOption) {
181
+ this.option(new option_1.MassargHelpFlag());
182
+ }
183
+ return this;
184
+ }
185
+ /**
186
+ * Configure the main function for this command. This command will run when no sub-commands
187
+ * are provided.
188
+ *
189
+ * If none is provided, help will be printed.
190
+ */
191
+ main(run) {
192
+ this._run = run;
193
+ return this;
194
+ }
195
+ /**
196
+ * Parse the given arguments and run the command or sub-commands along with the given options
197
+ * and flags.
198
+ *
199
+ * To parse the arguments without running any commands and only get the output args,
200
+ * use `getArgs` instead.
201
+ */
202
+ parse(argv, args, parent) {
203
+ this.getArgs(argv, args, parent, true);
204
+ }
205
+ parseOption(arg, argv) {
206
+ const option = this.options.find((o) => o._match(arg));
207
+ if (!option) {
208
+ throw new error_1.ValidationError({
209
+ path: [option_1.MassargOption.getName(arg)],
210
+ code: 'unknown_option',
211
+ message: 'Unknown option',
212
+ });
213
+ }
214
+ const res = option._parseDetails([arg, ...argv], { ...this.args });
215
+ this.args[res.key] = (0, utils_1.setOrPush)(res.value, this.args[res.key], option.isArray);
216
+ return res.argv;
217
+ }
218
+ getArgs(argv, args, parent, parseCommands = false) {
219
+ let _args = { ...this.args, ...args };
220
+ let _argv = [...argv];
221
+ const _a = this.args;
222
+ // fill defaults
223
+ for (const option of this.options) {
224
+ if (option.defaultValue !== undefined && _a[option.name] === undefined) {
225
+ _args[option.getOutputName()] = option.defaultValue;
226
+ }
227
+ }
228
+ // parse options
229
+ while (_argv.length) {
230
+ const arg = _argv.shift();
231
+ // make sure option exists
232
+ const found = this.options.some((o) => o._isOption(arg));
233
+ if (found) {
234
+ _argv = this.parseOption(arg, _argv);
235
+ _args = { ..._args, ...this.args };
236
+ continue;
237
+ }
238
+ // if not, try see if it's a command
239
+ const command = this.commands.find((c) => c.name === arg || c.aliases.includes(arg));
240
+ if (command) {
241
+ // this is dry run, just exit
242
+ if (!parseCommands) {
243
+ return command.getArgs(_argv, this.args, parent ?? this, false);
244
+ // break
245
+ }
246
+ // this is real run, parse command, pass unparsed args
247
+ return command.parse(_argv, this.args, parent ?? this);
248
+ }
249
+ // default option - passes arg value even without flag name
250
+ const defaultOption = this.options.find((o) => o.isDefault);
251
+ if (defaultOption) {
252
+ _argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv]);
253
+ continue;
254
+ }
255
+ // not parsed by any step, add to extra key
256
+ _a.extra ??= [];
257
+ _a.extra.push(arg);
258
+ }
259
+ // merge args
260
+ this.args = { ...this.args, ..._args };
261
+ // dry run, just exit
262
+ if (!parseCommands) {
263
+ return this.args;
264
+ }
265
+ // no sub command found, run main command
266
+ if (this._run) {
267
+ this._run(this.args, parent ?? this);
268
+ }
269
+ }
270
+ /**
271
+ * Generate the help output for this command, and return it as a string.
272
+ */
273
+ helpString() {
274
+ return new help_1.HelpGenerator(this).generate();
275
+ }
276
+ /**
277
+ * Print the help output for this command.
278
+ */
279
+ printHelp() {
280
+ console.log(this.helpString());
281
+ }
282
+ }
283
+ exports.MassargCommand = MassargCommand;
284
+ class MassargHelpCommand extends MassargCommand {
285
+ constructor(config = {}) {
286
+ super({
287
+ name: 'help',
288
+ aliases: ['h'],
289
+ description: 'Print help for this command, or a subcommand if specified',
290
+ run: (args, parent) => {
291
+ if (args.command) {
292
+ const command = parent.commands.find((c) => c.name === args.command);
293
+ if (command) {
294
+ command.printHelp();
295
+ return;
296
+ }
297
+ else {
298
+ throw new error_1.ParseError({
299
+ path: ['command'],
300
+ code: 'unknown_command',
301
+ message: 'Unknown command',
302
+ received: args.command,
303
+ });
304
+ }
305
+ }
306
+ parent.printHelp();
307
+ },
308
+ ...config,
309
+ });
310
+ this.option({
311
+ name: 'command',
312
+ aliases: ['c'],
313
+ description: 'Command to print help for',
314
+ isDefault: true,
315
+ });
316
+ }
317
+ }
318
+ exports.MassargHelpCommand = MassargHelpCommand;
319
+ //# sourceMappingURL=command.js.map
package/command.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.js","sourceRoot":"","sources":["../src/command.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AACvB,mCAAiE;AACjE,iCAAqE;AACrE,qCAMiB;AACjB,mCAA4D;AAC5D,uCAAyD;AAElD,MAAM,aAAa,GAAG,CAA0C,IAAwB,EAAE,EAAE,CACjG,OAAC,CAAC,MAAM,CAAC;IACP,mBAAmB;IACnB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;IAChB,wDAAwD;IACxD,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;4BACnB,IAAI,EAAE,CAAC,SAAS,CAAC;4BACjB,IAAI,EAAE,iBAAiB;4BACvB,OAAO,EAAE,iBAAiB;4BAC1B,QAAQ,EAAE,IAAI,CAAC,OAAO;yBACvB,CAAC,CAAA;qBACH;iBACF;gBACD,MAAM,CAAC,SAAS,EAAE,CAAA;YACpB,CAAC;YACD,GAAG,MAAM;SACV,CAAC,CAAA;QACF,IAAI,CAAC,MAAM,CAAC;YACV,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,CAAC,GAAG,CAAC;YACd,WAAW,EAAE,2BAA2B;YACxC,SAAS,EAAE,IAAI;SAChB,CAAC,CAAA;IACJ,CAAC;CACF;AAlCD,gDAkCC"}
package/error.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { z } from 'zod';
2
+ export type ValidationErrorOptions = {
3
+ path: string[];
4
+ code: string;
5
+ message: string;
6
+ };
7
+ /** This error is thrown when a validation fails. */
8
+ export declare class ValidationError extends Error {
9
+ /** The path to the value that failed validation. */
10
+ path: string[];
11
+ /** The error code. */
12
+ code: string;
13
+ /** The error message. */
14
+ message: string;
15
+ constructor({ path, code, message }: ValidationErrorOptions);
16
+ }
17
+ export type ParseErrorOptions = {
18
+ path: string[];
19
+ code: string;
20
+ message: string;
21
+ received?: unknown;
22
+ };
23
+ /** This error is thrown when a parse fails on an option value. */
24
+ export declare class ParseError extends Error {
25
+ /** The path to the value that failed parsing. */
26
+ path: string[];
27
+ /** The error code. */
28
+ code: string;
29
+ /** The error message. */
30
+ message: string;
31
+ /** The value that failed parsing. */
32
+ received: unknown;
33
+ constructor({ path, code, message, received }: ParseErrorOptions);
34
+ }
35
+ export declare function isZodError(e: unknown): e is z.ZodError;
36
+ //# sourceMappingURL=error.d.ts.map
package/error.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,MAAM,MAAM,sBAAsB,GAAG;IAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA;AAEtF,oDAAoD;AACpD,qBAAa,eAAgB,SAAQ,KAAK;IACxC,oDAAoD;IACpD,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,yBAAyB;IACzB,OAAO,EAAE,MAAM,CAAA;gBAEH,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,sBAAsB;CAQ5D;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,kEAAkE;AAClE,qBAAa,UAAW,SAAQ,KAAK;IACnC,iDAAiD;IACjD,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,yBAAyB;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,qCAAqC;IACrC,QAAQ,EAAE,OAAO,CAAA;gBAEL,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,iBAAiB;CAYjE;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAEtD"}
package/error.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isZodError = exports.ParseError = exports.ValidationError = void 0;
4
+ const zod_1 = require("zod");
5
+ /** This error is thrown when a validation fails. */
6
+ class ValidationError extends Error {
7
+ constructor({ path, code, message }) {
8
+ const msg = `${path.join('.')}: ${message}`;
9
+ super(msg);
10
+ this.path = path;
11
+ this.code = code;
12
+ this.message = msg;
13
+ this.name = 'ValidationError';
14
+ }
15
+ }
16
+ exports.ValidationError = ValidationError;
17
+ /** This error is thrown when a parse fails on an option value. */
18
+ class ParseError extends Error {
19
+ constructor({ path, code, message, received }) {
20
+ let msg = `${path.join('.')}: ${message}`;
21
+ if (received) {
22
+ msg += ` (received: ${received})`;
23
+ }
24
+ super(msg);
25
+ this.path = path;
26
+ this.code = code;
27
+ this.message = msg;
28
+ this.name = 'ParseError';
29
+ this.received = received;
30
+ }
31
+ }
32
+ exports.ParseError = ParseError;
33
+ function isZodError(e) {
34
+ return e instanceof zod_1.z.ZodError;
35
+ }
36
+ exports.isZodError = isZodError;
37
+ //# sourceMappingURL=error.js.map
package/error.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAAA,6BAAuB;AAIvB,oDAAoD;AACpD,MAAa,eAAgB,SAAQ,KAAK;IAQxC,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAA0B;QACzD,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAA;QAC3C,KAAK,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,OAAO,GAAG,GAAG,CAAA;QAClB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAA;IAC/B,CAAC;CACF;AAhBD,0CAgBC;AASD,kEAAkE;AAClE,MAAa,UAAW,SAAQ,KAAK;IAUnC,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAqB;QAC9D,IAAI,GAAG,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAA;QACzC,IAAI,QAAQ,EAAE;YACZ,GAAG,IAAI,eAAe,QAAQ,GAAG,CAAA;SAClC;QACD,KAAK,CAAC,GAAG,CAAC,CAAA;QACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,OAAO,GAAG,GAAG,CAAA;QAClB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAA;QACxB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;CACF;AAtBD,gCAsBC;AAED,SAAgB,UAAU,CAAC,CAAU;IACnC,OAAO,CAAC,YAAY,OAAC,CAAC,QAAQ,CAAA;AAChC,CAAC;AAFD,gCAEC"}
package/example.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import z from 'zod';
2
+ export declare const ExampleConfig: z.ZodObject<{
3
+ /** Description of the example. This will appear as a title above the input/output line(s). */
4
+ description: z.ZodOptional<z.ZodString>;
5
+ /**
6
+ * Input of the example. This will appear as a line below the description, with a `$` prefix.
7
+ * The prefix can be changed using the `help()` function on the command.
8
+ */
9
+ input: z.ZodOptional<z.ZodString>;
10
+ /**
11
+ * Output of the example. This will appear as a line below the input, with a `>` prefix.
12
+ * The prefix can be changed using the `help()` function on the command.
13
+ */
14
+ output: z.ZodOptional<z.ZodString>;
15
+ }, "strip", z.ZodTypeAny, {
16
+ description?: string | undefined;
17
+ input?: string | undefined;
18
+ output?: string | undefined;
19
+ }, {
20
+ description?: string | undefined;
21
+ input?: string | undefined;
22
+ output?: string | undefined;
23
+ }>;
24
+ export type ExampleConfig = z.infer<typeof ExampleConfig>;
25
+ export declare class MassargExample {
26
+ description: string | undefined;
27
+ input: string | undefined;
28
+ output: string | undefined;
29
+ constructor(config: ExampleConfig);
30
+ }
31
+ //# sourceMappingURL=example.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAGnB,eAAO,MAAM,aAAa;IACxB,8FAA8F;;IAE9F;;;OAGG;;IAEH;;;OAGG;;;;;;;;;;EAEH,CAAA;AACF,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAA;AAEzD,qBAAa,cAAc;IACzB,WAAW,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;gBAEd,MAAM,EAAE,aAAa;CAiBlC"}
package/example.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MassargExample = exports.ExampleConfig = void 0;
7
+ const zod_1 = __importDefault(require("zod"));
8
+ const error_1 = require("./error");
9
+ exports.ExampleConfig = zod_1.default.object({
10
+ /** Description of the example. This will appear as a title above the input/output line(s). */
11
+ description: zod_1.default.string().optional(),
12
+ /**
13
+ * Input of the example. This will appear as a line below the description, with a `$` prefix.
14
+ * The prefix can be changed using the `help()` function on the command.
15
+ */
16
+ input: zod_1.default.string().optional(),
17
+ /**
18
+ * Output of the example. This will appear as a line below the input, with a `>` prefix.
19
+ * The prefix can be changed using the `help()` function on the command.
20
+ */
21
+ output: zod_1.default.string().optional(),
22
+ });
23
+ class MassargExample {
24
+ constructor(config) {
25
+ exports.ExampleConfig.parse(config);
26
+ if (config.description === undefined &&
27
+ config.input === undefined &&
28
+ config.output === undefined) {
29
+ throw new error_1.ValidationError({
30
+ code: 'invalid_example',
31
+ message: 'Example must have at least one of description, input, or output',
32
+ path: ['example'],
33
+ });
34
+ }
35
+ this.description = config.description;
36
+ this.input = config.input;
37
+ this.output = config.output;
38
+ }
39
+ }
40
+ exports.MassargExample = MassargExample;
41
+ //# sourceMappingURL=example.js.map
package/example.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example.js","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAmB;AACnB,mCAAyC;AAE5B,QAAA,aAAa,GAAG,aAAC,CAAC,MAAM,CAAC;IACpC,8FAA8F;IAC9F,WAAW,EAAE,aAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAClC;;;OAGG;IACH,KAAK,EAAE,aAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC5B;;;OAGG;IACH,MAAM,EAAE,aAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAC9B,CAAC,CAAA;AAGF,MAAa,cAAc;IAKzB,YAAY,MAAqB;QAC/B,qBAAa,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QAC3B,IACE,MAAM,CAAC,WAAW,KAAK,SAAS;YAChC,MAAM,CAAC,KAAK,KAAK,SAAS;YAC1B,MAAM,CAAC,MAAM,KAAK,SAAS,EAC3B;YACA,MAAM,IAAI,uBAAe,CAAC;gBACxB,IAAI,EAAE,iBAAiB;gBACvB,OAAO,EAAE,iEAAiE;gBAC1E,IAAI,EAAE,CAAC,SAAS,CAAC;aAClB,CAAC,CAAA;SACH;QACD,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAA;QACrC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;IAC7B,CAAC;CACF;AAtBD,wCAsBC"}