Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 27, 2021
1 parent 62d4ec8 commit 995e663
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 120 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
141 changes: 65 additions & 76 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,90 @@
declare namespace envPaths {
export interface Options {
/**
__Don't use this option unless you really have to!__
export interface Options {
/**
__Don't use this option unless you really have to!__
Suffix appended to the project name to avoid name conflicts with native apps. Pass an empty string to disable it.
Suffix appended to the project name to avoid name conflicts with native apps. Pass an empty string to disable it.
@default 'nodejs'
*/
readonly suffix?: string;
}
@default 'nodejs'
*/
readonly suffix?: string;
}

export interface Paths {
/**
Directory for data files.
export interface Paths {
/**
Directory for data files.
Example locations (with the default `nodejs` suffix):
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Application Support/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Data` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Data`)
- Linux: `~/.local/share/MyApp-nodejs` (or `$XDG_DATA_HOME/MyApp-nodejs`)
*/
readonly data: string;
- macOS: `~/Library/Application Support/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Data` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Data`)
- Linux: `~/.local/share/MyApp-nodejs` (or `$XDG_DATA_HOME/MyApp-nodejs`)
*/
readonly data: string;

/**
Directory for data files.
/**
Directory for data files.
Example locations (with the default `nodejs` suffix):
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Preferences/MyApp-nodejs`
- Windows: `%APPDATA%\MyApp-nodejs\Config` (for example, `C:\Users\USERNAME\AppData\Roaming\MyApp-nodejs\Config`)
- Linux: `~/.config/MyApp-nodejs` (or `$XDG_CONFIG_HOME/MyApp-nodejs`)
*/
readonly config: string;
- macOS: `~/Library/Preferences/MyApp-nodejs`
- Windows: `%APPDATA%\MyApp-nodejs\Config` (for example, `C:\Users\USERNAME\AppData\Roaming\MyApp-nodejs\Config`)
- Linux: `~/.config/MyApp-nodejs` (or `$XDG_CONFIG_HOME/MyApp-nodejs`)
*/
readonly config: string;

/**
Directory for non-essential data files.
/**
Directory for non-essential data files.
Example locations (with the default `nodejs` suffix):
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Caches/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Cache` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Cache`)
- Linux: `~/.cache/MyApp-nodejs` (or `$XDG_CACHE_HOME/MyApp-nodejs`)
*/
readonly cache: string;
- macOS: `~/Library/Caches/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Cache` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Cache`)
- Linux: `~/.cache/MyApp-nodejs` (or `$XDG_CACHE_HOME/MyApp-nodejs`)
*/
readonly cache: string;

/**
Directory for log files.
/**
Directory for log files.
Example locations (with the default `nodejs` suffix):
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Logs/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Log` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Log`)
- Linux: `~/.local/state/MyApp-nodejs` (or `$XDG_STATE_HOME/MyApp-nodejs`)
*/
readonly log: string;
- macOS: `~/Library/Logs/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Log` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Log`)
- Linux: `~/.local/state/MyApp-nodejs` (or `$XDG_STATE_HOME/MyApp-nodejs`)
*/
readonly log: string;

/**
Directory for temporary files.
/**
Directory for temporary files.
Example locations (with the default `nodejs` suffix):
Example locations (with the default `nodejs` suffix):
- macOS: `/var/folders/jf/f2twvvvs5jl_m49tf034ffpw0000gn/T/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\Temp\MyApp-nodejs` (for example, `C:\Users\USERNAME\AppData\Local\Temp\MyApp-nodejs`)
- Linux: `/tmp/USERNAME/MyApp-nodejs`
*/
readonly temp: string;
}
- macOS: `/var/folders/jf/f2twvvvs5jl_m49tf034ffpw0000gn/T/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\Temp\MyApp-nodejs` (for example, `C:\Users\USERNAME\AppData\Local\Temp\MyApp-nodejs`)
- Linux: `/tmp/USERNAME/MyApp-nodejs`
*/
readonly temp: string;
}

declare const envPaths: {
/**
Get paths for storing things like data, config, cache, etc.
Note: It only generates the path strings. It doesn't create the directories for you. You could use [`make-dir`](https://github.com/sindresorhus/make-dir) to create the directories.
/**
Get paths for storing things like data, config, cache, etc.
@param name - Name of your project. Used to generate the paths.
@returns The paths to use for your project on current OS.
Note: It only generates the path strings. It doesn't create the directories for you. You could use [`make-dir`](https://github.com/sindresorhus/make-dir) to create the directories.
@example
```
import envPaths = require('env-paths');
@param name - The name of your project. Used to generate the paths.
@returns The paths to use for your project on current OS.
const paths = envPaths('MyApp');
@example
```
import envPaths from 'env-paths';
paths.data;
//=> '/home/sindresorhus/.local/share/MyApp-nodejs'
paths.config
//=> '/home/sindresorhus/.config/MyApp-nodejs'
```
*/
(name: string, options?: envPaths.Options): envPaths.Paths;
const paths = envPaths('MyApp');
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function envPaths(name: string, options?: envPaths.Options): envPaths.Paths;
// export = envPaths;
default: typeof envPaths;
};
paths.data;
//=> '/home/sindresorhus/.local/share/MyApp-nodejs'
export = envPaths;
paths.config
//=> '/home/sindresorhus/.config/MyApp-nodejs'
```
*/
export default function envPaths(name: string, options?: Options): Paths;
28 changes: 11 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const path = require('path');
const os = require('os');
import path from 'node:path';
import os from 'node:os';
import process from 'node:process';

const homedir = os.homedir();
const tmpdir = os.tmpdir();
Expand All @@ -14,7 +14,7 @@ const macos = name => {
config: path.join(library, 'Preferences', name),
cache: path.join(library, 'Caches', name),
log: path.join(library, 'Logs', name),
temp: path.join(tmpdir, name)
temp: path.join(tmpdir, name),
};
};

Expand All @@ -28,7 +28,7 @@ const windows = name => {
config: path.join(appData, name, 'Config'),
cache: path.join(localAppData, name, 'Cache'),
log: path.join(localAppData, name, 'Log'),
temp: path.join(tmpdir, name)
temp: path.join(tmpdir, name),
};
};

Expand All @@ -42,20 +42,18 @@ const linux = name => {
cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name),
// https://wiki.debian.org/XDGBaseDirectorySpecification#state
log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name),
temp: path.join(tmpdir, username, name)
temp: path.join(tmpdir, username, name),
};
};

const envPaths = (name, options) => {
export default function envPaths(name, {suffix = 'nodejs'} = {}) {
if (typeof name !== 'string') {
throw new TypeError(`Expected string, got ${typeof name}`);
throw new TypeError(`Expected a string, got ${typeof name}`);
}

options = Object.assign({suffix: 'nodejs'}, options);

if (options.suffix) {
if (suffix) {
// Add suffix to prevent possible conflict with native apps
name += `-${options.suffix}`;
name += `-${suffix}`;
}

if (process.platform === 'darwin') {
Expand All @@ -67,8 +65,4 @@ const envPaths = (name, options) => {
}

return linux(name);
};

module.exports = envPaths;
// TODO: Remove this for the next major release
module.exports.default = envPaths;
}
3 changes: 1 addition & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {expectType} from 'tsd';
import envPaths = require('.');
import {Paths} from '.';
import envPaths, {Paths} from './index.js';

expectType<Paths>(envPaths('MyApp'));
expectType<Paths>(envPaths('MyApp', {suffix: 'test'}));
Expand Down
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Get paths for storing things like data, config, cache, etc",
"license": "MIT",
"repository": "sindresorhus/env-paths",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=6"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -38,8 +41,8 @@
"unix"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
12 changes: 5 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
Uses the correct OS-specific paths. Most developers get this wrong.


## Install

```
$ npm install env-paths
```


## Usage

```js
const envPaths = require('env-paths');
import envPaths from 'env-paths';

const paths = envPaths('MyApp');

Expand All @@ -26,7 +24,6 @@ paths.config
//=> '/home/sindresorhus/.config/MyApp-nodejs'
```


## API

### paths = envPaths(name, options?)
Expand All @@ -37,18 +34,19 @@ Note: It only generates the path strings. It doesn't create the directories for

Type: `string`

Name of your project. Used to generate the paths.
The name of your project. Used to generate the paths.

#### options

Type: `object`

##### suffix

Type: `string`<br>
Type: `string`\
Default: `'nodejs'`

**Don't use this option unless you really have to!**<br>
**Don't use this option unless you really have to!**

Suffix appended to the project name to avoid name conflicts with native
apps. Pass an empty string to disable it.

Expand Down
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import process from 'node:process';
import test from 'ava';
import envPaths from '.';
import envPaths from './index.js';

test('default', t => {
const name = 'unicorn';
Expand All @@ -13,15 +14,14 @@ test('default', t => {

test('custom suffix', t => {
const name = 'unicorn';
const opts = {suffix: 'horn'};
const paths = envPaths(name, opts);
t.true(paths.data.endsWith(`${name}-${opts.suffix}`));
const options = {suffix: 'horn'};
const paths = envPaths(name, options);
t.true(paths.data.endsWith(`${name}-${options.suffix}`));
});

test('no suffix', t => {
const name = 'unicorn';
const opts = {suffix: false};
const paths = envPaths(name, opts);
const paths = envPaths(name, {suffix: false});
t.true(paths.data.endsWith(name));
});

Expand All @@ -32,7 +32,7 @@ if (process.platform === 'linux') {
data: 'XDG_DATA_HOME',
config: 'XDG_CONFIG_HOME',
cache: 'XDG_CACHE_HOME',
log: 'XDG_STATE_HOME'
log: 'XDG_STATE_HOME',
};

for (const env of Object.values(envVars)) {
Expand Down

0 comments on commit 995e663

Please sign in to comment.