Skip to content

Commit

Permalink
Exclude non-model exports from schemas in code (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
colodenn authored Jan 16, 2025
1 parent be36858 commit ebef884
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export default async (
logModelDiffs(definedModels, existingModels);
}

spinner.stop();
const modelDiff = await diffModels(definedModels, existingModels);
spinner.start();

if (modelDiff.length === 0) {
spinner.succeed('No changes detected');
Expand Down
2 changes: 1 addition & 1 deletion src/utils/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const diffFields = async (
const confirmRename =
rename ||
(await confirm({
message: `Did you mean to rename: ${field.from.slug} -> ${field.to.slug}`,
message: `Did you mean to rename field: ${field.from.slug} -> ${field.to.slug}`,
default: false,
}));

Expand Down
5 changes: 4 additions & 1 deletion src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ export const getModelDefinitions = async (): Promise<Array<Model>> => {
}

const sortedModels = sortModels(
Object.values(await import(MODEL_IN_CODE_PATH)) as Array<Model>,
Object.values(await import(MODEL_IN_CODE_PATH)).filter(
(value): value is Model =>
typeof value === 'object' && value !== null && 'slug' in value,
) as Array<Model>,
);

// Check for duplicate model slugs
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import type { Model } from '@ronin/compiler';
import { add } from 'ronin';
import { blob, boolean, date, link, model, number, string } from 'ronin/schema';

export const CONSTANTS = {
FIRSTNAME: 'Cornelius',
LASTNAME: 'Denninger',
};

const TestAccount = model({
slug: 'account',
fields: {
Expand Down
19 changes: 16 additions & 3 deletions tests/utils/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, describe, expect, mock, spyOn, test } from 'bun:test';

import fs from 'node:fs';
import {
InvalidResponseError,
MODEL_IN_CODE_PATH,
Expand All @@ -11,9 +12,7 @@ import {
sortModels,
} from '@/src/utils/misc';

import fs from 'node:fs';

import { Account, TestA, TestB } from '@/fixtures/index';
import { Account, CONSTANTS, TestA, TestB } from '@/fixtures/index';
import type { Model } from '@ronin/compiler';

describe('misc', () => {
Expand Down Expand Up @@ -112,6 +111,20 @@ describe('misc', () => {
expect(models).toStrictEqual([]);
});

test('should return models in code definitions and ignore constants - empty', async () => {
mock.module(MODEL_IN_CODE_PATH, () => {
return { CONSTANTS };
});

const existsSync = spyOn(fs, 'existsSync');
existsSync.mockReturnValue(true);
existsSync.mockClear();

const models = await getModelDefinitions();
expect(models).toHaveLength(0);
expect(models).toStrictEqual([]);
});

test('model file does not exist', async () => {
const existSpy = spyOn(process, 'exit').mockImplementation(() => {
throw new Error('process.exit called');
Expand Down

0 comments on commit ebef884

Please sign in to comment.