@event-driven-io/emmett 0.20.2-alpha.3 → 0.20.2-alpha.5
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/dist/chunk-FNITWKAR.cjs +130 -0
- package/dist/chunk-FNITWKAR.cjs.map +1 -0
- package/dist/cli.cjs +40 -96
- package/dist/cli.cjs.map +1 -1
- package/dist/index.cjs +229 -451
- package/dist/index.cjs.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/validation/index.ts
|
|
2
|
+
var ValidationErrors = /* @__PURE__ */ ((ValidationErrors2) => {
|
|
3
|
+
ValidationErrors2["NOT_A_NONEMPTY_STRING"] = "NOT_A_NONEMPTY_STRING";
|
|
4
|
+
ValidationErrors2["NOT_A_POSITIVE_NUMBER"] = "NOT_A_POSITIVE_NUMBER";
|
|
5
|
+
ValidationErrors2["NOT_AN_UNSIGNED_BIGINT"] = "NOT_AN_UNSIGNED_BIGINT";
|
|
6
|
+
return ValidationErrors2;
|
|
7
|
+
})(ValidationErrors || {});
|
|
8
|
+
var isNumber = (val) => typeof val === "number" && val === val;
|
|
9
|
+
var isString = (val) => typeof val === "string";
|
|
10
|
+
var assertNotEmptyString = (value) => {
|
|
11
|
+
if (!isString(value) || value.length === 0) {
|
|
12
|
+
throw new ValidationError("NOT_A_NONEMPTY_STRING" /* NOT_A_NONEMPTY_STRING */);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
};
|
|
16
|
+
var assertPositiveNumber = (value) => {
|
|
17
|
+
if (!isNumber(value) || value <= 0) {
|
|
18
|
+
throw new ValidationError("NOT_A_POSITIVE_NUMBER" /* NOT_A_POSITIVE_NUMBER */);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
};
|
|
22
|
+
var assertUnsignedBigInt = (value) => {
|
|
23
|
+
const number = BigInt(value);
|
|
24
|
+
if (number < 0) {
|
|
25
|
+
throw new ValidationError("NOT_AN_UNSIGNED_BIGINT" /* NOT_AN_UNSIGNED_BIGINT */);
|
|
26
|
+
}
|
|
27
|
+
return number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// src/errors/index.ts
|
|
31
|
+
var isErrorConstructor = (expect) => {
|
|
32
|
+
return typeof expect === "function" && expect.prototype && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
33
|
+
expect.prototype.constructor === expect;
|
|
34
|
+
};
|
|
35
|
+
var EmmettError = class _EmmettError extends Error {
|
|
36
|
+
|
|
37
|
+
constructor(options) {
|
|
38
|
+
const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : 500;
|
|
39
|
+
const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Emmett processing`;
|
|
40
|
+
super(message);
|
|
41
|
+
this.errorCode = errorCode;
|
|
42
|
+
Object.setPrototypeOf(this, _EmmettError.prototype);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var ConcurrencyError = class _ConcurrencyError extends EmmettError {
|
|
46
|
+
constructor(current, expected, message) {
|
|
47
|
+
super({
|
|
48
|
+
errorCode: 412,
|
|
49
|
+
message: _nullishCoalesce(message, () => ( `Expected version ${expected.toString()} does not match current ${_optionalChain([current, 'optionalAccess', _ => _.toString, 'call', _2 => _2()])}`))
|
|
50
|
+
});
|
|
51
|
+
this.current = current;
|
|
52
|
+
this.expected = expected;
|
|
53
|
+
Object.setPrototypeOf(this, _ConcurrencyError.prototype);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var ValidationError = class _ValidationError extends EmmettError {
|
|
57
|
+
constructor(message) {
|
|
58
|
+
super({
|
|
59
|
+
errorCode: 400,
|
|
60
|
+
message: _nullishCoalesce(message, () => ( `Validation Error ocurred during Emmett processing`))
|
|
61
|
+
});
|
|
62
|
+
Object.setPrototypeOf(this, _ValidationError.prototype);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var IllegalStateError = class _IllegalStateError extends EmmettError {
|
|
66
|
+
constructor(message) {
|
|
67
|
+
super({
|
|
68
|
+
errorCode: 403,
|
|
69
|
+
message: _nullishCoalesce(message, () => ( `Illegal State ocurred during Emmett processing`))
|
|
70
|
+
});
|
|
71
|
+
Object.setPrototypeOf(this, _IllegalStateError.prototype);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var NotFoundError = class _NotFoundError extends EmmettError {
|
|
75
|
+
constructor(options) {
|
|
76
|
+
super({
|
|
77
|
+
errorCode: 404,
|
|
78
|
+
message: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.message]), () => ( (_optionalChain([options, 'optionalAccess', _4 => _4.id]) ? options.type ? `${options.type} with ${options.id} was not found during Emmett processing` : `State with ${options.id} was not found during Emmett processing` : _optionalChain([options, 'optionalAccess', _5 => _5.type]) ? `${options.type} was not found during Emmett processing` : "State was not found during Emmett processing")))
|
|
79
|
+
});
|
|
80
|
+
Object.setPrototypeOf(this, _NotFoundError.prototype);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// src/validation/dates.ts
|
|
85
|
+
var formatDateToUtcYYYYMMDD = (date) => {
|
|
86
|
+
const formatter = new Intl.DateTimeFormat("en-CA", {
|
|
87
|
+
timeZone: "UTC",
|
|
88
|
+
year: "numeric",
|
|
89
|
+
month: "2-digit",
|
|
90
|
+
day: "2-digit"
|
|
91
|
+
});
|
|
92
|
+
return formatter.format(date);
|
|
93
|
+
};
|
|
94
|
+
var isValidYYYYMMDD = (dateString) => {
|
|
95
|
+
const regex = /^\d{4}-\d{2}-\d{2}$/;
|
|
96
|
+
return regex.test(dateString);
|
|
97
|
+
};
|
|
98
|
+
var parseDateFromUtcYYYYMMDD = (dateString) => {
|
|
99
|
+
const date = /* @__PURE__ */ new Date(dateString + "T00:00:00Z");
|
|
100
|
+
if (!isValidYYYYMMDD(dateString)) {
|
|
101
|
+
throw new ValidationError("Invalid date format, must be yyyy-mm-dd");
|
|
102
|
+
}
|
|
103
|
+
if (isNaN(date.getTime())) {
|
|
104
|
+
throw new ValidationError("Invalid date format");
|
|
105
|
+
}
|
|
106
|
+
return date;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/config/plugins/index.ts
|
|
110
|
+
var isPluginConfig = (plugin) => plugin !== void 0 && (typeof plugin === "string" || "name" in plugin && plugin.name !== void 0 && typeof plugin.name === "string");
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
exports.formatDateToUtcYYYYMMDD = formatDateToUtcYYYYMMDD; exports.isValidYYYYMMDD = isValidYYYYMMDD; exports.parseDateFromUtcYYYYMMDD = parseDateFromUtcYYYYMMDD; exports.ValidationErrors = ValidationErrors; exports.isNumber = isNumber; exports.isString = isString; exports.assertNotEmptyString = assertNotEmptyString; exports.assertPositiveNumber = assertPositiveNumber; exports.assertUnsignedBigInt = assertUnsignedBigInt; exports.isErrorConstructor = isErrorConstructor; exports.EmmettError = EmmettError; exports.ConcurrencyError = ConcurrencyError; exports.ValidationError = ValidationError; exports.IllegalStateError = IllegalStateError; exports.NotFoundError = NotFoundError; exports.isPluginConfig = isPluginConfig;
|
|
130
|
+
//# sourceMappingURL=chunk-FNITWKAR.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/emmett/emmett/src/packages/emmett/dist/chunk-FNITWKAR.cjs","../src/validation/index.ts","../src/errors/index.ts","../src/validation/dates.ts","../src/config/plugins/index.ts"],"names":["ValidationErrors"],"mappings":"AAAA;ACEO,IAAW,iBAAA,kBAAX,CAAA,CAAWA,iBAAAA,EAAAA,GAAX;AACL,EAAAA,iBAAAA,CAAA,uBAAA,EAAA,EAAwB,uBAAA;AACxB,EAAAA,iBAAAA,CAAA,uBAAA,EAAA,EAAwB,uBAAA;AACxB,EAAAA,iBAAAA,CAAA,wBAAA,EAAA,EAAyB,wBAAA;AAHT,EAAA,OAAAA,iBAAAA;AAAA,CAAA,CAAA,CAAA,iBAAA,GAAA,CAAA,CAAA,CAAA;AAMX,IAAM,SAAA,EAAW,CAAC,GAAA,EAAA,GACvB,OAAO,IAAA,IAAQ,SAAA,GAAY,IAAA,IAAQ,GAAA;AAE9B,IAAM,SAAA,EAAW,CAAC,GAAA,EAAA,GACvB,OAAO,IAAA,IAAQ,QAAA;AAEV,IAAM,qBAAA,EAAuB,CAAC,KAAA,EAAA,GAA2B;AAC9D,EAAA,GAAA,CAAI,CAAC,QAAA,CAAS,KAAK,EAAA,GAAK,KAAA,CAAM,OAAA,IAAW,CAAA,EAAG;AAC1C,IAAA,MAAM,IAAI,eAAA,CAAgB,mDAAsC,CAAA;AAAA,EAClE;AACA,EAAA,OAAO,KAAA;AACT,CAAA;AAEO,IAAM,qBAAA,EAAuB,CAAC,KAAA,EAAA,GAA2B;AAC9D,EAAA,GAAA,CAAI,CAAC,QAAA,CAAS,KAAK,EAAA,GAAK,MAAA,GAAS,CAAA,EAAG;AAClC,IAAA,MAAM,IAAI,eAAA,CAAgB,mDAAsC,CAAA;AAAA,EAClE;AACA,EAAA,OAAO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,CAAM;AAAA,MACJ,SAAA,EAAW,GAAA;AAAA,MACX,OAAA,mBACE,OAAA,UACA,CAAA,iBAAA,EAAoB,QAAA,CAAS,QAAA,CAAS,CAAC,CAAA,wBAAA,kBAA2B,OAAA,2BAAS,QAAA,mBAAS,GAAC,CAAA;AAAA,IAAA;AARlF,IAAA;AACA,IAAA;AAWP,IAAA;AAAsD,EAAA;AAE1D;AAEO;AAA0C,EAAA;AAE7C,IAAA;AAAM,MAAA;AACO,MAAA;AACS,IAAA;AAItB,IAAA;AAAqD,EAAA;AAEzD;AAEO;AAA4C,EAAA;AAE/C,IAAA;AAAM,MAAA;AACO,MAAA;AACS,IAAA;AAItB,IAAA;AAAuD,EAAA;AAE3D;AAEO;AAAwC,EAAA;AAE3C,IAAA;AAAM,MAAA;AACO,MAAA;AASH,IAAA;AAIV,IAAA;AAAmD,EAAA;AAEvD;AFzBA;AACA;AGjFO;AAEL,EAAA;AAAmD,IAAA;AACvC,IAAA;AACJ,IAAA;AACC,IAAA;AACF,EAAA;AAIP,EAAA;AACF;AAGO;AACL,EAAA;AACA,EAAA;AACF;AAEO;AACL,EAAA;AAEA,EAAA;AACE,IAAA;AAAmE,EAAA;AAGrE,EAAA;AACE,IAAA;AAA+C,EAAA;AAGjD,EAAA;AACF;AH0EA;AACA;AInFO;AJqFP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/emmett/emmett/src/packages/emmett/dist/chunk-FNITWKAR.cjs","sourcesContent":[null,"import { ValidationError } from '../errors';\n\nexport const enum ValidationErrors {\n NOT_A_NONEMPTY_STRING = 'NOT_A_NONEMPTY_STRING',\n NOT_A_POSITIVE_NUMBER = 'NOT_A_POSITIVE_NUMBER',\n NOT_AN_UNSIGNED_BIGINT = 'NOT_AN_UNSIGNED_BIGINT',\n}\n\nexport const isNumber = (val: unknown): val is number =>\n typeof val === 'number' && val === val;\n\nexport const isString = (val: unknown): val is string =>\n typeof val === 'string';\n\nexport const assertNotEmptyString = (value: unknown): string => {\n if (!isString(value) || value.length === 0) {\n throw new ValidationError(ValidationErrors.NOT_A_NONEMPTY_STRING);\n }\n return value;\n};\n\nexport const assertPositiveNumber = (value: unknown): number => {\n if (!isNumber(value) || value <= 0) {\n throw new ValidationError(ValidationErrors.NOT_A_POSITIVE_NUMBER);\n }\n return value;\n};\n\nexport const assertUnsignedBigInt = (value: string): bigint => {\n const number = BigInt(value);\n if (number < 0) {\n throw new ValidationError(ValidationErrors.NOT_AN_UNSIGNED_BIGINT);\n }\n return number;\n};\n\nexport * from './dates';\n","import { isNumber, isString } from '../validation';\n\nexport type ErrorConstructor<ErrorType extends Error> = new (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: any[]\n) => ErrorType;\n\nexport const isErrorConstructor = <ErrorType extends Error>(\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n expect: Function,\n): expect is ErrorConstructor<ErrorType> => {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return (\n typeof expect === 'function' &&\n expect.prototype &&\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n expect.prototype.constructor === expect\n );\n};\n\nexport class EmmettError extends Error {\n public errorCode: number;\n\n constructor(\n options?: { errorCode: number; message?: string } | string | number,\n ) {\n const errorCode =\n options && typeof options === 'object' && 'errorCode' in options\n ? options.errorCode\n : isNumber(options)\n ? options\n : 500;\n const message =\n options && typeof options === 'object' && 'message' in options\n ? options.message\n : isString(options)\n ? options\n : `Error with status code '${errorCode}' ocurred during Emmett processing`;\n\n super(message);\n this.errorCode = errorCode;\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, EmmettError.prototype);\n }\n}\n\nexport class ConcurrencyError extends EmmettError {\n constructor(\n public current: string | undefined,\n public expected: string,\n message?: string,\n ) {\n super({\n errorCode: 412,\n message:\n message ??\n `Expected version ${expected.toString()} does not match current ${current?.toString()}`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, ConcurrencyError.prototype);\n }\n}\n\nexport class ValidationError extends EmmettError {\n constructor(message?: string) {\n super({\n errorCode: 400,\n message: message ?? `Validation Error ocurred during Emmett processing`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, ValidationError.prototype);\n }\n}\n\nexport class IllegalStateError extends EmmettError {\n constructor(message?: string) {\n super({\n errorCode: 403,\n message: message ?? `Illegal State ocurred during Emmett processing`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, IllegalStateError.prototype);\n }\n}\n\nexport class NotFoundError extends EmmettError {\n constructor(options?: { id: string; type: string; message?: string }) {\n super({\n errorCode: 404,\n message:\n options?.message ??\n (options?.id\n ? options.type\n ? `${options.type} with ${options.id} was not found during Emmett processing`\n : `State with ${options.id} was not found during Emmett processing`\n : options?.type\n ? `${options.type} was not found during Emmett processing`\n : 'State was not found during Emmett processing'),\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, NotFoundError.prototype);\n }\n}\n","import { ValidationError } from '../errors';\n\nexport const formatDateToUtcYYYYMMDD = (date: Date) => {\n // Use the 'en-CA' locale which formats as 'yyyy-mm-dd'\n const formatter = new Intl.DateTimeFormat('en-CA', {\n timeZone: 'UTC',\n year: 'numeric',\n month: '2-digit',\n day: '2-digit',\n });\n\n // Format the date\n return formatter.format(date);\n};\n\n// Function to validate 'yyyy-mm-dd' format\nexport const isValidYYYYMMDD = (dateString: string) => {\n const regex = /^\\d{4}-\\d{2}-\\d{2}$/;\n return regex.test(dateString);\n};\n\nexport const parseDateFromUtcYYYYMMDD = (dateString: string) => {\n const date = new Date(dateString + 'T00:00:00Z');\n\n if (!isValidYYYYMMDD(dateString)) {\n throw new ValidationError('Invalid date format, must be yyyy-mm-dd');\n }\n\n if (isNaN(date.getTime())) {\n throw new ValidationError('Invalid date format');\n }\n\n return date;\n};\n","export type EmmettPluginConfig =\n | {\n name: string;\n register: EmmettPluginRegistration[];\n }\n | string;\n\nexport type EmmettPluginType = 'cli';\n\nexport type EmmettCliPluginRegistration = { pluginType: 'cli'; path?: string };\n\nexport type EmmettPluginRegistration = EmmettCliPluginRegistration;\n\nexport type EmmettCliCommand = {\n addCommand<CliCommand>(command: CliCommand): CliCommand;\n};\n\nexport type EmmettCliPlugin = {\n pluginType: 'cli';\n name: string;\n registerCommands: (program: EmmettCliCommand) => Promise<void> | void;\n};\n\nexport type EmmettPlugin = EmmettCliPlugin;\n\nexport const isPluginConfig = (\n plugin: Partial<EmmettPluginConfig> | string | undefined,\n): plugin is EmmettPluginConfig =>\n plugin !== undefined &&\n (typeof plugin === 'string' ||\n ('name' in plugin &&\n plugin.name !== undefined &&\n typeof plugin.name === 'string'));\n"]}
|
package/dist/cli.cjs
CHANGED
|
@@ -1,51 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __export = (target, all) => {
|
|
10
|
-
for (var name in all)
|
|
11
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
-
};
|
|
13
|
-
var __copyProps = (to, from, except, desc) => {
|
|
14
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
-
for (let key of __getOwnPropNames(from))
|
|
16
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
-
}
|
|
19
|
-
return to;
|
|
20
|
-
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
2
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkFNITWKARcjs = require('./chunk-FNITWKAR.cjs');
|
|
30
6
|
|
|
31
7
|
// src/cli.ts
|
|
32
|
-
var
|
|
33
|
-
__export(cli_exports, {
|
|
34
|
-
configCommand: () => configCommand,
|
|
35
|
-
default: () => cli_default,
|
|
36
|
-
generateConfigFile: () => generateConfigFile,
|
|
37
|
-
importPluginsConfig: () => importPluginsConfig,
|
|
38
|
-
loadPlugins: () => loadPlugins,
|
|
39
|
-
registerCliPlugins: () => registerCliPlugins,
|
|
40
|
-
sampleConfig: () => sampleConfig
|
|
41
|
-
});
|
|
42
|
-
module.exports = __toCommonJS(cli_exports);
|
|
43
|
-
var import_commander3 = require("commander");
|
|
8
|
+
var _commander = require('commander');
|
|
44
9
|
|
|
45
10
|
// src/commandLine/config.ts
|
|
46
|
-
|
|
47
|
-
var
|
|
48
|
-
var
|
|
11
|
+
|
|
12
|
+
var _fs = require('fs');
|
|
13
|
+
var _process = require('process');
|
|
49
14
|
var sampleConfig = (plugins = ["emmett-expressjs"]) => {
|
|
50
15
|
const pluginsNames = plugins.length > 0 ? `[
|
|
51
16
|
${plugins.map((p) => `"${p}"`).join(",\n")}
|
|
@@ -58,7 +23,7 @@ export default {
|
|
|
58
23
|
};
|
|
59
24
|
var generateConfigFile = (configPath, collectionNames) => {
|
|
60
25
|
try {
|
|
61
|
-
(0,
|
|
26
|
+
_fs.writeFileSync.call(void 0, configPath, sampleConfig(collectionNames), "utf8");
|
|
62
27
|
console.log(`Configuration file stored at: ${configPath}`);
|
|
63
28
|
} catch (error) {
|
|
64
29
|
console.error(`Error: Couldn't store config file: ${configPath}!`);
|
|
@@ -66,7 +31,7 @@ var generateConfigFile = (configPath, collectionNames) => {
|
|
|
66
31
|
process.exit(1);
|
|
67
32
|
}
|
|
68
33
|
};
|
|
69
|
-
var configCommand = new
|
|
34
|
+
var configCommand = new (0, _commander.Command)("config").description(
|
|
70
35
|
"Manage Pongo configuration"
|
|
71
36
|
);
|
|
72
37
|
configCommand.command("sample").description("Generate or print sample configuration").option(
|
|
@@ -85,7 +50,7 @@ configCommand.command("sample").description("Generate or print sample configurat
|
|
|
85
50
|
console.error(
|
|
86
51
|
"Error: Please provide either:\n--print param to print sample config or\n--generate to generate sample config file"
|
|
87
52
|
);
|
|
88
|
-
(0,
|
|
53
|
+
_process.exit.call(void 0, 1);
|
|
89
54
|
}
|
|
90
55
|
if ("print" in options) {
|
|
91
56
|
console.log(`${sampleConfig(plugins)}`);
|
|
@@ -94,36 +59,15 @@ configCommand.command("sample").description("Generate or print sample configurat
|
|
|
94
59
|
console.error(
|
|
95
60
|
"Error: You need to provide a config file through a --file"
|
|
96
61
|
);
|
|
97
|
-
(0,
|
|
62
|
+
_process.exit.call(void 0, 1);
|
|
98
63
|
}
|
|
99
64
|
generateConfigFile(options.file, plugins);
|
|
100
65
|
}
|
|
101
66
|
});
|
|
102
67
|
|
|
103
68
|
// src/commandLine/plugins.ts
|
|
104
|
-
var import_commander2 = require("commander");
|
|
105
|
-
var import_path = __toESM(require("path"), 1);
|
|
106
|
-
|
|
107
|
-
// src/config/plugins/index.ts
|
|
108
|
-
var isPluginConfig = (plugin) => plugin !== void 0 && (typeof plugin === "string" || "name" in plugin && plugin.name !== void 0 && typeof plugin.name === "string");
|
|
109
|
-
|
|
110
|
-
// src/validation/index.ts
|
|
111
|
-
var isNumber = (val) => typeof val === "number" && val === val;
|
|
112
|
-
var isString = (val) => typeof val === "string";
|
|
113
|
-
|
|
114
|
-
// src/errors/index.ts
|
|
115
|
-
var EmmettError = class _EmmettError extends Error {
|
|
116
|
-
errorCode;
|
|
117
|
-
constructor(options) {
|
|
118
|
-
const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : 500;
|
|
119
|
-
const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Emmett processing`;
|
|
120
|
-
super(message);
|
|
121
|
-
this.errorCode = errorCode;
|
|
122
|
-
Object.setPrototypeOf(this, _EmmettError.prototype);
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
69
|
|
|
126
|
-
|
|
70
|
+
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
127
71
|
var PluginsConfigImportError = {
|
|
128
72
|
missingDefaultExport: `Error: Config should contain default export, e.g.
|
|
129
73
|
|
|
@@ -134,31 +78,31 @@ ${sampleConfig()}`,
|
|
|
134
78
|
wrongPluginStructure: `Error: Plugin config should be either string with plugin name or object with plugin name, e.g. { name: 'emmett-expressjs' }`
|
|
135
79
|
};
|
|
136
80
|
var importPluginsConfig = async (options) => {
|
|
137
|
-
const configPath =
|
|
81
|
+
const configPath = _path2.default.join(
|
|
138
82
|
process.cwd(),
|
|
139
|
-
options
|
|
83
|
+
_nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.configPath]), () => ( "./dist/emmett.config.js"))
|
|
140
84
|
);
|
|
141
85
|
console.log("IMPORTING" + configPath);
|
|
142
86
|
try {
|
|
143
|
-
const imported = await
|
|
87
|
+
const imported = await Promise.resolve().then(() => _interopRequireWildcard(require(configPath)));
|
|
144
88
|
if (!imported.default) {
|
|
145
|
-
return new EmmettError(PluginsConfigImportError.missingDefaultExport);
|
|
89
|
+
return new (0, _chunkFNITWKARcjs.EmmettError)(PluginsConfigImportError.missingDefaultExport);
|
|
146
90
|
}
|
|
147
91
|
if (!imported.default.plugins || !Array.isArray(imported.default.plugins)) {
|
|
148
|
-
return new EmmettError(
|
|
92
|
+
return new (0, _chunkFNITWKARcjs.EmmettError)(
|
|
149
93
|
PluginsConfigImportError.missingPluginsPropertyExport
|
|
150
94
|
);
|
|
151
95
|
}
|
|
152
|
-
if (!imported.default.plugins.every(isPluginConfig)) {
|
|
153
|
-
return new EmmettError(PluginsConfigImportError.wrongPluginStructure);
|
|
96
|
+
if (!imported.default.plugins.every(_chunkFNITWKARcjs.isPluginConfig)) {
|
|
97
|
+
return new (0, _chunkFNITWKARcjs.EmmettError)(PluginsConfigImportError.wrongPluginStructure);
|
|
154
98
|
}
|
|
155
99
|
return { plugins: imported.default.plugins };
|
|
156
100
|
} catch (error) {
|
|
157
|
-
if (!options
|
|
101
|
+
if (!_optionalChain([options, 'optionalAccess', _2 => _2.configPath])) {
|
|
158
102
|
console.warn("Didn`t find config file: " + configPath, error);
|
|
159
103
|
return { plugins: [] };
|
|
160
104
|
}
|
|
161
|
-
return new EmmettError(
|
|
105
|
+
return new (0, _chunkFNITWKARcjs.EmmettError)(
|
|
162
106
|
`Error: Couldn't load file:` + error.toString()
|
|
163
107
|
);
|
|
164
108
|
}
|
|
@@ -166,21 +110,21 @@ var importPluginsConfig = async (options) => {
|
|
|
166
110
|
var loadPlugins = async (options) => {
|
|
167
111
|
try {
|
|
168
112
|
const pluginsConfig = await importPluginsConfig({
|
|
169
|
-
configPath: options
|
|
113
|
+
configPath: _optionalChain([options, 'optionalAccess', _3 => _3.configPath])
|
|
170
114
|
});
|
|
171
|
-
if (pluginsConfig instanceof EmmettError) throw pluginsConfig;
|
|
115
|
+
if (pluginsConfig instanceof _chunkFNITWKARcjs.EmmettError) throw pluginsConfig;
|
|
172
116
|
if (pluginsConfig.plugins.length === 0) {
|
|
173
|
-
console.log(`No extensions specified in config ${options
|
|
117
|
+
console.log(`No extensions specified in config ${_optionalChain([options, 'optionalAccess', _4 => _4.configPath])}.`);
|
|
174
118
|
return [];
|
|
175
119
|
}
|
|
176
120
|
const pluginsToLoad = filterPluginsByType(
|
|
177
121
|
pluginsConfig.plugins,
|
|
178
|
-
options
|
|
122
|
+
_optionalChain([options, 'optionalAccess', _5 => _5.pluginType])
|
|
179
123
|
);
|
|
180
124
|
const pluginsPromises = pluginsToLoad.map(async (pluginConfig) => {
|
|
181
|
-
const importPath = getImportPath(pluginConfig, options
|
|
125
|
+
const importPath = getImportPath(pluginConfig, _optionalChain([options, 'optionalAccess', _6 => _6.pluginType]));
|
|
182
126
|
try {
|
|
183
|
-
const plugin = await
|
|
127
|
+
const plugin = await Promise.resolve().then(() => _interopRequireWildcard(require(importPath)));
|
|
184
128
|
console.info(`Loaded plugin: ${importPath}`);
|
|
185
129
|
if (!plugin.default) {
|
|
186
130
|
throw new Error(`Plugin: ${importPath} is missing default export`);
|
|
@@ -195,7 +139,7 @@ var loadPlugins = async (options) => {
|
|
|
195
139
|
(plugin) => plugin !== void 0
|
|
196
140
|
);
|
|
197
141
|
} catch (error) {
|
|
198
|
-
console.error(`Failed to load config ${options
|
|
142
|
+
console.error(`Failed to load config ${_optionalChain([options, 'optionalAccess', _7 => _7.configPath])}:`, error);
|
|
199
143
|
return [];
|
|
200
144
|
}
|
|
201
145
|
};
|
|
@@ -218,12 +162,12 @@ var getImportPath = (pluginConfig, pluginType) => {
|
|
|
218
162
|
if (typeof pluginConfig === "string") {
|
|
219
163
|
return pluginType ? `${pluginConfig}/${pluginType}` : pluginConfig;
|
|
220
164
|
}
|
|
221
|
-
const pluginSubpath = pluginConfig.register.find((r) => pluginType && r.pluginType === pluginType)
|
|
165
|
+
const pluginSubpath = _nullishCoalesce(_optionalChain([pluginConfig, 'access', _8 => _8.register, 'access', _9 => _9.find, 'call', _10 => _10((r) => pluginType && r.pluginType === pluginType), 'optionalAccess', _11 => _11.path]), () => ( pluginType));
|
|
222
166
|
return pluginSubpath ? `${pluginConfig.name}/${pluginSubpath}` : pluginConfig.name;
|
|
223
167
|
};
|
|
224
168
|
|
|
225
169
|
// src/cli.ts
|
|
226
|
-
var program = new
|
|
170
|
+
var program = new (0, _commander.Command)();
|
|
227
171
|
program.name("emmett").description("CLI tool for Emmett").option("--config <path>", "Path to the configuration file");
|
|
228
172
|
var initCLI = async () => {
|
|
229
173
|
const configIndex = process.argv.indexOf("--config");
|
|
@@ -244,13 +188,13 @@ initCLI().catch((err) => {
|
|
|
244
188
|
console.error(err);
|
|
245
189
|
});
|
|
246
190
|
var cli_default = program;
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
exports.configCommand = configCommand; exports.default = cli_default; exports.generateConfigFile = generateConfigFile; exports.importPluginsConfig = importPluginsConfig; exports.loadPlugins = loadPlugins; exports.registerCliPlugins = registerCliPlugins; exports.sampleConfig = sampleConfig;
|
|
256
200
|
//# sourceMappingURL=cli.cjs.map
|
package/dist/cli.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cli.ts","../src/commandLine/config.ts","../src/commandLine/plugins.ts","../src/config/plugins/index.ts","../src/validation/index.ts","../src/errors/index.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { Command } from 'commander';\nimport { loadPlugins, registerCliPlugins } from './commandLine';\n\nconst program = new Command();\n\nprogram\n .name('emmett')\n .description('CLI tool for Emmett')\n .option('--config <path>', 'Path to the configuration file');\n\n// Load extensions and parse CLI arguments\nconst initCLI = async () => {\n const configIndex = process.argv.indexOf('--config');\n\n const configPath =\n configIndex !== -1 && process.argv.length > configIndex + 1\n ? process.argv[configIndex + 1]\n : undefined;\n\n try {\n const plugins = await loadPlugins({\n pluginType: 'cli',\n configPath: configPath,\n });\n await registerCliPlugins(program, plugins);\n\n // Parse the CLI arguments\n program.parse(process.argv);\n } catch (err) {\n console.error(`Failed to load config from ${configPath}:`, err);\n }\n};\n\n//Initialize CLI and handle errors\ninitCLI().catch((err) => {\n console.error(`CLI initialization failed:`);\n console.error(err);\n});\n\nexport default program;\nexport * from './commandLine';\n","import { Command as CliCommand } from 'commander';\n// eslint-disable-next-line no-restricted-imports\nimport { writeFileSync } from 'node:fs';\nimport { exit } from 'process';\n\nexport const sampleConfig = (plugins: string[] = ['emmett-expressjs']) => {\n const pluginsNames =\n plugins.length > 0\n ? `[\\n${plugins.map((p) => `\"${p}\"`).join(',\\n')} \\n]`\n : '[]';\n\n return `\nexport default {\n plugins: ${pluginsNames},\n};\n`;\n};\n\nexport const generateConfigFile = (\n configPath: string,\n collectionNames: string[],\n): void => {\n try {\n writeFileSync(configPath, sampleConfig(collectionNames), 'utf8');\n console.log(`Configuration file stored at: ${configPath}`);\n } catch (error) {\n console.error(`Error: Couldn't store config file: ${configPath}!`);\n console.error(error);\n process.exit(1);\n }\n};\n\nexport const configCommand = new CliCommand('config').description(\n 'Manage Pongo configuration',\n);\n\ntype SampleConfigOptions =\n | {\n plugin: string[];\n print?: boolean;\n }\n | {\n plugin: string[];\n generate?: boolean;\n file?: string;\n };\n\nconfigCommand\n .command('sample')\n .description('Generate or print sample configuration')\n .option(\n '-plg, --plugins <name>',\n 'Specify the plugin name',\n (value: string, previous: string[]) => {\n // Accumulate plugins names into an array (explicitly typing `previous` as `string[]`)\n return previous.concat([value]);\n },\n [] as string[],\n )\n .option(\n '-f, --file <path>',\n 'Path to configuration file with collection list',\n )\n .option('-g, --generate', 'Generate sample config file')\n .option('-p, --print', 'Print sample config file')\n .action((options: SampleConfigOptions) => {\n const plugins =\n options.plugin.length > 0\n ? options.plugin\n : ['@event-driven-io/emmett-expressjs'];\n\n if (!('print' in options) && !('generate' in options)) {\n console.error(\n 'Error: Please provide either:\\n--print param to print sample config or\\n--generate to generate sample config file',\n );\n exit(1);\n }\n\n if ('print' in options) {\n console.log(`${sampleConfig(plugins)}`);\n } else if ('generate' in options) {\n if (!options.file) {\n console.error(\n 'Error: You need to provide a config file through a --file',\n );\n exit(1);\n }\n\n generateConfigFile(options.file, plugins);\n }\n });\n","import { Command as CliCommand } from 'commander';\nimport path from 'path';\nimport {\n isPluginConfig,\n type EmmettCliCommand,\n type EmmettCliPlugin,\n type EmmettPlugin,\n type EmmettPluginConfig,\n type EmmettPluginsConfig,\n type EmmettPluginType,\n} from '../config';\nimport { EmmettError } from '../errors';\nimport { sampleConfig } from './config';\n\nconst PluginsConfigImportError = {\n missingDefaultExport: `Error: Config should contain default export, e.g.\\n\\n${sampleConfig()}`,\n missingPluginsPropertyExport: `Error: Config should contain default export with plugins array, e.g.\\n\\n${sampleConfig()}`,\n wrongPluginStructure: `Error: Plugin config should be either string with plugin name or object with plugin name, e.g. { name: 'emmett-expressjs' }`,\n};\nexport const importPluginsConfig = async (options?: {\n configPath?: string | undefined;\n}): Promise<EmmettPluginsConfig | EmmettError> => {\n const configPath = path.join(\n process.cwd(),\n options?.configPath ?? './dist/emmett.config.js',\n );\n\n console.log('IMPORTING' + configPath);\n\n try {\n const imported = (await import(configPath)) as {\n default: Partial<EmmettPluginsConfig>;\n };\n\n if (!imported.default) {\n return new EmmettError(PluginsConfigImportError.missingDefaultExport);\n }\n\n if (!imported.default.plugins || !Array.isArray(imported.default.plugins)) {\n return new EmmettError(\n PluginsConfigImportError.missingPluginsPropertyExport,\n );\n }\n\n if (!imported.default.plugins.every(isPluginConfig)) {\n return new EmmettError(PluginsConfigImportError.wrongPluginStructure);\n }\n\n return { plugins: imported.default.plugins };\n } catch (error) {\n if (!options?.configPath) {\n console.warn('Didn`t find config file: ' + configPath, error);\n return { plugins: [] };\n }\n return new EmmettError(\n `Error: Couldn't load file:` + (error as Error).toString(),\n );\n }\n};\n\nexport const loadPlugins = async (options?: {\n pluginType?: EmmettPluginType;\n configPath?: string;\n}): Promise<EmmettPlugin[]> => {\n try {\n const pluginsConfig = await importPluginsConfig({\n configPath: options?.configPath,\n });\n\n if (pluginsConfig instanceof EmmettError) throw pluginsConfig;\n\n if (pluginsConfig.plugins.length === 0) {\n console.log(`No extensions specified in config ${options?.configPath}.`);\n return [];\n }\n\n const pluginsToLoad = filterPluginsByType(\n pluginsConfig.plugins,\n options?.pluginType,\n );\n\n const pluginsPromises = pluginsToLoad.map(async (pluginConfig) => {\n const importPath = getImportPath(pluginConfig, options?.pluginType);\n try {\n const plugin = (await import(importPath)) as { default: EmmettPlugin };\n\n console.info(`Loaded plugin: ${importPath}`);\n\n if (!plugin.default) {\n throw new Error(`Plugin: ${importPath} is missing default export`);\n }\n\n return plugin.default;\n } catch (error) {\n console.error(`Failed to load extension \"${importPath}\":`, error);\n return undefined;\n }\n });\n\n return (await Promise.all(pluginsPromises)).filter(\n (plugin) => plugin !== undefined,\n );\n } catch (error) {\n console.error(`Failed to load config ${options?.configPath}:`, error);\n return [];\n }\n};\n\nexport const registerCliPlugins = async (\n program: CliCommand,\n plugins: EmmettCliPlugin[],\n): Promise<void> => {\n const result: EmmettCliPlugin[] = [];\n\n for (const plugin of plugins) {\n const pluginName = plugin.name;\n\n if (!('registerCommands' in plugin)) {\n console.warn(`No registerCommands function found in ${pluginName}`);\n }\n await plugin.registerCommands(program as EmmettCliCommand);\n console.log(`Loaded extension: ${plugin.name}`);\n result.push(plugin);\n }\n};\n\nconst filterPluginsByType = (\n plugins: EmmettPluginConfig[],\n pluginType?: EmmettPluginType,\n): EmmettPluginConfig[] =>\n plugins.filter(\n (p) =>\n typeof p === 'string' ||\n (pluginType &&\n (p.register === undefined ||\n p.register.some((r) => r.pluginType === pluginType))),\n );\n\nconst getImportPath = (\n pluginConfig: EmmettPluginConfig,\n pluginType: EmmettPluginType | undefined,\n) => {\n if (typeof pluginConfig === 'string') {\n return pluginType ? `${pluginConfig}/${pluginType}` : pluginConfig;\n }\n\n const pluginSubpath =\n pluginConfig.register.find((r) => pluginType && r.pluginType === pluginType)\n ?.path ?? pluginType;\n\n return pluginSubpath\n ? `${pluginConfig.name}/${pluginSubpath}`\n : pluginConfig.name;\n};\n","export type EmmettPluginConfig =\n | {\n name: string;\n register: EmmettPluginRegistration[];\n }\n | string;\n\nexport type EmmettPluginType = 'cli';\n\nexport type EmmettCliPluginRegistration = { pluginType: 'cli'; path?: string };\n\nexport type EmmettPluginRegistration = EmmettCliPluginRegistration;\n\nexport type EmmettCliCommand = {\n addCommand<CliCommand>(command: CliCommand): CliCommand;\n};\n\nexport type EmmettCliPlugin = {\n pluginType: 'cli';\n name: string;\n registerCommands: (program: EmmettCliCommand) => Promise<void> | void;\n};\n\nexport type EmmettPlugin = EmmettCliPlugin;\n\nexport const isPluginConfig = (\n plugin: Partial<EmmettPluginConfig> | string | undefined,\n): plugin is EmmettPluginConfig =>\n plugin !== undefined &&\n (typeof plugin === 'string' ||\n ('name' in plugin &&\n plugin.name !== undefined &&\n typeof plugin.name === 'string'));\n","import { ValidationError } from '../errors';\n\nexport const enum ValidationErrors {\n NOT_A_NONEMPTY_STRING = 'NOT_A_NONEMPTY_STRING',\n NOT_A_POSITIVE_NUMBER = 'NOT_A_POSITIVE_NUMBER',\n NOT_AN_UNSIGNED_BIGINT = 'NOT_AN_UNSIGNED_BIGINT',\n}\n\nexport const isNumber = (val: unknown): val is number =>\n typeof val === 'number' && val === val;\n\nexport const isString = (val: unknown): val is string =>\n typeof val === 'string';\n\nexport const assertNotEmptyString = (value: unknown): string => {\n if (!isString(value) || value.length === 0) {\n throw new ValidationError(ValidationErrors.NOT_A_NONEMPTY_STRING);\n }\n return value;\n};\n\nexport const assertPositiveNumber = (value: unknown): number => {\n if (!isNumber(value) || value <= 0) {\n throw new ValidationError(ValidationErrors.NOT_A_POSITIVE_NUMBER);\n }\n return value;\n};\n\nexport const assertUnsignedBigInt = (value: string): bigint => {\n const number = BigInt(value);\n if (number < 0) {\n throw new ValidationError(ValidationErrors.NOT_AN_UNSIGNED_BIGINT);\n }\n return number;\n};\n\nexport * from './dates';\n","import { isNumber, isString } from '../validation';\n\nexport type ErrorConstructor<ErrorType extends Error> = new (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: any[]\n) => ErrorType;\n\nexport const isErrorConstructor = <ErrorType extends Error>(\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n expect: Function,\n): expect is ErrorConstructor<ErrorType> => {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return (\n typeof expect === 'function' &&\n expect.prototype &&\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n expect.prototype.constructor === expect\n );\n};\n\nexport class EmmettError extends Error {\n public errorCode: number;\n\n constructor(\n options?: { errorCode: number; message?: string } | string | number,\n ) {\n const errorCode =\n options && typeof options === 'object' && 'errorCode' in options\n ? options.errorCode\n : isNumber(options)\n ? options\n : 500;\n const message =\n options && typeof options === 'object' && 'message' in options\n ? options.message\n : isString(options)\n ? options\n : `Error with status code '${errorCode}' ocurred during Emmett processing`;\n\n super(message);\n this.errorCode = errorCode;\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, EmmettError.prototype);\n }\n}\n\nexport class ConcurrencyError extends EmmettError {\n constructor(\n public current: string | undefined,\n public expected: string,\n message?: string,\n ) {\n super({\n errorCode: 412,\n message:\n message ??\n `Expected version ${expected.toString()} does not match current ${current?.toString()}`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, ConcurrencyError.prototype);\n }\n}\n\nexport class ValidationError extends EmmettError {\n constructor(message?: string) {\n super({\n errorCode: 400,\n message: message ?? `Validation Error ocurred during Emmett processing`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, ValidationError.prototype);\n }\n}\n\nexport class IllegalStateError extends EmmettError {\n constructor(message?: string) {\n super({\n errorCode: 403,\n message: message ?? `Illegal State ocurred during Emmett processing`,\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, IllegalStateError.prototype);\n }\n}\n\nexport class NotFoundError extends EmmettError {\n constructor(options?: { id: string; type: string; message?: string }) {\n super({\n errorCode: 404,\n message:\n options?.message ??\n (options?.id\n ? options.type\n ? `${options.type} with ${options.id} was not found during Emmett processing`\n : `State with ${options.id} was not found during Emmett processing`\n : options?.type\n ? `${options.type} was not found during Emmett processing`\n : 'State was not found during Emmett processing'),\n });\n\n // 👇️ because we are extending a built-in class\n Object.setPrototypeOf(this, NotFoundError.prototype);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,IAAAA,oBAAwB;;;ACDxB,uBAAsC;AAEtC,qBAA8B;AAC9B,qBAAqB;AAEd,IAAM,eAAe,CAAC,UAAoB,CAAC,kBAAkB,MAAM;AACxE,QAAM,eACJ,QAAQ,SAAS,IACb;AAAA,EAAM,QAAQ,IAAI,CAAC,MAAM,IAAI,CAAC,GAAG,EAAE,KAAK,KAAK,CAAC;AAAA,KAC9C;AAEN,SAAO;AAAA;AAAA,aAEI,YAAY;AAAA;AAAA;AAGzB;AAEO,IAAM,qBAAqB,CAChC,YACA,oBACS;AACT,MAAI;AACF,sCAAc,YAAY,aAAa,eAAe,GAAG,MAAM;A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mett/emmett/src/packages/emmett/dist/cli.cjs","../src/cli.ts","../src/commandLine/config.ts","../src/commandLine/plugins.ts"],"names":[],"mappings":"AAAA;AACA;AACE;AACA;AACF,wDAA6B;AAC7B;AACA;ACLA,sCAAwB;ADOxB;AACA;AETA;AAEA,wBAA8B;AAC9B,kCAAqB;AAEd,IAAM,aAAA,EAAe,CAAC,QAAA,EAAoB,CAAC,kBAAkB,CAAA,EAAA,GAAM;AACxE,EAAA,MAAM,aAAA,EACJ,OAAA,CAAQ,OAAA,EAAS,EAAA,EACb,CAAA;AAAA,EAAM,OAAA,CAAQ,GAAA,CAAI,CAAC,CAAA,EAAA,GAAM,CAAA,CAAA,EAAI,CAAC,CAAA,CAAA,CAAG,CAAA,CAAE,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA,CAAA,EAAA,EAC9C,IAAA;AAEN,EAAA,OAAO,CAAA;AAAA;AAAA,WAAA,EAEI,YAAY,CAAA;AAAA;AAAA,CAAA;AAGzB,CAAA;AAEO,IAAM,mBAAA,EAAqB,CAChC,UAAA,EACA,eAAA,EAAA,GACS;AACT,EAAA,IAAI;AACF,IAAA,+BAAA,UAAc,EAAY,YAAA,CAAa,eAAe,CAAA,EAAG,MAAM,CAAA;AAC/D,IAAA,OAAA,CAAQ,GAAA,CAAI,CAAA,8BAAA,EAAiC,UAAU,CAAA,CAAA;AACzC,EAAA;AACsC,IAAA;AACjC,IAAA;AACL,IAAA;AAChB,EAAA;AACF;AAEsD;AACpD,EAAA;AACF;AAee;AAEX,EAAA;AACA,EAAA;AACuC,EAAA;AAEP,IAAA;AAChC,EAAA;AACC,EAAA;AAEF;AACC,EAAA;AACA,EAAA;AAGD;AAIe,EAAA;AAGyC,EAAA;AAC7C,IAAA;AACN,MAAA;AACF,IAAA;AACM,IAAA;AACR,EAAA;AAEwB,EAAA;AACgB,IAAA;AACN,EAAA;AACb,IAAA;AACT,MAAA;AACN,QAAA;AACF,MAAA;AACM,MAAA;AACR,IAAA;AAEwC,IAAA;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,IAAA;AACwC,IAAA;AAGf,IAAA;AACd,EAAA;AAC0C,IAAA;AACxD,EAAA;AACF;AAGyB;AACmB,EAAA;AACzB,EAAA;AAClB;AAEc;ADsJ6C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/emmett/emmett/src/packages/emmett/dist/cli.cjs","sourcesContent":[null,"#!/usr/bin/env node\nimport { Command } from 'commander';\nimport { loadPlugins, registerCliPlugins } from './commandLine';\n\nconst program = new Command();\n\nprogram\n .name('emmett')\n .description('CLI tool for Emmett')\n .option('--config <path>', 'Path to the configuration file');\n\n// Load extensions and parse CLI arguments\nconst initCLI = async () => {\n const configIndex = process.argv.indexOf('--config');\n\n const configPath =\n configIndex !== -1 && process.argv.length > configIndex + 1\n ? process.argv[configIndex + 1]\n : undefined;\n\n try {\n const plugins = await loadPlugins({\n pluginType: 'cli',\n configPath: configPath,\n });\n await registerCliPlugins(program, plugins);\n\n // Parse the CLI arguments\n program.parse(process.argv);\n } catch (err) {\n console.error(`Failed to load config from ${configPath}:`, err);\n }\n};\n\n//Initialize CLI and handle errors\ninitCLI().catch((err) => {\n console.error(`CLI initialization failed:`);\n console.error(err);\n});\n\nexport default program;\nexport * from './commandLine';\n","import { Command as CliCommand } from 'commander';\n// eslint-disable-next-line no-restricted-imports\nimport { writeFileSync } from 'node:fs';\nimport { exit } from 'process';\n\nexport const sampleConfig = (plugins: string[] = ['emmett-expressjs']) => {\n const pluginsNames =\n plugins.length > 0\n ? `[\\n${plugins.map((p) => `\"${p}\"`).join(',\\n')} \\n]`\n : '[]';\n\n return `\nexport default {\n plugins: ${pluginsNames},\n};\n`;\n};\n\nexport const generateConfigFile = (\n configPath: string,\n collectionNames: string[],\n): void => {\n try {\n writeFileSync(configPath, sampleConfig(collectionNames), 'utf8');\n console.log(`Configuration file stored at: ${configPath}`);\n } catch (error) {\n console.error(`Error: Couldn't store config file: ${configPath}!`);\n console.error(error);\n process.exit(1);\n }\n};\n\nexport const configCommand = new CliCommand('config').description(\n 'Manage Pongo configuration',\n);\n\ntype SampleConfigOptions =\n | {\n plugin: string[];\n print?: boolean;\n }\n | {\n plugin: string[];\n generate?: boolean;\n file?: string;\n };\n\nconfigCommand\n .command('sample')\n .description('Generate or print sample configuration')\n .option(\n '-plg, --plugins <name>',\n 'Specify the plugin name',\n (value: string, previous: string[]) => {\n // Accumulate plugins names into an array (explicitly typing `previous` as `string[]`)\n return previous.concat([value]);\n },\n [] as string[],\n )\n .option(\n '-f, --file <path>',\n 'Path to configuration file with collection list',\n )\n .option('-g, --generate', 'Generate sample config file')\n .option('-p, --print', 'Print sample config file')\n .action((options: SampleConfigOptions) => {\n const plugins =\n options.plugin.length > 0\n ? options.plugin\n : ['@event-driven-io/emmett-expressjs'];\n\n if (!('print' in options) && !('generate' in options)) {\n console.error(\n 'Error: Please provide either:\\n--print param to print sample config or\\n--generate to generate sample config file',\n );\n exit(1);\n }\n\n if ('print' in options) {\n console.log(`${sampleConfig(plugins)}`);\n } else if ('generate' in options) {\n if (!options.file) {\n console.error(\n 'Error: You need to provide a config file through a --file',\n );\n exit(1);\n }\n\n generateConfigFile(options.file, plugins);\n }\n });\n","import { Command as CliCommand } from 'commander';\nimport path from 'path';\nimport {\n isPluginConfig,\n type EmmettCliCommand,\n type EmmettCliPlugin,\n type EmmettPlugin,\n type EmmettPluginConfig,\n type EmmettPluginsConfig,\n type EmmettPluginType,\n} from '../config';\nimport { EmmettError } from '../errors';\nimport { sampleConfig } from './config';\n\nconst PluginsConfigImportError = {\n missingDefaultExport: `Error: Config should contain default export, e.g.\\n\\n${sampleConfig()}`,\n missingPluginsPropertyExport: `Error: Config should contain default export with plugins array, e.g.\\n\\n${sampleConfig()}`,\n wrongPluginStructure: `Error: Plugin config should be either string with plugin name or object with plugin name, e.g. { name: 'emmett-expressjs' }`,\n};\nexport const importPluginsConfig = async (options?: {\n configPath?: string | undefined;\n}): Promise<EmmettPluginsConfig | EmmettError> => {\n const configPath = path.join(\n process.cwd(),\n options?.configPath ?? './dist/emmett.config.js',\n );\n\n console.log('IMPORTING' + configPath);\n\n try {\n const imported = (await import(configPath)) as {\n default: Partial<EmmettPluginsConfig>;\n };\n\n if (!imported.default) {\n return new EmmettError(PluginsConfigImportError.missingDefaultExport);\n }\n\n if (!imported.default.plugins || !Array.isArray(imported.default.plugins)) {\n return new EmmettError(\n PluginsConfigImportError.missingPluginsPropertyExport,\n );\n }\n\n if (!imported.default.plugins.every(isPluginConfig)) {\n return new EmmettError(PluginsConfigImportError.wrongPluginStructure);\n }\n\n return { plugins: imported.default.plugins };\n } catch (error) {\n if (!options?.configPath) {\n console.warn('Didn`t find config file: ' + configPath, error);\n return { plugins: [] };\n }\n return new EmmettError(\n `Error: Couldn't load file:` + (error as Error).toString(),\n );\n }\n};\n\nexport const loadPlugins = async (options?: {\n pluginType?: EmmettPluginType;\n configPath?: string;\n}): Promise<EmmettPlugin[]> => {\n try {\n const pluginsConfig = await importPluginsConfig({\n configPath: options?.configPath,\n });\n\n if (pluginsConfig instanceof EmmettError) throw pluginsConfig;\n\n if (pluginsConfig.plugins.length === 0) {\n console.log(`No extensions specified in config ${options?.configPath}.`);\n return [];\n }\n\n const pluginsToLoad = filterPluginsByType(\n pluginsConfig.plugins,\n options?.pluginType,\n );\n\n const pluginsPromises = pluginsToLoad.map(async (pluginConfig) => {\n const importPath = getImportPath(pluginConfig, options?.pluginType);\n try {\n const plugin = (await import(importPath)) as { default: EmmettPlugin };\n\n console.info(`Loaded plugin: ${importPath}`);\n\n if (!plugin.default) {\n throw new Error(`Plugin: ${importPath} is missing default export`);\n }\n\n return plugin.default;\n } catch (error) {\n console.error(`Failed to load extension \"${importPath}\":`, error);\n return undefined;\n }\n });\n\n return (await Promise.all(pluginsPromises)).filter(\n (plugin) => plugin !== undefined,\n );\n } catch (error) {\n console.error(`Failed to load config ${options?.configPath}:`, error);\n return [];\n }\n};\n\nexport const registerCliPlugins = async (\n program: CliCommand,\n plugins: EmmettCliPlugin[],\n): Promise<void> => {\n const result: EmmettCliPlugin[] = [];\n\n for (const plugin of plugins) {\n const pluginName = plugin.name;\n\n if (!('registerCommands' in plugin)) {\n console.warn(`No registerCommands function found in ${pluginName}`);\n }\n await plugin.registerCommands(program as EmmettCliCommand);\n console.log(`Loaded extension: ${plugin.name}`);\n result.push(plugin);\n }\n};\n\nconst filterPluginsByType = (\n plugins: EmmettPluginConfig[],\n pluginType?: EmmettPluginType,\n): EmmettPluginConfig[] =>\n plugins.filter(\n (p) =>\n typeof p === 'string' ||\n (pluginType &&\n (p.register === undefined ||\n p.register.some((r) => r.pluginType === pluginType))),\n );\n\nconst getImportPath = (\n pluginConfig: EmmettPluginConfig,\n pluginType: EmmettPluginType | undefined,\n) => {\n if (typeof pluginConfig === 'string') {\n return pluginType ? `${pluginConfig}/${pluginType}` : pluginConfig;\n }\n\n const pluginSubpath =\n pluginConfig.register.find((r) => pluginType && r.pluginType === pluginType)\n ?.path ?? pluginType;\n\n return pluginSubpath\n ? `${pluginConfig.name}/${pluginSubpath}`\n : pluginConfig.name;\n};\n"]}
|