Skip to content

Commit e22f560

Browse files
committed
rename package to cache-cmd
1 parent ce1ac72 commit e22f560

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
# debounce-cmd
1+
# cache-cmd
22

3-
Debounce a command based on
3+
Cache a command based on
44

55
- time since last run
66
- file change
77

88
## Install
99

1010
```sh
11-
yarn add --dev debounce-cmd
11+
yarn add --dev cache-cmd
1212
```
1313

1414
or
1515

1616
```sh
17-
npm install --save-dev debounce-cmd
17+
npm install --save-dev cache-cmd
1818
```
1919

2020
## Usage
2121

2222
```sh
2323
# Shows help
24-
yarn debounce-cmd --help
24+
yarn cache-cmd --help
2525

2626
# Runs command if it was not run in the last 20s
27-
yarn debounce-cmd "echo ran this command" --time 20s
27+
yarn cache-cmd "echo ran this command" --time 20s
2828

2929
# Runs comand if yarn.lock in current directory changed since last run
30-
yarn debounce-cmd "yarn install" --file yarn.lock
30+
yarn cache-cmd "yarn install" --file yarn.lock
3131

3232
# Additionally uses custom cache directory instead of default in node_modules
33-
yarn debounce-cmd "yarn install" --file yarn.lock --cache-dir .config/cache
33+
yarn cache-cmd "yarn install" --file yarn.lock --cache-dir .config/cache
3434

3535
# Runs command if it was not run in a month or any of the files changed
36-
yarn debounce-cmd "yarn install" --time 1mo --file yarn.lock --file package.json
36+
yarn cache-cmd "yarn install" --time 1mo --file yarn.lock --file package.json
3737

3838
# Shows path to cache directory
39-
yarn debounce-cmd cache dir
39+
yarn cache-cmd cache dir
4040

4141
# Clear cache
42-
yarn debounce-cmd cache clear
42+
yarn cache-cmd cache clear
4343
```
4444

4545
You can use it to execute commands conditionally in `package.json` scripts.
4646

4747
```json
4848
{
4949
"scripts": {
50-
"dev": "debounce-cmd \"yarn\" --file yarn.lock && start-dev-server"
50+
"dev": "cache-cmd \"yarn\" --file yarn.lock && start-dev-server"
5151
}
5252
}
5353
```

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"name": "debounce-cmd",
2+
"name": "cache-cmd",
33
"version": "0.2.0",
4-
"description": "Debounce a command based on various factors",
4+
"description": "Cache a command based on various factors",
55
"keywords": [
6+
"cache",
67
"debounce",
78
"cli",
89
"conditional",
@@ -30,7 +31,7 @@
3031
"url": "https://github.com/dcastil/debounce-cmd.git"
3132
},
3233
"scripts": {
33-
"debounce-cmd": "node ./dist/cli.cjs",
34+
"cache-cmd": "node ./dist/cli.cjs",
3435
"build": "rm -rf dist/* && microbundle --strict --target node --output dist/cli.ts --format cjs --generateTypes false",
3536
"type-check": "tsc --build",
3637
"preversion": "git checkout main && git pull",

src/cli.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ import { version } from '../package.json'
77

88
import { clearCacheDirectory } from './cache-clear'
99
import { showCacheDirectory } from './cache-dir'
10-
import { debounceCommand } from './run'
10+
import { runCommand } from './run'
1111

1212
hardRejection()
1313

14-
const program = sade('debounce-cmd')
14+
const program = sade('cache-cmd')
1515

1616
program
1717
.version(version)
18-
.describe('Debounce a command based on various factors')
18+
.describe('Run and cache a command based on various factors')
1919
.option(
2020
'-c, --cache-dir',
21-
'Cache directory to use (default: .cache/debounce-cmd in nearest node_modules)'
21+
'Cache directory to use (default: .cache/cache-cmd in nearest node_modules)'
2222
)
2323

2424
program
2525
.command(
2626
'run <command>',
27-
'Run debounced command (if no <command> provided, this is the default)',
27+
'Run cached command (if no <command> provided, this is the default)',
2828
{ default: true }
2929
)
3030
.option('-f, --file', 'Run command only when file content changes')
@@ -62,17 +62,17 @@ program
6262
throw Error('Invalid --cache-on-error supplied')
6363
}
6464

65-
debounceCommand({
65+
runCommand({
6666
relativeCacheDirectory: cacheDirectory,
6767
command,
68-
debounceByTime: time,
69-
debounceByFiles: files as string[],
68+
cacheByTime: time,
69+
cacheByFiles: files as string[],
7070
shouldCacheOnError,
7171
})
7272
})
7373

7474
program
75-
.command('cache dir', 'Show cache directory path used by debounce-cmd')
75+
.command('cache dir', 'Show cache directory path used by cache-cmd')
7676
.action((options: Record<string, unknown>) => {
7777
const cacheDirectory = options['cache-dir']
7878

@@ -84,7 +84,7 @@ program
8484
})
8585

8686
program
87-
.command('cache clear', 'Clear cache used by debounce-cmd')
87+
.command('cache clear', 'Clear cache used by cache-cmd')
8888
.action((options: Record<string, unknown>) => {
8989
const cacheDirectory = options['cache-dir']
9090

src/run.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ import { getDuration } from './utils/get-duration'
99
import { getFileHashes } from './utils/get-file-hashes'
1010
import { getFilePaths } from './utils/get-file-paths'
1111

12-
interface DebounceCommandProps {
12+
interface RunCommandProps {
1313
relativeCacheDirectory: string | undefined
1414
command: string
15-
debounceByTime: string | undefined
16-
debounceByFiles: string[]
15+
cacheByTime: string | undefined
16+
cacheByFiles: string[]
1717
shouldCacheOnError: boolean | undefined
1818
}
1919

20-
export async function debounceCommand({
20+
export async function runCommand({
2121
relativeCacheDirectory,
2222
command,
23-
debounceByTime,
24-
debounceByFiles,
23+
cacheByTime: cacheByTime,
24+
cacheByFiles: cacheByFiles,
2525
shouldCacheOnError,
26-
}: DebounceCommandProps) {
27-
if (!debounceByTime && debounceByFiles.length === 0) {
26+
}: RunCommandProps) {
27+
if (!cacheByTime && cacheByFiles.length === 0) {
2828
await execSh.promise(command)
2929
return
3030
}
3131

3232
const cache = flatCache.load('commands-cache.json', createCache(relativeCacheDirectory))
33-
const filePaths = getFilePaths(debounceByFiles)
34-
const duration = debounceByTime ? getDuration(debounceByTime) : undefined
33+
const filePaths = getFilePaths(cacheByFiles)
34+
const duration = cacheByTime ? getDuration(cacheByTime) : undefined
3535

3636
const cacheKey = getCacheKey({ duration, filePaths, command })
3737

@@ -41,7 +41,7 @@ export async function debounceCommand({
4141
const currentDate = new Date()
4242

4343
const areFileHashesEqual = isEqual((cacheData as any)?.fileHashes, fileHashes)
44-
const isCurrentTimeInDebounce = (() => {
44+
const isWithinCacheTime = (() => {
4545
if (!duration) {
4646
return true
4747
}
@@ -55,7 +55,7 @@ export async function debounceCommand({
5555
return isAfter(add(new Date(lastRun), duration), currentDate)
5656
})()
5757

58-
if (areFileHashesEqual && isCurrentTimeInDebounce) {
58+
if (areFileHashesEqual && isWithinCacheTime) {
5959
return
6060
}
6161

0 commit comments

Comments
 (0)