Skip to content

Commit bb797f7

Browse files
committed
lint, add unit tests, generate readme
1 parent bb885ee commit bb797f7

File tree

15 files changed

+424
-216
lines changed

15 files changed

+424
-216
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
module.exports = {
24
root: true,
35
extends: 'eslint:recommended',

README.md

Lines changed: 6 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @folder/xdg [![NPM version](https://img.shields.io/npm/v/@folder/xdg.svg?style=flat)](https://www.npmjs.com/package/@folder/xdg) [![NPM monthly downloads](https://img.shields.io/npm/dm/@folder/xdg.svg?style=flat)](https://npmjs.org/package/@folder/xdg) [![NPM total downloads](https://img.shields.io/npm/dt/@folder/xdg.svg?style=flat)](https://npmjs.org/package/@folder/xdg)
22

3-
> Get cross-platform XDG Base Directories or their equivalents. Works with Linux, Windows, or MacOS.
3+
> Get cross-platform XDG Base Directories or their equivalents. Works with Linux, Windows (win32), or MacOS (darwin).
44
55
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
66

@@ -251,7 +251,7 @@ The main export, and each method, returns an object with the following propertie
251251

252252
## Main Export
253253

254-
### [xdg](index.js#L18)
254+
### [xdg](index.js#L17)
255255

256256
Get the XDG Base Directory paths for Linux, or equivalent paths for Windows or MaxOS.
257257

@@ -260,7 +260,7 @@ Get the XDG Base Directory paths for Linux, or equivalent paths for Windows or M
260260
* `options` **{Object}**
261261
* `returns` **{Object}**: Returns an object of paths for the current platform.
262262

263-
### [.darwin](index.js#L44)
263+
### [.darwin](index.js#L42)
264264

265265
Get XDG equivalent paths for MacOS. Used by the main export when `process.platform` is `darwin`. Aliased as `xdg.macos()`.
266266

@@ -277,7 +277,7 @@ const dirs = xdg.darwin();
277277
const dirs = xdg.macos();
278278
```
279279

280-
### [.linux](index.js#L89)
280+
### [.linux](index.js#L93)
281281

282282
Get XDG equivalent paths for Linux. Used by the main export when `process.platform` is `linux`.
283283

@@ -290,7 +290,7 @@ Get XDG equivalent paths for Linux. Used by the main export when `process.platfo
290290
const dirs = xdg.linux();
291291
```
292292

293-
### [.win32](index.js#L136)
293+
### [.win32](index.js#L145)
294294

295295
Get XDG equivalent paths for MacOS. Used by the main export when `process.platform` is `win32`. Aliased as `xdg.windows()`.
296296

@@ -307,164 +307,6 @@ const dirs = xdg.win32();
307307
const dirs = xdg.windows();
308308
```
309309

310-
### [.userdirs()](lib/userdirs.js#L17)
311-
312-
Get the XDG User Directories for Linux, or equivalents for Windows or MaxOS.
313-
314-
**Params**
315-
316-
* `options` **{Object}**
317-
* `returns` **{Object}**: Returns an object of directory paths.
318-
319-
### [.userdirs.expand()](lib/userdirs.js#L39)
320-
321-
Returns an object to with paths to `user-dirs.*` files, as well as functions to
322-
load each file.
323-
324-
**Params**
325-
326-
* `options` **{Object}**
327-
* `paths` **{Object}**: Optionally pass the paths from the `userdirs()` function to avoid creating them again.
328-
* `returns` **{Object}**: Returns an object with a `paths` object, and `config`, `defaults`, `dirs`, and `create` functions for actually loading the user-dir files.
329-
330-
### [.userdirs.conf()](lib/userdirs.js#L76)
331-
332-
Loads and parses the contents of `user-dirs.conf`, if one exists in user home.
333-
334-
**Params**
335-
336-
* `options` **{Object}**
337-
* `returns` **{Object}**: Returns configur
338-
339-
**Example**
340-
341-
```js
342-
const config = userdirs.conf();
343-
console.log(config);
344-
//=> { enabled: true, filename_encoding: 'UTF-8' }
345-
```
346-
347-
### [.userdirs.defaults()](lib/userdirs.js#L105)
348-
349-
Loads and parses the contents of `user-dirs.defaults`, if one exists in user home.
350-
351-
// Example results:
352-
// {
353-
// XDG_DESKTOP_DIR: '/Users/jonschlinkert/Desktop',
354-
// XDG_DOWNLOAD_DIR: '/Users/jonschlinkert/Downloads',
355-
// XDG_TEMPLATES_DIR: '/Users/jonschlinkert/Templates',
356-
// XDG_PUBLICSHARE_DIR: '/Users/jonschlinkert/Public',
357-
// XDG_DOCUMENTS_DIR: '/Users/jonschlinkert/Documents',
358-
// XDG_MUSIC_DIR: '/Users/jonschlinkert/Music',
359-
// XDG_PICTURES_DIR: '/Users/jonschlinkert/Pictures',
360-
// XDG_VIDEOS_DIR: '/Users/jonschlinkert/Videos'
361-
// }
362-
363-
**Params**
364-
365-
* `options` **{Object}**
366-
* `returns` **{Object}**: Returns an object of paths.
367-
368-
**Example**
369-
370-
```js
371-
const defaults = userdirs.defaults();
372-
console.log(defaults);
373-
```
374-
375-
### [.userdirs.dirs()](lib/userdirs.js#L132)
376-
377-
Loads and parses the contents of `user-dirs.dirs`, if one exists in user home.
378-
379-
**Params**
380-
381-
* `options` **{Object}**
382-
* `returns` **{Object}**: Returns an object of paths.
383-
384-
**Example**
385-
386-
```js
387-
const dirs = userdirs.dirs();
388-
console.log(dirs);
389-
// Example results:
390-
// {
391-
// XDG_MUSIC_DIR: '/Users/jonschlinkert/Documents/Music',
392-
// XDG_PICTURES_DIR: '/Users/jonschlinkert/Documents/Pictures',
393-
// XDG_TEMPLATES_DIR: '/Users/jonschlinkert/Documents/Templates',
394-
// XDG_VIDEOS_DIR: '/Users/jonschlinkert/Documents/Videos'
395-
// }
396-
```
397-
398-
### [.userdirs.create()](lib/userdirs.js#L165)
399-
400-
Get the actual XDG User Directories to use for MacOS. Gets the `user-dirs.conf`, `user-dirs.defaults`, and the `user-dirs.dirs` files and, if not disabled in `user-dirs.conf`, merges the values in defaults and dirs to create the paths to use.
401-
402-
**Params**
403-
404-
* `options` **{Object}**
405-
* `returns` **{Object}**: Returns an object of paths.
406-
407-
**Example**
408-
409-
```js
410-
const dirs = userdirs.create();
411-
console.log(dirs);
412-
// Example results:
413-
// {
414-
// XDG_DESKTOP_DIR: '/Users/jonschlinkert/Desktop',
415-
// XDG_DOWNLOAD_DIR: '/Users/jonschlinkert/Downloads',
416-
// XDG_TEMPLATES_DIR: '/Users/jonschlinkert/Documents/Templates',
417-
// XDG_PUBLICSHARE_DIR: '/Users/jonschlinkert/Public',
418-
// XDG_DOCUMENTS_DIR: '/Users/jonschlinkert/Documents',
419-
// XDG_MUSIC_DIR: '/Users/jonschlinkert/Documents/Music',
420-
// XDG_PICTURES_DIR: '/Users/jonschlinkert/Documents/Pictures',
421-
// XDG_VIDEOS_DIR: '/Users/jonschlinkert/Documents/Videos'
422-
// }
423-
```
424-
425-
### [.userdirs.darwin()](lib/userdirs.js#L181)
426-
427-
Get the XDG User Directories for MacOS. This method is used by the main function when `process.platform` is `darwin`. Exposed as a method so you can call it directly if necessary. Also aliased as `userdirs.macos()`.
428-
429-
**Params**
430-
431-
* `options` **{Object}**
432-
* `returns` **{Object}**: Returns an object of paths.
433-
434-
**Example**
435-
436-
```js
437-
const { dirs, conf, defaults } = userdirs.darwin(); // or userdirs.macos();
438-
```
439-
440-
### [.userdirs.linux()](lib/userdirs.js#L209)
441-
442-
Gets the XDG User Directories for Linux. Used by the main export when `process.platform` is `linux`.
443-
444-
* `returns` **{Object}**: Returns an object of paths.
445-
* `returns` **{Object}**: Returns an object of paths.
446-
447-
**Example**
448-
449-
```js
450-
const { dirs, conf, defaults } = userdirs.linux();
451-
```
452-
453-
### [.userdirs.win32()](lib/userdirs.js#L235)
454-
455-
Gets the XDG User Directories for MacOS. Used by the `userdirs()` function when `process.platform` is `win32`. Also aliased as `userdirs.windows()`.
456-
457-
**Params**
458-
459-
* `options` **{Object}**
460-
* `returns` **{Object}**: Returns an object of paths.
461-
462-
**Example**
463-
464-
```js
465-
const { dirs, conf, defaults } = userdirs.win32(); // or userdirs.windows();
466-
```
467-
468310
## XDG Base Directories
469311

470312
This documentation is from the Arch Linux [XDG Base Directory](https://wiki.archlinux.org/index.php/XDG_Base_Directory) docs.
@@ -563,4 +405,4 @@ Released under the MIT License.
563405

564406
***
565407

566-
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on July 22, 2023._
408+
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on July 24, 2023._

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ xdg.macos = xdg.darwin;
200200
* Expose "user dirs"
201201
*/
202202

203-
xdg.userdirs = require('./lib/userdirs');
203+
xdg.userdirs = require('./lib/user-dirs');
204204

205205
/**
206206
* Expose "xdg"

lib/expand.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ const pico = require('picomatch');
99
const ini = require('ini');
1010
const toml = require('toml');
1111
const yaml = require('yaml');
12-
const userdirs = require('./userdirs');
12+
const userdirs = require('./user-dirs');
1313
const { dir, homedir, isObject, read, resolve } = require('./utils');
1414

1515
const readfile = config => (pathname, find = false) => {
1616
try {
17-
const fullpath = config.find && find ? config.find(pathname) : path.resolve(config.home, pathname);
17+
const fullpath = config.find && find
18+
? config.find(pathname)
19+
: path.resolve(config.home, pathname);
20+
1821
return read(fullpath);
1922
} catch (err) {
2023
if (err.code !== 'ENOENT') {
@@ -113,15 +116,9 @@ const writefile = dir => (pathname, ...args) => {
113116
};
114117

115118
const find = config => pathname => {
116-
// const cwd = config.parent?.cwd;
117-
118119
for (const dir of config.dirs) {
119120
const filepath = path.resolve(dir, pathname);
120121

121-
// if (dir === cwd) {
122-
// console.log({ dir, pathname, filepath });
123-
// }
124-
125122
if (fs.existsSync(filepath)) {
126123
return filepath;
127124
}

lib/userdirs.js renamed to lib/user-dirs.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ const userdirs = (options = {}) => {
2424
return fn(options);
2525
};
2626

27+
/**
28+
* Convenience methods
29+
*/
30+
31+
userdirs.home = (options = {}) => {
32+
return options.homedir || homedir(options.platform || process.platform);
33+
};
34+
2735
/**
2836
* Returns an object to with paths to `user-dirs.*` files, as well as functions to
2937
* load each file.
@@ -180,10 +188,20 @@ userdirs.create = (...args) => userdirs.expand(...args).create();
180188
userdirs.darwin = (options = {}) => {
181189
const env = options.env || process.env;
182190

183-
const etc = '/etc/xdg';
191+
// It seems that there is still considerable debate about whether to use
192+
// `Application Support` or `Preferences` for the home folder on MacOS.
193+
// This spec is for linux, so we'll continue to have these debates until
194+
// it's formalized for other OSes. For now, we'll use `Application Support`,
195+
// since that seems to be the most commonly agreed upon. If you disagree,
196+
// You can pass `homeFolder: 'Preferences'` on the options.
197+
const homeFolder = options.homeFolder || 'Application Support';
198+
199+
// Non-standard, but this gives us an escape hatch
200+
const etc = env.XDG_ETC || options.etc || '/etc/xdg';
201+
184202
const home = options.homedir || homedir('darwin');
185203
const lib = path.join(home, 'Library');
186-
const app = env.XDG_CONFIG_HOME || path.join(lib, 'Application Support');
204+
const app = env.XDG_CONFIG_HOME || path.join(lib, homeFolder);
187205

188206
return {
189207
conf: env.XDG_USER_DIRS_CONF || path.join(etc, 'user-dirs.conf'),
@@ -207,9 +225,11 @@ userdirs.darwin = (options = {}) => {
207225

208226
userdirs.linux = (options = {}) => {
209227
const env = options.env || process.env;
228+
229+
// Non-standard, but this gives us an escape hatch
230+
const etc = env.XDG_ETC || options.etc || '/etc/xdg';
210231
const home = options.homedir || homedir('linux');
211232
const config = env.XDG_CONFIG_HOME || path.join(home, '.config');
212-
const etc = '/etc/xdg';
213233

214234
return {
215235
conf: env.XDG_USER_DIRS_CONF || path.join(etc, 'user-dirs.conf'),
@@ -245,15 +265,6 @@ userdirs.win32 = (options = {}) => {
245265
};
246266
};
247267

248-
/**
249-
* Convenience methods
250-
*/
251-
252-
userdirs.home = (options = {}) => {
253-
const platform = options.platform || process.platform;
254-
return options.homedir || homedir(platform);
255-
};
256-
257268
/**
258269
* Expose "userdirs" API
259270
*/

0 commit comments

Comments
 (0)