@featurevisor/core 4.13.0 → 4.14.0
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/lib/builder/applyContextToDatafile.js +21 -1
- package/lib/builder/applyContextToDatafile.js.map +1 -1
- package/lib/builder/buildDatafile.d.ts +15 -1
- package/lib/builder/buildDatafile.js +48 -13
- package/lib/builder/buildDatafile.js.map +1 -1
- package/lib/builder/buildProject.js +9 -0
- package/lib/builder/buildProject.js.map +1 -1
- package/lib/builder/environments.d.ts +1 -2
- package/lib/builder/hashes.d.ts +9 -0
- package/lib/builder/hashes.js +16 -0
- package/lib/builder/hashes.js.map +1 -1
- package/lib/builder/schemas.d.ts +76 -0
- package/lib/builder/schemas.js +406 -0
- package/lib/builder/schemas.js.map +1 -0
- package/lib/catalog.d.ts +2 -0
- package/lib/catalog.js +3 -0
- package/lib/catalog.js.map +1 -1
- package/lib/cli/options.js +31 -4
- package/lib/cli/options.js.map +1 -1
- package/lib/config/allowedConditions.d.ts +6 -3
- package/lib/config/allowedConditions.js +9 -2
- package/lib/config/allowedConditions.js.map +1 -1
- package/lib/convert/check.js +5 -1
- package/lib/convert/check.js.map +1 -1
- package/lib/convert/plan.js +8 -2
- package/lib/convert/plan.js.map +1 -1
- package/lib/datasource/conditionLocations.d.ts +1 -1
- package/lib/datasource/conditionLocations.js +26 -0
- package/lib/datasource/conditionLocations.js.map +1 -1
- package/lib/datasource/datasource.d.ts +2 -0
- package/lib/datasource/datasource.js +5 -3
- package/lib/datasource/datasource.js.map +1 -1
- package/lib/datasource/expressions.d.ts +1 -1
- package/lib/datasource/testSpecs.js +3 -3
- package/lib/datasource/testSpecs.js.map +1 -1
- package/lib/diff/conditions.js +1 -1
- package/lib/diff/conditions.js.map +1 -1
- package/lib/diff/index.js +68 -1
- package/lib/diff/index.js.map +1 -1
- package/lib/diff/schemaWidening.d.ts +15 -0
- package/lib/diff/schemaWidening.js +275 -0
- package/lib/diff/schemaWidening.js.map +1 -0
- package/lib/find-usage/index.d.ts +16 -2
- package/lib/find-usage/index.js +114 -10
- package/lib/find-usage/index.js.map +1 -1
- package/lib/formatter/expressions.js +5 -2
- package/lib/formatter/expressions.js.map +1 -1
- package/lib/formatter/layout.d.ts +3 -2
- package/lib/formatter/layout.js +23 -4
- package/lib/formatter/layout.js.map +1 -1
- package/lib/formatter/style.d.ts +20 -2
- package/lib/formatter/style.js +49 -4
- package/lib/formatter/style.js.map +1 -1
- package/lib/generate-code/index.d.ts +4 -0
- package/lib/generate-code/index.js +10 -0
- package/lib/generate-code/index.js.map +1 -1
- package/lib/generate-code/typescript.d.ts +5 -0
- package/lib/generate-code/typescript.js +265 -25
- package/lib/generate-code/typescript.js.map +1 -1
- package/lib/generate-code/widenMutations.d.ts +9 -0
- package/lib/generate-code/widenMutations.js +134 -0
- package/lib/generate-code/widenMutations.js.map +1 -0
- package/lib/linter/composedOverrideSchema.js +3 -0
- package/lib/linter/composedOverrideSchema.js.map +1 -1
- package/lib/linter/conditionKinds.d.ts +1 -1
- package/lib/linter/conditionKinds.js +4 -0
- package/lib/linter/conditionKinds.js.map +1 -1
- package/lib/linter/conditionSchema.d.ts +21 -1
- package/lib/linter/conditionSchema.js +261 -135
- package/lib/linter/conditionSchema.js.map +1 -1
- package/lib/linter/contextWarnings.js +2 -0
- package/lib/linter/contextWarnings.js.map +1 -1
- package/lib/linter/diagnostics.d.ts +10 -3
- package/lib/linter/diagnostics.js +16 -2
- package/lib/linter/diagnostics.js.map +1 -1
- package/lib/linter/expressionDiagnostics.js +2 -2
- package/lib/linter/expressionDiagnostics.js.map +1 -1
- package/lib/linter/featureSchema.d.ts +10 -9
- package/lib/linter/featureSchema.js +60 -158
- package/lib/linter/featureSchema.js.map +1 -1
- package/lib/linter/lintProject.js +229 -2
- package/lib/linter/lintProject.js.map +1 -1
- package/lib/linter/messageWarnings.d.ts +16 -0
- package/lib/linter/messageWarnings.js +181 -0
- package/lib/linter/messageWarnings.js.map +1 -0
- package/lib/linter/regexSafety.d.ts +17 -2
- package/lib/linter/regexSafety.js +257 -13
- package/lib/linter/regexSafety.js.map +1 -1
- package/lib/linter/reservedNames.d.ts +21 -0
- package/lib/linter/reservedNames.js +79 -0
- package/lib/linter/reservedNames.js.map +1 -1
- package/lib/linter/schema.d.ts +45 -3
- package/lib/linter/schema.js +341 -35
- package/lib/linter/schema.js.map +1 -1
- package/lib/linter/schemaAnnotations.d.ts +34 -0
- package/lib/linter/schemaAnnotations.js +409 -0
- package/lib/linter/schemaAnnotations.js.map +1 -0
- package/lib/linter/schemaEngine.d.ts +7 -0
- package/lib/linter/schemaEngine.js +80 -0
- package/lib/linter/schemaEngine.js.map +1 -0
- package/lib/linter/schemaFormats.d.ts +6 -0
- package/lib/linter/schemaFormats.js +20 -0
- package/lib/linter/schemaFormats.js.map +1 -1
- package/lib/linter/schemaMutations.d.ts +38 -0
- package/lib/linter/schemaMutations.js +617 -0
- package/lib/linter/schemaMutations.js.map +1 -0
- package/lib/linter/schemaPatterns.d.ts +26 -0
- package/lib/linter/schemaPatterns.js +316 -0
- package/lib/linter/schemaPatterns.js.map +1 -0
- package/lib/linter/schemaReferences.d.ts +18 -0
- package/lib/linter/schemaReferences.js +43 -0
- package/lib/linter/schemaReferences.js.map +1 -0
- package/lib/linter/targetSchema.d.ts +3 -0
- package/lib/linter/targetSchema.js +3 -0
- package/lib/linter/targetSchema.js.map +1 -1
- package/lib/linter/testSchema.d.ts +1 -1
- package/lib/linter/testSchema.js +70 -11
- package/lib/linter/testSchema.js.map +1 -1
- package/lib/linter/variableSchema.d.ts +5 -5
- package/lib/linter/variableSchema.js +5 -5
- package/lib/linter/variableSchema.js.map +1 -1
- package/lib/linter/warningCodes.d.ts +1 -1
- package/lib/linter/warningCodes.js +4 -0
- package/lib/linter/warningCodes.js.map +1 -1
- package/lib/list/index.js +45 -4
- package/lib/list/index.js.map +1 -1
- package/lib/list/matrix.d.ts +6 -1
- package/lib/list/matrix.js +31 -0
- package/lib/list/matrix.js.map +1 -1
- package/lib/promoter/index.js +15 -0
- package/lib/promoter/index.js.map +1 -1
- package/lib/rewrite/conditions.d.ts +2 -1
- package/lib/rewrite/conditions.js +1 -0
- package/lib/rewrite/conditions.js.map +1 -1
- package/lib/schemaNodes.d.ts +50 -0
- package/lib/schemaNodes.js +146 -0
- package/lib/schemaNodes.js.map +1 -0
- package/lib/targeting.d.ts +28 -1
- package/lib/targeting.js +111 -0
- package/lib/targeting.js.map +1 -1
- package/lib/tester/segments.d.ts +3 -0
- package/lib/tester/testProject.d.ts +1 -1
- package/lib/tester/testProject.js +16 -3
- package/lib/tester/testProject.js.map +1 -1
- package/lib/tester/testSchema.d.ts +9 -0
- package/lib/tester/testSchema.js +130 -0
- package/lib/tester/testSchema.js.map +1 -0
- package/package.json +7 -6
package/lib/formatter/style.js
CHANGED
|
@@ -147,8 +147,43 @@ exports.HOUSE_STYLE = {
|
|
|
147
147
|
],
|
|
148
148
|
segment: [...LIFECYCLE, "description", "metadata", "conditions"],
|
|
149
149
|
attribute: [...LIFECYCLE, "description", "metadata", ...SCHEMA_KEYS],
|
|
150
|
-
/**
|
|
151
|
-
|
|
150
|
+
/**
|
|
151
|
+
* A schema file's root. Only it has `tags` and `annotationsSchema`, and lists its
|
|
152
|
+
* `mutations` by environment in a project with environments. Only schema files have
|
|
153
|
+
* `messages`, `optionSchema`, `annotations`, `mutations` and `issues`, after the rules they
|
|
154
|
+
* word, describe, change and add to.
|
|
155
|
+
*/
|
|
156
|
+
schema: [
|
|
157
|
+
...LIFECYCLE,
|
|
158
|
+
"description",
|
|
159
|
+
"tags",
|
|
160
|
+
"metadata",
|
|
161
|
+
"annotationsSchema",
|
|
162
|
+
"optionSchema",
|
|
163
|
+
...SCHEMA_KEYS,
|
|
164
|
+
"messages",
|
|
165
|
+
"annotations",
|
|
166
|
+
"mutations",
|
|
167
|
+
"issues",
|
|
168
|
+
],
|
|
169
|
+
/**
|
|
170
|
+
* Every schema nested in a schema, attribute or variable: the root's order, with
|
|
171
|
+
* `optionSchema` right before `annotations`, since it says what their `options` hold.
|
|
172
|
+
*/
|
|
173
|
+
schemaNode: [
|
|
174
|
+
...LIFECYCLE,
|
|
175
|
+
"description",
|
|
176
|
+
...SCHEMA_KEYS,
|
|
177
|
+
"messages",
|
|
178
|
+
"optionSchema",
|
|
179
|
+
"annotations",
|
|
180
|
+
"mutations",
|
|
181
|
+
"issues",
|
|
182
|
+
],
|
|
183
|
+
/** A mutation in a schema's mutations, at any node, with the mutations nested under it last */
|
|
184
|
+
schemaMutation: ["key", "description", "conditions", "mutate", "mutations"],
|
|
185
|
+
/** An entry in a schema's issues, at any object node, with the entries nested under it last */
|
|
186
|
+
schemaIssue: ["key", "description", "conditions", "path", "message", "issues"],
|
|
152
187
|
target: [
|
|
153
188
|
"promotable",
|
|
154
189
|
"description",
|
|
@@ -159,6 +194,9 @@ exports.HOUSE_STYLE = {
|
|
|
159
194
|
"excludeFeatures",
|
|
160
195
|
"includeVariables",
|
|
161
196
|
"excludeVariables",
|
|
197
|
+
"includeSchemas",
|
|
198
|
+
"excludeSchemas",
|
|
199
|
+
"includeAnnotations",
|
|
162
200
|
"context",
|
|
163
201
|
"schemaVersion",
|
|
164
202
|
],
|
|
@@ -167,8 +205,8 @@ exports.HOUSE_STYLE = {
|
|
|
167
205
|
* so do a deadline type's own fields, after these.
|
|
168
206
|
*/
|
|
169
207
|
deadline: ["by", "done", "description"],
|
|
170
|
-
/** A feature, segment or
|
|
171
|
-
test: ["feature", "segment", "variable", "promotable", "assertions"],
|
|
208
|
+
/** A feature, segment, variable or schema test spec */
|
|
209
|
+
test: ["feature", "segment", "variable", "schema", "promotable", "assertions"],
|
|
172
210
|
/** An assertion, its children, and a scenario spec, which is one assertion */
|
|
173
211
|
assertion: [
|
|
174
212
|
"key",
|
|
@@ -181,6 +219,7 @@ exports.HOUSE_STYLE = {
|
|
|
181
219
|
"context",
|
|
182
220
|
"stickyFeatures",
|
|
183
221
|
"stickyVariables",
|
|
222
|
+
"value",
|
|
184
223
|
"defaultVariationValue",
|
|
185
224
|
"defaultVariableValues",
|
|
186
225
|
"defaultVariableValue",
|
|
@@ -191,6 +230,8 @@ exports.HOUSE_STYLE = {
|
|
|
191
230
|
"expectedValue",
|
|
192
231
|
"expectedEvaluations",
|
|
193
232
|
"expectedEvaluation",
|
|
233
|
+
"expectedToBeValid",
|
|
234
|
+
"expectedIssues",
|
|
194
235
|
"checks",
|
|
195
236
|
"children",
|
|
196
237
|
],
|
|
@@ -240,6 +281,10 @@ exports.HOUSE_STYLE = {
|
|
|
240
281
|
variableOverrideEnvironments: true,
|
|
241
282
|
/** Between the properties of a schema */
|
|
242
283
|
schemaProperties: false,
|
|
284
|
+
/** Between a schema's mutations, in a list or under each environment, as between rules */
|
|
285
|
+
schemaMutations: true,
|
|
286
|
+
/** Between the environments that group a schema's mutations at its root */
|
|
287
|
+
schemaMutationEnvironments: true,
|
|
243
288
|
/** Between the deadlines in a definition's metadata */
|
|
244
289
|
deadlines: true,
|
|
245
290
|
/** Between a spec's assertions */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"style.js","sourceRoot":"","sources":["../../src/formatter/style.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,SAAS,GAAG,CAAC,UAAU,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;AAE3D,MAAM,WAAW,GAAG;IAClB,MAAM;IACN,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,MAAM;IACN,OAAO;IACP,SAAS;IACT,SAAS;IACT,WAAW;IACX,WAAW;IACX,SAAS;IACT,OAAO;IACP,UAAU;IACV,UAAU;IACV,aAAa;IACb,YAAY;IACZ,UAAU;IACV,sBAAsB;CACvB,CAAC;AAEW,QAAA,WAAW,GAAG;IACzB,mEAAmE;IACnE,MAAM,EAAE,CAAC;IAET;;;OAGG;IACH,SAAS,EAAE,MAAgC;IAE3C;;;;OAIG;IACH,cAAc,EAAE,IAAI;IAEpB;;;;;;;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AAE,UAAU,EAAE,YAAY,CAAC;QAChE,SAAS,EAAE,CAAC,GAAG,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC;QACpE
|
|
1
|
+
{"version":3,"file":"style.js","sourceRoot":"","sources":["../../src/formatter/style.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,SAAS,GAAG,CAAC,UAAU,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;AAE3D,MAAM,WAAW,GAAG;IAClB,MAAM;IACN,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,MAAM;IACN,OAAO;IACP,SAAS;IACT,SAAS;IACT,WAAW;IACX,WAAW;IACX,SAAS;IACT,OAAO;IACP,UAAU;IACV,UAAU;IACV,aAAa;IACb,YAAY;IACZ,UAAU;IACV,sBAAsB;CACvB,CAAC;AAEW,QAAA,WAAW,GAAG;IACzB,mEAAmE;IACnE,MAAM,EAAE,CAAC;IAET;;;OAGG;IACH,SAAS,EAAE,MAAgC;IAE3C;;;;OAIG;IACH,cAAc,EAAE,IAAI;IAEpB;;;;;;;OAOG;IACH,WAAW,EAAE;QACX,uFAAuF;QACvF,YAAY,EAAE,IAAI;QAClB;;;WAGG;QACH,kBAAkB,EAAE,CAAC;QACrB;;;;WAIG;QACH,aAAa,EAAE,EAAE;KAClB;IAED;;;OAGG;IACH,QAAQ,EAAE;QACR,OAAO,EAAE;YACP,GAAG,SAAS;YACZ,aAAa;YACb,MAAM;YACN,UAAU;YACV,UAAU;YACV,YAAY;YACZ,wBAAwB;YACxB,iBAAiB;YACjB,YAAY;YACZ,OAAO;YACP,OAAO;YACP,QAAQ;SACT;QACD,IAAI,EAAE;YACJ,KAAK;YACL,aAAa;YACb,YAAY;YACZ,YAAY;YACZ,YAAY;YACZ,SAAS;YACT,WAAW;YACX,kBAAkB;YAClB,WAAW;YACX,mBAAmB;SACpB;QACD,KAAK,EAAE,CAAC,YAAY,EAAE,SAAS,EAAE,WAAW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;QAC7E,KAAK,EAAE,IAAI;QACX,oEAAoE;QACpE,iBAAiB,EAAE,IAAI;QACvB,wDAAwD;QACxD,kBAAkB,EAAE,IAAI;QACxB,qCAAqC;QACrC,UAAU,EAAE,IAAI;QAChB,2DAA2D;QAC3D,eAAe,EAAE,KAAK;QACtB,2EAA2E;QAC3E,wBAAwB,EAAE,KAAK;QAC/B,6DAA6D;QAC7D,iBAAiB,EAAE,IAAI;QACvB,wEAAwE;QACxE,4BAA4B,EAAE,IAAI;QAClC,yCAAyC;QACzC,gBAAgB,EAAE,KAAK;QACvB,0FAA0F;QAC1F,eAAe,EAAE,IAAI;QACrB,2EAA2E;QAC3E,0BAA0B,EAAE,IAAI;QAChC,uDAAuD;QACvD,SAAS,EAAE,IAAI;QACf,kCAAkC;QAClC,UAAU,EAAE,IAAI;QAChB,yDAAyD;QACzD,iBAAiB,EAAE,IAAI;QACvB,wDAAwD;QACxD,MAAM,EAAE,KAAK;KACd;IAED,wFAAwF;IACxF,OAAO,EAAE;QACP,GAAG,EAAE;YACH,gDAAgD;YAChD,KAAK,EAAE,QAAQ;YACf,gBAAgB,EAAE,IAAI;YACtB,SAAS,EAAE,CAAC;YACZ,6EAA6E;YAC7E,eAAe,EAAE,IAAI;YACrB,yFAAyF;YACzF,aAAa,EAAE,IAAI;SACO;QAC5B,IAAI,EAAE;YACJ,uCAAuC;YACvC,SAAS,EAAE,EAAE;SACc;KAC9B;CACF,CAAC;AAEF,4EAA4E;AAC/D,QAAA,iBAAiB,GAAG;IAC/B,MAAM,EAAE,mBAAW,CAAC,MAAM;IAC1B,kBAAkB,EAAE,mBAAW,CAAC,WAAW,CAAC,kBAAkB;IAC9D,aAAa,EAAE,mBAAW,CAAC,WAAW,CAAC,aAAa;CACrD,CAAC"}
|
|
@@ -7,8 +7,12 @@ export interface GenerateCodeCLIOptions {
|
|
|
7
7
|
tag?: string | string[];
|
|
8
8
|
target?: string | string[];
|
|
9
9
|
react?: boolean;
|
|
10
|
+
schemas?: boolean;
|
|
11
|
+
/** The environment schema types come from, for schemas that vary by environment. */
|
|
12
|
+
environment?: string;
|
|
10
13
|
importSdkPath?: string;
|
|
11
14
|
importReactPath?: string;
|
|
15
|
+
importSchemasPath?: string;
|
|
12
16
|
}
|
|
13
17
|
export declare function generateCodeForProject(deps: Dependencies, cliOptions: GenerateCodeCLIOptions): Promise<void>;
|
|
14
18
|
export declare const generateCodePlugin: Plugin;
|
|
@@ -67,8 +67,11 @@ async function generateCodeForProject(deps, cliOptions) {
|
|
|
67
67
|
tag: cliOptions.tag,
|
|
68
68
|
target: cliOptions.target,
|
|
69
69
|
react: cliOptions.react,
|
|
70
|
+
schemas: cliOptions.schemas,
|
|
71
|
+
environment: cliOptions.environment,
|
|
70
72
|
importSdkPath: cliOptions.importSdkPath,
|
|
71
73
|
importReactPath: cliOptions.importReactPath,
|
|
74
|
+
importSchemasPath: cliOptions.importSchemasPath,
|
|
72
75
|
});
|
|
73
76
|
}
|
|
74
77
|
throw new Error(`Language ${cliOptions.language} is not supported`);
|
|
@@ -93,8 +96,11 @@ exports.generateCodePlugin = {
|
|
|
93
96
|
tag: parsed.tag,
|
|
94
97
|
target: parsed.target,
|
|
95
98
|
react: parsed.react,
|
|
99
|
+
schemas: parsed.schemas,
|
|
100
|
+
environment: parsed.environment,
|
|
96
101
|
importSdkPath: parsed.importSdkPath,
|
|
97
102
|
importReactPath: parsed.importReactPath,
|
|
103
|
+
importSchemasPath: parsed.importSchemasPath,
|
|
98
104
|
});
|
|
99
105
|
}
|
|
100
106
|
},
|
|
@@ -111,6 +117,10 @@ exports.generateCodePlugin = {
|
|
|
111
117
|
command: "generate-code --language typescript --out-dir src/generated --target web",
|
|
112
118
|
description: "Generate TypeScript code for definitions selected by a target",
|
|
113
119
|
},
|
|
120
|
+
{
|
|
121
|
+
command: "generate-code --language typescript --out-dir src/generated --target web --schemas --react",
|
|
122
|
+
description: "Also generate a typed createSchemas and typed schema hooks for @featurevisor/schemas",
|
|
123
|
+
},
|
|
114
124
|
],
|
|
115
125
|
};
|
|
116
126
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generate-code/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generate-code/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,wDA8CC;AAvED,uCAAyB;AACzB,2CAA6B;AAE7B,6CAAgE;AAGhE,kCAAkE;AAClE,mDAAgF;AAEnE,QAAA,qCAAqC,GAAG,CAAC,YAAY,CAAC,CAAC;AAgB7D,KAAK,UAAU,sBAAsB,CAC1C,IAAkB,EAClB,UAAkC;IAElC,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;IAEnC,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;IAED,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;IAED,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;IAExE,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC;QACjC,OAAO,CAAC,GAAG,CAAC,8BAA8B,IAAA,oBAAQ,EAAC,YAAY,EAAE,0BAAc,CAAC,EAAE,CAAC,CAAC;QACpF,EAAE,CAAC,SAAS,CAAC,YAAY,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAClD,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,GAAG,CAAC,uCAAuC,IAAA,oBAAQ,EAAC,YAAY,EAAE,0BAAc,C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AU,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QAExF,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACnC,IAAA,qBAAc,EAAC,aAAa,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC;YAE7C,MAAM,sBAAsB,CAC1B;gBACE,iBAAiB;gBACjB,aAAa,EAAE,SAAS,CAAC,aAAa;gBACtC,UAAU,EAAE,SAAS,CAAC,UAAU;gBAChC,OAAO,EAAE,MAAM;aAChB,EACD;gBACE,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,GAAG,EAAE,MAAM,CAAC,GAAG;gBACf,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,aAAa,EAAE,MAAM,CAAC,aAAa;gBACnC,eAAe,EAAE,MAAM,CAAC,eAAe;gBACvC,iBAAiB,EAAE,MAAM,CAAC,iBAAiB;aAC5C,CACF,CAAC;QACJ,CAAC;IACH,CAAC;IACD,QAAQ,EAAE;QACR;YACE,OAAO,EAAE,6DAA6D;YACtE,WAAW,EAAE,0CAA0C;SACxD;QACD;YACE,OAAO,EAAE,+EAA+E;YACxF,WAAW,EAAE,kEAAkE;SAChF;QACD;YACE,OAAO,EAAE,0EAA0E;YACnF,WAAW,EAAE,+DAA+D;SAC7E;QACD;YACE,OAAO,EACL,4FAA4F;YAC9F,WAAW,EACT,sFAAsF;SACzF;KACF;CACF,CAAC"}
|
|
@@ -3,7 +3,12 @@ export interface TypeScriptGenerationOptions {
|
|
|
3
3
|
tag?: string | string[];
|
|
4
4
|
target?: string | string[];
|
|
5
5
|
react?: boolean;
|
|
6
|
+
/** A typed `createSchemas` for `@featurevisor/schemas`, and typed schema hooks with `react`. */
|
|
7
|
+
schemas?: boolean;
|
|
8
|
+
/** The environment whose version of each schema types are written from. The one starting with `prod` by default. */
|
|
9
|
+
environment?: string;
|
|
6
10
|
importSdkPath?: string;
|
|
7
11
|
importReactPath?: string;
|
|
12
|
+
importSchemasPath?: string;
|
|
8
13
|
}
|
|
9
14
|
export declare function generateTypeScriptCodeForProject(deps: Dependencies, outputPath: string, options?: TypeScriptGenerationOptions): Promise<void>;
|
|
@@ -36,11 +36,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
36
36
|
exports.generateTypeScriptCodeForProject = generateTypeScriptCodeForProject;
|
|
37
37
|
const fs = __importStar(require("fs"));
|
|
38
38
|
const path = __importStar(require("path"));
|
|
39
|
+
const error_1 = require("../error");
|
|
40
|
+
const schemas_1 = require("../builder/schemas");
|
|
41
|
+
const schemaNodes_1 = require("../schemaNodes");
|
|
42
|
+
const widenMutations_1 = require("./widenMutations");
|
|
39
43
|
const dependencies_1 = require("../datasource/dependencies");
|
|
40
44
|
const composition_1 = require("../datasource/composition");
|
|
41
45
|
const targeting_1 = require("../targeting");
|
|
42
46
|
const DEFAULT_SDK_IMPORT_PATH = "@featurevisor/sdk";
|
|
43
47
|
const DEFAULT_REACT_IMPORT_PATH = "@featurevisor/react";
|
|
48
|
+
const DEFAULT_SCHEMAS_IMPORT_PATH = "@featurevisor/schemas";
|
|
44
49
|
function shouldWrapArrayItemType(typeName) {
|
|
45
50
|
const trimmed = typeName.trim();
|
|
46
51
|
return trimmed.includes("|") || trimmed.includes("&");
|
|
@@ -53,15 +58,24 @@ function buildObjectTypeFromSchema(schema, schemasByKey, schemaTypeNames, positi
|
|
|
53
58
|
const additional = schema.additionalProperties;
|
|
54
59
|
const hasProps = props && typeof props === "object" && Object.keys(props).length > 0;
|
|
55
60
|
const hasAdditional = additional && typeof additional === "object";
|
|
56
|
-
|
|
61
|
+
// Each listed property as `name: type`, with `?` when it is not required.
|
|
62
|
+
const propertyEntries = () => {
|
|
57
63
|
const requiredSet = new Set(schema.required || []);
|
|
58
|
-
|
|
64
|
+
return Object.entries(props).map(([k, v]) => {
|
|
59
65
|
const propType = schemaToTypeScriptType(v, schemasByKey, schemaTypeNames, position);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
: `${formatObjectKey(k)}: ${propType}`;
|
|
66
|
+
return requiredSet.has(k)
|
|
67
|
+
? `${formatObjectKey(k)}: ${propType}`
|
|
68
|
+
: `${formatObjectKey(k)}?: ${propType}`;
|
|
64
69
|
});
|
|
70
|
+
};
|
|
71
|
+
// `additionalProperties: true` allows any other key with any value.
|
|
72
|
+
if (additional === true) {
|
|
73
|
+
if (!hasProps)
|
|
74
|
+
return "Record<string, unknown>";
|
|
75
|
+
return `{ ${propertyEntries().join("; ")}; [key: string]: unknown }`;
|
|
76
|
+
}
|
|
77
|
+
if (hasProps && hasAdditional) {
|
|
78
|
+
const propEntries = propertyEntries();
|
|
65
79
|
const propUnion = Object.entries(props)
|
|
66
80
|
.map(([, v]) => schemaToTypeScriptType(v, schemasByKey, schemaTypeNames, position))
|
|
67
81
|
.join(" | ");
|
|
@@ -69,19 +83,8 @@ function buildObjectTypeFromSchema(schema, schemasByKey, schemaTypeNames, positi
|
|
|
69
83
|
const indexType = [additionalType, propUnion].filter(Boolean).join(" | ");
|
|
70
84
|
return `{ ${propEntries.join("; ")}; [key: string]: ${indexType} }`;
|
|
71
85
|
}
|
|
72
|
-
if (hasProps)
|
|
73
|
-
|
|
74
|
-
const entries = Object.entries(props)
|
|
75
|
-
.map(([k, v]) => {
|
|
76
|
-
const propType = schemaToTypeScriptType(v, schemasByKey, schemaTypeNames, position);
|
|
77
|
-
const optional = !requiredSet.has(k);
|
|
78
|
-
return optional
|
|
79
|
-
? `${formatObjectKey(k)}?: ${propType}`
|
|
80
|
-
: `${formatObjectKey(k)}: ${propType}`;
|
|
81
|
-
})
|
|
82
|
-
.join("; ");
|
|
83
|
-
return `{ ${entries} }`;
|
|
84
|
-
}
|
|
86
|
+
if (hasProps)
|
|
87
|
+
return `{ ${propertyEntries().join("; ")} }`;
|
|
85
88
|
if (hasAdditional) {
|
|
86
89
|
const additionalType = schemaToTypeScriptType(additional, schemasByKey, schemaTypeNames, position);
|
|
87
90
|
return `Record<string, ${additionalType}>`;
|
|
@@ -105,7 +108,9 @@ function resolveSchema(schema, schemasByKey) {
|
|
|
105
108
|
}
|
|
106
109
|
/** Emit a TypeScript literal type for a primitive const value, or null if not a primitive. */
|
|
107
110
|
function constToLiteralType(constVal) {
|
|
108
|
-
if (constVal === null
|
|
111
|
+
if (constVal === null)
|
|
112
|
+
return "null";
|
|
113
|
+
if (constVal === undefined)
|
|
109
114
|
return null;
|
|
110
115
|
if (typeof constVal === "string")
|
|
111
116
|
return JSON.stringify(constVal);
|
|
@@ -259,12 +264,15 @@ function getVariableTypeForFeaturesMap(variableSchema, schemasByKey, schemaTypeN
|
|
|
259
264
|
/**
|
|
260
265
|
* Generates the content of Schemas.ts: one exported type per schema key, using schema refs between schemas.
|
|
261
266
|
*/
|
|
262
|
-
function generateSchemasFileContent(schemaKeys, schemasByKey) {
|
|
267
|
+
function generateSchemasFileContent(schemaKeys, schemasByKey, options) {
|
|
263
268
|
const schemaTypeNames = {};
|
|
264
269
|
for (const k of schemaKeys) {
|
|
265
270
|
schemaTypeNames[k] = getPascalCase(k) + "Schema";
|
|
266
271
|
}
|
|
267
272
|
const lines = [];
|
|
273
|
+
if (options.createSchemas) {
|
|
274
|
+
lines.push(`import { createSchemas as createSchemasBase, type SchemasOptions } from ${JSON.stringify(options.importSchemasPath)};`, `import type { Featurevisor } from ${JSON.stringify(options.importSdkPath)};`, "");
|
|
275
|
+
}
|
|
268
276
|
for (const key of schemaKeys) {
|
|
269
277
|
const schema = schemasByKey[key];
|
|
270
278
|
if (!schema)
|
|
@@ -273,8 +281,190 @@ function generateSchemasFileContent(schemaKeys, schemasByKey) {
|
|
|
273
281
|
const typeStr = schemaToTypeScriptType(schema, schemasByKey, schemaTypeNames);
|
|
274
282
|
lines.push(`export type ${name} = ${typeStr};`);
|
|
275
283
|
}
|
|
284
|
+
// The schemas the selected targets deliver, by key, for typed handles from @featurevisor/schemas.
|
|
285
|
+
lines.push("", "export interface SchemaTypes {");
|
|
286
|
+
for (const key of options.keys) {
|
|
287
|
+
lines.push(` ${formatObjectKey(key)}: ${schemaTypeNames[key]};`);
|
|
288
|
+
}
|
|
289
|
+
lines.push("}");
|
|
290
|
+
// What each delivered schema's vocabularies declare, so annotations and options are typed too.
|
|
291
|
+
lines.push("", "/** What each schema's annotationsSchema and optionSchema declare, by key, and the choice fields that follow another. */", "export interface SchemaVocabularies {");
|
|
292
|
+
const typeOf = (vocabularies) => vocabularies.length > 0
|
|
293
|
+
? vocabularies
|
|
294
|
+
.map((vocabulary) => schemaToTypeScriptType(vocabulary, schemasByKey, schemaTypeNames))
|
|
295
|
+
.join(" | ")
|
|
296
|
+
: "{}";
|
|
297
|
+
for (const key of options.keys) {
|
|
298
|
+
const schema = schemasByKey[key];
|
|
299
|
+
// Both keys always, `{}` for a vocabulary the schema lacks, so it reads as typed in every compiler mode.
|
|
300
|
+
const parts = [
|
|
301
|
+
`annotations: ${schema?.annotationsSchema ? typeOf([schema.annotationsSchema]) : "{}"}`,
|
|
302
|
+
// What the root's own choices carry, for a schema that is a list of choices, and `{}` for a form.
|
|
303
|
+
`options: ${typeOf((schema && getChoiceVocabularies(schema, schemasByKey)) || [])}`,
|
|
304
|
+
];
|
|
305
|
+
// Each list of choices below the root, by canonical path, with its own vocabulary.
|
|
306
|
+
const fields = schema ? getFieldVocabularies(schema, schemasByKey) : [];
|
|
307
|
+
if (fields.length > 0) {
|
|
308
|
+
const entries = fields.map(([path, vocabularies]) => `${JSON.stringify(path)}: ${typeOf(vocabularies)}`);
|
|
309
|
+
parts.push(`fields: { ${entries.join("; ")} }`);
|
|
310
|
+
}
|
|
311
|
+
lines.push(` ${formatObjectKey(key)}: { ${parts.join("; ")} };`);
|
|
312
|
+
}
|
|
313
|
+
lines.push("}");
|
|
314
|
+
if (options.createSchemas) {
|
|
315
|
+
lines.push("", "/** createSchemas from @featurevisor/schemas, with keys, values, annotations and options typed. */", "export function createSchemas(f: Featurevisor, options?: SchemasOptions) {", " return createSchemasBase<SchemaTypes, SchemaVocabularies>(f, options);", "}");
|
|
316
|
+
}
|
|
276
317
|
return lines.join("\n") + "\n";
|
|
277
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* What a node's choices carry: the `optionSchema` beside them, on the node
|
|
321
|
+
* with the `enum` or the `oneOf` of consts, in `items` for a multiple select,
|
|
322
|
+
* on each `enum` branch of a nullable list, or a referenced schema's own.
|
|
323
|
+
* Undefined when the node holds no choices, and empty when its choices carry
|
|
324
|
+
* titles only, since nothing inherits a vocabulary.
|
|
325
|
+
*/
|
|
326
|
+
function getChoiceVocabularies(node, schemasByKey, seen = new Set()) {
|
|
327
|
+
if (!(0, schemaNodes_1.isSchemaNode)(node))
|
|
328
|
+
return undefined;
|
|
329
|
+
if (typeof node.schema === "string") {
|
|
330
|
+
const referenced = schemasByKey[node.schema];
|
|
331
|
+
if (!referenced || seen.has(node.schema))
|
|
332
|
+
return undefined;
|
|
333
|
+
return getChoiceVocabularies(referenced, schemasByKey, new Set([...seen, node.schema]));
|
|
334
|
+
}
|
|
335
|
+
const own = (0, schemaNodes_1.isSchemaNode)(node.optionSchema) ? [node.optionSchema] : [];
|
|
336
|
+
if (Array.isArray(node.enum))
|
|
337
|
+
return own;
|
|
338
|
+
if (Array.isArray(node.oneOf)) {
|
|
339
|
+
const isConst = (branch) => (0, schemaNodes_1.isSchemaNode)(branch) && "const" in resolveSchema(branch, schemasByKey);
|
|
340
|
+
if (node.oneOf.every(isConst))
|
|
341
|
+
return own;
|
|
342
|
+
// Any other oneOf holds choices when each branch does, as a nullable list, each branch with its own.
|
|
343
|
+
const found = [];
|
|
344
|
+
for (const branch of node.oneOf) {
|
|
345
|
+
if (isConst(branch))
|
|
346
|
+
continue;
|
|
347
|
+
const each = getChoiceVocabularies(branch, schemasByKey, seen);
|
|
348
|
+
if (!each)
|
|
349
|
+
return undefined;
|
|
350
|
+
for (const vocabulary of each)
|
|
351
|
+
if (!found.includes(vocabulary))
|
|
352
|
+
found.push(vocabulary);
|
|
353
|
+
}
|
|
354
|
+
return found;
|
|
355
|
+
}
|
|
356
|
+
if (node.type === "array" && (0, schemaNodes_1.isSchemaNode)(node.items))
|
|
357
|
+
return getChoiceVocabularies(node.items, schemasByKey, seen);
|
|
358
|
+
return undefined;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* The lists of choices below a schema's root whose vocabulary differs from
|
|
362
|
+
* the root's own choices', which for a form is none. Keyed by canonical path,
|
|
363
|
+
* as `VocabularyAt` in @featurevisor/schemas reads it: `plan`,
|
|
364
|
+
* `address.country`, `items[].size`, `labels.*`, and a multiple select by its
|
|
365
|
+
* own path.
|
|
366
|
+
*/
|
|
367
|
+
function getFieldVocabularies(root, schemasByKey) {
|
|
368
|
+
const found = [];
|
|
369
|
+
const own = getChoiceVocabularies(root, schemasByKey) || [];
|
|
370
|
+
const same = (vocabularies) => vocabularies.length === own.length && vocabularies.every((each, index) => each === own[index]);
|
|
371
|
+
const join = (path, step) => (path === "" ? step : `${path}.${step}`);
|
|
372
|
+
const walk = (node, path, seen) => {
|
|
373
|
+
if (!(0, schemaNodes_1.isSchemaNode)(node))
|
|
374
|
+
return;
|
|
375
|
+
if (typeof node.schema === "string") {
|
|
376
|
+
const referenced = schemasByKey[node.schema];
|
|
377
|
+
if (!referenced || seen.has(node.schema))
|
|
378
|
+
return;
|
|
379
|
+
walk(referenced, path, new Set([...seen, node.schema]));
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (path !== "") {
|
|
383
|
+
const vocabularies = getChoiceVocabularies(node, schemasByKey, seen);
|
|
384
|
+
if (vocabularies && !same(vocabularies))
|
|
385
|
+
found.push([path, vocabularies]);
|
|
386
|
+
}
|
|
387
|
+
if ((0, schemaNodes_1.isSchemaNode)(node.properties))
|
|
388
|
+
for (const [key, property] of Object.entries(node.properties))
|
|
389
|
+
walk(property, join(path, key), seen);
|
|
390
|
+
if ((0, schemaNodes_1.isSchemaNode)(node.items))
|
|
391
|
+
walk(node.items, `${path}[]`, seen);
|
|
392
|
+
if ((0, schemaNodes_1.isSchemaNode)(node.additionalProperties))
|
|
393
|
+
walk(node.additionalProperties, join(path, "*"), seen);
|
|
394
|
+
};
|
|
395
|
+
walk(root, "", new Set());
|
|
396
|
+
return found;
|
|
397
|
+
}
|
|
398
|
+
function generateSchemasReactFileContent(importSchemasPath) {
|
|
399
|
+
return `
|
|
400
|
+
import {
|
|
401
|
+
useSchema as useSchemaBase,
|
|
402
|
+
useSchemas as useSchemasBase,
|
|
403
|
+
useValidation as useValidationBase,
|
|
404
|
+
type ValidationOptions,
|
|
405
|
+
type ValidationResult,
|
|
406
|
+
} from ${JSON.stringify(`${importSchemasPath}/react`)};
|
|
407
|
+
import type {
|
|
408
|
+
HandleOptions,
|
|
409
|
+
IsAsync,
|
|
410
|
+
SchemaHandle,
|
|
411
|
+
Schemas,
|
|
412
|
+
SearchOnlyOptions,
|
|
413
|
+
} from ${JSON.stringify(importSchemasPath)};
|
|
414
|
+
|
|
415
|
+
import type { SchemaTypes, SchemaVocabularies } from "./schemas.js";
|
|
416
|
+
|
|
417
|
+
export { useSearch, type SearchHookOptions, type SearchState } from ${JSON.stringify(`${importSchemasPath}/react`)};
|
|
418
|
+
|
|
419
|
+
/** useSchema from @featurevisor/schemas/react, with keys, values, annotations and options typed. */
|
|
420
|
+
export function useSchema<
|
|
421
|
+
K extends keyof SchemaTypes & string,
|
|
422
|
+
TResult extends boolean | Promise<boolean> = boolean,
|
|
423
|
+
>(
|
|
424
|
+
key: K,
|
|
425
|
+
options?: SearchOnlyOptions<SchemaTypes[K], TResult>,
|
|
426
|
+
): SchemaHandle<SchemaTypes[K], IsAsync<TResult>, SchemaVocabularies[K]>;
|
|
427
|
+
/** A lookup can answer later, so a handle with one validates asynchronously in its types. */
|
|
428
|
+
export function useSchema<
|
|
429
|
+
K extends keyof SchemaTypes & string,
|
|
430
|
+
TResult extends boolean | Promise<boolean> = boolean,
|
|
431
|
+
>(
|
|
432
|
+
key: K,
|
|
433
|
+
options: HandleOptions<SchemaTypes[K], TResult>,
|
|
434
|
+
): SchemaHandle<SchemaTypes[K], true, SchemaVocabularies[K]>;
|
|
435
|
+
export function useSchema(
|
|
436
|
+
key: keyof SchemaTypes & string,
|
|
437
|
+
options?: HandleOptions<unknown>,
|
|
438
|
+
): SchemaHandle<unknown, boolean, unknown> {
|
|
439
|
+
// Typed by this wrapper's own signatures, which the base hook cannot relate for an open K.
|
|
440
|
+
return useSchemaBase(key, options as never) as never;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** useSchemas from @featurevisor/schemas/react, with keys, values, annotations and options typed. */
|
|
444
|
+
export function useSchemas(): Schemas<SchemaTypes, SchemaVocabularies> {
|
|
445
|
+
return useSchemasBase<SchemaTypes, SchemaVocabularies>();
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** useValidation from @featurevisor/schemas/react, with keys and values typed, for a schema's key or any handle, such as a field's. */
|
|
449
|
+
export function useValidation<K extends keyof SchemaTypes & string>(
|
|
450
|
+
key: K,
|
|
451
|
+
value: unknown,
|
|
452
|
+
options?: ValidationOptions,
|
|
453
|
+
): ValidationResult<SchemaTypes[K]>;
|
|
454
|
+
export function useValidation<T>(
|
|
455
|
+
handle: SchemaHandle<T, boolean>,
|
|
456
|
+
value: unknown,
|
|
457
|
+
options?: ValidationOptions,
|
|
458
|
+
): ValidationResult<T>;
|
|
459
|
+
export function useValidation(
|
|
460
|
+
keyOrHandle: string | SchemaHandle<unknown, boolean>,
|
|
461
|
+
value: unknown,
|
|
462
|
+
options?: ValidationOptions,
|
|
463
|
+
): ValidationResult<unknown> {
|
|
464
|
+
return useValidationBase(keyOrHandle, value, options);
|
|
465
|
+
}
|
|
466
|
+
`.trimStart();
|
|
467
|
+
}
|
|
278
468
|
function generateAttributesFileContent(attributes, schemaTypeNames) {
|
|
279
469
|
const schemaTypesUsed = new Set();
|
|
280
470
|
for (const attribute of attributes) {
|
|
@@ -309,6 +499,22 @@ export function getInstance(): Featurevisor {
|
|
|
309
499
|
}
|
|
310
500
|
`.trimStart();
|
|
311
501
|
}
|
|
502
|
+
/** The environment schema types come from: the one named, or the only one whose name starts with `prod`. */
|
|
503
|
+
function getTypesEnvironment(environments, named) {
|
|
504
|
+
const configured = Array.isArray(environments) ? environments : [];
|
|
505
|
+
if (named !== undefined) {
|
|
506
|
+
if (!configured.includes(named)) {
|
|
507
|
+
throw new error_1.FeaturevisorCLIError(`Unknown environment "${named}". Available environments: ${configured.join(", ")}.`, { code: "invalid_environment", details: { environment: named, available: configured } });
|
|
508
|
+
}
|
|
509
|
+
return named;
|
|
510
|
+
}
|
|
511
|
+
const production = configured.filter((environment) => environment.startsWith("prod"));
|
|
512
|
+
if (production.length === 1)
|
|
513
|
+
return production[0];
|
|
514
|
+
throw new error_1.FeaturevisorCLIError(`A schema varies by environment, so code generation needs to know which environment's version to type. ${production.length === 0
|
|
515
|
+
? "No environment's name starts with prod."
|
|
516
|
+
: `Several environments' names start with prod: ${production.join(", ")}.`} Pass --environment.`, { code: "invalid_environment", details: { available: configured } });
|
|
517
|
+
}
|
|
312
518
|
async function generateTypeScriptCodeForProject(deps, outputPath, options = {}) {
|
|
313
519
|
const { rootDirectoryPath, projectConfig, datasource } = deps;
|
|
314
520
|
const selectedTags = (0, targeting_1.normalizeOptionValues)(options.tag);
|
|
@@ -337,10 +543,33 @@ async function generateTypeScriptCodeForProject(deps, outputPath, options = {})
|
|
|
337
543
|
// Schema file may be invalid; skip for code gen
|
|
338
544
|
}
|
|
339
545
|
}
|
|
546
|
+
// A schema that varies by environment is typed from one environment's version.
|
|
547
|
+
if (Object.values(schemasByKey).some(schemas_1.hasMutationsByEnvironment)) {
|
|
548
|
+
const environment = getTypesEnvironment(projectConfig.environments, options.environment);
|
|
549
|
+
for (const key of Object.keys(schemasByKey)) {
|
|
550
|
+
schemasByKey[key] = (0, schemas_1.resolveSchemaEnvironment)(schemasByKey[key], environment, projectConfig);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
// Any mutation can match when a value is validated, so types hold whatever one widens.
|
|
554
|
+
for (const key of Object.keys(schemasByKey)) {
|
|
555
|
+
schemasByKey[key] = (0, widenMutations_1.widenSchemaMutations)(schemasByKey[key]);
|
|
556
|
+
}
|
|
340
557
|
const schemaKeys = Object.keys(schemasByKey);
|
|
341
|
-
const hasSchemasFile = schemaKeys.length > 0;
|
|
558
|
+
const hasSchemasFile = schemaKeys.length > 0 || Boolean(options.schemas);
|
|
559
|
+
const importSchemasPath = options.importSchemasPath || DEFAULT_SCHEMAS_IMPORT_PATH;
|
|
560
|
+
// Schemas the selected targets deliver, or those with a selected tag, or every schema.
|
|
561
|
+
const deliveredSchemaKeys = selectedTargets.length > 0
|
|
562
|
+
? (0, targeting_1.getTargetsSchemaKeys)(selectedTargets, schemaKeys, schemasByKey)
|
|
563
|
+
: selectedTags.length > 0
|
|
564
|
+
? (0, targeting_1.getTargetSchemaKeys)({ tags: { or: selectedTags } }, schemaKeys, schemasByKey)
|
|
565
|
+
: schemaKeys;
|
|
342
566
|
if (hasSchemasFile) {
|
|
343
|
-
const schemasContent = generateSchemasFileContent(schemaKeys, schemasByKey
|
|
567
|
+
const schemasContent = generateSchemasFileContent(schemaKeys, schemasByKey, {
|
|
568
|
+
keys: deliveredSchemaKeys,
|
|
569
|
+
createSchemas: Boolean(options.schemas),
|
|
570
|
+
importSdkPath,
|
|
571
|
+
importSchemasPath,
|
|
572
|
+
});
|
|
344
573
|
const schemasFilePath = path.join(outputPath, "schemas.ts");
|
|
345
574
|
fs.writeFileSync(schemasFilePath, schemasContent);
|
|
346
575
|
console.log(`Schemas type file written at: ${getRelativePath(rootDirectoryPath, schemasFilePath)}`);
|
|
@@ -566,6 +795,10 @@ export type Variation<F extends FeatureKey> = Features[F] extends { variation: i
|
|
|
566
795
|
const variablesFilePath = path.join(outputPath, "variables.ts");
|
|
567
796
|
fs.writeFileSync(variablesFilePath, variablesFileContent);
|
|
568
797
|
console.log(`Variables file written at: ${getRelativePath(rootDirectoryPath, variablesFilePath)}`);
|
|
798
|
+
// With neither features nor global variables, both key types are never, and TypeScript rejects
|
|
799
|
+
// overloads beside an implementation that takes never (TS2394). The implementation then takes
|
|
800
|
+
// any key, and the overloads still accept none.
|
|
801
|
+
const keyParameterType = parsedFeatures.length + parsedVariables.length > 0 ? "FeatureKey | VariableKey" : "string";
|
|
569
802
|
const functionsFileContent = `
|
|
570
803
|
import type { FeatureKey, Variation, FeatureVariableKey, FeatureVariableType } from "./features.js";
|
|
571
804
|
import type { VariableKey, VariableType } from "./variables.js";
|
|
@@ -593,7 +826,7 @@ export function getVariable<K extends VariableKey>(
|
|
|
593
826
|
context?: Context,
|
|
594
827
|
): VariableType<K> | null;
|
|
595
828
|
export function getVariable(
|
|
596
|
-
featureKeyOrVariableKey:
|
|
829
|
+
featureKeyOrVariableKey: ${keyParameterType},
|
|
597
830
|
variableKeyOrContext: string | Context = {},
|
|
598
831
|
context: Context = {},
|
|
599
832
|
): unknown {
|
|
@@ -664,7 +897,7 @@ export function useVariable<K extends VariableKey>(
|
|
|
664
897
|
context?: Context,
|
|
665
898
|
): VariableType<K> | null;
|
|
666
899
|
export function useVariable(
|
|
667
|
-
featureKeyOrVariableKey:
|
|
900
|
+
featureKeyOrVariableKey: ${keyParameterType},
|
|
668
901
|
variableKeyOrContext: string | Context = {},
|
|
669
902
|
context: Context = {},
|
|
670
903
|
): unknown {
|
|
@@ -677,6 +910,12 @@ export function useVariable(
|
|
|
677
910
|
fs.writeFileSync(reactFilePath, reactFileContent);
|
|
678
911
|
console.log(`React file written at: ${getRelativePath(rootDirectoryPath, reactFilePath)}`);
|
|
679
912
|
}
|
|
913
|
+
const shouldGenerateSchemasReact = Boolean(options.schemas) && shouldGenerateReact;
|
|
914
|
+
if (shouldGenerateSchemasReact) {
|
|
915
|
+
const schemasReactFilePath = path.join(outputPath, "schemas-react.ts");
|
|
916
|
+
fs.writeFileSync(schemasReactFilePath, generateSchemasReactFileContent(importSchemasPath));
|
|
917
|
+
console.log(`Schemas React file written at: ${getRelativePath(rootDirectoryPath, schemasReactFilePath)}`);
|
|
918
|
+
}
|
|
680
919
|
// index
|
|
681
920
|
const indexContent = [
|
|
682
921
|
`export * from "./attributes.js";`,
|
|
@@ -687,6 +926,7 @@ export function useVariable(
|
|
|
687
926
|
`export * from "./variables.js";`,
|
|
688
927
|
`export * from "./functions.js";`,
|
|
689
928
|
...(shouldGenerateReact ? [`export * from "./react.js";`] : []),
|
|
929
|
+
...(shouldGenerateSchemasReact ? [`export * from "./schemas-react.js";`] : []),
|
|
690
930
|
].join("\n") + "\n";
|
|
691
931
|
const indexFilePath = path.join(outputPath, "index.ts");
|
|
692
932
|
fs.writeFileSync(indexFilePath, indexContent);
|