Skip to content

Commit

Permalink
Added spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
colodenn committed Jan 10, 2025
1 parent fca6adc commit f89f0ba
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
Binary file modified bun.lockb
Binary file not shown.
22 changes: 8 additions & 14 deletions src/commands/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { getModels } from '@/src/utils/model';
import { Protocol } from '@/src/utils/protocol';
import { getSpaces } from '@/src/utils/space';
import { type Status, spinner } from '@/src/utils/spinner';
import type { Model } from '@ronin/compiler';
import type { Database } from '@ronin/engine';

Expand All @@ -28,9 +29,6 @@ export const MIGRATION_FLAGS = {

type Flags = BaseFlags & Partial<Record<keyof typeof MIGRATION_FLAGS, boolean>>;

/** Current status of the migration creation process */
type Status = 'readingConfig' | 'readingModels' | 'comparing' | 'syncing';

/**
* Handles migration commands for creating and applying database migrations.
*/
Expand All @@ -51,15 +49,12 @@ export default async function main(
await create(appToken, sessionToken, flags);
break;
default: {
console.error('Please specify a valid sub command.');
spinner.fail('Please specify a valid sub command.');
process.exit(1);
}
}
} catch (error) {
console.error(
'An unexpected error occurred:',
error instanceof Error ? error.message : error,
);
spinner.fail(`An unexpected error occurred: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
}
Expand All @@ -75,7 +70,7 @@ const applyMigrationStatements = async (
slug: string,
): Promise<void> => {
if (flags.prod) {
console.log('\nApplying migration to production database');
spinner.info('Applying migration to production database');

await fetch(`https://data.ronin.co/?data-selector=${slug}`, {
method: 'POST',
Expand All @@ -94,7 +89,7 @@ const applyMigrationStatements = async (
return;
}

console.log('\nApplying migration to local database');
spinner.info('Applying migration to local database');

await db.query(statements.map(({ statement }) => statement));
fs.writeFileSync('.ronin/db.sqlite', await db.getContents());
Expand All @@ -109,8 +104,8 @@ const create = async (
flags: Flags,
): Promise<void> => {
let status: Status = 'readingConfig';
const spinner = ora('Reading configuration').start();

spinner.start('Reading configuration');
const db = await initializeDatabase();

try {
Expand Down Expand Up @@ -190,8 +185,7 @@ const create = async (

process.exit(0);
} catch (err) {
spinner.fail(`Failed during ${status}:\n`);
console.error(err instanceof Error ? err.message : err);
spinner.fail(`Failed during ${status}:\n ${err instanceof Error ? err.message : err}`);
throw err;
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import migrate, { MIGRATION_FLAGS } from '@/src/commands/migration';
import { printHelp, printVersion } from '@/src/utils/info';
import { BASE_FLAGS, type BaseFlags } from '@/src/utils/misc';
import { getSession } from '@/src/utils/session';
import { spinner } from '@/src/utils/spinner';

let flags: BaseFlags;
let positionals: Array<string>;
Expand All @@ -19,7 +20,7 @@ try {
}));
} catch (err) {
if (err instanceof Error) {
console.error(err.message);
spinner.fail(err.message);
} else {
throw err;
}
Expand Down Expand Up @@ -51,7 +52,7 @@ const run = async (): Promise<void> => {
message +=
'a `RONIN_TOKEN` environment variable containing an app token must be provided.';

console.error(message);
spinner.fail(message);
process.exit(1);
}

Expand Down
12 changes: 4 additions & 8 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import type { parseArgs } from 'node:util';
import { fieldsToCreate, fieldsToDrop } from '@/src/utils/field';
import { spinner } from '@/src/utils/spinner';
import type { Model, Result } from '@ronin/compiler';
import type * as SyntaxPackage from '@ronin/syntax/queries';
import resolveFrom from 'resolve-from';
Expand Down Expand Up @@ -172,7 +173,7 @@ export const logTableDiff = (tableB: Model, tableA: Model, tableName: string): v
*/
export const getModelDefinitions = async (): Promise<Array<Model>> => {
if (!fs.existsSync(MODEL_IN_CODE_PATH)) {
console.error('Could not find a model definition file schema/index.ts');
spinner.fail('Could not find a model definition file schema/index.ts');
process.exit(1);
}

Expand All @@ -186,7 +187,7 @@ export const getModelDefinitions = async (): Promise<Array<Model>> => {
const count = slugCounts.get(model.slug) ?? 0;
slugCounts.set(model.slug, count + 1);
if (count > 0) {
console.error(
spinner.fail(
`Duplicate model slug found: "${model.slug}". You cannot have two models with the same slug.`,
);
process.exit(1);
Expand Down Expand Up @@ -360,12 +361,7 @@ export const getSyntaxPackage = (): Promise<typeof SyntaxPackage> => {
const roninSyntaxPath = resolveFrom.silent(process.cwd(), '@ronin/syntax/queries');

if (!roninSyntaxPath) {
// The newline is needed because a spinner is running before this message is printed.
// TODO: Make the spinner accessible in `Protocol`.
console.error(
'\nThe "ronin" package must be installed in your project in order to create migrations.',
);
process.exit(1);
throw new Error('The "ronin" package must be installed in your project in order to create migrations.')
}

return import(roninSyntaxPath);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { spinner } from '@/src/utils/spinner';
import toml, { type JsonMap } from '@iarna/toml';
import ini from 'ini';

Expand Down Expand Up @@ -59,7 +60,7 @@ const readConfigFile = async <T>(
configContents = parser(contents);
} catch (err) {
if ((err as { code: string }).code !== 'ENOENT') {
console.error(`Failed to read ${name} config file at ${filePath}`, err);
spinner.fail(`Failed to read ${name} config file at ${filePath}`);
}
configContents = {} as T; // Return an empty object of type T
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/spinner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ora, { type Ora } from 'ora';
import { version } from '../../package.json';

/** Current status of the migration creation process */
export type Status = 'readingConfig' | 'readingModels' | 'comparing' | 'syncing';

export const spinner: Ora = ora(`RONIN ${version}`).start();

0 comments on commit f89f0ba

Please sign in to comment.