diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c1870cf..41fe626 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,6 @@ jobs: node-version: - 14 - 12 - - 10 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 diff --git a/index.js b/index.js index dde3928..3166654 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,23 @@ -'use strict'; -const path = require('path'); -const os = require('os'); -const fs = require('graceful-fs'); -const xdgBasedir = require('xdg-basedir'); -const writeFileAtomic = require('write-file-atomic'); -const dotProp = require('dot-prop'); -const uniqueString = require('unique-string'); - -const configDirectory = xdgBasedir.config || path.join(os.tmpdir(), uniqueString()); +import path from 'path'; +import os from 'os'; +import fs from 'graceful-fs'; +import {xdgConfig} from 'xdg-basedir'; +import writeFileAtomic from 'write-file-atomic'; +import dotProp from 'dot-prop'; +import uniqueString from 'unique-string'; + +const configDirectory = xdgConfig || path.join(os.tmpdir(), uniqueString()); const permissionError = 'You don\'t have access to this file.'; const mkdirOptions = {mode: 0o0700, recursive: true}; const writeFileOptions = {mode: 0o0600}; -class Configstore { +export default class Configstore { constructor(id, defaults, options = {}) { const pathPrefix = options.globalConfigPath ? path.join(id, 'config.json') : path.join('configstore', `${id}.json`); - this.path = options.configPath || path.join(configDirectory, pathPrefix); + this._path = options.configPath || path.join(configDirectory, pathPrefix); if (defaults) { this.all = { @@ -30,7 +29,7 @@ class Configstore { get all() { try { - return JSON.parse(fs.readFileSync(this.path, 'utf8')); + return JSON.parse(fs.readFileSync(this._path, 'utf8')); } catch (error) { // Create directory if it doesn't exist if (error.code === 'ENOENT') { @@ -44,7 +43,7 @@ class Configstore { // Empty the file if it encounters invalid JSON if (error.name === 'SyntaxError') { - writeFileAtomic.sync(this.path, '', writeFileOptions); + writeFileAtomic.sync(this._path, '', writeFileOptions); return {}; } @@ -55,9 +54,9 @@ class Configstore { set all(value) { try { // Make sure the folder exists as it could have been deleted in the meantime - fs.mkdirSync(path.dirname(this.path), mkdirOptions); + fs.mkdirSync(path.dirname(this._path), mkdirOptions); - writeFileAtomic.sync(this.path, JSON.stringify(value, undefined, '\t'), writeFileOptions); + writeFileAtomic.sync(this._path, JSON.stringify(value, undefined, '\t'), writeFileOptions); } catch (error) { // Improve the message of permission errors if (error.code === 'EACCES') { @@ -103,6 +102,8 @@ class Configstore { clear() { this.all = {}; } -} -module.exports = Configstore; + get path() { + return this._path; + } +} diff --git a/package.json b/package.json index e9ce1db..f660511 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=10.13" + "node": ">=12" }, "scripts": { "test": "xo && ava" @@ -33,14 +35,17 @@ "save" ], "dependencies": { - "dot-prop": "^5.2.0", - "graceful-fs": "^4.1.2", - "unique-string": "^2.0.0", - "write-file-atomic": "^3.0.0", - "xdg-basedir": "^4.0.0" + "dot-prop": "^6.0.1", + "graceful-fs": "^4.2.6", + "unique-string": "^3.0.0", + "write-file-atomic": "^3.0.3", + "xdg-basedir": "^5.0.1" }, "devDependencies": { - "ava": "^2.1.0", - "xo": "^0.24.0" + "ava": "^3.15.0", + "xo": "^0.38.2" + }, + "ava": { + "serial": true } } diff --git a/readme.md b/readme.md index f5e84bf..356ea1f 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ The config is stored in a JSON file located in `$XDG_CONFIG_HOME` or `~/.config` Example: `~/.config/configstore/some-id.json` *If you need this for Electron, check out [`electron-store`](https://github.com/sindresorhus/electron-store) instead.*\ -*And check out [`conf`](https://github.com/sindresorhus/conf) for an updated approach to this concept.* +*And check out [`conf`](https://github.com/sindresorhus/conf) for a more modern version of `configstore`.* ## Install @@ -17,10 +17,11 @@ $ npm install configstore ## Usage ```js -const Configstore = require('configstore'); -const packageJson = require('./package.json'); +import Configstore from 'configstore'; -// Create a Configstore instance +const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); + +// Create a Configstore instance. const config = new Configstore(packageJson.name, {foo: 'bar'}); console.log(config.get('foo')); @@ -30,7 +31,7 @@ config.set('awesome', true); console.log(config.get('awesome')); //=> true -// Use dot-notation to access nested properties +// Use dot-notation to access nested properties. config.set('bar.baz', true); console.log(config.get('bar')); //=> {baz: true} diff --git a/test.js b/test.js index 2007129..30c2f6a 100644 --- a/test.js +++ b/test.js @@ -1,8 +1,8 @@ import fs from 'fs'; import path from 'path'; import os from 'os'; -import {serial as test} from 'ava'; -import Configstore from '.'; +import test from 'ava'; +import Configstore from './index.js'; const configstorePath = new Configstore('configstore-test').path;