Skip to content

Commit

Permalink
Merge pull request #180 from divriots/chore/no-global-state
Browse files Browse the repository at this point in the history
chore: remove global state/conf
  • Loading branch information
muryoh authored Jul 23, 2024
2 parents 116ec40 + 11a8c23 commit d7aa403
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 226 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@divriots/jampack",
"version": "0.24.4",
"version": "0.25.0",
"cache-version": {
"img": "v1",
"img-ext": "v1"
Expand All @@ -26,6 +26,7 @@
"./state": "./dist/state.js"
},
"dependencies": {
"@commander-js/extra-typings": "^12.0.0",
"@divriots/cheerio": "1.0.0-rc.12",
"@proload/core": "^0.3.3",
"@swc/core": "^1.4.16",
Expand All @@ -40,7 +41,7 @@
"hasha": "^6.0.0",
"html-minifier-terser": "^7.2.0",
"kleur": "^4.1.5",
"lightningcss": "^1.24.1",
"lightningcss": "^1.25.1",
"mini-svg-data-uri": "^1.4.4",
"ora": "^8.0.1",
"quicklink": "^2.3.0",
Expand Down
73 changes: 42 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 19 additions & 13 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hashSync as hasha } from 'hasha';
import path from 'path';
import * as fs from 'fs/promises';
import $state from './state.js';
import { GlobalState } from './state.js';
import { CACHE_VERSIONS } from './packagejson.js';

const listOfCategories = ['img', 'img-ext'] as const;
Expand All @@ -10,12 +10,12 @@ export type Category = (typeof listOfCategories)[number];

export type CacheData = { buffer: Buffer; meta: any };

function getCacheFolder(): string {
return $state.args.cache_folder || '.jampack/cache';
function getCacheFolder(state: GlobalState): string {
return state.args.cache_folder || '.jampack/cache';
}

async function cleanCache(full: boolean) {
const CACHE_FOLDER = getCacheFolder();
async function cleanCache(state: GlobalState, full?: boolean) {
const CACHE_FOLDER = getCacheFolder(state);

if (full) {
try {
Expand Down Expand Up @@ -59,8 +59,8 @@ async function cleanCache(full: boolean) {
}
}

function computeCacheHash(buffer: Buffer, options?: any) {
if ($state.args.nocache) {
function computeCacheHash(state: GlobalState, buffer: Buffer, options?: any) {
if (state.args.nocache) {
return '';
}

Expand All @@ -75,8 +75,12 @@ function getVersionOfCategory(category: Category): string {
return CACHE_VERSIONS[category];
}

function getLocation(hash: string, category: Category): string {
const CACHE_FOLDER = getCacheFolder();
function getLocation(
state: GlobalState,
hash: string,
category: Category
): string {
const CACHE_FOLDER = getCacheFolder(state);

return path.join(
CACHE_FOLDER,
Expand All @@ -87,14 +91,15 @@ function getLocation(hash: string, category: Category): string {
}

async function getFromCache(
state: GlobalState,
category: Category,
hash: string
): Promise<CacheData | undefined> {
if ($state.args.nocache) {
if (state.args.nocache) {
return undefined;
}

const location = getLocation(hash, category);
const location = getLocation(state, hash, category);

try {
const buffer = await fs.readFile(path.join(location, 'data'));
Expand All @@ -111,15 +116,16 @@ async function getFromCache(
}

async function addToCache(
state: GlobalState,
category: Category,
hash: string,
data: CacheData
): Promise<void> {
if ($state.args.nocache) {
if (state.args.nocache) {
return;
}

const location = getLocation(hash, category);
const location = getLocation(state, hash, category);
await fs.mkdir(location, { recursive: true });
await fs.writeFile(path.join(location, 'data'), data.buffer);
await fs.writeFile(path.join(location, 'meta'), JSON.stringify(data.meta));
Expand Down
Loading

0 comments on commit d7aa403

Please sign in to comment.