Skip to content

Commit

Permalink
added esm and ts config
Browse files Browse the repository at this point in the history
  • Loading branch information
flodlc committed Mar 16, 2023
1 parent 3fd8b44 commit a48a4bb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@flodlc/pg-mate",
"version": "1.0.6",
"repository": "https://github.com/flodlc/pg-migration.git",
"repository": "https://github.com/flodlc/pg-mate.git",
"author": "flodlc <[email protected]>",
"license": "MIT",
"main": "./dist/index.js",
Expand Down
7 changes: 5 additions & 2 deletions playground/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as migration_1677869240309 from "./migration_1677869240309.js";

import * as migration_1677869240309 from "./migration_1677869240309";


export const migrations = {
migration_1677869240309,
migration_1677869240309
};

2 changes: 0 additions & 2 deletions playground/migrations/migration_1677869240309.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Client } from "pg";

export const up = async () => {};

export const down = async () => {};
4 changes: 2 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "playground",
"type": "module",
"type": "commonjs",
"private": true,
"scripts": {
"build": "tsup src/index.ts --dts",
"dev": "tsup src/index.ts --watch .",
"start": "node dist/pg-mate.js",
"pg-migrate-esm": "NODE_ENV=prod ts-node --esm pg-mate.ts",
"pg-migrate-esm": "NODE_ENV=prod ts-node -T --esm pg-mate.ts",
"pg-migrate": "ts-node --compilerOptions '{\"module\": \"CommonJS\"}' pg-mate.ts"
},
"peerDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions playground/pg-mate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { pgMate, PgMateConfig } from "@flodlc/pg-mate";
import { migrations } from "./migrations/index.js";
import { pgMate, PgMateConfig } from "../dist/index";
import { migrations } from "./migrations/index";

export const config: PgMateConfig = {
connexionUrl: "postgresql://postgres:password@localhost:5432/postgres",
migrationImports: migrations,
migrationDir: "migrations",
esm: false,
ts: true,
};

pgMate.initCli(config);
22 changes: 16 additions & 6 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,27 @@ export const rollback = async ({
}
};

const create = async ({ migrationDir }: CommandArgs) => {
const create = async ({ migrationDir, esm, ts }: CommandArgs) => {
const name = `migration_${Date.now()}`;
console.log(`create migration ${name} in ${migrationDir}`);
await fs.mkdir(migrationDir, { recursive: true });
await fs.writeFile(`${migrationDir}/${name}.ts`, template);
await refreshIndex({ migrationDir });
await fs.writeFile(`${migrationDir}/${name}.${ts ? "ts" : "js"}`, template);
await refreshIndex({ migrationDir, esm, ts });
};

const refreshIndex = async ({ migrationDir }: { migrationDir: string }) => {
const refreshIndex = async ({
migrationDir,
esm,
ts,
}: {
migrationDir: string;
esm: boolean;
ts: boolean;
}) => {
const sortedFiles = await getSortedMigrationFiles({ migrationDir });
const importContent = sortedFiles.reduce(
(acc, name) => `${acc}
import * as ${name} from "./${name}";
import * as ${name} from "./${name}${esm ? ".js" : ""}";
`,
""
);
Expand All @@ -102,7 +110,7 @@ export const migrations = {
${exportList.slice(2)}
};
`;
await fs.writeFile(`${migrationDir}/index.ts`, content);
await fs.writeFile(`${migrationDir}/index.${ts ? "ts" : "js"}`, content);
};

export const initCli = async (config: PgMateConfig) => {
Expand Down Expand Up @@ -136,6 +144,8 @@ export const init = async (config: PgMateConfig) => {
client: (await config.getClient?.()) ?? internalClient,
migrationDir: config.migrationDir ?? "migrations",
migrationImports: config.migrationImports,
esm: config.esm ?? false,
ts: config.ts ?? false,
};

return {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ export type PgMateConfig = {
getClient?: () => Promise<any>;
migrationDir?: string;
migrationImports: MigrationFiles;
esm?: boolean;
ts?: boolean;
};

export type CommandArgs = {
client: Client;
internalClient: Client;
migrationDir: string;
migrationImports: MigrationFiles;
esm: boolean;
ts: boolean;
};

0 comments on commit a48a4bb

Please sign in to comment.