Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 5, 2021
1 parent a9421d8 commit e1b957d
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 168 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
158 changes: 77 additions & 81 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,77 @@
declare const xdgBasedir: {
/**
Directory for user-specific data files.
@example
```js
import xdgBasedir = require('xdg-basedir');
xdgBasedir.data;
//=> '/home/sindresorhus/.local/share'
```
*/
readonly data?: string;

/**
Directory for user-specific configuration files.
@example
```js
import xdgBasedir = require('xdg-basedir');
xdgBasedir.config;
//=> '/home/sindresorhus/.config'
```
*/
readonly config?: string;

/**
Directory for user-specific non-essential data files.
@example
```js
import xdgBasedir = require('xdg-basedir');
xdgBasedir.cache;
//=> '/home/sindresorhus/.cache'
```
*/
readonly cache?: string;

/**
Directory for user-specific non-essential runtime files and other file objects (such as sockets, named pipes, etc).
@example
```js
import xdgBasedir = require('xdg-basedir');
xdgBasedir.runtime;
//=> '/run/user/sindresorhus'
```
*/
readonly runtime?: string;

/**
Preference-ordered array of base directories to search for data files in addition to `.data`.
@example
```js
import xdgBasedir = require('xdg-basedir');
xdgBasedir.dataDirs
//=> ['/home/sindresorhus/.local/share', '/usr/local/share/', '/usr/share/']
```
*/
readonly dataDirs: readonly string[];

/**
Preference-ordered array of base directories to search for configuration files in addition to `.config`.
@example
```js
import xdgBasedir = require('xdg-basedir');
xdgBasedir.configDirs;
//=> ['/home/sindresorhus/.config', '/etc/xdg']
```
*/
readonly configDirs: readonly string[];
};

export = xdgBasedir;
/**
Directory for user-specific data files.
@example
```
import {xdgData} from 'xdg-basedir';
console.log(xdgData);
//=> '/home/sindresorhus/.local/share'
```
*/
export const xdgData: string | undefined;

/**
Directory for user-specific configuration files.
@example
```
import {xdgConfig} from 'xdg-basedir';
console.log(xdgConfig);
//=> '/home/sindresorhus/.config'
```
*/
export const xdgConfig: string | undefined;

/**
Directory for user-specific non-essential data files.
@example
```
import {xdgCache} from 'xdg-basedir';
console.log(xdgCache);
//=> '/home/sindresorhus/.cache'
```
*/
export const xdgCache: string | undefined;

/**
Directory for user-specific non-essential runtime files and other file objects (such as sockets, named pipes, etc).
@example
```
import {xdgRuntime} from 'xdg-basedir';
console.log(xdgRuntime);
//=> '/run/user/sindresorhus'
```
*/
export const xdgRuntime: string | undefined;

/**
Preference-ordered array of base directories to search for data files in addition to `xdgData`.
@example
```
import {xdgDataDirectories} from 'xdg-basedir';
console.log(xdgDataDirectories);
//=> ['/home/sindresorhus/.local/share', '/usr/local/share/', '/usr/share/']
```
*/
export const xdgDataDirectories: readonly string[];

/**
Preference-ordered array of base directories to search for configuration files in addition to `xdgConfig`.
@example
```
import {xdgConfigDirectories} from 'xdg-basedir';
console.log(xdgConfigDirectories);
//=> ['/home/sindresorhus/.config', '/etc/xdg']
```
*/
export const xdgConfigDirectories: readonly string[];
25 changes: 12 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
'use strict';
const os = require('os');
const path = require('path');
import os from 'os';
import path from 'path';

const homeDirectory = os.homedir();
const {env} = process;

exports.data = env.XDG_DATA_HOME ||
export const data = env.XDG_DATA_HOME ||
(homeDirectory ? path.join(homeDirectory, '.local', 'share') : undefined);

exports.config = env.XDG_CONFIG_HOME ||
export const config = env.XDG_CONFIG_HOME ||
(homeDirectory ? path.join(homeDirectory, '.config') : undefined);

exports.cache = env.XDG_CACHE_HOME || (homeDirectory ? path.join(homeDirectory, '.cache') : undefined);
export const cache = env.XDG_CACHE_HOME || (homeDirectory ? path.join(homeDirectory, '.cache') : undefined);

exports.runtime = env.XDG_RUNTIME_DIR || undefined;
export const runtime = env.XDG_RUNTIME_DIR || undefined;

exports.dataDirs = (env.XDG_DATA_DIRS || '/usr/local/share/:/usr/share/').split(':');
export const dataDirs = (env.XDG_DATA_DIRS || '/usr/local/share/:/usr/share/').split(':');

if (exports.data) {
exports.dataDirs.unshift(exports.data);
if (data) {
dataDirs.unshift(data);
}

exports.configDirs = (env.XDG_CONFIG_DIRS || '/etc/xdg').split(':');
export const configDirs = (env.XDG_CONFIG_DIRS || '/etc/xdg').split(':');

if (exports.config) {
exports.configDirs.unshift(exports.config);
if (config) {
configDirs.unshift(config);
}
26 changes: 13 additions & 13 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {expectType, expectError} from 'tsd';
import xdgBasedir = require('.');
import {xdgData, xdgConfig, xdgCache, xdgRuntime, xdgConfigDirectories, xdgDataDirectories} from './index.js';

expectType<string | undefined>(xdgBasedir.data);
expectError<string>(xdgBasedir.data);
expectType<string | undefined>(xdgBasedir.config);
expectError<string>(xdgBasedir.config);
expectType<string | undefined>(xdgBasedir.cache);
expectError<string>(xdgBasedir.cache);
expectType<string | undefined>(xdgBasedir.runtime);
expectError<string>(xdgBasedir.runtime);
expectType<readonly string[]>(xdgBasedir.configDirs);
expectError<string[]>(xdgBasedir.configDirs);
expectType<readonly string[]>(xdgBasedir.dataDirs);
expectError<string[]>(xdgBasedir.dataDirs);
expectType<string | undefined>(xdgData);
expectError<string>(xdgData);
expectType<string | undefined>(xdgCache);
expectError<string>(xdgCache);
expectType<string | undefined>(xdgCache);
expectError<string>(xdgCache);
expectType<string | undefined>(xdgRuntime);
expectError<string>(xdgRuntime);
expectType<readonly string[]>(xdgConfigDirectories);
expectError<string[]>(xdgConfigDirectories);
expectType<readonly string[]>(xdgDataDirectories);
expectError<string[]>(xdgDataDirectories);
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 <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (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
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Get XDG Base Directory paths",
"license": "MIT",
"repository": "sindresorhus/xdg-basedir",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -37,5 +40,8 @@
"import-fresh": "^3.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"ava": {
"serial": true
}
}
32 changes: 14 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,56 @@

> Get [XDG Base Directory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) paths
This package is meant for Linux. You should not use XDG on macOS and Windows. Instead, you should follow their platform conventions. You can use [`env-paths`](https://github.com/sindresorhus/env-paths) for that.

This package is meant for Linux. You should not use XDG on macOS or Windows. Instead, you should follow their platform conventions. You can use [`env-paths`](https://github.com/sindresorhus/env-paths) for that.

## Install

```
$ npm install xdg-basedir
```


## Usage

```js
const xdgBasedir = require('xdg-basedir');
import {xdgData, xdgConfig, xdgDataDirectories} from 'xdg-basedir';

xdgBasedir.data;
console.log(xdgData);
//=> '/home/sindresorhus/.local/share'

xdgBasedir.config;
console.log(xdgConfig);
//=> '/home/sindresorhus/.config'

xdgBasedir.dataDirs
console.log(xdgDataDirectories);
//=> ['/home/sindresorhus/.local/share', '/usr/local/share/', '/usr/share/']
```


## API

The properties `.data`, `.config`, `.cache`, `.runtime` will return `null` in the uncommon case that both the XDG environment variable is not set and the users home directory can't be found. You need to handle this case. A common solution is to [fall back to a temp directory](https://github.com/yeoman/configstore/blob/b82690fc401318ad18dcd7d151a0003a4898a314/index.js#L15).
The exports `xdgData`, `xdgConfig`, `xdgCache`, `xdgRuntime` will return `undefined` in the uncommon case that both the XDG environment variable is not set and the users home directory can't be found. You need to handle this case. A common solution is to [fall back to a temporary directory](https://github.com/yeoman/configstore/blob/b82690fc401318ad18dcd7d151a0003a4898a314/index.js#L15).

### .data
### xdgData

Directory for user-specific data files.

### .config
### xdgConfig

Directory for user-specific configuration files.

### .cache
### xdgCache

Directory for user-specific non-essential data files.

### .runtime
### xdgRuntime

Directory for user-specific non-essential runtime files and other file objects (such as sockets, named pipes, etc).

### .dataDirs

Preference-ordered array of base directories to search for data files in addition to `.data`.
### xdgDataDirectories

### .configDirs
Preference-ordered array of base directories to search for data files in addition to `xdgData`.

Preference-ordered array of base directories to search for configuration files in addition to `.config`.
### xdgConfigDirectories

Preference-ordered array of base directories to search for configuration files in addition to `xdgConfig`.

---

Expand Down
Loading

0 comments on commit e1b957d

Please sign in to comment.