Skip to content
This repository was archived by the owner on Oct 27, 2023. It is now read-only.

Commit 58845f6

Browse files
committed
🚀 Initial commit
0 parents  commit 58845f6

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/npm-debug.log

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# node-application-cache-path
2+
3+
Store your application cache in the right location.
4+
5+
## Installation
6+
7+
```bash
8+
npm install --save application-cache-path
9+
```
10+
11+
## Usage
12+
13+
```javascript
14+
var applicationCachePath = require('application-cache-path')
15+
16+
// cachePath is a string with the path to a directory
17+
// where you can store your cache.
18+
var cachePath = applicationCachePath('My App')
19+
```
20+
21+
## API
22+
23+
### `applicationCachePath(name)`
24+
25+
Return a string with the path to a directory where you can store your
26+
application specific cache.
27+
28+
## Acknowledgments
29+
30+
Heavily inspired by [node-application-config-path](https://github.com/LinusU/node-application-config-path)
31+
32+
## License
33+
34+
MIT

index.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var os = require('os')
2+
var path = require('path')
3+
4+
function darwin (name) {
5+
return path.join(process.env['HOME'], 'Library', 'Caches', name)
6+
}
7+
8+
function linux (name) {
9+
if (process.env['XDG_CACHE_HOME']) {
10+
return path.join(process.env['XDG_CACHE_HOME'], name)
11+
}
12+
13+
return path.join(process.env['HOME'], '.cache', name)
14+
}
15+
16+
function win32 (name) {
17+
if (process.env['LOCALAPPDATA']) {
18+
return path.join(process.env['LOCALAPPDATA'], 'data', 'Cache', name)
19+
}
20+
21+
return path.join(process.env['USERPROFILE'], 'Application Data', 'Cache', name)
22+
}
23+
24+
module.exports = function applicationCachePath (name) {
25+
if (typeof name !== 'string') {
26+
throw new TypeError('First parameter must be a string')
27+
}
28+
29+
switch (os.platform()) {
30+
case 'darwin': return darwin(name)
31+
case 'linux': return linux(name)
32+
case 'win32': return win32(name)
33+
default: throw new Error('Platform not supported')
34+
}
35+
}

package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "application-cache-path",
3+
"version": "0.0.0",
4+
"description": "Get a directory where the OS wants you to store cache",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && node test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/cstruct/node-application-cache-path.git"
12+
},
13+
"author": "Albin Mattsson <[email protected]>",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/cstruct/node-application-cache-path/issues"
17+
},
18+
"homepage": "https://github.com/cstruct/node-application-cache-path#readme",
19+
"devDependencies": {
20+
"standard": "^7.1.2"
21+
}
22+
}

test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var path = require('path')
2+
var assert = require('assert')
3+
var applicationCachePath = require('./')
4+
5+
assert.ok(path.isAbsolute(applicationCachePath('test')), 'should return an absolute path')
6+
7+
assert.throws(function () {
8+
applicationCachePath()
9+
}, TypeError, 'should throw error when called incorrectly without parameter')
10+
11+
assert.throws(function () {
12+
applicationCachePath(1)
13+
}, TypeError, 'should throw error when called incorrectly with wrong type')

0 commit comments

Comments
 (0)