Skip to content

Commit

Permalink
Improve schema parser (#122)
Browse files Browse the repository at this point in the history
* Improve schema types

* Remove unused schema types

* Refactor schema parser

* Add test snapshots

* Add more tests and comments

* Throw error instead of just logging it

* Cover more lines

* Cover missing lines

* Cover one more missing line

* Add new `skipWords`

* Remove unused `path` import

* Add todo comment

* Add test case of generated field IDs

* Reduce coverage to `0.99`

* Reduce coverage to `0.995`

* Improve `error.ts`

* Add comment

* Apply suggestions from code review

Co-authored-by: Ben <[email protected]>

* Apply suggestions

* Add more test for string utils

* Update

* Bring back coverage to 1

* Update bunfig.toml

* Make types stronger

* Update types

* Remove non-null assertions

* Make use of `Map`s

* Update test order

---------

Co-authored-by: Ben <[email protected]>
  • Loading branch information
deebov and NuroDev authored Aug 28, 2024
1 parent c73230b commit 3827648
Show file tree
Hide file tree
Showing 14 changed files with 1,896 additions and 544 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
"cryptographically",
"programatically",
"Ds",
"str"
"str",
"namespace",
"namespaces",
"deebov"
],
"comments": true,
"strings": false,
Expand Down
10 changes: 3 additions & 7 deletions src/bin/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { select } from '@inquirer/prompts';
import ora from 'ora';
import path from 'path';

import { parseSchemaDefinitionFile } from '@/src/bin/parser';
import { readConfig, resetConfig, saveConfig } from '@/src/bin/utils/config';
import { exists } from '@/src/bin/utils/file';
import { safeParseJson } from '@/src/bin/utils/json';
import { parseSchemaDefinitionFile } from '@/src/bin/utils/schema';
import { compareSchemas, getSchemas, getSpaces, replaceFieldIdsWithExisting } from '@/src/bin/utils/sync';

type Status = 'readingConfig' | 'readingSchemas' | 'comparing' | 'syncing';
Expand Down Expand Up @@ -81,10 +81,7 @@ export default async (positionals: string[], appToken?: string, sessionToken?: s
spinner.text = 'Reading schema definitions';

const schemaFile = path.join(config.schemasDir || 'schemas', 'index.ts');
let schemaDefinitions = await parseSchemaDefinitionFile(schemaFile, (error) => {
spinner.fail(error);
process.exit(1);
});
let schemaDefinitions = await parseSchemaDefinitionFile(schemaFile);

status = 'comparing';
spinner.start('Retrieving existing schemas');
Expand All @@ -97,8 +94,7 @@ export default async (positionals: string[], appToken?: string, sessionToken?: s
// Add summary to schema definitions.
schemaDefinitions = schemaDefinitions.map((schema) => ({
...schema,
summary:
schema.summary || remoteSchemas.find((s) => s.id === schema.id)?.summary || DEFAULT_SCHEMA_SUMMARY,
summary: remoteSchemas.find(({ slug }) => slug === schema.slug)?.summary || DEFAULT_SCHEMA_SUMMARY,
}));

status = 'comparing';
Expand Down
51 changes: 51 additions & 0 deletions src/bin/parser/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pluralize from 'pluralize';

// Advanced fields types exported from 'ronin/schema'.
const ADVANCED_FIELD_TYPES = ['Blob', 'JSON'];
// Basic field types supported in defining schemas.
const BASIC_FIELD_TYPES = ['string', 'number', 'boolean', 'Date'];

export function createMissingSchemaError(missingSchemas: { name: string; parent: string; source: string }[]) {
return (
`The following schemas were used as a reference but weren't included in ` +
`the \`Schemas\` interface:\n\n` +
`${missingSchemas
.map(({ name, parent, source }) => ` - \`${name}\` in \`${parent}\` (${source})`)
.join('\n')}\n\n` +
`Please include them in the \`Schemas\` interface or remove their references.`
);
}

export function createMissingPluralError(
schemaName: string,
schemaSlug: string,
pluralSchemaSlug: string,
schemaRecordsAlias: string,
) {
const pluralTypeName = pluralize(schemaName);

return (
`The schema \`${schemaName}\` does not have a plural slug and name defined.\n\n` +
`Please define them in your schema definition file and include them in the \`Schemas\` interface:\n\n` +
`import type * as Schema from 'ronin/schema';\n\n` +
`type ${pluralTypeName} = Schema.${schemaRecordsAlias}<${schemaName}>;\n\n` +
`interface Schemas {\n ${schemaSlug}: ${schemaName};\n ${pluralize(pluralSchemaSlug)}: ${pluralTypeName};\n}`
);
}

export function createUnknownFieldError(
unknownFields: { parent: string; name: string; type: string; source: string }[],
) {
return (
`The type of the following fields could not be determined:\n\n` +
`${unknownFields
.map(
({ name, parent, type, source }) => ` - \`${parent}.${name}\` is typed as \`${type}\` (${source})`,
)
.join('\n')}\n\n` +
`Please make sure that the field is typed as any of the available field types:\n\n` +
`${BASIC_FIELD_TYPES.map((type) => ` - \`${type}\``).join('\n')}\n` +
`${ADVANCED_FIELD_TYPES.map((type) => ` - \`Schema.${type}\``).join('\n')}\n` +
` - or a reference to another schema.`
);
}
Loading

0 comments on commit 3827648

Please sign in to comment.