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 5825c70 commit f7dd457
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
37 changes: 19 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand 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') {
Expand All @@ -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 {};
}

Expand All @@ -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') {
Expand Down Expand Up @@ -103,6 +102,8 @@ class Configstore {
clear() {
this.all = {};
}
}

module.exports = Configstore;
get path() {
return this._path;
}
}
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10.13"
"node": ">=12"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -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
}
}
11 changes: 6 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'));
Expand All @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit f7dd457

Please sign in to comment.