@kosdev-code/kos-codegen-core 0.1.0-next.780 → 0.1.0-next.782

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/index.js +77 -39
  2. package/index.js.map +1 -1
  3. package/index.mjs +77 -39
  4. package/index.mjs.map +1 -1
  5. package/lib/codegen-filesystem.d.ts.map +1 -1
  6. package/lib/generators/add-container-support/generate-add-container-support.d.ts.map +1 -1
  7. package/lib/generators/add-future-to-model/registration-transformer.d.ts.map +1 -1
  8. package/lib/generators/augment/ts-toolkit.d.ts.map +1 -1
  9. package/lib/generators/component/plugin-handlers/base.d.ts.map +1 -1
  10. package/lib/generators/component/plugin-handlers/custom-handler.d.ts.map +1 -1
  11. package/lib/generators/component/utils/validation.d.ts.map +1 -1
  12. package/lib/generators/describe/describe-model.d.ts.map +1 -1
  13. package/lib/generators/generate-container-model.d.ts +1 -0
  14. package/lib/generators/generate-container-model.d.ts.map +1 -1
  15. package/lib/generators/generate-context.d.ts.map +1 -1
  16. package/lib/generators/generate-hook.d.ts.map +1 -1
  17. package/lib/generators/generate-model.d.ts.map +1 -1
  18. package/lib/generators/member-mutators/add-computed.d.ts.map +1 -1
  19. package/lib/generators/member-mutators/add-config-property.d.ts.map +1 -1
  20. package/lib/generators/member-mutators/add-dependency.d.ts.map +1 -1
  21. package/lib/generators/member-mutators/add-model-effect.d.ts.map +1 -1
  22. package/lib/generators/member-mutators/add-property.d.ts.map +1 -1
  23. package/lib/generators/member-mutators/add-service-request.d.ts.map +1 -1
  24. package/lib/generators/member-mutators/add-topic-handler.d.ts.map +1 -1
  25. package/lib/generators/types/lookup-sdk-type.d.ts.map +1 -1
  26. package/lib/generators/update-model-index.d.ts.map +1 -1
  27. package/lib/generators/validate/validate-model.d.ts.map +1 -1
  28. package/lib/kos-config.d.ts.map +1 -1
  29. package/lib/project-discovery.d.ts.map +1 -1
  30. package/package.json +2 -2
  31. package/templates/kos-container-model/model/__nameDashCase__-model.ts.template +14 -7
package/index.js CHANGED
@@ -607,6 +607,13 @@ function updateModelIndex(codegenFs, indexPath, modelPath) {
607
607
  ts__namespace.ScriptTarget.Latest,
608
608
  true
609
609
  );
610
+ const alreadyExported = sourceFile.statements.some(
611
+ (st) => ts__namespace.isExportDeclaration(st) && st.moduleSpecifier !== void 0 && ts__namespace.isStringLiteral(st.moduleSpecifier) && st.moduleSpecifier.text === `./${modelPath}`
612
+ );
613
+ if (alreadyExported) {
614
+ logger.info(`Export for ${modelPath} already present in ${indexPath}`);
615
+ return;
616
+ }
610
617
  const exportDeclaration = ts__namespace.factory.createExportDeclaration(
611
618
  void 0,
612
619
  false,
@@ -668,7 +675,12 @@ function generateHook(codegenFs, templateDir, options, cwd, projects) {
668
675
  generateFilesFromTemplates(
669
676
  codegenFs,
670
677
  templateDir,
671
- path__namespace.join(projectRoot, options.appDirectory, "hooks", normalized.nameDashCase),
678
+ path__namespace.join(
679
+ projectRoot,
680
+ options.appDirectory,
681
+ "hooks",
682
+ normalized.nameDashCase
683
+ ),
672
684
  normalized
673
685
  );
674
686
  appendBarrelExport(
@@ -787,9 +799,7 @@ function generateContainerModel(codegenFs, templateDir, options, cwd, projects)
787
799
  { ...normalized, internal }
788
800
  );
789
801
  if (options.dataServices) {
790
- logger.info(
791
- `Generating data services for ${modelNameDashCase}`
792
- );
802
+ logger.info(`Generating data services for ${modelNameDashCase}`);
793
803
  generateFilesFromTemplates(
794
804
  codegenFs,
795
805
  path__namespace.join(templateDir, "services"),
@@ -895,9 +905,7 @@ function generateModel(params) {
895
905
  const modelLocation = kosConfig?.generator?.defaults?.model?.folder || "";
896
906
  const internal = !!kosConfig?.generator?.internal;
897
907
  options.modelDirectory = options.modelDirectory || modelLocation;
898
- logger.info(
899
- `Generating model ${normalized.nameDashCase} in ${projectRoot}`
900
- );
908
+ logger.info(`Generating model ${normalized.nameDashCase} in ${projectRoot}`);
901
909
  generateFilesFromTemplates(
902
910
  codegenFs,
903
911
  path__namespace.join(modelTemplateDir, "model"),
@@ -1494,9 +1502,7 @@ class RegistrationFileTransformer {
1494
1502
  const logger = getCodegenLogger();
1495
1503
  const { registrationFilePath } = this.options;
1496
1504
  if (!registrationFilePath || !this.codegenFs.exists(registrationFilePath)) {
1497
- logger.warn(
1498
- "Registration file not found, skipping registration updates"
1499
- );
1505
+ logger.warn("Registration file not found, skipping registration updates");
1500
1506
  return;
1501
1507
  }
1502
1508
  let content = this.codegenFs.read(registrationFilePath);
@@ -1688,9 +1694,7 @@ function ensureNamedImport(sourceFile, moduleSpecifier, names) {
1688
1694
  }
1689
1695
  }
1690
1696
  function resolveSdkModuleSpecifier(sourceFile) {
1691
- const decl = sourceFile.getImportDeclarations().find(
1692
- (d) => d.getNamedImports().some((n) => n.getName() === "kosModel")
1693
- );
1697
+ const decl = sourceFile.getImportDeclarations().find((d) => d.getNamedImports().some((n) => n.getName() === "kosModel"));
1694
1698
  return decl?.getModuleSpecifierValue() ?? "@kosdev-code/kos-ui-sdk";
1695
1699
  }
1696
1700
  function getModelClass(sourceFile, preferName) {
@@ -1709,7 +1713,10 @@ function addClassDecorator(cls, name, opts) {
1709
1713
  const decorators = cls.getDecorators();
1710
1714
  const kosModelIdx = decorators.findIndex((d) => d.getName() === "kosModel");
1711
1715
  const insertIdx = kosModelIdx >= 0 ? kosModelIdx + 1 : decorators.length;
1712
- cls.insertDecorator(insertIdx, { name: `${name}${typeArgs}`, arguments: args ? [args] : [] });
1716
+ cls.insertDecorator(insertIdx, {
1717
+ name: `${name}${typeArgs}`,
1718
+ arguments: args ? [args] : []
1719
+ });
1713
1720
  }
1714
1721
  function ensureDeclarationMerge(sourceFile, interfaceName, extendsExpr, typeParameters = []) {
1715
1722
  const baseType = extendsExpr.split("<")[0].trim();
@@ -1741,7 +1748,9 @@ function addDecoratedMethod(cls, spec) {
1741
1748
  name: spec.name,
1742
1749
  isAsync: spec.isAsync,
1743
1750
  returnType: spec.returnType,
1744
- parameters: spec.parameters?.map((p) => ({ name: p.name, type: p.type })),
1751
+ parameters: spec.parameters?.map((p) => {
1752
+ return { name: p.name, type: p.type };
1753
+ }),
1745
1754
  statements: spec.statements,
1746
1755
  decorators: [
1747
1756
  {
@@ -1807,7 +1816,9 @@ function buildDecoratorArgs(options) {
1807
1816
  top.push(`containerProperty: ${JSON.stringify(options.containerProperty)}`);
1808
1817
  }
1809
1818
  if (options.sortKey) {
1810
- top.push(`containerOptions: { sortKey: ${JSON.stringify(options.sortKey)} }`);
1819
+ top.push(
1820
+ `containerOptions: { sortKey: ${JSON.stringify(options.sortKey)} }`
1821
+ );
1811
1822
  }
1812
1823
  return top.length ? `{ ${top.join(", ")} }` : "";
1813
1824
  }
@@ -1864,7 +1875,9 @@ function addModelEffectToModel(codegenFs, options, projects) {
1864
1875
  if (!codegenFs.exists(modelFilePath)) {
1865
1876
  throw new Error(`Model file not found: ${modelFilePath}`);
1866
1877
  }
1867
- logger.info(`Adding @kosModelEffect "${options.methodName}" to ${options.modelName}`);
1878
+ logger.info(
1879
+ `Adding @kosModelEffect "${options.methodName}" to ${options.modelName}`
1880
+ );
1868
1881
  transformSourceFile(codegenFs, modelFilePath, (sf) => {
1869
1882
  const sdk = resolveSdkModuleSpecifier(sf);
1870
1883
  ensureNamedImport(sf, sdk, [{ name: "kosModelEffect" }]);
@@ -1888,7 +1901,9 @@ function addDependencyToModel(codegenFs, options, projects) {
1888
1901
  if (!codegenFs.exists(modelFilePath)) {
1889
1902
  throw new Error(`Model file not found: ${modelFilePath}`);
1890
1903
  }
1891
- logger.info(`Adding @kosDependency "${options.propertyName}" to ${options.modelName}`);
1904
+ logger.info(
1905
+ `Adding @kosDependency "${options.propertyName}" to ${options.modelName}`
1906
+ );
1892
1907
  const argsParts = [`modelType: ${options.modelTypeRef}`];
1893
1908
  if (options.id) argsParts.push(`id: ${JSON.stringify(options.id)}`);
1894
1909
  transformSourceFile(codegenFs, modelFilePath, (sf) => {
@@ -1896,7 +1911,9 @@ function addDependencyToModel(codegenFs, options, projects) {
1896
1911
  ensureNamedImport(sf, sdk, [{ name: "kosDependency" }]);
1897
1912
  if (options.dependencyPackage) {
1898
1913
  const named = [];
1899
- const bean = options.modelTypeRef.match(/^([A-Za-z_$][\w$]*)\.type$/)?.[1];
1914
+ const bean = options.modelTypeRef.match(
1915
+ /^([A-Za-z_$][\w$]*)\.type$/
1916
+ )?.[1];
1900
1917
  if (bean) named.push({ name: bean });
1901
1918
  const depType = options.dependencyType?.trim();
1902
1919
  if (depType && /^[A-Za-z_$][\w$]*$/.test(depType) && !FRAMEWORK_TYPES.has(depType)) {
@@ -1922,7 +1939,9 @@ function addTopicHandlerToModel(codegenFs, options, projects) {
1922
1939
  if (!codegenFs.exists(modelFilePath)) {
1923
1940
  throw new Error(`Model file not found: ${modelFilePath}`);
1924
1941
  }
1925
- logger.info(`Adding @kosTopicHandler "${options.handlerName}" to ${options.modelName}`);
1942
+ logger.info(
1943
+ `Adding @kosTopicHandler "${options.handlerName}" to ${options.modelName}`
1944
+ );
1926
1945
  const websocket = options.websocket ?? true;
1927
1946
  const eventType = options.eventType || "unknown";
1928
1947
  transformSourceFile(codegenFs, modelFilePath, (sf) => {
@@ -1948,7 +1967,9 @@ function addConfigPropertyToModel(codegenFs, options, projects) {
1948
1967
  if (!codegenFs.exists(modelFilePath)) {
1949
1968
  throw new Error(`Model file not found: ${modelFilePath}`);
1950
1969
  }
1951
- logger.info(`Adding @kosConfigProperty "${options.propertyName}" to ${options.modelName}`);
1970
+ logger.info(
1971
+ `Adding @kosConfigProperty "${options.propertyName}" to ${options.modelName}`
1972
+ );
1952
1973
  const valueType = options.valueType || "unknown";
1953
1974
  transformSourceFile(codegenFs, modelFilePath, (sf) => {
1954
1975
  const sdk = resolveSdkModuleSpecifier(sf);
@@ -1989,7 +2010,9 @@ function addPropertyToModel(codegenFs, options, projects) {
1989
2010
  }
1990
2011
  const type = options.type || "string";
1991
2012
  const initializer = options.initializer !== void 0 ? options.initializer : defaultInitializer(type);
1992
- logger.info(`Adding property "${options.propertyName}: ${type}" to ${options.modelName}`);
2013
+ logger.info(
2014
+ `Adding property "${options.propertyName}: ${type}" to ${options.modelName}`
2015
+ );
1993
2016
  transformSourceFile(codegenFs, modelFilePath, (sf) => {
1994
2017
  const cls = getModelClass(sf);
1995
2018
  addPlainProperty(cls, {
@@ -2008,7 +2031,9 @@ function addComputedToModel(codegenFs, options, projects) {
2008
2031
  if (!codegenFs.exists(modelFilePath)) {
2009
2032
  throw new Error(`Model file not found: ${modelFilePath}`);
2010
2033
  }
2011
- logger.info(`Adding computed getter "${options.name}" to ${options.modelName}`);
2034
+ logger.info(
2035
+ `Adding computed getter "${options.name}" to ${options.modelName}`
2036
+ );
2012
2037
  transformSourceFile(codegenFs, modelFilePath, (sf) => {
2013
2038
  const cls = getModelClass(sf);
2014
2039
  addGetter(cls, {
@@ -2069,7 +2094,9 @@ function addServiceRequestToModel(codegenFs, options, projects) {
2069
2094
  `Adding @kosServiceRequest "${options.methodName}" (${method} ${options.servicePath}) to ${options.modelName}`
2070
2095
  );
2071
2096
  transformSourceFile(codegenFs, modelFilePath, (sf) => {
2072
- ensureNamedImport(sf, serviceImport, [{ name: "kosServiceRequest" }]);
2097
+ ensureNamedImport(sf, serviceImport, [
2098
+ { name: "kosServiceRequest" }
2099
+ ]);
2073
2100
  ensureNamedImport(sf, resolveSdkModuleSpecifier(sf), [
2074
2101
  { name: "DependencyLifecycle" }
2075
2102
  ]);
@@ -2079,7 +2106,9 @@ function addServiceRequestToModel(codegenFs, options, projects) {
2079
2106
  decoratorName: "kosServiceRequest",
2080
2107
  decoratorArgsText: `{ path: ${JSON.stringify(
2081
2108
  options.servicePath
2082
- )}, method: ${JSON.stringify(method)}, lifecycle: DependencyLifecycle.${lifecycle} }`,
2109
+ )}, method: ${JSON.stringify(
2110
+ method
2111
+ )}, lifecycle: DependencyLifecycle.${lifecycle} }`,
2083
2112
  returnType: "void",
2084
2113
  statements: "// TODO: handle the typed response"
2085
2114
  });
@@ -2101,7 +2130,9 @@ function validateModel(codegenFs, options, projects) {
2101
2130
  throw new Error(`Model file not found: ${modelFilePath}`);
2102
2131
  }
2103
2132
  const project = new tsMorph.Project({ useInMemoryFileSystem: true });
2104
- const sf = project.createSourceFile(modelFilePath, content, { overwrite: true });
2133
+ const sf = project.createSourceFile(modelFilePath, content, {
2134
+ overwrite: true
2135
+ });
2105
2136
  const findings = [];
2106
2137
  const hasKosModel = sf.getClasses().some((c) => c.getDecorator("kosModel"));
2107
2138
  if (!hasKosModel) {
@@ -2180,11 +2211,7 @@ function collectDecorated(cls, decoratorName) {
2180
2211
  for (const m of cls.getMethods()) {
2181
2212
  const dec = m.getDecorator(decoratorName);
2182
2213
  if (dec) {
2183
- visit(
2184
- m.getName(),
2185
- () => dec.getText(),
2186
- m.getReturnTypeNode()?.getText()
2187
- );
2214
+ visit(m.getName(), () => dec.getText(), m.getReturnTypeNode()?.getText());
2188
2215
  }
2189
2216
  }
2190
2217
  for (const p of cls.getProperties()) {
@@ -2202,7 +2229,9 @@ function describeModel(codegenFs, options, projects) {
2202
2229
  throw new Error(`Model file not found: ${modelFilePath}`);
2203
2230
  }
2204
2231
  const project = new tsMorph.Project({ useInMemoryFileSystem: true });
2205
- const sf = project.createSourceFile(modelFilePath, content, { overwrite: true });
2232
+ const sf = project.createSourceFile(modelFilePath, content, {
2233
+ overwrite: true
2234
+ });
2206
2235
  let modelType;
2207
2236
  const modelTypeDecl = sf.getVariableDeclaration("MODEL_TYPE");
2208
2237
  if (modelTypeDecl) {
@@ -2277,7 +2306,10 @@ function resolveCandidateEntries(codegenFs, options, projects) {
2277
2306
  const out = [];
2278
2307
  if (options.entryFile) {
2279
2308
  const abs = path__namespace.isAbsolute(options.entryFile) ? options.entryFile : path__namespace.join(root, options.entryFile);
2280
- out.push({ file: abs, mode: abs.endsWith(".d.ts") ? "declarations" : "source" });
2309
+ out.push({
2310
+ file: abs,
2311
+ mode: abs.endsWith(".d.ts") ? "declarations" : "source"
2312
+ });
2281
2313
  }
2282
2314
  const sdkProject = findProjectByName(root, sdkPackage, projects);
2283
2315
  const sourceRoot = sdkProject ? sdkProject.sourceRoot || path__namespace.join(sdkProject.root, "src") : void 0;
@@ -2406,11 +2438,13 @@ function lookupSdkType(codegenFs, options, projects) {
2406
2438
  mode: candidate.mode,
2407
2439
  entryFile: path__namespace.relative(codegenFs.root, candidate.file),
2408
2440
  triedEntries,
2409
- declarations: decls.map((d) => ({
2410
- kind: d.getKindName(),
2411
- file: path__namespace.relative(codegenFs.root, d.getSourceFile().getFilePath()),
2412
- signature: signatureText(d)
2413
- }))
2441
+ declarations: decls.map((d) => {
2442
+ return {
2443
+ kind: d.getKindName(),
2444
+ file: path__namespace.relative(codegenFs.root, d.getSourceFile().getFilePath()),
2445
+ signature: signatureText(d)
2446
+ };
2447
+ })
2414
2448
  };
2415
2449
  }
2416
2450
  return {
@@ -2873,7 +2907,11 @@ function validateOptions(codegenFs, options, projects) {
2873
2907
  if (!options.appProject) {
2874
2908
  throw new ValidationError("App project is required");
2875
2909
  }
2876
- const project = findProjectByName(codegenFs.root, options.appProject, projects);
2910
+ const project = findProjectByName(
2911
+ codegenFs.root,
2912
+ options.appProject,
2913
+ projects
2914
+ );
2877
2915
  if (!project) {
2878
2916
  throw new ValidationError(
2879
2917
  `Project "${options.appProject}" not found in workspace`