Skip to content

Commit 697dbe5

Browse files
committed
Improve type-safety in readSchemas and warn for legacy schemas
1 parent 64eba31 commit 697dbe5

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

packages/ts-codegen/src/utils/schemas.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,37 @@ export const readSchemas = async ({
3434
const fn = clean
3535
? cleanse
3636
: (schema: JSONSchema[] | Partial<IDLObject>) => schema;
37-
const files = await findSchemaFiles(schemaDir);
3837

39-
const schemas: JSONSchema[] = files.map((file) =>
40-
JSON.parse(readFileSync(file, 'utf-8'))
38+
const files = (await findSchemaFiles(schemaDir)).map((path) =>
39+
readFileSync(path, 'utf-8')
4140
);
4241

43-
if (schemas.length > 1) {
42+
if (files.length > 1) {
4443
// legacy
4544
// TODO add console.warn here
45+
console.warn(
46+
'Found a multiple schema files. This mode will be removed in the next major version. Please migrate the schemas that contain a single <contract_name>.json IDL file (CosmWasm 1.1+).'
47+
);
48+
49+
const schemas: JSONSchema[] = files.map((file) => JSON.parse(file));
4650
return {
4751
schemas: fn(schemas),
4852
};
4953
}
5054

51-
if (schemas.length === 0) {
55+
if (files.length === 0) {
5256
throw new Error(
5357
'Error [too few files]: requires one schema file per contract'
5458
);
5559
}
5660

57-
if (schemas.length !== 1) {
61+
if (files.length !== 1) {
5862
throw new Error(
5963
'Error [too many files]: CosmWasm v1.1 schemas supports one file'
6064
);
6165
}
6266

63-
const idlObject: Partial<IDLObject> = schemas[0] as Partial<IDLObject>;
67+
const idlObject: Partial<IDLObject> = JSON.parse(files[0]);
6468
const {
6569
// contract_name,
6670
// contract_version,
@@ -75,8 +79,13 @@ export const readSchemas = async ({
7579

7680
if (typeof idl_version !== 'string') {
7781
// legacy
82+
// fall back to a single JSON Schema file
83+
console.warn(
84+
'Found a single schema file with missing idl_version. This mode will be removed in the next major version. Please migrate the schemas that contain a single <contract_name>.json IDL file (CosmWasm 1.1+).'
85+
);
86+
const schema: JSONSchema = JSON.parse(files[0]);
7887
return {
79-
schemas: fn(schemas),
88+
schemas: fn([schema]),
8089
};
8190
}
8291

0 commit comments

Comments
 (0)