|  | 
|  | 1 | +const { readdir: _readdir, stat: _stat } = require('fs'); | 
|  | 2 | +const { basename, join } = require('path'); | 
|  | 3 | + | 
|  | 4 | +const _rimraf = require('rimraf'); | 
|  | 5 | + | 
|  | 6 | +const logMessages = require('./util/log-messages'); | 
|  | 7 | +const pluginCompat = require('./util/plugin-compat'); | 
|  | 8 | +const promisify = require('./util/promisify'); | 
|  | 9 | + | 
|  | 10 | +const readdir = promisify(_readdir); | 
|  | 11 | +const rimraf = promisify(_rimraf); | 
|  | 12 | +const stat = promisify(_stat); | 
|  | 13 | + | 
|  | 14 | +const directorySize = async dir => { | 
|  | 15 | +  const _stat = await stat(dir); | 
|  | 16 | +  if (_stat.isFile()) { | 
|  | 17 | +    return _stat.size; | 
|  | 18 | +  } | 
|  | 19 | + | 
|  | 20 | +  if (_stat.isDirectory()) { | 
|  | 21 | +    const names = await readdir(dir); | 
|  | 22 | +    let size = 0; | 
|  | 23 | +    for (const name of names) { | 
|  | 24 | +      size += await directorySize(join(dir, name)); | 
|  | 25 | +    } | 
|  | 26 | +    return size; | 
|  | 27 | +  } | 
|  | 28 | + | 
|  | 29 | +  return 0; | 
|  | 30 | +}; | 
|  | 31 | + | 
|  | 32 | +class CacheInfo { | 
|  | 33 | +  constructor(id = '') { | 
|  | 34 | +    this.id = id; | 
|  | 35 | +    this.lastModified = 0; | 
|  | 36 | +    this.size = 0; | 
|  | 37 | +  } | 
|  | 38 | + | 
|  | 39 | +  static async fromDirectory(dir) { | 
|  | 40 | +    const info = new CacheInfo(basename(dir)); | 
|  | 41 | +    info.lastModified = new Date( | 
|  | 42 | +      (await stat(join(dir, 'stamp'))).mtime, | 
|  | 43 | +    ).getTime(); | 
|  | 44 | +    info.size = await directorySize(dir); | 
|  | 45 | +    return info; | 
|  | 46 | +  } | 
|  | 47 | + | 
|  | 48 | +  static async fromDirectoryChildren(dir) { | 
|  | 49 | +    const children = []; | 
|  | 50 | +    const names = await readdir(dir); | 
|  | 51 | +    for (const name of names) { | 
|  | 52 | +      children.push(await CacheInfo.fromDirectory(join(dir, name))); | 
|  | 53 | +    } | 
|  | 54 | +    return children; | 
|  | 55 | +  } | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +// Compilers for webpack with multiple parallel configurations might try to | 
|  | 59 | +// delete caches at the same time. Mutex lock the process of pruning to keep | 
|  | 60 | +// from multiple pruning runs from colliding with each other. | 
|  | 61 | +let deleteLock = null; | 
|  | 62 | + | 
|  | 63 | +class PruneCachesSystem { | 
|  | 64 | +  constructor(cacheRoot, options = {}) { | 
|  | 65 | +    this.cacheRoot = cacheRoot; | 
|  | 66 | + | 
|  | 67 | +    this.options = Object.assign( | 
|  | 68 | +      { | 
|  | 69 | +        // Caches younger than `maxAge` are not considered for deletion. They | 
|  | 70 | +        // must be at least this (default: 2 days) old in milliseconds. | 
|  | 71 | +        maxAge: 2 * 24 * 60 * 60 * 1000, | 
|  | 72 | +        // All caches together must be larger than `sizeThreshold` before any | 
|  | 73 | +        // caches will be deleted. Together they must be at least this | 
|  | 74 | +        // (default: 50 MB) big in bytes. | 
|  | 75 | +        sizeThreshold: 50 * 1024 * 1024, | 
|  | 76 | +      }, | 
|  | 77 | +      options, | 
|  | 78 | +    ); | 
|  | 79 | +  } | 
|  | 80 | + | 
|  | 81 | +  apply(compiler) { | 
|  | 82 | +    const compilerHooks = pluginCompat.hooks(compiler); | 
|  | 83 | + | 
|  | 84 | +    const deleteOldCaches = async () => { | 
|  | 85 | +      while (deleteLock !== null) { | 
|  | 86 | +        await deleteLock; | 
|  | 87 | +      } | 
|  | 88 | + | 
|  | 89 | +      let resolveLock; | 
|  | 90 | + | 
|  | 91 | +      let infos; | 
|  | 92 | +      try { | 
|  | 93 | +        deleteLock = new Promise(resolve => { | 
|  | 94 | +          resolveLock = resolve; | 
|  | 95 | +        }); | 
|  | 96 | + | 
|  | 97 | +        infos = await CacheInfo.fromDirectoryChildren(this.cacheRoot); | 
|  | 98 | + | 
|  | 99 | +        // Sort lastModified in descending order. More recently modified at the | 
|  | 100 | +        // beginning of the array. | 
|  | 101 | +        infos.sort((a, b) => b.lastModified - a.lastModified); | 
|  | 102 | + | 
|  | 103 | +        const totalSize = infos.reduce((carry, info) => carry + info.size, 0); | 
|  | 104 | +        const oldInfos = infos.filter( | 
|  | 105 | +          info => info.lastModified < Date.now() - this.options.maxAge, | 
|  | 106 | +        ); | 
|  | 107 | +        const oldTotalSize = oldInfos.reduce( | 
|  | 108 | +          (carry, info) => carry + info.size, | 
|  | 109 | +          0, | 
|  | 110 | +        ); | 
|  | 111 | + | 
|  | 112 | +        if (oldInfos.length > 0 && totalSize > this.options.sizeThreshold) { | 
|  | 113 | +          const newInfos = infos.filter( | 
|  | 114 | +            info => info.lastModified >= Date.now() - this.options.maxAge, | 
|  | 115 | +          ); | 
|  | 116 | + | 
|  | 117 | +          for (const info of oldInfos) { | 
|  | 118 | +            rimraf(join(this.cacheRoot, info.id)); | 
|  | 119 | +          } | 
|  | 120 | + | 
|  | 121 | +          const newTotalSize = newInfos.reduce( | 
|  | 122 | +            (carry, info) => carry + info.size, | 
|  | 123 | +            0, | 
|  | 124 | +          ); | 
|  | 125 | + | 
|  | 126 | +          logMessages.deleteOldCaches(compiler, { | 
|  | 127 | +            infos, | 
|  | 128 | +            totalSize, | 
|  | 129 | +            newInfos, | 
|  | 130 | +            newTotalSize, | 
|  | 131 | +            oldInfos, | 
|  | 132 | +            oldTotalSize, | 
|  | 133 | +          }); | 
|  | 134 | +        } else { | 
|  | 135 | +          logMessages.keepCaches(compiler, { | 
|  | 136 | +            infos, | 
|  | 137 | +            totalSize, | 
|  | 138 | +          }); | 
|  | 139 | +        } | 
|  | 140 | +      } catch (error) { | 
|  | 141 | +        if (error.code !== 'ENOENT') { | 
|  | 142 | +          throw error; | 
|  | 143 | +        } | 
|  | 144 | +      } finally { | 
|  | 145 | +        if (typeof resolveLock === 'function') { | 
|  | 146 | +          deleteLock = null; | 
|  | 147 | +          resolveLock(); | 
|  | 148 | +        } | 
|  | 149 | +      } | 
|  | 150 | +    }; | 
|  | 151 | + | 
|  | 152 | +    compilerHooks.watchRun.tapPromise( | 
|  | 153 | +      'HardSource - PruneCachesSystem', | 
|  | 154 | +      deleteOldCaches, | 
|  | 155 | +    ); | 
|  | 156 | +    compilerHooks.run.tapPromise( | 
|  | 157 | +      'HardSource - PruneCachesSystem', | 
|  | 158 | +      deleteOldCaches, | 
|  | 159 | +    ); | 
|  | 160 | +  } | 
|  | 161 | +} | 
|  | 162 | + | 
|  | 163 | +module.exports = PruneCachesSystem; | 
0 commit comments