Skip to content

Commit

Permalink
Merge pull request #37 from rxdi/feature/36-remove-gapi-build-replace…
Browse files Browse the repository at this point in the history
…-with-esbuild

feat(esbuild): added esbuild transpiler and make it default
  • Loading branch information
Stradivario authored Apr 22, 2024
2 parents 890f3d0 + 87cc2ab commit eb81fc5
Show file tree
Hide file tree
Showing 16 changed files with 1,260 additions and 1,319 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v14.21.2
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"typescript.tsdk": "node_modules/typescript/lib",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"files.exclude": {
"**/.classpath": true,
Expand Down
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Migration library for `Mongodb` and `Mongoose` written in `TypeScript`
- ACID transactions provided by MongoDB
- `error` and `success` logs for `up`/`down` migrations
- Infinite rrror log with `append` NodeJS streaming technique
- 100% TypeScript support with JIT compilation provided by [@gapi/cli](https://github.com/Stradivario/gapi-cli) and [ParcelJS](https://parceljs.org)
- 100% TypeScript support with JIT compilation provided by [esbuild](https://esbuild.github.io/)

## Installation

Expand Down Expand Up @@ -61,6 +61,7 @@ export default async () => {
migrationsDir: './migrations',
defaultTemplate: 'es6',
typescript: true,
builder: 'ESBUILD',
outDir: './.xmigrate',
/* Custom datetime formatting can be applied like so */
// dateTimeFormat: () => new Date().toISOString(),
Expand Down Expand Up @@ -255,7 +256,7 @@ export async function down(client: MongoClient) {
}
```

## TypeScript migrations
## TypeScript migrations (Deprecated use option `builder: 'ESBUILD' in your xmigrate config file`)

To be able to run migrations with TypeScript you need to set `typescript: true` inside `xmigrate.js` and install `@gapi/cli` globally

Expand Down Expand Up @@ -367,12 +368,6 @@ Up migration error log

When you change your configuration file to `xmigrate.ts` it will automatically transpile to `ES5` and will be loaded

This type of configuration requires `@gapi/cli` to be installed since we will transpile configuration with it!

```bash
npm i -g @gapi/cli
```

```typescript
import { Config } from '@rxdi/xmigrate';

Expand Down
168 changes: 168 additions & 0 deletions package-lock.json

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

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

export const DEFAULT_CONFIG: Config = {
changelogCollectionName: 'migrations',
migrationsDir: 'migrations',
defaultTemplate: 'es6',
typescript: true,
outDir: './.xmigrate',
builder: BuilderType.ESBUILD,
// dateTimeFormat: () => '1212',
logger: {
folder: './migrations-log',
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/log-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Logger {
});
}
log(res: unknown) {
return new Promise((resolve) => {
return new Promise<void | Error>((resolve) => {
if (!this.successFinished) {
return this.successLogger.write(
this.getLogTemplate(res, '🚀'),
Expand All @@ -36,7 +36,7 @@ export class Logger {
});
}
error(res: unknown) {
return new Promise((resolve) => {
return new Promise<void | Error>((resolve) => {
if (!this.errorFinished) {
return this.errorLogger.write(this.getLogTemplate(res, '🔥'), resolve);
}
Expand Down
32 changes: 29 additions & 3 deletions src/helpers/typescript-builder.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
import { spawn } from 'child_process';
import { exit } from 'process';

export const TranspileTypescript = (paths: string[], outDir: string) => {
/* 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',
`${paths.toString()}`,
`${entryPoints.toString()}`,
'--outDir',
outDir,
outdir,
]);
// child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on('close', (code: number) => resolve(code));
});
};

export const TranspileTypescriptESBuild = async (
entryPoints: string[],
outdir: string,
) => {
try {
return (await import('esbuild')).build({
entryPoints,
bundle: true,
sourcemap: true,
minify: false,
platform: 'node',
format: 'cjs',
outdir,
logLevel: 'info',
});
} catch (e) {
console.error(e);
exit(1);
}
};
6 changes: 6 additions & 0 deletions src/injection.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface LoggerConfig {
};
}

export enum BuilderType {
ESBUILD = 'ESBUILD',
GAPI = 'GAPI',
}

export interface Config {
mongodb: {
url: string;
Expand All @@ -44,6 +49,7 @@ export interface Config {
logger?: LoggerConfig;
defaultTemplate?: TemplateTypes;
typescript?: boolean;
builder?: BuilderType;
}

export type Tasks =
Expand Down
22 changes: 17 additions & 5 deletions src/migrations.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ 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 @@ -82,10 +86,18 @@ export class MigrationsModule {
'./.xmigrate/config.temp',
);
const TranspileAndWriteTemp = async (stats: Stats) => {
await TranspileTypescript(
[`/${configFilename}.ts`],
config.outDir,
);
if (config.builder === BuilderType.GAPI) {
await TranspileTypescript(
[`/${configFilename}.ts`],
config.outDir,
);
} else {
await TranspileTypescriptESBuild(
[`./${configFilename}.ts`],
config.outDir,
);
}

console.log('Transpile complete!');
await promisify(writeFile)(
'./.xmigrate/config.temp',
Expand Down
Loading

0 comments on commit eb81fc5

Please sign in to comment.