@atomic-ehr/codegen 0.0.1-canary.20251010140912.b42d372 → 0.0.1-canary.20251013100511.ff2ef29
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/cli/index.js +19 -19
- package/dist/index.d.ts +3 -0
- package/dist/index.js +73 -25
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -757,6 +757,7 @@ interface APIBuilderOptions {
|
|
|
757
757
|
typeSchemaConfig?: TypeSchemaConfig;
|
|
758
758
|
logger?: CodegenLogger;
|
|
759
759
|
manager?: ReturnType<typeof CanonicalManager> | null;
|
|
760
|
+
typeSchemaOutputDir?: string /** if .ndjson -- put in one file, else -- split into separated files*/;
|
|
760
761
|
throwException?: boolean;
|
|
761
762
|
}
|
|
762
763
|
/**
|
|
@@ -826,6 +827,8 @@ declare class APIBuilder {
|
|
|
826
827
|
verbose(enabled?: boolean): APIBuilder;
|
|
827
828
|
throwException(enabled?: boolean): APIBuilder;
|
|
828
829
|
cleanOutput(enabled?: boolean): APIBuilder;
|
|
830
|
+
writeTypeSchemas(target: string): this;
|
|
831
|
+
private tryWriteTypeSchema;
|
|
829
832
|
generate(): Promise<GenerationResult>;
|
|
830
833
|
/**
|
|
831
834
|
* Generate and return the results without writing to files
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { CanonicalManager } from '@atomic-ehr/fhir-canonical-manager';
|
|
|
2
2
|
import * as fhirschema from '@atomic-ehr/fhirschema';
|
|
3
3
|
import * as fs from 'fs';
|
|
4
4
|
import { existsSync, mkdirSync } from 'fs';
|
|
5
|
+
import * as afs from 'fs/promises';
|
|
5
6
|
import { readdir, stat, unlink, readFile, writeFile, access, mkdir, rm } from 'fs/promises';
|
|
6
7
|
import * as Path2 from 'path';
|
|
7
8
|
import { join, resolve, dirname, relative } from 'path';
|
|
@@ -2945,6 +2946,33 @@ var generateTypeSchemas = async (register, logger) => {
|
|
|
2945
2946
|
return fhirSchemas;
|
|
2946
2947
|
};
|
|
2947
2948
|
|
|
2949
|
+
// src/api/writer-generator/utils.ts
|
|
2950
|
+
var words = (s) => {
|
|
2951
|
+
return s.split(/(?<=[a-z])(?=[A-Z])|[-_.\s]/).filter(Boolean);
|
|
2952
|
+
};
|
|
2953
|
+
var kebabCase = (s) => {
|
|
2954
|
+
return words(s).map((s2) => s2.toLowerCase()).join("-");
|
|
2955
|
+
};
|
|
2956
|
+
var capitalCase = (s) => {
|
|
2957
|
+
if (s.length === 0) throw new Error("Empty string");
|
|
2958
|
+
return s[0]?.toUpperCase() + s.substring(1).toLowerCase();
|
|
2959
|
+
};
|
|
2960
|
+
var camelCase = (s) => {
|
|
2961
|
+
if (s.length === 0) throw new Error("Empty string");
|
|
2962
|
+
const [first, ...rest] = words(s);
|
|
2963
|
+
return [first?.toLowerCase(), ...rest.map(capitalCase)].join("");
|
|
2964
|
+
};
|
|
2965
|
+
var pascalCase = (s) => {
|
|
2966
|
+
return words(s).map(capitalCase).join("");
|
|
2967
|
+
};
|
|
2968
|
+
var uppercaseFirstLetter = (str) => {
|
|
2969
|
+
if (!str || str.length === 0) return str;
|
|
2970
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2971
|
+
};
|
|
2972
|
+
var uppercaseFirstLetterOfEach = (strings) => {
|
|
2973
|
+
return strings.map((str) => uppercaseFirstLetter(str));
|
|
2974
|
+
};
|
|
2975
|
+
|
|
2948
2976
|
// src/typeschema/utils.ts
|
|
2949
2977
|
var groupByPackages = (typeSchemas) => {
|
|
2950
2978
|
const grouped = {};
|
|
@@ -4162,8 +4190,8 @@ function toCamelCase(str) {
|
|
|
4162
4190
|
return str.replace(/[-_\s]+(.)?/g, (_, char) => char?.toUpperCase() || "");
|
|
4163
4191
|
}
|
|
4164
4192
|
function toPascalCase(str) {
|
|
4165
|
-
const
|
|
4166
|
-
return
|
|
4193
|
+
const camelCase2 = toCamelCase(str);
|
|
4194
|
+
return camelCase2.charAt(0).toUpperCase() + camelCase2.slice(1);
|
|
4167
4195
|
}
|
|
4168
4196
|
function toSnakeCase(str) {
|
|
4169
4197
|
return str.replace(/([A-Z])/g, "_$1").replace(/[-\s]+/g, "_").toLowerCase().replace(/^_/, "");
|
|
@@ -5112,28 +5140,6 @@ ${nestedInterfaces}`;
|
|
|
5112
5140
|
);
|
|
5113
5141
|
}
|
|
5114
5142
|
};
|
|
5115
|
-
|
|
5116
|
-
// src/api/writer-generator/utils.ts
|
|
5117
|
-
var words = (s) => {
|
|
5118
|
-
return s.split(/(?<=[a-z])(?=[A-Z])|[-_.\s]/).filter(Boolean);
|
|
5119
|
-
};
|
|
5120
|
-
var kebabCase = (s) => {
|
|
5121
|
-
return words(s).map((s2) => s2.toLowerCase()).join("-");
|
|
5122
|
-
};
|
|
5123
|
-
var capitalCase = (s) => {
|
|
5124
|
-
if (s.length === 0) throw new Error("Empty string");
|
|
5125
|
-
return s[0]?.toUpperCase() + s.substring(1).toLowerCase();
|
|
5126
|
-
};
|
|
5127
|
-
var pascalCase = (s) => {
|
|
5128
|
-
return words(s).map(capitalCase).join("");
|
|
5129
|
-
};
|
|
5130
|
-
var uppercaseFirstLetter = (str) => {
|
|
5131
|
-
if (!str || str.length === 0) return str;
|
|
5132
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
5133
|
-
};
|
|
5134
|
-
var uppercaseFirstLetterOfEach = (strings) => {
|
|
5135
|
-
return strings.map((str) => uppercaseFirstLetter(str));
|
|
5136
|
-
};
|
|
5137
5143
|
var FileSystemWriter = class {
|
|
5138
5144
|
opts;
|
|
5139
5145
|
currentDir;
|
|
@@ -5669,6 +5675,7 @@ var writerToGenerator = (writerGen) => {
|
|
|
5669
5675
|
build: async (_schemas) => getGeneratedFiles()
|
|
5670
5676
|
};
|
|
5671
5677
|
};
|
|
5678
|
+
var normalizeFileName = (str) => str.replace(/[^a-zA-Z0-9]/g, "");
|
|
5672
5679
|
var APIBuilder = class {
|
|
5673
5680
|
schemas = [];
|
|
5674
5681
|
options;
|
|
@@ -5689,7 +5696,8 @@ var APIBuilder = class {
|
|
|
5689
5696
|
cleanOutput: options.cleanOutput ?? true,
|
|
5690
5697
|
typeSchemaConfig: options.typeSchemaConfig,
|
|
5691
5698
|
manager: options.manager || null,
|
|
5692
|
-
throwException: options.throwException || false
|
|
5699
|
+
throwException: options.throwException || false,
|
|
5700
|
+
typeSchemaOutputDir: options.typeSchemaOutputDir
|
|
5693
5701
|
};
|
|
5694
5702
|
this.typeSchemaConfig = options.typeSchemaConfig;
|
|
5695
5703
|
this.logger = options.logger || createLogger({
|
|
@@ -5792,6 +5800,45 @@ var APIBuilder = class {
|
|
|
5792
5800
|
this.options.cleanOutput = enabled;
|
|
5793
5801
|
return this;
|
|
5794
5802
|
}
|
|
5803
|
+
writeTypeSchemas(target) {
|
|
5804
|
+
this.options.typeSchemaOutputDir = target;
|
|
5805
|
+
return this;
|
|
5806
|
+
}
|
|
5807
|
+
async tryWriteTypeSchema(typeSchemas) {
|
|
5808
|
+
if (!this.options.typeSchemaOutputDir) return;
|
|
5809
|
+
try {
|
|
5810
|
+
if (this.options.cleanOutput) fs.rmSync(this.options.typeSchemaOutputDir, { recursive: true, force: true });
|
|
5811
|
+
await afs.mkdir(this.options.typeSchemaOutputDir, { recursive: true });
|
|
5812
|
+
let writtenCount = 0;
|
|
5813
|
+
let overrideCount = 0;
|
|
5814
|
+
const usedNames = {};
|
|
5815
|
+
this.logger.info(`Writing TypeSchema files to ${this.options.typeSchemaOutputDir}...`);
|
|
5816
|
+
for (const ts of typeSchemas) {
|
|
5817
|
+
const package_name = camelCase(ts.identifier.package.replaceAll("/", "-"));
|
|
5818
|
+
const name = normalizeFileName(ts.identifier.name.toString());
|
|
5819
|
+
const baseName = Path2.join(this.options.typeSchemaOutputDir, package_name, name);
|
|
5820
|
+
let fullName;
|
|
5821
|
+
if (usedNames[baseName] !== void 0) {
|
|
5822
|
+
usedNames[baseName]++;
|
|
5823
|
+
fullName = `${baseName}-${usedNames[baseName]}.typeschema.json`;
|
|
5824
|
+
} else {
|
|
5825
|
+
usedNames[baseName] = 0;
|
|
5826
|
+
fullName = `${baseName}.typeschema.json`;
|
|
5827
|
+
}
|
|
5828
|
+
await afs.mkdir(Path2.dirname(fullName), { recursive: true });
|
|
5829
|
+
afs.writeFile(fullName, JSON.stringify(ts, null, 2));
|
|
5830
|
+
if (await afs.exists(fullName)) overrideCount++;
|
|
5831
|
+
else writtenCount++;
|
|
5832
|
+
}
|
|
5833
|
+
this.logger.info(`Created ${writtenCount} new TypeSchema files, overrode ${overrideCount} files`);
|
|
5834
|
+
} catch (error) {
|
|
5835
|
+
if (this.options.throwException) throw error;
|
|
5836
|
+
this.logger.error(
|
|
5837
|
+
"Failed to write TypeSchema output",
|
|
5838
|
+
error instanceof Error ? error : new Error(String(error))
|
|
5839
|
+
);
|
|
5840
|
+
}
|
|
5841
|
+
}
|
|
5795
5842
|
async generate() {
|
|
5796
5843
|
const startTime = performance.now();
|
|
5797
5844
|
const result = {
|
|
@@ -5823,6 +5870,7 @@ var APIBuilder = class {
|
|
|
5823
5870
|
await manager.init();
|
|
5824
5871
|
const register = await registerFromManager(manager, this.logger);
|
|
5825
5872
|
const typeSchemas = await generateTypeSchemas(register, this.logger);
|
|
5873
|
+
await this.tryWriteTypeSchema(typeSchemas);
|
|
5826
5874
|
this.logger.debug(`Executing ${this.generators.size} generators`);
|
|
5827
5875
|
await this.executeGenerators(result, typeSchemas);
|
|
5828
5876
|
this.logger.info("Generation completed successfully");
|