├── .editorconfig ├── .gitattributes ├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── lib ├── index.d.ts └── index.js ├── main └── main.js ├── package-lock.json └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | indent_style = space 8 | indent_size = 2 9 | end_of_line = lf 10 | indent_brace_style = K&R 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | max_line_length = 100 14 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce `lf` for text files (even on Windows) 2 | text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Editor # 3 | ############################################################################### 4 | # JetBrains products (Webstorm, IntelliJ IDEA, ...) 5 | .idea/ 6 | *.iml 7 | 8 | ############################################################################### 9 | # Dependencies # 10 | ############################################################################### 11 | # Node dependencies directory 12 | node_modules 13 | 14 | ############################################################################### 15 | # Temporary files # 16 | ############################################################################### 17 | *.log 18 | *.tmp 19 | tmp/ 20 | logs/ 21 | 22 | ############################################################################### 23 | # Other # 24 | ############################################################################### 25 | # Runtime data 26 | pids 27 | *.pid 28 | *.seed 29 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 1.0.0 (2018-09-04) 2 | 3 | - **[Feature]** Expose function as main export for commonjs. 4 | - **[Fix]** Fix invalid `platform` access in `getFallback`. 5 | - **[Fix]** Do not ignore `app` when using `APPDATA` env var. 6 | 7 | # 0.1.2 (2017-06-20) 8 | 9 | - **[Fix]** Fix Typescript type definitions 10 | 11 | # 0.1.1 (2017-06-17T15:15:00) 12 | 13 | - **[Feature]** Add Typescript type definitions 14 | 15 | # 0.1.0 (2017-06-17T15:05:00) 16 | 17 | - First release 18 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright © 2017-2018 Charles Samborski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AppData Path 2 | 3 | ## Installation 4 | 5 | ```shell 6 | npm install -S appdata-path 7 | ``` 8 | 9 | ## Usage 10 | 11 | ```javascript 12 | import getAppDataPath from "appdata-path"; 13 | // Or 14 | // const getAppDataPath = require("appdata-path"); 15 | 16 | console.log(getAppDataPath()); 17 | /* 18 | * Linux: 19 | * "/home/demurgos/.config" 20 | * 21 | * Mac: 22 | * "/Users/demurgos/Library/Application Support" 23 | * 24 | * Windows: 25 | * "C:\Users\demurgos\AppData\Roaming" 26 | * 27 | * System with custom `APPDATA` environment variable: 28 | * process.env["APPDATA"] 29 | * For example: 30 | * process.env["APPDATA"] = "/home/demurgos/apps" 31 | * => "/home/demurgos/apps" 32 | */ 33 | 34 | console.log(getAppDataPath("my-app")); 35 | /* 36 | * Linux: 37 | * "/home/demurgos/.config/my-app" 38 | * 39 | * Mac: 40 | * "/Users/demurgos/Library/Application Support/my-app" 41 | * 42 | * Windows: 43 | * "C:\Users\demurgos\AppData\Roaming\my-app" 44 | * 45 | * System with custom `APPDATA` environment variable: 46 | * path.join(process.env["APPDATA"], "my-app") 47 | * or (if `process.env["APPDATA"] === os.homedir()`) 48 | * path.join(process.env["APPDATA"], ".my-app") 49 | * For example: 50 | * process.env["APPDATA"] = "/home/demurgos/apps" 51 | * => "/home/demurgos/apps/my-app" 52 | * process.env["APPDATA"] = "/home/demurgos" 53 | * => "/home/demurgos/.my-app" 54 | */ 55 | 56 | ``` 57 | 58 | ## API 59 | 60 | ### `getAppDataPath(app?: string): string` 61 | 62 | Returns the absolute system-dependant path for the place where applications 63 | should store their data for the current user. 64 | If you pass the name of your app directory or file as an argument, 65 | it will return the absolute path for the place where you should store the 66 | data of your application. It will prefix you file / directory name by a dot 67 | if the AppData directory happens to also be the user's home, otherwise it 68 | will just use it as is. See the examples above. 69 | 70 | ## Changelog 71 | 72 | See [CHANGELOG.md](./CHANGELOG.md). 73 | 74 | ## License 75 | 76 | See [LICENSE.md](./LICENSE.md). 77 | -------------------------------------------------------------------------------- /lib/index.d.ts: -------------------------------------------------------------------------------- 1 | export function getAppDataPath(app?: string): string; 2 | export default getAppDataPath; 3 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const {platform, homedir} = require("os"); 4 | const {join} = require("path"); 5 | 6 | function getForWindows() { 7 | return join(homedir(), "AppData", "Roaming"); 8 | } 9 | 10 | function getForMac() { 11 | return join(homedir(), "Library", "Application Support"); 12 | } 13 | 14 | function getForLinux() { 15 | return join(homedir(), ".config"); 16 | } 17 | 18 | function getFallback() { 19 | if (platform().startsWith("win")) { 20 | // Who knows, maybe its win64? 21 | return getForWindows(); 22 | } 23 | return getForLinux(); 24 | } 25 | 26 | function getAppDataPath(app) { 27 | let appDataPath = process.env["APPDATA"]; 28 | 29 | if (appDataPath === undefined) { 30 | switch (platform()) { 31 | case "win32": 32 | appDataPath = getForWindows(); 33 | break; 34 | case "darwin": 35 | appDataPath = getForMac(); 36 | break; 37 | case "linux": 38 | appDataPath = getForLinux(); 39 | break; 40 | default: 41 | appDataPath = getFallback(); 42 | } 43 | } 44 | 45 | if (app === undefined) { 46 | return appDataPath; 47 | } 48 | 49 | const normalizedAppName = appDataPath !== homedir() ? app : "." + app; 50 | return join(appDataPath, normalizedAppName); 51 | } 52 | 53 | module.exports = Object.assign( 54 | getAppDataPath, 55 | { 56 | getAppDataPath: getAppDataPath, 57 | default: getAppDataPath, 58 | }, 59 | ); 60 | -------------------------------------------------------------------------------- /main/main.js: -------------------------------------------------------------------------------- 1 | const {getAppDataPath} = require("../lib/index"); 2 | 3 | console.log(getAppDataPath()); 4 | console.log(getAppDataPath("my-app")); 5 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appdata-path", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appdata-path", 3 | "version": "1.0.0", 4 | "homepage": "https://github.com/demurgos/appdata-path", 5 | "description": "Get the AppData path", 6 | "main": "lib/index.js", 7 | "types": "lib/index.d.ts", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/demurgos/appdata-path.git" 11 | }, 12 | "scripts": { 13 | "start": "node main/main.js" 14 | }, 15 | "keywords": [ 16 | "appdata", 17 | "os", 18 | "environment" 19 | ], 20 | "license": "MIT", 21 | "dependencies": {}, 22 | "devDependencies": {} 23 | } 24 | --------------------------------------------------------------------------------