Skip to content

Commit

Permalink
feat: show logs in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
theBenForce committed Mar 5, 2020
1 parent 13b9e8b commit 8f18f3d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
6 changes: 6 additions & 0 deletions packages/data-migration-cli/src/utils/createLogger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { appendFileSync } from "fs";
import * as path from "path";
import { Logger } from "data-migration";
import { Subscriber } from "rxjs";

export const logFile = path.join(process.cwd(), "migration.log");

export default function createLogger(
labels: Array<string> = [],
subscriber?: Subscriber<string>,
cliOut?: (message: string) => void
): Logger {
return (message: string) => {
Expand All @@ -22,5 +24,9 @@ export default function createLogger(
if (cliOut) {
cliOut(message);
}

if (subscriber) {
subscriber.next(message);
}
};
}
77 changes: 43 additions & 34 deletions packages/data-migration/src/methods/GetScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import MigrationScript, { ScriptContext, InitializedMigrationScript } from "../M
import { getAllScripts } from "../Utils";
import { Logger } from "../Logger";
import { ExecutionTrackerInstance, ExecutionInformation } from "../ExecutionTracker";
import { Observable, Subscriber } from "rxjs";

export default async function getScripts(
config: Configuration,
context: ScriptContext,
log: Logger,
createLogger: (scriptName: string) => Logger,
createLogger: (scriptName: string, subscriber?: Subscriber<string>) => Logger,
tracker?: ExecutionTrackerInstance
): Promise<Array<InitializedMigrationScript>> {
let result = new Array<InitializedMigrationScript>();
Expand All @@ -28,48 +29,56 @@ export default async function getScripts(
hasRun = Boolean(executionInformation);
}

const scriptLogger = createLogger(fname);

result.push({
name: fname,
executionInformation,
hasRun,
async up() {
let result;
const start = new Date();
try {
result = await script.up(context, scriptLogger);
if (tracker) {
await tracker.markDone(fname, start, context.getDriversUsed());
up: () =>
// @ts-ignore
new Observable(async (subscriber: Subscriber<string>) => {
let result;
const start = new Date();
const scriptLogger = createLogger(fname, subscriber);
try {
result = await script.up(context, scriptLogger);
if (tracker) {
scriptLogger(`Marking as done`);
await tracker.markDone(fname, start, context.getDriversUsed());
}
} catch (ex) {
scriptLogger(
`Something went wrong while executing ${fname} up script: ${JSON.stringify(ex)}`
);
throw ex;
}
} catch (ex) {
scriptLogger(
`Something went wrong while executing ${fname} up script: ${JSON.stringify(ex)}`
);
throw ex;
}

return result;
},
async down() {
let result;
try {
if (script.down) {
result = await script.down(context, scriptLogger);
}
subscriber.complete();
return result;
}),
down: () =>
// @ts-ignore
new Observable(async (subscriber: Subscriber<string>) => {
let result;
const scriptLogger = createLogger(fname, subscriber);
try {
if (script.down) {
result = await script.down(context, scriptLogger);
}

if (tracker) {
await tracker.remove(fname);
if (tracker) {
scriptLogger(`Removing execution record`);
await tracker.remove(fname);
}
} catch (ex) {
scriptLogger(
`Something went wrong while executing ${fname} down script: ${JSON.stringify(ex)}`
);
throw ex;
}
} catch (ex) {
scriptLogger(
`Something went wrong while executing ${fname} down script: ${JSON.stringify(ex)}`
);
throw ex;
}

return result;
},
subscriber.complete();
return result;
}),
});
}

Expand Down

0 comments on commit 8f18f3d

Please sign in to comment.