Skip to content

Commit

Permalink
fix(estimator): cache config file results
Browse files Browse the repository at this point in the history
To retain them while checking out a previous commit that does not
contain the config file.
  • Loading branch information
pmelab committed Jun 6, 2024
1 parent ca7bb4d commit e5dbff3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
21 changes: 21 additions & 0 deletions packages/npm/@amazeelabs/estimator/src/effects/estimate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ test('returns the diff and velocity', async ({ effectValue, repo }) => {
secondsPerPoint: 60,
});
});

test('works if the parent commit does not contain the config', async ({
repo,
effectValue,
}) => {
await repo.write('schema.graphql', 'type Query { hello: String }');
const commit = await repo.commit('Initial commit');
// Config is added "after" the parent commit.
await repo.write('.estimatorrc.yml', 'documents: ["*.graphql"]');
await repo.write(
'schema.graphql',
'type Query { hello: String! }\ntype Foo { bar: String }',
);
await repo.commit('Second commit');
await effectValue(writeHistory);
const result = await effectValue(estimate, { PARENT_COMMIT: commit.commit });
expect(result).toEqual({
diff: 8,
secondsPerPoint: 60,
});
});
46 changes: 28 additions & 18 deletions packages/npm/@amazeelabs/estimator/src/services/configfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cosmiconfig, CosmiconfigResult } from 'cosmiconfig';
import { Context, Data, Effect } from 'effect';
import { Cache, Context, Data, Duration, Effect } from 'effect';

class ConfigFileError extends Data.TaggedError('ConfigFileError')<{
msg: string;
Expand All @@ -19,26 +19,36 @@ export class ConfigFile extends Context.Tag('ConfigFile')<
IConfigFile
>() {}

const loadConfigFile = (directory: string) =>
Effect.tryPromise({
try: async () => {
const res = await cosmiconfig('estimator').search(directory);
return (
res || {
filepath: `${directory}/.estimatorrc.yml`,
config: {},
isEmpty: true,
}
);
},
catch: (error: any) => {
return new ConfigFileError({
msg: error.message,
filepath: error.filepath,
});
},
});

export const makeConfigFile = (directory: string) =>
Effect.gen(function* () {
const cache = yield* Cache.make({
timeToLive: Duration.infinity,
lookup: loadConfigFile,
capacity: 1,
});
return {
content: Effect.tryPromise({
try: async () => {
const res = await cosmiconfig('estimator').search(directory);
return (
res || {
filepath: `${directory}/.estimatorrc.yml`,
config: {},
isEmpty: true,
}
);
},
catch: (error: any) => {
return new ConfigFileError({
msg: error.message,
filepath: error.filepath,
});
},
content: Effect.gen(function* () {
return yield* cache.get(directory);
}),
} satisfies IConfigFile;
});

0 comments on commit e5dbff3

Please sign in to comment.