Skip to content

Commit

Permalink
feat(prepare)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stradivario committed Apr 22, 2024
1 parent cf3b868 commit 6a111af
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 98 deletions.
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@rxdi/core": "^0.7.37",
"chalk": "2.4.2",
"esbuild": "^0.20.2",
"esbuild-plugin-tsc": "^0.4.0",
"esm": "3.2.25",
"mongodb": "3.3.3",
"mongoose": "5.7.6"
Expand Down
6 changes: 4 additions & 2 deletions src/default.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { BuilderType, Config } from './injection.tokens';
import { Config } from './injection.tokens';

export const DEFAULT_CONFIG: Config = {
changelogCollectionName: 'migrations',
migrationsDir: 'migrations',
defaultTemplate: 'es6',
typescript: true,
outDir: './.xmigrate',
builder: BuilderType.ESBUILD,
// bundler: {
// build: () => Promise.resolve(),
// },
// dateTimeFormat: () => '1212',
logger: {
folder: './migrations-log',
Expand Down
31 changes: 4 additions & 27 deletions src/helpers/typescript-builder.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
import { spawn } from 'child_process';
import { exit } from 'process';

/* Deprecated */
export const TranspileTypescript = (entryPoints: string[], outdir: string) => {
console.warn(
'***Deprecated Warning*** using `gapi` as a build for migrations is deprecated consider using builder type esbuild',
);
return new Promise((resolve) => {
const child = spawn('npx', [
'gapi',
'build',
'--glob',
`${entryPoints.toString()}`,
'--outDir',
outdir,
]);
// child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on('close', (code: number) => resolve(code));
});
};

export const TranspileTypescriptESBuild = async (
export const TranspileTypescript = async (
entryPoints: string[],
outdir: string,
) => {
try {
const tscPlugin = await import('esbuild-plugin-tsc');
return (await import('esbuild')).build({
return await (await import('esbuild')).build({
entryPoints,
bundle: true,
sourcemap: true,
bundle: false,
sourcemap: false,
minify: false,
platform: 'node',
format: 'cjs',
outdir,
logLevel: 'info',
plugins: [tscPlugin.default()],
});
} catch (e) {
console.error(e);
Expand Down
7 changes: 3 additions & 4 deletions src/injection.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ export interface LoggerConfig {
};
}

export enum BuilderType {
ESBUILD = 'ESBUILD',
GAPI = 'GAPI',
export interface BundlerConfig {
build(entryPoints: string[], outdir: string): Promise<void>;
}

export interface Config {
Expand All @@ -50,7 +49,7 @@ export interface Config {
logger?: LoggerConfig;
defaultTemplate?: TemplateTypes;
typescript?: boolean;
builder?: BuilderType;
bundler?: BundlerConfig;
}

export type Tasks =
Expand Down
21 changes: 5 additions & 16 deletions src/migrations.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import { DEFAULT_CONFIG } from './default.config';
import { ensureDir } from './helpers';
import { includes, nextOrDefault } from './helpers/args-extractors';
import { LogFactory } from './helpers/log-factory';
import { TranspileTypescript } from './helpers/typescript-builder';
import {
TranspileTypescript,
TranspileTypescriptESBuild,
} from './helpers/typescript-builder';
import {
BuilderType,
CommandInjector,
Config,
LoggerConfig,
Expand Down Expand Up @@ -86,17 +82,10 @@ export class MigrationsModule {
'./.xmigrate/config.temp',
);
const TranspileAndWriteTemp = async (stats: Stats) => {
if (config.builder === BuilderType.GAPI) {
await TranspileTypescript(
[`/${configFilename}.ts`],
config.outDir,
);
} else {
await TranspileTypescriptESBuild(
[`./${configFilename}.ts`],
config.outDir,
);
}
await TranspileTypescript(
[`./${configFilename}.ts`],
config.outDir,
);

console.log('Transpile complete!');
await promisify(writeFile)(
Expand Down
13 changes: 5 additions & 8 deletions src/services/migration/migration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { promisify } from 'util';
import { nowAsString } from '../../helpers/date';
import { ErrorMap } from '../../helpers/error';
import { LogFactory } from '../../helpers/log-factory';
import { BuilderType, ReturnType } from '../../injection.tokens';
import { ReturnType } from '../../injection.tokens';
import { TemplateTypes } from '../../templates/index';
import * as templates from '../../templates/index';
import { ConfigService } from '../config/config.service';
Expand Down Expand Up @@ -42,9 +42,7 @@ export class MigrationService {
.filter((item) => this.migrationsResolver.isTypescript(item.fileName))
.map((m) => m.fileName);
if (typescriptMigrations.length) {
await this.migrationsResolver.transpileMigrations(typescriptMigrations, {
builder: BuilderType[this.configService.config.builder],
});
await this.migrationsResolver.transpileMigrations(typescriptMigrations);
}
const migrateItem = async (item: ReturnType) => {
let result: unknown;
Expand Down Expand Up @@ -118,10 +116,9 @@ export class MigrationService {
const client = await this.connect();

if (isTypescript) {
await this.migrationsResolver.transpileMigrations(
[lastAppliedItem.fileName],
{ builder: BuilderType.ESBUILD },
);
await this.migrationsResolver.transpileMigrations([
lastAppliedItem.fileName,
]);
}
try {
const migration = await this.migrationsResolver.loadMigration(
Expand Down
28 changes: 12 additions & 16 deletions src/services/migrations-resolver/migrations-resolver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import { readdir, unlink } from 'fs';
import { extname, join } from 'path';
import { promisify } from 'util';

import {
TranspileTypescript,
TranspileTypescriptESBuild,
} from '../../helpers/typescript-builder';
import { BuilderType, MigrationSchema } from '../../injection.tokens';
import { TranspileTypescript } from '../../helpers/typescript-builder';
import { MigrationSchema } from '../../injection.tokens';
import { ConfigService } from '../config/config.service';

@Injectable()
Expand Down Expand Up @@ -42,8 +39,10 @@ export class MigrationsResolver {
// eslint-disable-next-line @typescript-eslint/no-var-requires
migration = require('esm')(module)(this.getFilePath(fileName));
}
migration.prepare = migration.prepare || (() => Promise.resolve());
return migration;
return {
...migration,
prepare: migration.prepare || (() => Promise.resolve()),
};
}

getFilePath(fileName: string) {
Expand Down Expand Up @@ -75,20 +74,17 @@ export class MigrationsResolver {
}

async loadTsCompiledMigration(fileName: string) {
return import(this.getTsCompiledFilePath(fileName));
return require(this.getTsCompiledFilePath(fileName));
}

async transpileMigrations(
migrations: string[],
{ builder = BuilderType.ESBUILD }: { builder: BuilderType },
) {
if (builder === BuilderType.GAPI) {
await TranspileTypescript(
migrations.map((fileName) => this.getRelativePath(fileName)),
async transpileMigrations(migrations: string[]) {
if (this.configService.config.bundler) {
await this.configService.config.bundler.build(
migrations.map((fileName) => this.getFilePath(fileName)),
this.configService.config.outDir,
);
} else {
await TranspileTypescriptESBuild(
await TranspileTypescript(
migrations.map((fileName) => this.getFilePath(fileName)),
this.configService.config.outDir,
);
Expand Down
14 changes: 4 additions & 10 deletions tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { promisify } from 'util';

import { DEFAULT_CONFIG } from '../src/default.config';
import { ensureDir, LogFactory } from '../src/helpers';
import { BuilderType, Config, LoggerConfig } from '../src/injection.tokens';
import { Config, LoggerConfig } from '../src/injection.tokens';
import { ConfigService } from '../src/services/config/config.service';
import { DatabaseService } from '../src/services/database/database.service';
import { GenericRunner } from '../src/services/generic-runner/generic-runner.service';
Expand Down Expand Up @@ -86,9 +86,7 @@ describe('Global Xmigrate Tests', () => {
);
const fileNames = await migrationResolver.getFileNames();
expect(fileNames.length).toBe(1);
await migrationResolver.transpileMigrations(fileNames, {
builder: BuilderType.GAPI,
});
await migrationResolver.transpileMigrations(fileNames);
const migration = await migrationResolver.loadMigration(fileNames[0]);
const spy = spyOn(databaseService, 'connect').and.callFake(() =>
FakeMongoClient(response),
Expand Down Expand Up @@ -383,9 +381,7 @@ describe('Global Xmigrate Tests', () => {
);
const fileNames = await migrationResolver.getFileNames();
expect(fileNames.length).toBe(1);
await migrationResolver.transpileMigrations(fileNames, {
builder: BuilderType.GAPI,
});
await migrationResolver.transpileMigrations(fileNames);
const migration = await migrationResolver.loadMigration(fileNames[0]);
const spy = spyOn(databaseService, 'connect').and.callFake(() =>
FakeMongoClient(true),
Expand Down Expand Up @@ -414,9 +410,7 @@ describe('Global Xmigrate Tests', () => {
);
const fileNames = await migrationResolver.getFileNames();
expect(fileNames.length).toBe(1);
await migrationResolver.transpileMigrations(fileNames, {
builder: BuilderType.GAPI,
});
await migrationResolver.transpileMigrations(fileNames);
const migration = await migrationResolver.loadMigration(fileNames[0]);
const spy = spyOn(databaseService, 'connect').and.callFake(() =>
FakeMongoClient(true),
Expand Down
20 changes: 19 additions & 1 deletion xmigrate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import esbuild from 'esbuild';
import pluginTsc from 'esbuild-plugin-tsc';

export default async () => {
return {
defaultTemplate: 'typescript',
outDir: './.xmigrate',
typescript: true,
builder: 'esbuild',
builder: 'custom',
bundler: {
build(entryPoints: string[], outdir: string) {
return esbuild.build({
entryPoints,
bundle: true,
sourcemap: true,
minify: false,
platform: 'node',
format: 'cjs',
outdir,
logLevel: 'info',
plugins: [pluginTsc()],
});
},
},
// dateTimeFormat: () => new Date().toISOString(),
mongodb: {
url: 'mongodb://localhost:27017',
Expand Down

0 comments on commit 6a111af

Please sign in to comment.