@divriots/jampack 0.12.2 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cache.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- export type Category = 'img' | 'img-ext';
2
+ declare const listOfCategories: readonly ["img", "img-ext"];
3
+ export type Category = (typeof listOfCategories)[number];
3
4
  export type CacheData = {
4
5
  buffer: Buffer;
5
6
  meta: any;
package/dist/cache.js CHANGED
@@ -1,10 +1,13 @@
1
1
  import hasha from 'hasha';
2
2
  import path from 'path';
3
3
  import * as fs from 'fs/promises';
4
- import { VERSION } from './version.js';
5
4
  import $state from './state.js';
5
+ const listOfCategories = ['img', 'img-ext'];
6
6
  const CACHE_FOLDER = '.jampack/cache';
7
- const CACHE_FOLDER_VERSION = CACHE_FOLDER + '/' + VERSION;
7
+ const CACHE_VERSIONS = {
8
+ img: 'v1',
9
+ 'img-ext': 'v1',
10
+ };
8
11
  async function cleanCache(full) {
9
12
  if (full) {
10
13
  try {
@@ -15,19 +18,36 @@ async function cleanCache(full) {
15
18
  }
16
19
  return;
17
20
  }
18
- // List versions in cache
19
- let files;
21
+ // Delete old cache category
22
+ let catFolders = [];
20
23
  try {
21
- files = await fs.readdir(CACHE_FOLDER);
24
+ catFolders = await fs.readdir(CACHE_FOLDER);
22
25
  }
23
26
  catch (e) {
24
- // Nothing to do probably no cache available
25
- return;
27
+ // No problem
28
+ }
29
+ for (const f of catFolders) {
30
+ // @ts-ignore
31
+ if (!listOfCategories.includes(f))
32
+ fs.rm(path.join(CACHE_FOLDER, f), { recursive: true });
33
+ }
34
+ // Loop cache folders
35
+ for (const cat of listOfCategories) {
36
+ const location = path.join(CACHE_FOLDER, cat);
37
+ // List versions in cache
38
+ let folders;
39
+ try {
40
+ folders = await fs.readdir(location);
41
+ }
42
+ catch (e) {
43
+ continue;
44
+ }
45
+ // Delete old cache versions
46
+ for (const f of folders) {
47
+ if (f !== CACHE_VERSIONS[cat])
48
+ fs.rm(path.join(location, f), { recursive: true });
49
+ }
26
50
  }
27
- // Delete old cache versions
28
- await files
29
- .filter((f) => f !== VERSION)
30
- .forEach(async (f) => fs.rm(path.join(CACHE_FOLDER, f), { recursive: true }));
31
51
  }
32
52
  function computeCacheHash(buffer, options) {
33
53
  if ($state.args.nocache) {
@@ -39,8 +59,11 @@ function computeCacheHash(buffer, options) {
39
59
  }
40
60
  return hash;
41
61
  }
62
+ function getVersionOfCategory(category) {
63
+ return CACHE_VERSIONS[category];
64
+ }
42
65
  function getLocation(hash, category) {
43
- return path.join(CACHE_FOLDER_VERSION, category, hash);
66
+ return path.join(CACHE_FOLDER, category, getVersionOfCategory(category), hash);
44
67
  }
45
68
  async function getFromCache(category, hash) {
46
69
  if ($state.args.nocache) {
package/dist/index.js CHANGED
@@ -53,7 +53,7 @@ program
53
53
  fast();
54
54
  }
55
55
  // Clean cache
56
- cleanCache(options.cleancache);
56
+ await cleanCache(options.cleancache);
57
57
  if (!options.onlycomp) {
58
58
  printTitle('PASS 1 - Optimizing');
59
59
  console.time('Done');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@divriots/jampack",
3
- "version": "0.12.2",
3
+ "version": "0.13.0",
4
4
  "description": "Packer for static websites",
5
5
  "author": "Georges Gomes @georges-gomes",
6
6
  "bin": {