Skip to content

Commit

Permalink
#831 Error when generating types from non ontology
Browse files Browse the repository at this point in the history
  • Loading branch information
Polleps authored and joepio committed Mar 26, 2024
1 parent fbb1e7d commit 28dd28f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
7 changes: 7 additions & 0 deletions browser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This changelog covers all three packages, as they are (for now) updated as a who

### @tomic/lib

- [#840](https://github.com/atomicdata-dev/atomic-server/issues/840) Added `store.search()`.
- Deprecated `resource.getSubject()` in favor of `resource.subject`.
- Deprecated `store.getResouceAsync()` in favor of `store.getResource()`.
- Deprecated `resource.pushPropval()` in favor of `resource.push()`.
Expand All @@ -34,6 +35,12 @@ This changelog covers all three packages, as they are (for now) updated as a who
- `resource.removeClasses()`
- `resource.addClasses()`

### @tomic/cli

- [#837](https://github.com/atomicdata-dev/atomic-server/issues/837) Fix timestamp is mapped to string instead of number.
- [#831](https://github.com/atomicdata-dev/atomic-server/issues/831) Give clear error when trying to generate types from a non ontology resource
- Use type import in generated files.

## v0.37.0

### Atomic Browser
Expand Down
18 changes: 12 additions & 6 deletions browser/cli/src/commands/ontologies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ import { atomicConfig } from '../config.js';
import { generateIndex } from '../generateIndex.js';
import { PropertyRecord } from '../PropertyRecord.js';
import { generateExternals } from '../generateExternals.js';
import { validateOntologies } from '../validateOntologies.js';

export const ontologiesCommand = async (_args: string[]) => {
const propertyRecord = new PropertyRecord();

console.log(
chalk.blue(
`Found ${chalk.red(
Object.keys(atomicConfig.ontologies).length,
)} ontologies`,
),
chalk.blue(`Found ${chalk.red(atomicConfig.ontologies.length)} ontologies`),
);

for (const subject of Object.values(atomicConfig.ontologies)) {
const [valid, report] = await validateOntologies(atomicConfig.ontologies);

if (!valid) {
console.log(chalk.red('Could not generate ontologies'));
console.log(report);

return;
}

for (const subject of atomicConfig.ontologies) {
await write(await generateOntology(subject, propertyRecord));
}

Expand Down
3 changes: 1 addition & 2 deletions browser/cli/src/generateOntology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { generateBaseObject } from './generateBaseObject.js';
import { generateClasses } from './generateClasses.js';
import { store } from './store.js';
import { camelCaseify } from './utils.js';
// TODO: Replace with actual project config file.
import { generatePropTypeMapping } from './generatePropTypeMapping.js';
import { generateSubjectToNameMapping } from './generateSubjectToNameMapping.js';
import { generateClassExports } from './generateClassExports.js';
Expand All @@ -26,7 +25,7 @@ const TEMPLATE = `
* For more info on how to use ontologies: https://github.com/atomicdata-dev/atomic-server/blob/develop/browser/cli/readme.md
* -------------------------------- */
import { BaseProps } from '${Inserts.MODULE_ALIAS}'
import type { BaseProps } from '${Inserts.MODULE_ALIAS}'
${Inserts.BASE_OBJECT}
Expand Down
29 changes: 29 additions & 0 deletions browser/cli/src/validateOntologies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { core } from '@tomic/lib';
import { store } from './store.js';
import chalk from 'chalk';

export const validateOntologies = async (
ontologies: string[],
): Promise<[valid: boolean, report: string]> => {
let isValid = true;
let report = '';

for (const subject of ontologies) {
try {
const resource = await store.getResourceAsync(subject);

if (!resource.hasClasses(core.classes.ontology)) {
isValid = false;
const isA = await store.getResourceAsync(resource.getClasses()[0]);
report += `Expected ${chalk.cyan(
resource.title,
)} to have class Ontology but found ${chalk.cyan(isA.title)}\n`;
}
} catch (e) {
isValid = false;
report += `Could not fetch ontology at ${subject}\n`;
}
}

return [isValid, report];
};

0 comments on commit 28dd28f

Please sign in to comment.