Skip to content

Commit

Permalink
chore(deps): upgrade TypeScript to v5.5 and enable isolatedDeclarations
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimbeng committed Jun 27, 2024
1 parent ef848a0 commit d779286
Show file tree
Hide file tree
Showing 31 changed files with 512 additions and 458 deletions.
10 changes: 10 additions & 0 deletions .changeset/purple-garlics-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@emigrate/reporter-pino': patch
'@emigrate/plugin-tools': patch
'@emigrate/postgres': patch
'@emigrate/tsconfig': patch
'@emigrate/mysql': patch
'@emigrate/cli': patch
---

Upgrade TypeScript to v5.5 and enable [isolatedDeclarations](https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/#isolated-declarations)
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
"lint-staged": "15.2.0",
"npm-run-all": "4.1.5",
"prettier": "3.1.1",
"tsx": "4.7.0",
"tsx": "4.15.7",
"turbo": "1.10.16",
"typescript": "5.3.3",
"typescript": "5.5.2",
"xo": "0.56.0"
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/collect-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type MigrationHistoryEntry, type MigrationMetadata, type MigrationMetadataFinished } from '@emigrate/types';
import { toMigrationMetadata } from './to-migration-metadata.js';
import { getMigrations as getMigrationsOriginal } from './get-migrations.js';
import { getMigrations as getMigrationsOriginal, type GetMigrationsFunction } from './get-migrations.js';

export async function* collectMigrations(
cwd: string,
directory: string,
history: AsyncIterable<MigrationHistoryEntry>,
getMigrations = getMigrationsOriginal,
getMigrations: GetMigrationsFunction = getMigrationsOriginal,
): AsyncIterable<MigrationMetadata | MigrationMetadataFinished> {
const allMigrations = await getMigrations(cwd, directory);
const seen = new Set<string>();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function listCommand({
storage: storageConfig,
color,
cwd,
}: Config & ExtraFlags) {
}: Config & ExtraFlags): Promise<number> {
if (!directory) {
throw MissingOptionError.fromOption('directory');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ExtraFlags = {
export default async function newCommand(
{ directory, template, reporter: reporterConfig, plugins = [], cwd, extension, color }: Config & ExtraFlags,
name: string,
) {
): Promise<void> {
if (!directory) {
throw MissingOptionError.fromOption('directory');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default async function removeCommand(
getMigrations,
}: Config & ExtraFlags,
name: string,
) {
): Promise<number> {
if (!directory) {
throw MissingOptionError.fromOption('directory');
}
Expand Down
28 changes: 14 additions & 14 deletions packages/cli/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { serializeError, errorConstructors, deserializeError } from 'serialize-e

const formatter = new Intl.ListFormat('en', { style: 'long', type: 'disjunction' });

export const toError = (error: unknown) => (error instanceof Error ? error : new Error(String(error)));
export const toError = (error: unknown): Error => (error instanceof Error ? error : new Error(String(error)));

export const toSerializedError = (error: unknown) => {
const errorInstance = toError(error);
Expand All @@ -30,7 +30,7 @@ export class EmigrateError extends Error {
export class ShowUsageError extends EmigrateError {}

export class MissingOptionError extends ShowUsageError {
static fromOption(option: string | string[]) {
static fromOption(option: string | string[]): MissingOptionError {
return new MissingOptionError(
`Missing required option: ${Array.isArray(option) ? formatter.format(option) : option}`,
undefined,
Expand All @@ -48,7 +48,7 @@ export class MissingOptionError extends ShowUsageError {
}

export class MissingArgumentsError extends ShowUsageError {
static fromArgument(argument: string) {
static fromArgument(argument: string): MissingArgumentsError {
return new MissingArgumentsError(`Missing required argument: ${argument}`, undefined, argument);
}

Expand All @@ -62,7 +62,7 @@ export class MissingArgumentsError extends ShowUsageError {
}

export class OptionNeededError extends ShowUsageError {
static fromOption(option: string, message: string) {
static fromOption(option: string, message: string): OptionNeededError {
return new OptionNeededError(message, undefined, option);
}

Expand All @@ -76,7 +76,7 @@ export class OptionNeededError extends ShowUsageError {
}

export class BadOptionError extends ShowUsageError {
static fromOption(option: string, message: string) {
static fromOption(option: string, message: string): BadOptionError {
return new BadOptionError(message, undefined, option);
}

Expand All @@ -96,7 +96,7 @@ export class UnexpectedError extends EmigrateError {
}

export class MigrationHistoryError extends EmigrateError {
static fromHistoryEntry(entry: FailedMigrationHistoryEntry) {
static fromHistoryEntry(entry: FailedMigrationHistoryEntry): MigrationHistoryError {
return new MigrationHistoryError(`Migration ${entry.name} is in a failed state, it should be fixed and removed`, {
cause: deserializeError(entry.error),
});
Expand All @@ -108,7 +108,7 @@ export class MigrationHistoryError extends EmigrateError {
}

export class MigrationLoadError extends EmigrateError {
static fromMetadata(metadata: MigrationMetadata, cause?: Error) {
static fromMetadata(metadata: MigrationMetadata, cause?: Error): MigrationLoadError {
return new MigrationLoadError(`Failed to load migration file: ${metadata.relativeFilePath}`, { cause });
}

Expand All @@ -118,7 +118,7 @@ export class MigrationLoadError extends EmigrateError {
}

export class MigrationRunError extends EmigrateError {
static fromMetadata(metadata: FailedMigrationMetadata) {
static fromMetadata(metadata: FailedMigrationMetadata): MigrationRunError {
return new MigrationRunError(`Failed to run migration: ${metadata.relativeFilePath}`, { cause: metadata.error });
}

Expand All @@ -128,7 +128,7 @@ export class MigrationRunError extends EmigrateError {
}

export class MigrationNotRunError extends EmigrateError {
static fromMetadata(metadata: MigrationMetadata, cause?: Error) {
static fromMetadata(metadata: MigrationMetadata, cause?: Error): MigrationNotRunError {
return new MigrationNotRunError(`Migration "${metadata.name}" is not in the migration history`, { cause });
}

Expand All @@ -138,7 +138,7 @@ export class MigrationNotRunError extends EmigrateError {
}

export class MigrationRemovalError extends EmigrateError {
static fromMetadata(metadata: MigrationMetadata, cause?: Error) {
static fromMetadata(metadata: MigrationMetadata, cause?: Error): MigrationRemovalError {
return new MigrationRemovalError(`Failed to remove migration: ${metadata.relativeFilePath}`, { cause });
}

Expand All @@ -148,7 +148,7 @@ export class MigrationRemovalError extends EmigrateError {
}

export class StorageInitError extends EmigrateError {
static fromError(error: Error) {
static fromError(error: Error): StorageInitError {
return new StorageInitError('Could not initialize storage', { cause: error });
}

Expand All @@ -158,11 +158,11 @@ export class StorageInitError extends EmigrateError {
}

export class CommandAbortError extends EmigrateError {
static fromSignal(signal: NodeJS.Signals) {
static fromSignal(signal: NodeJS.Signals): CommandAbortError {
return new CommandAbortError(`Command aborted due to signal: ${signal}`);
}

static fromReason(reason: string, cause?: unknown) {
static fromReason(reason: string, cause?: unknown): CommandAbortError {
return new CommandAbortError(`Command aborted: ${reason}`, { cause });
}

Expand All @@ -172,7 +172,7 @@ export class CommandAbortError extends EmigrateError {
}

export class ExecutionDesertedError extends EmigrateError {
static fromReason(reason: string, cause?: Error) {
static fromReason(reason: string, cause?: Error): ExecutionDesertedError {
return new ExecutionDesertedError(`Execution deserted: ${reason}`, { cause });
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/get-duration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process';

export const getDuration = (start: [number, number]) => {
export const getDuration = (start: [number, number]): number => {
const [seconds, nanoseconds] = process.hrtime(start);
return seconds * 1000 + nanoseconds / 1_000_000;
};
2 changes: 1 addition & 1 deletion packages/cli/src/get-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export const getMigrations = async (cwd: string, directory: string): Promise<Mig
extension: withLeadingPeriod(path.extname(name)),
directory,
cwd,
} satisfies MigrationMetadata;
};
});
};
5 changes: 4 additions & 1 deletion packages/cli/src/get-package-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ const getPackageInfo = async () => {
throw new UnexpectedError(`Could not read package info from: ${packageInfoPath}`);
};

export const { version } = await getPackageInfo();
const packageInfo = await getPackageInfo();

// eslint-disable-next-line prefer-destructuring
export const version: string = packageInfo.version;
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './types.js';

export const emigrate = () => {
export const emigrate = (): void => {
// console.log('Done!');
};
2 changes: 1 addition & 1 deletion packages/cli/src/reporters/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,6 @@ class DefaultReporter implements Required<EmigrateReporter> {
}
}

const reporterDefault = interactive ? new DefaultFancyReporter() : new DefaultReporter();
const reporterDefault: EmigrateReporter = interactive ? new DefaultFancyReporter() : new DefaultReporter();

export default reporterDefault;
5 changes: 3 additions & 2 deletions packages/cli/src/reporters/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { EmigrateReporter } from '@emigrate/types';
import { type Config } from '../types.js';
import * as reporters from './index.js';

export const getStandardReporter = (reporter?: Config['reporter']) => {
export const getStandardReporter = (reporter?: Config['reporter']): EmigrateReporter | undefined => {
if (!reporter) {
return reporters.pretty;
}
Expand All @@ -10,5 +11,5 @@ export const getStandardReporter = (reporter?: Config['reporter']) => {
return reporters[reporter as keyof typeof reporters];
}

return; // eslint-disable-line no-useless-return
return undefined;
};
2 changes: 1 addition & 1 deletion packages/cli/src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ class JsonReporter implements EmigrateReporter {
}
}

const jsonReporter = new JsonReporter() as EmigrateReporter;
const jsonReporter: EmigrateReporter = new JsonReporter();

export default jsonReporter;
8 changes: 3 additions & 5 deletions packages/cli/src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type Mocked<T> = {
[K in keyof T]: Mock<T[K]>;
};

export async function noop() {
export async function noop(): Promise<void> {
// noop
}

Expand All @@ -31,8 +31,8 @@ export function getErrorCause(error: Error | undefined): Error | SerializedError
return undefined;
}

export function getMockedStorage(historyEntries: Array<string | MigrationHistoryEntry>) {
const storage: Mocked<Storage> = {
export function getMockedStorage(historyEntries: Array<string | MigrationHistoryEntry>): Mocked<Storage> {
return {
lock: mock.fn(async (migrations) => migrations),
unlock: mock.fn(async () => {
// void
Expand All @@ -45,8 +45,6 @@ export function getMockedStorage(historyEntries: Array<string | MigrationHistory
onError: mock.fn(),
end: mock.fn(),
};

return storage;
}

export function getMockedReporter(): Mocked<Required<EmigrateReporter>> {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/with-leading-period.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const withLeadingPeriod = (string: string) => (string.startsWith('.') ? string : `.${string}`);
export const withLeadingPeriod = (string: string): string => (string.startsWith('.') ? string : `.${string}`);
7 changes: 1 addition & 6 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"extends": "@emigrate/tsconfig/build.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
"extends": "@emigrate/tsconfig/build.json"
}
47 changes: 28 additions & 19 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
} from 'mysql2/promise';
import { getTimestampPrefix, sanitizeMigrationName } from '@emigrate/plugin-tools';
import {
type Awaitable,
type MigrationMetadata,
type MigrationFunction,
type EmigrateStorage,
type LoaderPlugin,
type Storage,
Expand Down Expand Up @@ -345,17 +347,6 @@ export const createMysqlStorage = ({ table = defaultTable, connection }: MysqlSt
};
};

export const { initializeStorage } = createMysqlStorage({
table: process.env['MYSQL_TABLE'],
connection: process.env['MYSQL_URL'] ?? {
host: process.env['MYSQL_HOST'],
port: process.env['MYSQL_PORT'] ? Number.parseInt(process.env['MYSQL_PORT'], 10) : undefined,
user: process.env['MYSQL_USER'],
password: process.env['MYSQL_PASSWORD'],
database: process.env['MYSQL_DATABASE'],
},
});

export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlugin => {
return {
loadableExtensions: ['.sql'],
Expand All @@ -374,7 +365,16 @@ export const createMysqlLoader = ({ connection }: MysqlLoaderOptions): LoaderPlu
};
};

export const { loadableExtensions, loadMigration } = createMysqlLoader({
export const generateMigration: GenerateMigrationFunction = async (name) => {
return {
filename: `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.sql`,
content: `-- Migration: ${name}
`,
};
};

const storage = createMysqlStorage({
table: process.env['MYSQL_TABLE'],
connection: process.env['MYSQL_URL'] ?? {
host: process.env['MYSQL_HOST'],
port: process.env['MYSQL_PORT'] ? Number.parseInt(process.env['MYSQL_PORT'], 10) : undefined,
Expand All @@ -384,13 +384,22 @@ export const { loadableExtensions, loadMigration } = createMysqlLoader({
},
});

export const generateMigration: GenerateMigrationFunction = async (name) => {
return {
filename: `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.sql`,
content: `-- Migration: ${name}
`,
};
};
const loader = createMysqlLoader({
connection: process.env['MYSQL_URL'] ?? {
host: process.env['MYSQL_HOST'],
port: process.env['MYSQL_PORT'] ? Number.parseInt(process.env['MYSQL_PORT'], 10) : undefined,
user: process.env['MYSQL_USER'],
password: process.env['MYSQL_PASSWORD'],
database: process.env['MYSQL_DATABASE'],
},
});

// eslint-disable-next-line prefer-destructuring
export const initializeStorage: () => Promise<Storage> = storage.initializeStorage;
// eslint-disable-next-line prefer-destructuring
export const loadableExtensions: string[] = loader.loadableExtensions;
// eslint-disable-next-line prefer-destructuring
export const loadMigration: (migration: MigrationMetadata) => Awaitable<MigrationFunction> = loader.loadMigration;

const defaultExport: EmigrateStorage & LoaderPlugin & GeneratorPlugin = {
initializeStorage,
Expand Down
7 changes: 1 addition & 6 deletions packages/mysql/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"extends": "@emigrate/tsconfig/build.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
"extends": "@emigrate/tsconfig/build.json"
}
7 changes: 1 addition & 6 deletions packages/plugin-generate-js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"extends": "@emigrate/tsconfig/build.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
"extends": "@emigrate/tsconfig/build.json"
}
Loading

0 comments on commit d779286

Please sign in to comment.