@open-mercato/shared 0.10.1-develop.8231.1.ae76b1d2c5 → 0.10.1-develop.8257.1.g7228410734
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/.turbo/turbo-build.log +1 -1
- package/dist/lib/cli/parseModuleCliOptions.js +29 -0
- package/dist/lib/cli/parseModuleCliOptions.js.map +7 -0
- package/dist/lib/version.js +1 -1
- package/dist/lib/version.js.map +1 -1
- package/package.json +2 -2
- package/src/lib/cli/__tests__/parseModuleCliOptions.test.ts +60 -0
- package/src/lib/cli/parseModuleCliOptions.ts +41 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
function parseModuleCliOptions(tokens, config = {}) {
|
|
2
|
+
const options = {};
|
|
3
|
+
const flagValue = Object.prototype.hasOwnProperty.call(config, "flagValue") ? config.flagValue : true;
|
|
4
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
5
|
+
const token = tokens[index];
|
|
6
|
+
if (!token?.startsWith("--")) continue;
|
|
7
|
+
const option = token.slice(2);
|
|
8
|
+
const separator = option.indexOf("=");
|
|
9
|
+
const rawKey = separator === -1 ? option : option.slice(0, separator);
|
|
10
|
+
const key = config.trimKeys ? rawKey.trim() : rawKey;
|
|
11
|
+
if (config.skipEmptyKeys && !key) continue;
|
|
12
|
+
if (separator !== -1) {
|
|
13
|
+
options[key] = option.slice(separator + 1);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const next = tokens[index + 1];
|
|
17
|
+
if (next !== void 0 && (config.consumeEmptyValues || next !== "") && !next.startsWith("--")) {
|
|
18
|
+
options[key] = next;
|
|
19
|
+
if (config.advancePastValue !== false) index += 1;
|
|
20
|
+
} else if (flagValue !== void 0) {
|
|
21
|
+
options[key] = flagValue;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return options;
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
parseModuleCliOptions
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=parseModuleCliOptions.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/cli/parseModuleCliOptions.ts"],
|
|
4
|
+
"sourcesContent": ["export type ModuleCliOptionsConfig<FlagValue extends string | boolean | undefined = true> = {\n flagValue?: FlagValue\n trimKeys?: boolean\n skipEmptyKeys?: boolean\n consumeEmptyValues?: boolean\n advancePastValue?: boolean\n}\n\nexport function parseModuleCliOptions<FlagValue extends string | boolean | undefined = true>(\n tokens: readonly string[],\n config: ModuleCliOptionsConfig<FlagValue> = {},\n): Record<string, string | Exclude<FlagValue, undefined>> {\n const options: Record<string, string | Exclude<FlagValue, undefined>> = {}\n const flagValue = Object.prototype.hasOwnProperty.call(config, 'flagValue') ? config.flagValue : true\n\n for (let index = 0; index < tokens.length; index += 1) {\n const token = tokens[index]\n if (!token?.startsWith('--')) continue\n\n const option = token.slice(2)\n const separator = option.indexOf('=')\n const rawKey = separator === -1 ? option : option.slice(0, separator)\n const key = config.trimKeys ? rawKey.trim() : rawKey\n if (config.skipEmptyKeys && !key) continue\n\n if (separator !== -1) {\n options[key] = option.slice(separator + 1)\n continue\n }\n\n const next = tokens[index + 1]\n if (next !== undefined && (config.consumeEmptyValues || next !== '') && !next.startsWith('--')) {\n options[key] = next\n if (config.advancePastValue !== false) index += 1\n } else if (flagValue !== undefined) {\n options[key] = flagValue as Exclude<FlagValue, undefined>\n }\n }\n\n return options\n}\n"],
|
|
5
|
+
"mappings": "AAQO,SAAS,sBACd,QACA,SAA4C,CAAC,GACW;AACxD,QAAM,UAAkE,CAAC;AACzE,QAAM,YAAY,OAAO,UAAU,eAAe,KAAK,QAAQ,WAAW,IAAI,OAAO,YAAY;AAEjG,WAAS,QAAQ,GAAG,QAAQ,OAAO,QAAQ,SAAS,GAAG;AACrD,UAAM,QAAQ,OAAO,KAAK;AAC1B,QAAI,CAAC,OAAO,WAAW,IAAI,EAAG;AAE9B,UAAM,SAAS,MAAM,MAAM,CAAC;AAC5B,UAAM,YAAY,OAAO,QAAQ,GAAG;AACpC,UAAM,SAAS,cAAc,KAAK,SAAS,OAAO,MAAM,GAAG,SAAS;AACpE,UAAM,MAAM,OAAO,WAAW,OAAO,KAAK,IAAI;AAC9C,QAAI,OAAO,iBAAiB,CAAC,IAAK;AAElC,QAAI,cAAc,IAAI;AACpB,cAAQ,GAAG,IAAI,OAAO,MAAM,YAAY,CAAC;AACzC;AAAA,IACF;AAEA,UAAM,OAAO,OAAO,QAAQ,CAAC;AAC7B,QAAI,SAAS,WAAc,OAAO,sBAAsB,SAAS,OAAO,CAAC,KAAK,WAAW,IAAI,GAAG;AAC9F,cAAQ,GAAG,IAAI;AACf,UAAI,OAAO,qBAAqB,MAAO,UAAS;AAAA,IAClD,WAAW,cAAc,QAAW;AAClC,cAAQ,GAAG,IAAI;AAAA,IACjB;AAAA,EACF;AAEA,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/lib/version.js
CHANGED
package/dist/lib/version.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/version.ts"],
|
|
4
|
-
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.
|
|
4
|
+
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.10.1-develop.8257.1.g7228410734';\nexport const appVersion = APP_VERSION;\n"],
|
|
5
5
|
"mappings": "AACO,MAAM,cAAc;AACpB,MAAM,aAAa;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/shared",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8257.1.g7228410734",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -130,7 +130,7 @@
|
|
|
130
130
|
"@mikro-orm/core": "^7.1.14",
|
|
131
131
|
"@mikro-orm/decorators": "^7.1.14",
|
|
132
132
|
"@mikro-orm/postgresql": "^7.1.14",
|
|
133
|
-
"@open-mercato/cache": "0.10.1-develop.
|
|
133
|
+
"@open-mercato/cache": "0.10.1-develop.8257.1.g7228410734",
|
|
134
134
|
"@types/html-to-text": "^9.0.4",
|
|
135
135
|
"@types/sanitize-html": "^2.16.1",
|
|
136
136
|
"dotenv": "^17.4.2",
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { parseModuleCliOptions } from '../parseModuleCliOptions'
|
|
2
|
+
|
|
3
|
+
describe('parseModuleCliOptions', () => {
|
|
4
|
+
it.each([
|
|
5
|
+
[['--key=a=b'], { key: 'a=b' }],
|
|
6
|
+
[['--key='], { key: '' }],
|
|
7
|
+
[['--key==a=='], { key: '=a==' }],
|
|
8
|
+
[['--key', 'a=b'], { key: 'a=b' }],
|
|
9
|
+
[['--key', 'value'], { key: 'value' }],
|
|
10
|
+
[['--flag', '--other=true'], { flag: true, other: 'true' }],
|
|
11
|
+
[['positional', '-short', '', '--key=value', 'ignored'], { key: 'value' }],
|
|
12
|
+
[['--key=first', '--key', 'second'], { key: 'second' }],
|
|
13
|
+
[['--key', '', '--next'], { key: true, next: true }],
|
|
14
|
+
[['--=value', '--', '-- spaced =value'], { '': true, ' spaced ': 'value' }],
|
|
15
|
+
])('parses %j', (tokens, expected) => {
|
|
16
|
+
expect(parseModuleCliOptions(tokens)).toEqual(expected)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('ignores bare flags for string-only catalog, sales, planner, resources and staff CLIs', () => {
|
|
20
|
+
const options: Record<string, string> = parseModuleCliOptions(
|
|
21
|
+
['--bare', '--enabled=true', '--empty=', '--name', 'value'],
|
|
22
|
+
{ flagValue: undefined },
|
|
23
|
+
)
|
|
24
|
+
expect(options).toEqual({ enabled: 'true', empty: '', name: 'value' })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('preserves string flags and non-advancing values for customers and seeds', () => {
|
|
28
|
+
const options: Record<string, string> = parseModuleCliOptions(
|
|
29
|
+
['--bare', '--name', '', '--=ignored', '--key=a=b'],
|
|
30
|
+
{ flagValue: 'true', skipEmptyKeys: true, consumeEmptyValues: true, advancePastValue: false },
|
|
31
|
+
)
|
|
32
|
+
expect(options).toEqual({ bare: 'true', name: '', key: 'a=b' })
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('accepts empty space-separated values for configs, search and query_index', () => {
|
|
36
|
+
expect(parseModuleCliOptions(['--key', '', '--next', '--=ignored'], {
|
|
37
|
+
consumeEmptyValues: true,
|
|
38
|
+
skipEmptyKeys: true,
|
|
39
|
+
})).toEqual({ key: '', next: true })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('trims keys and ignores empty keys for attachments and customer_groups', () => {
|
|
43
|
+
expect(parseModuleCliOptions(['-- name = a=b ', '-- =ignored', '--', '--empty', ''], {
|
|
44
|
+
trimKeys: true,
|
|
45
|
+
skipEmptyKeys: true,
|
|
46
|
+
})).toEqual({ name: ' a=b ', empty: true })
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('skips empty keys without trimming whitespace for feature_toggles', () => {
|
|
50
|
+
expect(parseModuleCliOptions(['--=ignored', '-- name =value', '--empty', ''], {
|
|
51
|
+
skipEmptyKeys: true,
|
|
52
|
+
})).toEqual({ ' name ': 'value', empty: true })
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('does not mutate the input tokens', () => {
|
|
56
|
+
const tokens = Object.freeze(['--key', 'value', '--flag'])
|
|
57
|
+
expect(parseModuleCliOptions(tokens)).toEqual({ key: 'value', flag: true })
|
|
58
|
+
expect(tokens).toEqual(['--key', 'value', '--flag'])
|
|
59
|
+
})
|
|
60
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type ModuleCliOptionsConfig<FlagValue extends string | boolean | undefined = true> = {
|
|
2
|
+
flagValue?: FlagValue
|
|
3
|
+
trimKeys?: boolean
|
|
4
|
+
skipEmptyKeys?: boolean
|
|
5
|
+
consumeEmptyValues?: boolean
|
|
6
|
+
advancePastValue?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parseModuleCliOptions<FlagValue extends string | boolean | undefined = true>(
|
|
10
|
+
tokens: readonly string[],
|
|
11
|
+
config: ModuleCliOptionsConfig<FlagValue> = {},
|
|
12
|
+
): Record<string, string | Exclude<FlagValue, undefined>> {
|
|
13
|
+
const options: Record<string, string | Exclude<FlagValue, undefined>> = {}
|
|
14
|
+
const flagValue = Object.prototype.hasOwnProperty.call(config, 'flagValue') ? config.flagValue : true
|
|
15
|
+
|
|
16
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
17
|
+
const token = tokens[index]
|
|
18
|
+
if (!token?.startsWith('--')) continue
|
|
19
|
+
|
|
20
|
+
const option = token.slice(2)
|
|
21
|
+
const separator = option.indexOf('=')
|
|
22
|
+
const rawKey = separator === -1 ? option : option.slice(0, separator)
|
|
23
|
+
const key = config.trimKeys ? rawKey.trim() : rawKey
|
|
24
|
+
if (config.skipEmptyKeys && !key) continue
|
|
25
|
+
|
|
26
|
+
if (separator !== -1) {
|
|
27
|
+
options[key] = option.slice(separator + 1)
|
|
28
|
+
continue
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const next = tokens[index + 1]
|
|
32
|
+
if (next !== undefined && (config.consumeEmptyValues || next !== '') && !next.startsWith('--')) {
|
|
33
|
+
options[key] = next
|
|
34
|
+
if (config.advancePastValue !== false) index += 1
|
|
35
|
+
} else if (flagValue !== undefined) {
|
|
36
|
+
options[key] = flagValue as Exclude<FlagValue, undefined>
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return options
|
|
41
|
+
}
|