mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-03 13:30:26 -05:00
feat: Print folder sizes with cypress cache list command. (#8590)
This commit is contained in:
@@ -102,6 +102,7 @@ const descriptions = {
|
||||
cacheClear: 'delete all cached binaries',
|
||||
cacheList: 'list cached binary versions',
|
||||
cachePath: 'print the path to the binary cache',
|
||||
cacheSize: 'Used with the list command to show the sizes of the cached folders',
|
||||
ciBuildId: 'the unique identifier for a run on your CI provider. typically a "BUILD_ID" env var. this value is automatically detected for most CI providers',
|
||||
config: 'sets configuration values. separate multiple values with a comma. overrides any value in cypress.json.',
|
||||
configFile: 'path to JSON file where configuration values are set. defaults to "cypress.json". pass "false" to disable.',
|
||||
@@ -386,6 +387,7 @@ module.exports = {
|
||||
.option('list', text('cacheList'))
|
||||
.option('path', text('cachePath'))
|
||||
.option('clear', text('cacheClear'))
|
||||
.option('--size', text('cacheSize'))
|
||||
.action(function (opts, args) {
|
||||
if (!args || !args.length) {
|
||||
this.outputHelp()
|
||||
@@ -398,6 +400,12 @@ module.exports = {
|
||||
unknownOption.call(this, `cache ${command}`, 'command')
|
||||
}
|
||||
|
||||
if (command === 'list') {
|
||||
cache.list(opts.size)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cache[command]()
|
||||
})
|
||||
|
||||
|
||||
+38
-5
@@ -7,12 +7,14 @@ const Table = require('cli-table3')
|
||||
const moment = require('moment')
|
||||
const chalk = require('chalk')
|
||||
const _ = require('lodash')
|
||||
const getFolderSize = require('./get-folder-size')
|
||||
|
||||
// output colors for the table
|
||||
const colors = {
|
||||
titles: chalk.white,
|
||||
dates: chalk.cyan,
|
||||
values: chalk.green,
|
||||
size: chalk.gray,
|
||||
}
|
||||
|
||||
const logCachePath = () => {
|
||||
@@ -25,29 +27,46 @@ const clear = () => {
|
||||
return fs.removeAsync(state.getCacheDir())
|
||||
}
|
||||
|
||||
const fileSizeInMB = (size) => {
|
||||
return `${(size / 1024 / 1024).toFixed(1)}MB`
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all cached versions, finds when each was used
|
||||
* and prints a table with results to the terminal
|
||||
*/
|
||||
const list = () => {
|
||||
return getCachedVersions()
|
||||
const list = (showSize) => {
|
||||
return getCachedVersions(showSize)
|
||||
.then((binaries) => {
|
||||
const head = [colors.titles('version'), colors.titles('last used')]
|
||||
|
||||
if (showSize) {
|
||||
head.push(colors.titles('size'))
|
||||
}
|
||||
|
||||
const table = new Table({
|
||||
head: [colors.titles('version'), colors.titles('last used')],
|
||||
head,
|
||||
})
|
||||
|
||||
binaries.forEach((binary) => {
|
||||
const versionString = colors.values(binary.version)
|
||||
const lastUsed = binary.accessed ? colors.dates(binary.accessed) : 'unknown'
|
||||
const row = [versionString, lastUsed]
|
||||
|
||||
return table.push([versionString, lastUsed])
|
||||
if (showSize) {
|
||||
const size = colors.size(fileSizeInMB(binary.size))
|
||||
|
||||
row.push(size)
|
||||
}
|
||||
|
||||
return table.push(row)
|
||||
})
|
||||
|
||||
logger.always(table.toString())
|
||||
})
|
||||
}
|
||||
|
||||
const getCachedVersions = () => {
|
||||
const getCachedVersions = (showSize) => {
|
||||
const cacheDir = state.getCacheDir()
|
||||
|
||||
return fs
|
||||
@@ -84,6 +103,20 @@ const getCachedVersions = () => {
|
||||
return binary
|
||||
})
|
||||
})
|
||||
.mapSeries((binary) => {
|
||||
if (showSize) {
|
||||
const binaryDir = state.getBinaryDir(binary.version)
|
||||
|
||||
return getFolderSize(binaryDir).then((size) => {
|
||||
return {
|
||||
...binary,
|
||||
size,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return binary
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
const fs = require('../fs')
|
||||
const { join } = require('path')
|
||||
const Bluebird = require('bluebird')
|
||||
|
||||
/**
|
||||
* Get the size of a folder or a file.
|
||||
*
|
||||
* This function returns the actual file size of the folder (size), not the allocated space on disk (size on disk).
|
||||
* For more details between the difference, check this link:
|
||||
* https://www.howtogeek.com/180369/why-is-there-a-big-difference-between-size-and-size-on-disk/
|
||||
*
|
||||
* @param {string} path path to the file or the folder.
|
||||
*/
|
||||
async function getSize (path) {
|
||||
const stat = await fs.lstat(path)
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
const list = await fs.readdir(path)
|
||||
|
||||
return Bluebird.resolve(list).reduce(async (prev, curr) => {
|
||||
const currPath = join(path, curr)
|
||||
|
||||
const s = await fs.lstat(currPath)
|
||||
|
||||
if (s.isDirectory()) {
|
||||
return prev + await getSize(currPath)
|
||||
}
|
||||
|
||||
return prev + s.size
|
||||
}, 0)
|
||||
}
|
||||
|
||||
return stat.size
|
||||
}
|
||||
|
||||
module.exports = getSize
|
||||
Reference in New Issue
Block a user