-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcache.js
More file actions
117 lines (107 loc) · 3.36 KB
/
cache.js
File metadata and controls
117 lines (107 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const Table = require('cli-table3')
const inquirer = require('inquirer')
const printStats = (table, name, stats) => {
const nameColor = stats.isDirectory() ? chalk.blue : chalk.green
const mtime = stats.mtime
.toISOString()
.replace(/T/, ' ')
.replace(/\..+/, '')
table.push([nameColor(name), stats.size, mtime])
}
const parse = async ({ item = '/', getCache }) => {
const cache = getCache()
let loc
try {
loc = cache.getCacheLocation(item) // Check that we have a valid argument!
} catch (e) {
reporter.debug(`getCacheLocation error: ${e}`)
reporter.error(`Cache location ${chalk.bold(item)} is invalid.`)
}
const isRoot = loc === cache.getCacheLocation('/')
const exists = await cache.exists(item)
return {
item,
loc,
isRoot,
exists,
cache,
}
}
const builder = (yargs) => {
yargs.command(
'location [item]',
'Get the filesystem location of a cache item',
{},
async (argv) => {
const { loc } = await parse(argv)
reporter.print(loc)
}
)
yargs.command('list [item]', 'List cache items', {}, async (argv) => {
const { item, cache, exists } = await parse(argv)
if (!exists) {
reporter.info(`Cached item ${chalk.bold(item)} does not exist`)
return
}
const stat = await cache.stat(item)
const table = new Table({
head: ['Name', 'Size', 'Modified'],
})
if (stat.children) {
Object.keys(stat.children).forEach((name) => {
printStats(table, name, stat.children[name])
})
} else {
printStats(table, stat.name, stat.stats)
}
reporter.print(table)
})
yargs.command(
'status [item]',
'Get cache item status',
{},
async (argv) => {
const { item, exists, loc } = await parse(argv)
reporter.print('CACHE STATUS')
reporter.print(
`\t${chalk.green.bold(item)}\t\t${
exists
? `${chalk.blue.bold('EXISTS')}`
: chalk.red.bold('DOES NOT EXIST')
}\t@ ${loc}`
)
}
)
yargs.command('purge [item]', 'Purge cache item', {}, async (argv) => {
const { item, isRoot, exists, cache } = await parse(argv)
if (!exists) {
reporter.info(`Cached item ${chalk.bold(item)} does not exist`)
return
}
const answers = await inquirer.prompt({
type: 'confirm',
name: 'confirmed',
message: `Are you sure you want to remove ${
isRoot ? 'the ENTIRE d2 cache' : `cache item "${item}"`
}?`,
default: false,
})
if (!answers.confirmed) {
return
}
try {
await cache.purge(item)
reporter.info(`Purged cache item ${chalk.bold(item)}`)
} catch (e) {
reporter.debug(`Purge error: ${e}`)
reporter.error(`Failed to purge cache item ${chalk.bold(item)}`)
}
})
yargs.demandCommand()
}
module.exports = {
command: 'cache',
desc: 'Inspect and control the application cache',
builder,
}