Skip to content

Commit

Permalink
Merge pull request #6 from LinusU/v2
Browse files Browse the repository at this point in the history
💥 Change API to return Promises
  • Loading branch information
LinusU authored Nov 16, 2019
2 parents 1be020a + f54f7ad commit 33f8299
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 128 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ node_js:
- 12
- 10
- 8
- 6
- 4
- 8.3.0
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `node-application-config`
# Node.js Application Config

Store your applications config where the operating system wants you to.

Expand All @@ -14,19 +14,13 @@ npm install --save application-config
var cfg = require('application-config')('App Name')

// Read the stored data
cfg.read(function (err, data) {

})
const data = await cfg.read()

// Write new config
cfg.write({ n: 1337 }, function (err) {

})
await cfg.write({ n: 1337 })

// Trash the stored config
cfg.trash(function (err) {

})
await cfg.trash()
```

## API
Expand All @@ -35,17 +29,17 @@ cfg.trash(function (err) {

Creates and return a new instance with the provided name.

### `cfg.read(cb)`
### `cfg.read()`

Read the stored configuration. Callback receives `(err, data)`.
Read the stored configuration. Returns a Promise that settles with the data.

### `cfg.write(data, cb)`
### `cfg.write(data)`

Write new configuration. Callback receives `(err)`.
Write new configuration. Returns a Promise.

### `cfg.trash(cb)`
### `cfg.trash()`

Remove the stored configuration. Callback receives `(err)`.
Remove the stored configuration. Returns a Promise.

### `cfg.filePath`

Expand Down
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare class ApplicationConfig {
filePath: string
read(): Promise<unknown>
write(data: object): Promise<void>
trash(): Promise<void>
}

declare function createApplicationConfig (mame: String): ApplicationConfig
export = createApplicationConfig
82 changes: 32 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,46 @@
var fs = require('fs')
var path = require('path')
var applicationConfigPath = require('application-config-path')
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')

function ApplicationConfig (name) {
this.filePath = path.join(applicationConfigPath(name), 'config.json')
}
const applicationConfigPath = require('application-config-path')
const loadJsonFile = require('load-json-file')

const rmdir = promisify(fs.rmdir)
const unlink = promisify(fs.unlink)

ApplicationConfig.prototype.read = function (cb) {
var self = this
fs.readFile(self.filePath, function (err, raw) {
if (err && err.code === 'ENOENT') return cb(null, {})
if (err) return cb(err)
class ApplicationConfig {
constructor (name) {
this.filePath = path.join(applicationConfigPath(name), 'config.json')
}

var data
async read () {
try {
data = JSON.parse(raw.toString())
return await loadJsonFile(this.filePath)
} catch (err) {
return cb(err)
if (err.code === 'ENOENT') return {}
throw err
}

cb(null, data)
})
}

ApplicationConfig.prototype.write = function (data, cb) {
var self = this
var mkdirp = require('mkdirp')
if (typeof data !== 'object' || data === null) {
throw new TypeError('data is not an object')
}
var directoryPath = path.dirname(self.filePath)
mkdirp(directoryPath, function (err) {
if (err) { return cb(err) }

var tempFilePath =
self.filePath + '-' +
Math.random().toString().substr(2) +
Date.now().toString() +
path.extname(self.filePath)

fs.writeFile(tempFilePath, JSON.stringify(data, null, 2), function (err) {
if (err) { return cb(err) }

fs.rename(tempFilePath, self.filePath, cb)
})
})
}
async write (data) {
if (typeof data !== 'object' || data === null) {
throw new TypeError('data is not an object')
}

ApplicationConfig.prototype.trash = function (cb) {
var self = this
fs.unlink(self.filePath, function (err) {
if (err && err.code !== 'ENOENT') return cb(err)
const writeJsonFile = require('write-json-file')

var directoryPath = path.dirname(self.filePath)
fs.rmdir(directoryPath, function (err) {
if (err && err.code !== 'ENOENT') return cb(err)
await writeJsonFile(this.filePath, data)
}

cb(null)
})
})
async trash () {
try {
await unlink(this.filePath)
await rmdir(path.dirname(this.filePath))
} catch (err) {
if (err.code === 'ENOENT') return
throw err
}
}
}

module.exports = function createApplicationConfig (name) {
Expand Down
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
"name": "application-config",
"version": "1.0.1",
"license": "MIT",
"author": "Linus Unnebäck <[email protected]>",
"main": "index.js",
"repository": {
"type": "git",
"url": "http://github.com/LinusU/node-application-config.git"
"repository": "LinusU/node-application-config",
"files": [
"index.d.ts",
"index.js"
],
"scripts": {
"test": "standard && mocha"
},
"dependencies": {
"application-config-path": "^0.1.0",
"mkdirp": "^0.5.1"
"load-json-file": "^6.2.0",
"write-json-file": "^4.2.0"
},
"devDependencies": {
"mocha": "^2.1.0",
"standard": "*"
"assert-rejects": "^1.0.0",
"mocha": "^6.2.2",
"standard": "^14.3.1"
},
"scripts": {
"test": "standard && mocha"
"engines": {
"node": ">=8.3"
}
}
73 changes: 23 additions & 50 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,41 @@
/* eslint-env mocha */

var lib = require('./')('linusu-test')
var fs = require('fs')
var assert = require('assert')
const lib = require('./')('linusu-test')
const fs = require('fs')
const assert = require('assert')
const assertRejects = require('assert-rejects')

var payload = { n: 1337 }
const payload = { n: 1337 }

describe('application-config', function () {
it('should write', function (done) {
lib.write(payload, done)
describe('application-config', () => {
it('should write', async () => {
await lib.write(payload)
})

it('should read', function (done) {
lib.read(function (err, data) {
assert.ifError(err)
assert.strictEqual(payload.n, data.n)

done()
})
it('should read', async () => {
assert.deepStrictEqual(await lib.read(), payload)
})

it('should export path to file', function (done) {
it('should export path to file', () => {
assert.strictEqual(typeof lib.filePath, 'string')

fs.readFile(lib.filePath, function (err, raw) {
assert.ifError(err)
assert.deepStrictEqual(JSON.parse(raw.toString()), payload)

done()
})
const raw = fs.readFileSync(lib.filePath)
assert.deepStrictEqual(JSON.parse(raw.toString()), payload)
})

it('should trash', function (done) {
lib.trash(done)
it('should trash', async () => {
await lib.trash()
})

it('should be gone', function (done) {
lib.read(function (err, data) {
assert.ifError(err)
assert.strictEqual(undefined, data.n)

done()
})
it('should be gone', async () => {
assert.deepStrictEqual(await lib.read(), {})
})

it('should throw', function () {
assert.throws(function () {
lib.write()
}, TypeError)

assert.throws(function () {
lib.write(null)
}, TypeError)

assert.throws(function () {
lib.write(undefined)
}, TypeError)

assert.throws(function () {
lib.write(1)
}, TypeError)

assert.throws(function () {
lib.write('test')
}, TypeError)
it('should throw', () => {
assertRejects(lib.write(), TypeError)
assertRejects(lib.write(null), TypeError)
assertRejects(lib.write(undefined), TypeError)
assertRejects(lib.write(1), TypeError)
assertRejects(lib.write('test'), TypeError)
})
})

0 comments on commit 33f8299

Please sign in to comment.