Skip to content

Commit

Permalink
test: basic caching function
Browse files Browse the repository at this point in the history
  • Loading branch information
yanthomasdev committed Sep 14, 2024
1 parent 70e39d0 commit fd58a7d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { loadConfig, validateConfig } from './config/config.js';
import type { LunariaConfig, LunariaUserConfig, Pattern } from './config/types.js';
import { FileConfigNotFound } from './errors/errors.js';
import { createPathResolver } from './files/paths.js';
import { useCache } from './status/cache.js';
import { LunariaGitInstance } from './status/git.js';
import { getDictionaryCompletion, isFileLocalizable } from './status/status.js';
import type { LunariaStatus, StatusLocalizationEntry } from './status/types.js';
Expand Down Expand Up @@ -130,6 +131,9 @@ export class Lunaria {
);
}

// Save the existing git data into the cache for next builds.
useCache(this.#config.cacheDir, 'git').write(this.#git.cache);

return status;
}

Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/status/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { jsonLoader } from '../files/loaders.js';

export function useCache(cacheDir: string, entry: string) {
const resolvedCacheDir = resolve(cacheDir);
const file = `${entry}.json`;

if (!existsSync(resolvedCacheDir)) {
mkdirSync(resolvedCacheDir, { recursive: true });
}

const cachePath = join(resolvedCacheDir, file);

return {
contents: existsSync(cachePath) ? jsonLoader(cachePath) : undefined,
write(contents: unknown) {
// TODO: Test with writeFile instead.
return writeFileSync(cachePath, JSON.stringify(contents));
},
};
}
36 changes: 17 additions & 19 deletions packages/core/src/status/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,52 @@ import type { ConsolaInstance } from 'consola';
import picomatch from 'picomatch';
import { type DefaultLogFields, type ListLogLine, simpleGit } from 'simple-git';
import type { LunariaConfig } from '../config/types.js';
import { FileCommitsNotFound, UncommittedFileFound } from '../errors/errors.js';
import { UncommittedFileFound } from '../errors/errors.js';
import type { RegExpGroups } from '../utils/types.js';
import { useCache } from './cache.js';

export class LunariaGitInstance {
#git = simpleGit({
maxConcurrentProcesses: Math.max(2, Math.min(32, cpus().length)),
});
#config: LunariaConfig;
#logger: ConsolaInstance;
cache: Record<string, string>;

constructor(config: LunariaConfig, logger: ConsolaInstance) {
this.#logger = logger;
this.#config = config;
}

// TODO: Try to cache the latest changes hash for each file so that you don't have to fetch the entire history every run, only new ones.
async #getFileHistory(path: string) {
try {
const log = await this.#git.log({
file: resolve(path),
strictDate: true,
});

return log;
} catch (e) {
this.#logger.error(FileCommitsNotFound.message(path));
throw e;
}
this.cache = useCache(this.#config.cacheDir, 'git').contents ?? {};
}

async getFileLatestChanges(path: string) {
const logHistory = await this.#getFileHistory(path);
// The cache will keep the latest tracked change hash, that means it will be able
// to completely skip older commits.
const log = await this.#git.log({
file: resolve(path),
strictDate: true,
from: this.cache[path] ? `${this.cache[path]}^1` : undefined,
});

console.log(path, log.latest, log.all.length);

const latestChange = logHistory.latest;
const latestChange = log.latest;
/**
* Edge case: it might be possible all the changes for a file have
* been purposefully ignored in Lunaria, therefore we need to define
* the latest change as the latest tracked change.
* TODO: Check if this is not an stupid assumption.
*/
const latestTrackedChange =
findLatestTrackedCommit(this.#config.tracking, path, logHistory.all) ?? latestChange;
findLatestTrackedCommit(this.#config.tracking, path, log.all) ?? latestChange;

if (!latestChange || !latestTrackedChange) {
this.#logger.error(UncommittedFileFound.message(path));
process.exit(1);
}

this.cache[path] = latestTrackedChange.hash;

return {
latestChange: {
date: latestChange.date,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/status/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type Dictionary = {
[k: string]: string | Dictionary;
};

type FileGitData = {
export type FileGitData = {
latestChange: {
message: string;
date: string;
Expand Down

0 comments on commit fd58a7d

Please sign in to comment.