~sjm/l8r

588f934208d0e67d699d1c258260c205a01a8b11 — Sam Marshall 7 months ago 00dffc9
start to convert from TE to Effect
2 files changed, 23 insertions(+), 14 deletions(-)

A l8r.log
M src/index.ts
A l8r.log => l8r.log +1 -0
@@ 0,0 1,1 @@
{}

M src/index.ts => src/index.ts +22 -14
@@ 18,11 18,24 @@ const command = Args.Args.pipe(Effect.flatMap((x) => x.command));

const LOG_LOCATION = "l8r.log";

const effectFromTaskEither = <E, A>(
  te: TE.TaskEither<E, A>,
): Effect.Effect<never, E, A> =>
  Effect.promise(() =>
    te().then(
      E.fold(
        (err) => Effect.fail(err) as Effect.Effect<never, E, A>,
        (a) => Effect.succeed(a) as Effect.Effect<never, E, A>
      ),
    )
  ).pipe(Effect.flatten);

const queue = pipe(
  TE.Do,
  TE.bind("log", () => readLog),
  TE.bind("path", () => getPath),
  TE.chain(({ log, path }) => writeLog(L.add(path)(log))),
  effectFromTaskEither
);

const review = pipe(


@@ 44,6 57,7 @@ const review = pipe(
      ),
    );
  }),
  effectFromTaskEither
);

const due = pipe(


@@ 57,6 71,7 @@ const due = pipe(
      (x) => console.log(x),
    );
  }),
  effectFromTaskEither
);

/** Modify an item within the Log */


@@ 67,6 82,7 @@ const modifyItem = (f: (i: I.Item) => I.Item) =>
    TE.bind("path", () => getPath),
    TE.map(({ log, path }) => L.modifyItem(path, f)(log)),
    TE.chain((log) => writeLog(log)),
    effectFromTaskEither,
  );

/**


@@ 82,6 98,7 @@ const remove = pipe(
  TE.bind("log", () => readLog),
  TE.bind("path", () => getPath),
  TE.chain(({ log, path }) => writeLog(L.remove(path)(log))),
  effectFromTaskEither
);

const stats = pipe(


@@ 97,11 114,12 @@ const stats = pipe(
      (stats) => console.log(stats),
    ),
  ),
  effectFromTaskEither
);

command.pipe(
  Effect.tap((command) => {
    pipe(
  Effect.flatMap((command) => {
    return pipe(
      match(command)
        .with("queue", () => queue)
        .with("review", () => review)


@@ 113,18 131,8 @@ command.pipe(
        .with("due", () => due)
        .with("stats", () => stats)
        .exhaustive(),
      TE.fold(
        (err) => {
          console.error(err);
          process.exit(1);
        },
        () => {
          process.exit(0);
        },
      ),
    )();
    );

    return Effect.unit;
  }),
  Effect.catchTags({
    CommandNotPresentError: (_) =>


@@ 137,7 145,7 @@ command.pipe(
      ),
  }),
  Effect.provide(Args.ArgsLive),
  Effect.runSync,
  Effect.runPromise,
);

const readRawLogFile: TE.TaskEither<Error, string> = TE.tryCatch(