From 84c740e16cc7e502c8eb3775004a853fa46f07a8 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Tue, 19 Nov 2019 00:34:59 +0000 Subject: [PATCH 01/13] refactor: change source code to typescript --- .eslintrc | 4 + .gitignore | 2 + index.d.ts | 333 ------------------------------------ index.test-d.ts | 138 --------------- package.json | 46 ++++- index.js => source/index.ts | 177 ++++++++++++------- test.js => test/index.ts | 247 +++++++++++++------------- tsconfig.json | 13 ++ 8 files changed, 301 insertions(+), 659 deletions(-) create mode 100644 .eslintrc delete mode 100644 index.d.ts delete mode 100644 index.test-d.ts rename index.js => source/index.ts (72%) rename test.js => test/index.ts (81%) create mode 100644 tsconfig.json diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..399b89f --- /dev/null +++ b/.eslintrc @@ -0,0 +1,4 @@ +{ + "extends": ["xo", "xo-typescript"], + "parser": "@typescript-eslint/parser" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 239ecff..c5fb9e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules yarn.lock +.nyc_output +dist \ No newline at end of file diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index dc8eb80..0000000 --- a/index.d.ts +++ /dev/null @@ -1,333 +0,0 @@ -/// -import {JSONSchema} from 'json-schema-typed'; - -declare namespace Conf { - type Schema = JSONSchema; - - interface Options { - /** - Config used if there are no existing config. - - **Note:** The values in `defaults` will overwrite the `default` key in the `schema` option. - */ - readonly defaults?: Readonly; - - /** - [JSON Schema](https://json-schema.org) to validate your config data. - - Under the hood, the JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats). - - You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties). - - @example - ``` - import Conf = require('conf'); - - const schema = { - foo: { - type: 'number', - maximum: 100, - minimum: 1, - default: 50 - }, - bar: { - type: 'string', - format: 'url' - } - }; - - const config = new Conf({schema}); - - console.log(config.get('foo')); - //=> 50 - - config.set('foo', '1'); - // [Error: Config schema violation: `foo` should be number] - ``` - - **Note:** The `default` value will be overwritten by the `defaults` option if set. - */ - readonly schema?: {[P in keyof T]: Schema}; - - /** - Name of the config file (without extension). - - Useful if you need multiple config files for your app or module. For example, different config files between two major versions. - - @default 'config' - */ - readonly configName?: string; - - /** - You only need to specify this if you don't have a package.json file in your project or if it doesn't have a name defined within it. - - Default: The name field in the `package.json` closest to where `conf` is imported. - */ - readonly projectName?: string; - - /** - You only need to specify this if you don't have a package.json file in your project or if it doesn't have a version defined within it. - - Default: The name field in the `package.json` closest to where `conf` is imported. - */ - readonly projectVersion?: string; - - /* - _Don't use this feature until [this issue](https://github.com/sindresorhus/conf/issues/92) has been fixed._ - - You can use migrations to perform operations to the store whenever a version is changed. - - The `migrations` object should consist of a key-value pair of `'version': handler`. The `version` can also be a [semver range](https://github.com/npm/node-semver#ranges). - - @example - ``` - import Conf = require('conf'); - - const store = new Conf({ - migrations: { - '0.0.1': store => { - store.set('debugPhase', true); - }, - '1.0.0': store => { - store.delete('debugPhase'); - store.set('phase', '1.0.0'); - }, - '1.0.2': store => { - store.set('phase', '1.0.2'); - }, - '>=2.0.0': store => { - store.set('phase', '>=2.0.0'); - } - } - }); - ``` - */ - readonly migrations?: {[version: string]: (store: Conf) => void}; - - /** - __You most likely don't need this. Please don't use it unless you really have to.__ - - The only use-case I can think of is having the config located in the app directory or on some external storage. Default: System default user [config directory](https://github.com/sindresorhus/env-paths#pathsconfig). - */ - readonly cwd?: string; - - /** - Note that this is __not intended for security purposes__, since the encryption key would be easily found inside a plain-text Node.js app. - - Its main use is for obscurity. If a user looks through the config directory and finds the config file, since it's just a JSON file, they may be tempted to modify it. By providing an encryption key, the file will be obfuscated, which should hopefully deter any users from doing so. - - It also has the added bonus of ensuring the config file's integrity. If the file is changed in any way, the decryption will not work, in which case the store will just reset back to its default state. - - When specified, the store will be encrypted using the [`aes-256-cbc`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation) encryption algorithm. - */ - readonly encryptionKey?: string | Buffer | NodeJS.TypedArray | DataView; - - /** - Extension of the config file. - - You would usually not need this, but could be useful if you want to interact with a file with a custom file extension that can be associated with your app. These might be simple save/export/preference files that are intended to be shareable or saved outside of the app. - - @default 'json' - */ - readonly fileExtension?: string; - - /** - The config is cleared if reading the config file causes a `SyntaxError`. This is a good default, as the config file is not intended to be hand-edited, so it usually means the config is corrupt and there's nothing the user can do about it anyway. However, if you let the user edit the config file directly, mistakes might happen and it could be more useful to throw an error when the config is invalid instead of clearing. Disabling this option will make it throw a `SyntaxError` on invalid config instead of clearing. - - @default true - */ - readonly clearInvalidConfig?: boolean; - - /** - Function to serialize the config object to a UTF-8 string when writing the config file. - - You would usually not need this, but it could be useful if you want to use a format other than JSON. - - @default value => JSON.stringify(value, null, '\t') - */ - readonly serialize?: (value: T) => string; - - /** - Function to deserialize the config object from a UTF-8 string when reading the config file. - - You would usually not need this, but it could be useful if you want to use a format other than JSON. - - @default JSON.parse - */ - readonly deserialize?: (text: string) => T; - - /** - __You most likely don't need this. Please don't use it unless you really have to.__ - - Suffix appended to `projectName` during config file creation to avoid name conflicts with native apps. - - You can pass an empty string to remove the suffix. - - For example, on macOS, the config file will be stored in the `~/Library/Preferences/foo-nodejs` directory, where `foo` is the `projectName`. - - @default 'nodejs' - */ - readonly projectSuffix?: string; - - /** - Access nested properties by dot notation. - - @default true - - @example - ``` - const config = new Conf(); - - config.set({ - foo: { - bar: { - foobar: '🦄' - } - } - }); - - console.log(config.get('foo.bar.foobar')); - //=> '🦄' - ``` - - Alternatively, you can set this option to `false` so the whole string would be treated as one key. - - @example - ``` - const config = new Conf({accessPropertiesByDotNotation: false}); - - config.set({ - `foo.bar.foobar`: '🦄' - }); - - console.log(config.get('foo.bar.foobar')); - //=> '🦄' - ``` - - */ - readonly accessPropertiesByDotNotation?: boolean; - - /** - Watch for any changes in the config file and call the callback for `onDidChange` if set. This is useful if there are multiple processes changing the same config file. - - __Currently this option doesn't work on Node.js 8 on macOS.__ - - @default false - */ - readonly watch?: boolean; - } -} - -/** -Simple config handling for your app or module. -*/ -declare class Conf implements Iterable<[keyof T, T[keyof T]]> { - store: T; - readonly path: string; - readonly size: number; - - /** - Changes are written to disk atomically, so if the process crashes during a write, it will not corrupt the existing config. - - @example - ``` - import Conf = require('conf'); - - type StoreType = { - isRainbow: boolean, - unicorn?: string - } - - const config = new Conf({ - defaults: { - isRainbow: true - } - }); - - config.get('isRainbow'); - //=> true - - config.set('unicorn', '🦄'); - console.log(config.get('unicorn')); - //=> '🦄' - - config.delete('unicorn'); - console.log(config.get('unicorn')); - //=> undefined - ``` - */ - constructor(options?: Conf.Options); - - /** - Set an item. - - @param key - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. - @param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`. - */ - set(key: K, value: T[K]): void; - - /** - Set multiple items at once. - - @param object - A hashmap of items to set at once. - */ - set(object: Partial): void; - - /** - Get an item. - - @param key - The key of the item to get. - @param defaultValue - The default value if the item does not exist. - */ - get(key: K, defaultValue?: T[K]): T[K]; - - /** - Reset items to their default values, as defined by the `defaults` or `schema` option. - - @param keys - The keys of the items to reset. - */ - reset(...keys: K[]): void; - - /** - Check if an item exists. - - @param key - The key of the item to check. - */ - has(key: K): boolean; - - /** - Delete an item. - - @param key - The key of the item to delete. - */ - delete(key: K): void; - - /** - Delete all items. - */ - clear(): void; - - /** - Watches the given `key`, calling `callback` on any changes. - - @param key - The key wo watch. - @param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`. - */ - onDidChange( - key: K, - callback: (newValue?: T[K], oldValue?: T[K]) => void - ): () => void; - - /** - Watches the whole config object, calling `callback` on any changes. - - @param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`. - */ - onDidAnyChange( - callback: (newValue?: Readonly, oldValue?: Readonly) => void - ): () => void; - - [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]>; -} - -export = Conf; diff --git a/index.test-d.ts b/index.test-d.ts deleted file mode 100644 index 7e930fd..0000000 --- a/index.test-d.ts +++ /dev/null @@ -1,138 +0,0 @@ -import {expectType, expectError} from 'tsd'; -import Conf = require('.'); - -type UnicornFoo = { - foo: string; - unicorn: boolean; - hello?: number; -}; - -const conf = new Conf(); -new Conf({ - defaults: { - foo: 'bar', - unicorn: false - } -}); -new Conf({configName: ''}); -new Conf({projectName: 'foo'}); -new Conf({cwd: ''}); -new Conf({encryptionKey: ''}); -new Conf({encryptionKey: new Buffer('')}); -new Conf({encryptionKey: new Uint8Array([1])}); -new Conf({encryptionKey: new DataView(new ArrayBuffer(2))}); -new Conf({fileExtension: '.foo'}); -new Conf({clearInvalidConfig: false}); -new Conf({serialize: value => 'foo'}); -new Conf({deserialize: string => ({foo: 'foo', unicorn: true})}); -new Conf({projectSuffix: 'foo'}); -new Conf({watch: true}); - -new Conf({ - schema: { - foo: { - type: 'string', - default: 'foobar' - }, - unicorn: { - type: 'boolean' - }, - hello: { - type: 'number' - } - } -}); - -expectError( - new Conf({ - schema: { - foo: { - type: 'nope' - }, - unicorn: { - type: 'nope' - }, - hello: { - type: 'nope' - } - } - }) -); - -conf.set('hello', 1); -conf.set('unicorn', false); -conf.set({foo: 'nope'}); - -expectType(conf.get('foo')); -expectType(conf.reset('foo', 'unicorn')); -expectType(conf.get('foo', 'bar')); -conf.delete('foo'); -expectType(conf.has('foo')); -conf.clear(); -const off = conf.onDidChange('foo', (oldValue, newValue) => { - expectType(oldValue); - expectType(newValue); -}); - -expectType<() => void>(off); -off(); - -conf.store = { - foo: 'bar', - unicorn: false -}; -expectType(conf.path); -expectType(conf.size); - -expectType>( - conf[Symbol.iterator]() -); -for (const [key, value] of conf) { - expectType(key); - expectType(value); -} - - -// -- Docs examples -- - -type StoreType = { - isRainbow: boolean, - unicorn?: string -} - -const config = new Conf({ - defaults: { - isRainbow: true - } -}); - -config.get('isRainbow'); -//=> true - -config.set('unicorn', '🦄'); -console.log(config.get('unicorn')); -//=> '🦄' - -config.delete('unicorn'); -console.log(config.get('unicorn')); -//=> undefined - -// -- - - -// -- Migrations -- -const store = new Conf({ - migrations: { - '0.0.1': store => { - store.set('debug phase', true); - }, - '1.0.0': store => { - store.delete('debug phase'); - store.set('phase', '1.0'); - }, - '1.0.2': store => { - store.set('phase', '>1.0'); - } - } -}); -// -- diff --git a/package.json b/package.json index b12085c..e630017 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Simple config handling for your app or module", "license": "MIT", "repository": "sindresorhus/conf", + "main": "dist/index.js", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", @@ -13,12 +14,15 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava && tsd" + "build": "del dist && tsc", + "test": "xo && npm run build && nyc ava", + "bench": "ts-node bench.ts", + "prepublishOnly": "npm run build" }, "files": [ - "index.js", - "index.d.ts" + "dist" ], + "types": "dist", "keywords": [ "config", "store", @@ -38,6 +42,32 @@ "write", "cache" ], + "ava": { + "babel": false, + "compileEnhancements": false, + "extensions": [ + "ts" + ], + "require": [ + "ts-node/register" + ] + }, + "xo": { + "extends": "xo-typescript", + "extensions": [ + "ts" + ], + "rules": { + "import/first": "off", + "import/newline-after-import": "off", + "@typescript-eslint/member-ordering": "off" + } + }, + "nyc": { + "extension": [ + ".ts" + ] + }, "dependencies": { "ajv": "^6.10.2", "debounce-fn": "^3.0.1", @@ -51,14 +81,24 @@ "write-file-atomic": "^3.0.0" }, "devDependencies": { + "@sindresorhus/tsconfig": "^0.4.0", "@types/node": "^12.7.4", + "@types/semver": "^6.2.0", + "@types/write-file-atomic": "^2.1.2", + "@typescript-eslint/eslint-plugin": "^1.11.0", + "@typescript-eslint/parser": "^1.11.0", "ava": "^2.3.0", "clear-module": "^4.0.0", "del": "^5.1.0", + "del-cli": "^3.0.0", "delay": "^4.3.0", + "eslint-config-xo-typescript": "^0.15.0", + "nyc": "^14.1.1", "p-event": "^4.1.0", "tempy": "^0.3.0", + "ts-node": "^8.3.0", "tsd": "^0.7.4", + "typescript": "^3.7.2", "xo": "^0.24.0" } } diff --git a/index.js b/source/index.ts similarity index 72% rename from index.js rename to source/index.ts index c4daca9..3d9054f 100644 --- a/index.js +++ b/source/index.ts @@ -1,28 +1,26 @@ -/* eslint-disable node/no-deprecated-api */ -'use strict'; -const fs = require('fs'); -const path = require('path'); -const crypto = require('crypto'); -const assert = require('assert'); -const EventEmitter = require('events'); -const dotProp = require('dot-prop'); -const makeDir = require('make-dir'); -const pkgUp = require('pkg-up'); -const envPaths = require('env-paths'); -const writeFileAtomic = require('write-file-atomic'); -const Ajv = require('ajv'); -const debounceFn = require('debounce-fn'); -const semver = require('semver'); -const onetime = require('onetime'); - -const plainObject = () => Object.create(null); +import fs = require('fs'); +import path = require('path'); +import crypto = require('crypto'); +import assert = require('assert'); +import EventEmitter = require('events'); +import dotProp = require('dot-prop'); +import makeDir = require('make-dir'); +import pkgUp = require('pkg-up'); +import envPaths = require('env-paths'); +import writeFileAtomic = require('write-file-atomic'); +import Ajv = require('ajv'); +import debounceFn = require('debounce-fn'); +import semver = require('semver'); +import onetime = require('onetime'); + +const plainObject: () => object = () => Object.create(null); const encryptionAlgorithm = 'aes-256-cbc'; // Prevent caching of this module so module.parent is always accurate delete require.cache[__filename]; const parentDir = path.dirname((module.parent && module.parent.filename) || '.'); -const checkValueType = (key, value) => { +const checkValueType = (key: string, value: StoreValue): void => { const nonJsonTypes = [ 'undefined', 'symbol', @@ -39,21 +37,67 @@ const checkValueType = (key, value) => { const INTERNAL_KEY = '__internal__'; const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`; -class Conf { - constructor(options) { +type ConfSerializer = (args: any) => ArrayBuffer | Buffer | string; +type ConfDeserializer = (args: any) => any; +type ConfDefaultValues = { + [key: string]: object; +}; +type ConfMigrations = { + [key: string]: (store: Conf) => void; +}; +type GenericCallback = () => any; +type GenericVoidCallback = (...args: any) => void; +type StoreValue = any; + +type ConfOptions = { + accessPropertiesByDotNotation?: boolean; + clearInvalidConfig?: boolean; + configName?: string; + cwd?: string; + defaults?: object; + encryptionKey?: string; + fileExtension?: string; + migrations?: ConfMigrations; + projectName?: string; + projectSuffix?: string; + projectVersion?: string; + schema?: object; + watch?: boolean; + serialize?: ConfSerializer; + deserialize?: ConfDeserializer; +}; + +export default class Conf { + _options: ConfOptions; + + _defaultValues: ConfDefaultValues = {}; + + _validator?: Ajv.ValidateFunction; + + encryptionKey?: string; + + events?: EventEmitter; + + serialize?: ConfSerializer; + + deserialize?: ConfDeserializer; + + path: string; + + constructor(options?: ConfOptions) { options = { configName: 'config', fileExtension: 'json', projectSuffix: 'nodejs', clearInvalidConfig: true, - serialize: value => JSON.stringify(value, null, '\t'), + serialize: (value: object) => JSON.stringify(value, null, '\t'), deserialize: JSON.parse, accessPropertiesByDotNotation: true, ...options }; const getPackageData = onetime(() => { - const packagePath = pkgUp.sync(parentDir); + const packagePath = pkgUp.sync({cwd: parentDir}); // Can't use `require` because of Webpack being annoying: // https://github.com/webpack/webpack/issues/196 const packageData = packagePath && JSON.parse(fs.readFileSync(packagePath, 'utf8')); @@ -74,7 +118,6 @@ class Conf { } this._options = options; - this._defaultValues = {}; if (options.schema) { if (typeof options.schema !== 'object') { @@ -142,27 +185,33 @@ class Conf { } } - _validate(data) { + _validate(data: any): boolean { if (!this._validator) { - return; + return false; } const valid = this._validator(data); - if (!valid) { - const errors = this._validator.errors.reduce((error, {dataPath, message}) => - error + ` \`${dataPath.slice(1)}\` ${message};`, ''); - throw new Error('Config schema violation:' + errors.slice(0, -1)); + if (valid) { + return true; } + + if (!this._validator.errors) { + return false; + } + + const errors = this._validator.errors.reduce((error, {dataPath, message}) => + error + ` \`${dataPath.slice(1)}\` ${message};`, ''); + throw new Error('Config schema violation:' + errors.slice(0, -1)); } - _ensureDirectory() { + _ensureDirectory(): void { // TODO: Use `fs.mkdirSync` `recursive` option when targeting Node.js 12. // Ensure the directory exists as it could have been deleted in the meantime. makeDir.sync(path.dirname(this.path)); } - _write(value) { - let data = this.serialize(value); + _write(value: StoreValue): void { + let data: any = this.serialize && this.serialize(value); if (this.encryptionKey) { const initializationVector = crypto.randomBytes(16); @@ -180,7 +229,7 @@ class Conf { } } - _watch() { + _watch(): void { this._ensureDirectory(); if (!fs.existsSync(this.path)) { @@ -189,11 +238,11 @@ class Conf { fs.watch(this.path, {persistent: false}, debounceFn(() => { // On Linux and Windows, writing to the config file emits a `rename` event, so we skip checking the event type. - this.events.emit('change'); + this.events?.emit('change'); }, {wait: 100})); } - _migrate(migrations, versionToMigrate) { + _migrate(migrations: ConfMigrations, versionToMigrate: string): void { let previousMigratedVersion = this._get(MIGRATION_KEY, '0.0.0'); const newerVersions = Object.keys(migrations) @@ -224,7 +273,7 @@ class Conf { } } - _containsReservedKey(key) { + _containsReservedKey(key: string): boolean { if (typeof key === 'object') { const firstKey = Object.keys(key)[0]; @@ -248,11 +297,11 @@ class Conf { return false; } - _isVersionInRangeFormat(version) { + _isVersionInRangeFormat(version: string): boolean { return semver.clean(version) === null; } - _shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate) { + _shouldPerformMigration(candidateVersion: string, previousMigratedVersion: string, versionToMigrate: string): boolean { if (this._isVersionInRangeFormat(candidateVersion)) { if (previousMigratedVersion !== '0.0.0' && semver.satisfies(previousMigratedVersion, candidateVersion)) { return false; @@ -272,18 +321,18 @@ class Conf { return true; } - _get(key, defaultValue) { + _get(key: string, defaultValue?: StoreValue): any { return dotProp.get(this.store, key, defaultValue); } - _set(key, value) { + _set(key: string, value?: StoreValue): void { const {store} = this; dotProp.set(store, key, value); this.store = store; } - get(key, defaultValue) { + get(key: string, defaultValue?: any): any { if (this._options.accessPropertiesByDotNotation) { return dotProp.get(this.store, key, defaultValue); } @@ -291,7 +340,7 @@ class Conf { return key in this.store ? this.store[key] : defaultValue; } - set(key, value) { + set(key: any, value?: StoreValue): void { if (typeof key !== 'string' && typeof key !== 'object') { throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`); } @@ -306,7 +355,7 @@ class Conf { const {store} = this; - const set = (key, value) => { + const set = (key: string, value: StoreValue): void => { checkValueType(key, value); if (this._options.accessPropertiesByDotNotation) { dotProp.set(store, key, value); @@ -327,7 +376,7 @@ class Conf { this.store = store; } - has(key) { + has(key: string): boolean { if (this._options.accessPropertiesByDotNotation) { return dotProp.has(this.store, key); } @@ -335,7 +384,7 @@ class Conf { return key in this.store; } - reset(...keys) { + reset(...keys: any): void { for (const key of keys) { if (this._defaultValues[key]) { this.set(key, this._defaultValues[key]); @@ -343,7 +392,7 @@ class Conf { } } - delete(key) { + delete(key: string): void { const {store} = this; if (this._options.accessPropertiesByDotNotation) { dotProp.delete(store, key); @@ -354,11 +403,11 @@ class Conf { this.store = store; } - clear() { + clear(): void{ this.store = plainObject(); } - onDidChange(key, callback) { + onDidChange(key: string, callback: GenericVoidCallback): GenericCallback { if (typeof key !== 'string') { throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof key}`); } @@ -367,25 +416,25 @@ class Conf { throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); } - const getter = () => this.get(key); + const getter: GenericCallback = () => this.get(key); return this.handleChange(getter, callback); } - onDidAnyChange(callback) { + onDidAnyChange(callback: GenericCallback): GenericCallback { if (typeof callback !== 'function') { throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); } - const getter = () => this.store; + const getter: GenericCallback = () => this.store; return this.handleChange(getter, callback); } - handleChange(getter, callback) { + handleChange(getter: GenericCallback, callback: GenericVoidCallback): () => void { let currentValue = getter(); - const onChange = () => { + const onChange: GenericCallback = () => { const oldValue = currentValue; const newValue = getter(); @@ -398,17 +447,17 @@ class Conf { } }; - this.events.on('change', onChange); - return () => this.events.removeListener('change', onChange); + this.events?.on('change', onChange); + return () => this.events?.removeListener('change', onChange); } - get size() { + get size(): number { return Object.keys(this.store).length; } - get store() { + get store(): StoreValue { try { - let data = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8'); + let data: any = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8'); if (this.encryptionKey) { try { @@ -425,7 +474,7 @@ class Conf { } catch (_) {} } - data = this.deserialize(data); + data = this.deserialize && this.deserialize(data); this._validate(data); return Object.assign(plainObject(), data); } catch (error) { @@ -442,20 +491,18 @@ class Conf { } } - set store(value) { + set store(value: StoreValue) { this._ensureDirectory(); this._validate(value); this._write(value); - this.events.emit('change'); + this.events?.emit('change'); } - * [Symbol.iterator]() { + * [Symbol.iterator](): any { for (const [key, value] of Object.entries(this.store)) { yield [key, value]; } } } - -module.exports = Conf; diff --git a/test.js b/test/index.ts similarity index 81% rename from test.js rename to test/index.ts index 2682ad1..abfceef 100644 --- a/test.js +++ b/test/index.ts @@ -1,36 +1,38 @@ -import fs from 'fs'; -import path from 'path'; -import {serial as test} from 'ava'; -import tempy from 'tempy'; -import del from 'del'; -import pkgUp from 'pkg-up'; -import clearModule from 'clear-module'; -import pEvent from 'p-event'; -import delay from 'delay'; -import Conf from '.'; +/* eslint-disable no-new */ +/* eslint-disable ava/no-ignored-test-files */ +import fs = require('fs'); +import path = require('path'); +import tempy = require('tempy'); +import del = require('del'); +import pkgUp = require('pkg-up'); +import clearModule = require('clear-module'); +import pEvent = require('p-event'); +import delay = require('delay'); +import test from 'ava'; +import Conf from '../source'; const fixture = '🦄'; -test.beforeEach(t => { +test.beforeEach((t: any) => { t.context.config = new Conf({cwd: tempy.directory()}); t.context.configWithoutDotNotation = new Conf({cwd: tempy.directory(), accessPropertiesByDotNotation: false}); }); -test('.get()', t => { +test('.get()', (t: any) => { t.is(t.context.config.get('foo'), undefined); t.is(t.context.config.get('foo', '🐴'), '🐴'); t.context.config.set('foo', fixture); t.is(t.context.config.get('foo'), fixture); }); -test('.set()', t => { +test('.set()', (t: any) => { t.context.config.set('foo', fixture); t.context.config.set('baz.boo', fixture); t.is(t.context.config.get('foo'), fixture); t.is(t.context.config.get('baz.boo'), fixture); }); -test('.set() - with object', t => { +test('.set() - with object', (t: any) => { t.context.config.set({ foo1: 'bar1', foo2: 'bar2', @@ -49,13 +51,13 @@ test('.set() - with object', t => { t.is(t.context.config.get('baz.foo.bar'), 'baz'); }); -test('.set() - with undefined', t => { +test('.set() - with undefined', (t: any) => { t.throws(() => { t.context.config.set('foo', undefined); }, 'Use `delete()` to clear values'); }); -test('.set() - with unsupported values', t => { +test('.set() - with unsupported values', (t: any) => { t.throws(() => { t.context.config.set('a', () => {}); }, /not supported by JSON/); @@ -83,13 +85,13 @@ test('.set() - with unsupported values', t => { }, /not supported by JSON/); }); -test('.set() - invalid key', t => { +test('.set() - invalid key', (t: any) => { t.throws(() => { t.context.config.set(1, 'unicorn'); }, 'Expected `key` to be of type `string` or `object`, got number'); }); -test('.has()', t => { +test('.has()', (t: any) => { t.context.config.set('foo', fixture); t.context.config.set('baz.boo', fixture); t.true(t.context.config.has('foo')); @@ -97,7 +99,7 @@ test('.has()', t => { t.false(t.context.config.has('missing')); }); -test('.reset()', t => { +test('.reset()', (t: any) => { t.context.configWithSchema = new Conf({ cwd: tempy.directory(), schema: { @@ -131,7 +133,7 @@ test('.reset()', t => { t.is(t.context.configWithDefaults.get('bar'), 99); }); -test('.delete()', t => { +test('.delete()', (t: any) => { const {config} = t.context; config.set('foo', 'bar'); config.set('baz.boo', true); @@ -148,7 +150,7 @@ test('.delete()', t => { t.is(config.get('foo.bar.zoo.awesome'), 'redpanda'); }); -test('.clear()', t => { +test('.clear()', (t: any) => { t.context.config.set('foo', 'bar'); t.context.config.set('foo1', 'bar1'); t.context.config.set('baz.boo', true); @@ -156,12 +158,12 @@ test('.clear()', t => { t.is(t.context.config.size, 0); }); -test('.size', t => { +test('.size', (t: any) => { t.context.config.set('foo', 'bar'); t.is(t.context.config.size, 1); }); -test('.store', t => { +test('.store', (t: any) => { t.context.config.set('foo', 'bar'); t.context.config.set('baz.boo', true); t.deepEqual(t.context.config.store, { @@ -172,7 +174,7 @@ test('.store', t => { }); }); -test('`defaults` option', t => { +test('`defaults` option', (t: any) => { const config = new Conf({ cwd: tempy.directory(), defaults: { @@ -183,7 +185,7 @@ test('`defaults` option', t => { t.is(config.get('foo'), 'bar'); }); -test('`configName` option', t => { +test('`configName` option', (t: any) => { const configName = 'alt-config'; const config = new Conf({ cwd: tempy.directory(), @@ -195,12 +197,12 @@ test('`configName` option', t => { t.is(path.basename(config.path, '.json'), configName); }); -test('no `suffix` option', t => { +test('no `suffix` option', (t: any) => { const config = new Conf(); t.true(config.path.includes('-nodejs')); }); -test('with `suffix` option set to empty string', t => { +test('with `suffix` option set to empty string', (t: any) => { const projectSuffix = ''; const projectName = 'conf-temp1-project'; const config = new Conf({projectSuffix, projectName}); @@ -209,7 +211,7 @@ test('with `suffix` option set to empty string', t => { t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length); }); -test('with `projectSuffix` option set to non-empty string', t => { +test('with `projectSuffix` option set to non-empty string', (t: any) => { const projectSuffix = 'new-projectSuffix'; const projectName = 'conf-temp2-project'; const config = new Conf({projectSuffix, projectName}); @@ -219,7 +221,7 @@ test('with `projectSuffix` option set to non-empty string', t => { t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length); }); -test('`fileExtension` option', t => { +test('`fileExtension` option', (t: any) => { const fileExtension = 'alt-ext'; const config = new Conf({ cwd: tempy.directory(), @@ -231,7 +233,7 @@ test('`fileExtension` option', t => { t.is(path.extname(config.path), `.${fileExtension}`); }); -test('`fileExtension` option = empty string', t => { +test('`fileExtension` option = empty string', (t: any) => { const configName = 'unicorn'; const config = new Conf({ cwd: tempy.directory(), @@ -241,16 +243,16 @@ test('`fileExtension` option = empty string', t => { t.is(path.basename(config.path), configName); }); -test('`serialize` and `deserialize` options', t => { +test('`serialize` and `deserialize` options', (t: any) => { t.plan(4); const serialized = `foo:${fixture}`; const deserialized = {foo: fixture}; - const serialize = value => { + const serialize = (value: any): string => { t.is(value, deserialized); return serialized; }; - const deserialize = value => { + const deserialize = (value: any): any => { t.is(value, serialized); return deserialized; }; @@ -265,7 +267,7 @@ test('`serialize` and `deserialize` options', t => { t.deepEqual(config.store, deserialized); }); -test('`projectName` option', t => { +test('`projectName` option', (t: any) => { const projectName = 'conf-fixture-project-name'; const config = new Conf({projectName}); t.is(config.get('foo'), undefined); @@ -275,7 +277,7 @@ test('`projectName` option', t => { del.sync(config.path, {force: true}); }); -test('ensure `.store` is always an object', t => { +test('ensure `.store` is always an object', (t: any) => { const cwd = tempy.directory(); const config = new Conf({cwd}); @@ -286,7 +288,7 @@ test('ensure `.store` is always an object', t => { }); }); -test('instance is iterable', t => { +test('instance is iterable', (t: any) => { t.context.config.set({ foo: fixture, bar: fixture @@ -297,7 +299,7 @@ test('instance is iterable', t => { ); }); -test('automatic `projectName` inference', t => { +test('automatic `projectName` inference', (t: any) => { const config = new Conf(); config.set('foo', fixture); t.is(config.get('foo'), fixture); @@ -305,10 +307,10 @@ test('automatic `projectName` inference', t => { del.sync(config.path, {force: true}); }); -test('`cwd` option overrides `projectName` option', t => { +test('`cwd` option overrides `projectName` option', (t: any) => { const cwd = tempy.directory(); - let config; + let config: any; t.notThrows(() => { config = new Conf({cwd, projectName: ''}); }); @@ -320,11 +322,11 @@ test('`cwd` option overrides `projectName` option', t => { del.sync(config.path, {force: true}); }); -test('safely handle missing package.json', t => { +test('safely handle missing package.json', (t: any) => { const pkgUpSyncOrig = pkgUp.sync; pkgUp.sync = () => null; - let config; + let config: any; t.notThrows(() => { config = new Conf({projectName: 'conf-fixture-project-name'}); }); @@ -333,11 +335,11 @@ test('safely handle missing package.json', t => { pkgUp.sync = pkgUpSyncOrig; }); -test('handle `cwd` being set and `projectName` not being set', t => { +test('handle `cwd` being set and `projectName` not being set', (t: any) => { const pkgUpSyncOrig = pkgUp.sync; pkgUp.sync = () => null; - let config; + let config: any; t.notThrows(() => { config = new Conf({cwd: 'conf-fixture-cwd'}); }); @@ -347,14 +349,15 @@ test('handle `cwd` being set and `projectName` not being set', t => { }); // See #11 -test('fallback to cwd if `module.filename` is `null`', t => { - const preservedFilename = module.filename; - module.filename = null; +test('fallback to cwd if `module.filename` is `null`', (t: any) => { + const preservedFilename: string = module.filename; + const filename: any = null; + module.filename = filename; clearModule('.'); - let config; + let config: any; t.notThrows(() => { - const Conf = require('.'); + const Conf = require('../dist').default; config = new Conf({cwd: 'conf-fixture-fallback-module-filename-null'}); }); @@ -362,7 +365,7 @@ test('fallback to cwd if `module.filename` is `null`', t => { del.sync(path.dirname(config.path)); }); -test('encryption', t => { +test('encryption', (t: any) => { const config = new Conf({cwd: tempy.directory(), encryptionKey: 'abc123'}); t.is(config.get('foo'), undefined); t.is(config.get('foo', '🐴'), '🐴'); @@ -372,7 +375,7 @@ test('encryption', t => { t.is(config.get('baz.boo'), fixture); }); -test('encryption - upgrade', t => { +test('encryption - upgrade', (t: any) => { const cwd = tempy.directory(); const before = new Conf({cwd}); @@ -383,7 +386,7 @@ test('encryption - upgrade', t => { t.is(after.get('foo'), fixture); }); -test('encryption - corrupt file', t => { +test('encryption - corrupt file', (t: any) => { const cwd = tempy.directory(); const before = new Conf({cwd, encryptionKey: 'abc123'}); @@ -396,7 +399,7 @@ test('encryption - corrupt file', t => { t.is(after.get('foo'), undefined); }); -test('decryption - migration to initialization vector', t => { +test('decryption - migration to initialization vector', (t: any) => { // The `test/config-encrypted-with-conf-4-1-0.json` file contains `{"unicorn": "🦄"}` JSON data which is encrypted with conf@4.1.0 and password `abcd1234` const config = new Conf({ cwd: 'test', @@ -407,17 +410,17 @@ test('decryption - migration to initialization vector', t => { t.deepEqual(config.store, {unicorn: '🦄'}); }); -test('onDidChange()', t => { +test('onDidChange()', (t: any) => { const {config} = t.context; t.plan(8); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: any, oldValue: any): void => { t.is(newValue, '🐴'); t.is(oldValue, fixture); }; - const checkBaz = (newValue, oldValue) => { + const checkBaz = (newValue: any, oldValue: any): void => { t.is(newValue, '🐴'); t.is(oldValue, fixture); }; @@ -434,12 +437,12 @@ test('onDidChange()', t => { unsubscribe(); config.set('baz.boo', fixture); - const checkUndefined = (newValue, oldValue) => { + const checkUndefined = (newValue: any, oldValue: any): void => { t.is(oldValue, fixture); t.is(newValue, undefined); }; - const checkSet = (newValue, oldValue) => { + const checkSet = (newValue: any, oldValue: any): void => { t.is(oldValue, undefined); t.is(newValue, '🐴'); }; @@ -453,17 +456,17 @@ test('onDidChange()', t => { config.set('foo', fixture); }); -test('onDidAnyChange()', t => { +test('onDidAnyChange()', (t: any) => { const {config} = t.context; t.plan(8); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: any, oldValue: any): void => { t.deepEqual(newValue, {foo: '🐴'}); t.deepEqual(oldValue, {foo: fixture}); }; - const checkBaz = (newValue, oldValue) => { + const checkBaz = (newValue: any, oldValue: any): void => { t.deepEqual(newValue, { foo: fixture, baz: {boo: '🐴'} @@ -486,7 +489,7 @@ test('onDidAnyChange()', t => { unsubscribe(); config.set('baz.boo', fixture); - const checkUndefined = (newValue, oldValue) => { + const checkUndefined = (newValue: any, oldValue: any): void => { t.deepEqual(oldValue, { foo: '🦄', baz: {boo: '🦄'} @@ -497,7 +500,7 @@ test('onDidAnyChange()', t => { }); }; - const checkSet = (newValue, oldValue) => { + const checkSet = (newValue: any, oldValue: any): void => { t.deepEqual(oldValue, { baz: {boo: fixture} }); @@ -518,7 +521,7 @@ test('onDidAnyChange()', t => { }); // See #32 -test('doesn\'t write to disk upon instanciation if and only if the store didn\'t change', t => { +test('doesn\'t write to disk upon instanciation if and only if the store didn\'t change', (t: any) => { let exists = fs.existsSync(t.context.config.path); t.is(exists, false); @@ -532,7 +535,7 @@ test('doesn\'t write to disk upon instanciation if and only if the store didn\'t t.is(exists, true); }); -test('`clearInvalidConfig` option - invalid data', t => { +test('`clearInvalidConfig` option - invalid data', (t: any) => { const config = new Conf({cwd: tempy.directory(), clearInvalidConfig: false}); fs.writeFileSync(config.path, '🦄'); @@ -541,20 +544,20 @@ test('`clearInvalidConfig` option - invalid data', t => { }, {instanceOf: SyntaxError}); }); -test('`clearInvalidConfig` option - valid data', t => { +test('`clearInvalidConfig` option - valid data', (t: any) => { const config = new Conf({cwd: tempy.directory(), clearInvalidConfig: false}); config.set('foo', 'bar'); t.deepEqual(config.store, {foo: 'bar'}); }); -test('schema - should be an object', t => { - const schema = 'object'; +test('schema - should be an object', (t: any) => { + const schema: any = 'object'; t.throws(() => { - new Conf({cwd: tempy.directory(), schema}); // eslint-disable-line no-new + new Conf({cwd: tempy.directory(), schema}); }, 'The `schema` option must be an object.'); }); -test('schema - valid set', t => { +test('schema - valid set', (t: any) => { const schema = { foo: { type: 'object', @@ -575,7 +578,7 @@ test('schema - valid set', t => { }); }); -test('schema - one violation', t => { +test('schema - one violation', (t: any) => { const schema = { foo: { type: 'string' @@ -587,7 +590,7 @@ test('schema - one violation', t => { }, 'Config schema violation: `foo` should be string'); }); -test('schema - multiple violations', t => { +test('schema - multiple violations', (t: any) => { const schema = { foo: { type: 'object', @@ -608,7 +611,7 @@ test('schema - multiple violations', t => { }, 'Config schema violation: `foo.bar` should be number; `foo.foobar` should be <= 100'); }); -test('schema - complex schema', t => { +test('schema - complex schema', (t: any) => { const schema = { foo: { type: 'string', @@ -633,7 +636,7 @@ test('schema - complex schema', t => { }, 'Config schema violation: `bar` should NOT have more than 3 items; `bar[3]` should be integer; `bar` should NOT have duplicate items (items ## 1 and 0 are identical)'); }); -test('schema - invalid write to config file', t => { +test('schema - invalid write to config file', (t: any) => { const schema = { foo: { type: 'string' @@ -648,7 +651,7 @@ test('schema - invalid write to config file', t => { }, 'Config schema violation: `foo` should be string'); }); -test('schema - default', t => { +test('schema - default', (t: any) => { const schema = { foo: { type: 'string', @@ -662,7 +665,7 @@ test('schema - default', t => { t.is(config.get('foo'), 'bar'); }); -test('schema - Conf defaults overwrites schema default', t => { +test('schema - Conf defaults overwrites schema default', (t: any) => { const schema = { foo: { type: 'string', @@ -679,14 +682,14 @@ test('schema - Conf defaults overwrites schema default', t => { t.is(config.get('foo'), 'foo'); }); -test('schema - validate Conf default', t => { +test('schema - validate Conf default', (t: any) => { const schema = { foo: { type: 'string' } }; t.throws(() => { - new Conf({ // eslint-disable-line no-new + new Conf({ cwd: tempy.directory(), defaults: { foo: 1 @@ -696,21 +699,21 @@ test('schema - validate Conf default', t => { }, 'Config schema violation: `foo` should be string'); }); -test('.get() - without dot notation', t => { +test('.get() - without dot notation', (t: any) => { t.is(t.context.configWithoutDotNotation.get('foo'), undefined); t.is(t.context.configWithoutDotNotation.get('foo', '🐴'), '🐴'); t.context.configWithoutDotNotation.set('foo', fixture); t.is(t.context.configWithoutDotNotation.get('foo'), fixture); }); -test('.set() - without dot notation', t => { +test('.set() - without dot notation', (t: any) => { t.context.configWithoutDotNotation.set('foo', fixture); t.context.configWithoutDotNotation.set('baz.boo', fixture); t.is(t.context.configWithoutDotNotation.get('foo'), fixture); t.is(t.context.configWithoutDotNotation.get('baz.boo'), fixture); }); -test('.set() - with object - without dot notation', t => { +test('.set() - with object - without dot notation', (t: any) => { t.context.configWithoutDotNotation.set({ foo1: 'bar1', foo2: 'bar2', @@ -728,7 +731,7 @@ test('.set() - with object - without dot notation', t => { t.is(t.context.configWithoutDotNotation.get('baz.foo.bar'), undefined); }); -test('.has() - without dot notation', t => { +test('.has() - without dot notation', (t: any) => { t.context.configWithoutDotNotation.set('foo', fixture); t.context.configWithoutDotNotation.set('baz.boo', fixture); t.true(t.context.configWithoutDotNotation.has('foo')); @@ -736,7 +739,7 @@ test('.has() - without dot notation', t => { t.false(t.context.configWithoutDotNotation.has('missing')); }); -test('.delete() - without dot notation', t => { +test('.delete() - without dot notation', (t: any) => { const {configWithoutDotNotation} = t.context; configWithoutDotNotation.set('foo', 'bar'); configWithoutDotNotation.set('baz.boo', true); @@ -753,7 +756,7 @@ test('.delete() - without dot notation', t => { t.deepEqual(configWithoutDotNotation.get('foo.bar.zoo'), {awesome: 'redpanda'}); }); -test('`watch` option watches for config file changes by another process', async t => { +test('`watch` option watches for config file changes by another process', async (t: any) => { if (process.platform === 'darwin' && process.version.split('.')[0] === 'v8') { t.plan(0); return; @@ -766,7 +769,7 @@ test('`watch` option watches for config file changes by another process', async t.plan(4); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: any, oldValue: any): void => { t.is(newValue, '🐴'); t.is(oldValue, '👾'); }; @@ -780,10 +783,12 @@ test('`watch` option watches for config file changes by another process', async conf2.set('foo', '🐴'); })(); - await pEvent(conf1.events, 'change'); + const {events}: any = conf1; + + await pEvent(events, 'change'); }); -test('`watch` option watches for config file changes by file write', async t => { +test('`watch` option watches for config file changes by file write', async (t: any) => { // TODO: Remove this when targeting Node.js 10. if (process.platform === 'darwin' && process.version.split('.')[0] === 'v8') { t.plan(0); @@ -796,7 +801,7 @@ test('`watch` option watches for config file changes by file write', async t => t.plan(2); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: any, oldValue: any): void => { t.is(newValue, '🦄'); t.is(oldValue, '🐴'); }; @@ -808,10 +813,12 @@ test('`watch` option watches for config file changes by file write', async t => fs.writeFileSync(path.join(cwd, 'config.json'), JSON.stringify({foo: '🦄'})); })(); - await pEvent(conf.events, 'change'); + const {events}: any = conf; + + await pEvent(events, 'change'); }); -test('migrations - should save the project version as the initial migrated version', t => { +test('migrations - should save the project version as the initial migrated version', (t: any) => { const cwd = tempy.directory(); const conf = new Conf({cwd, projectVersion: '0.0.2', migrations: {}}); @@ -819,11 +826,11 @@ test('migrations - should save the project version as the initial migrated versi t.is(conf._get('__internal__.migrations.version'), '0.0.2'); }); -test('migrations - should save the project version when a migration occurs', t => { +test('migrations - should save the project version when a migration occurs', (t: any) => { const cwd = tempy.directory(); const migrations = { - '0.0.3': store => { + '0.0.3': (store: any) => { store.set('foo', 'cool stuff'); } }; @@ -838,11 +845,11 @@ test('migrations - should save the project version when a migration occurs', t = t.is(conf2.get('foo'), 'cool stuff'); }); -test('migrations - should NOT run the migration when the version doesn\'t change', t => { +test('migrations - should NOT run the migration when the version doesn\'t change', (t: any) => { const cwd = tempy.directory(); const migrations = { - '1.0.0': store => { + '1.0.0': (store: any) => { store.set('foo', 'cool stuff'); } }; @@ -857,11 +864,11 @@ test('migrations - should NOT run the migration when the version doesn\'t change t.false(conf2.has('foo')); }); -test('migrations - should run the migration when the version changes', t => { +test('migrations - should run the migration when the version changes', (t: any) => { const cwd = tempy.directory(); const migrations = { - '1.0.0': store => { + '1.0.0': (store: any) => { store.set('foo', 'cool stuff'); } }; @@ -877,10 +884,10 @@ test('migrations - should run the migration when the version changes', t => { t.is(conf2.get('foo'), 'cool stuff'); }); -test('migrations - should run the migration when the version uses semver comparisons', t => { +test('migrations - should run the migration when the version uses semver comparisons', (t: any) => { const cwd = tempy.directory(); const migrations = { - '>=1.0': store => { + '>=1.0': (store: any) => { store.set('foo', 'cool stuff'); } }; @@ -890,13 +897,13 @@ test('migrations - should run the migration when the version uses semver compari t.is(conf.get('foo'), 'cool stuff'); }); -test('migrations - should run the migration when the version uses multiple semver comparisons', t => { +test('migrations - should run the migration when the version uses multiple semver comparisons', (t: any) => { const cwd = tempy.directory(); const migrations = { - '>=1.0': store => { + '>=1.0': (store: any) => { store.set('foo', 'cool stuff'); }, - '>2.0.0': store => { + '>2.0.0': (store: any) => { store.set('foo', 'modern cool stuff'); } }; @@ -910,17 +917,17 @@ test('migrations - should run the migration when the version uses multiple semve t.is(conf2.get('foo'), 'modern cool stuff'); }); -test('migrations - should run all valid migrations when the version uses multiple semver comparisons', t => { +test('migrations - should run all valid migrations when the version uses multiple semver comparisons', (t: any) => { const cwd = tempy.directory(); const migrations = { - '>=1.0': store => { + '>=1.0': (store: any) => { store.set('foo', 'cool stuff'); }, - '>2.0.0': store => { + '>2.0.0': (store: any) => { store.set('woof', 'oof'); store.set('medium', 'yes'); }, - '<3.0.0': store => { + '<3.0.0': (store: any) => { store.set('woof', 'woof'); store.set('heart', '❤'); } @@ -934,17 +941,17 @@ test('migrations - should run all valid migrations when the version uses multipl t.is(conf.get('heart'), '❤'); }); -test('migrations - should cleanup migrations with non-numeric values', t => { +test('migrations - should cleanup migrations with non-numeric values', (t: any) => { const cwd = tempy.directory(); const migrations = { - '1.0.1-alpha': store => { + '1.0.1-alpha': (store: any) => { store.set('foo', 'cool stuff'); }, - '>2.0.0-beta': store => { + '>2.0.0-beta': (store: any) => { store.set('woof', 'oof'); store.set('medium', 'yes'); }, - '<3.0.0': store => { + '<3.0.0': (store: any) => { store.set('woof', 'woof'); store.set('heart', '❤'); } @@ -958,20 +965,20 @@ test('migrations - should cleanup migrations with non-numeric values', t => { t.is(conf.get('heart'), '❤'); }); -test('migrations - should infer the applicationVersion from the package.json when it isn\'t specified', t => { +test('migrations - should infer the applicationVersion from the package.json when it isn\'t specified', (t: any) => { const cwd = tempy.directory(); const conf = new Conf({cwd, migrations: { - '2000.0.0': store => { + '2000.0.0': (store: any) => { store.set('foo', 'bar'); } }}); t.false(conf.has('foo')); - t.is(conf._get('__internal__.migrations.version'), require('./package.json').version); + t.is(conf._get('__internal__.migrations.version'), require('../package.json').version); }); -test('migrations - should NOT throw an error when project version is unspecified but there are no migrations', t => { +test('migrations - should NOT throw an error when project version is unspecified but there are no migrations', (t: any) => { const cwd = tempy.directory(); t.notThrows(() => { @@ -980,21 +987,21 @@ test('migrations - should NOT throw an error when project version is unspecified }); }); -test('migrations - should not create the previous migration key if the migrations aren\'t needed', t => { +test('migrations - should not create the previous migration key if the migrations aren\'t needed', (t: any) => { const cwd = tempy.directory(); const conf = new Conf({cwd}); t.false(conf.has('__internal__.migrations.version')); }); -test('migrations error handling - should rollback changes if a migration failed', t => { +test('migrations error handling - should rollback changes if a migration failed', (t: any) => { const cwd = tempy.directory(); const failingMigrations = { - '1.0.0': store => { + '1.0.0': (store: any) => { store.set('foo', 'initial update'); }, - '1.0.1': store => { + '1.0.1': (store: any) => { store.set('foo', 'updated before crash'); throw new Error('throw the migration and rollback'); @@ -1005,7 +1012,7 @@ test('migrations error handling - should rollback changes if a migration failed' }; const passingMigrations = { - '1.0.0': store => { + '1.0.0': (store: any) => { store.set('foo', 'initial update'); } }; @@ -1021,7 +1028,7 @@ test('migrations error handling - should rollback changes if a migration failed' t.is(conf.get('foo'), 'initial update'); }); -test('__internal__ keys - should not be accessible by the user', t => { +test('__internal__ keys - should not be accessible by the user', (t: any) => { const cwd = tempy.directory(); const conf = new Conf({cwd}); @@ -1031,7 +1038,7 @@ test('__internal__ keys - should not be accessible by the user', t => { }, /Please don't use the __internal__ key/); }); -test('__internal__ keys - should not be accessible by the user even without dot notation', t => { +test('__internal__ keys - should not be accessible by the user even without dot notation', (t: any) => { const cwd = tempy.directory(); const conf = new Conf({cwd, accessPropertiesByDotNotation: false}); @@ -1045,7 +1052,7 @@ test('__internal__ keys - should not be accessible by the user even without dot }, /Please don't use the __internal__ key/); }); -test('__internal__ keys - should only match specific "__internal__" entry', t => { +test('__internal__ keys - should only match specific "__internal__" entry', (t: any) => { const cwd = tempy.directory(); const conf = new Conf({cwd}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..80ba6a1 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "@sindresorhus/tsconfig", + "compilerOptions": { + "outDir": "dist", + "target": "es2017", + "lib": [ + "es2017" + ] + }, + "include": [ + "source/*" + ] +} \ No newline at end of file From 32e0f4563557280de782d5aee000c9b1ca04b349 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Tue, 19 Nov 2019 11:55:52 +0000 Subject: [PATCH 02/13] fix: EOL --- .eslintrc | 2 +- .gitignore | 2 +- tsconfig.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index 399b89f..78000bc 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,4 @@ { "extends": ["xo", "xo-typescript"], "parser": "@typescript-eslint/parser" -} \ No newline at end of file +} diff --git a/.gitignore b/.gitignore index c5fb9e7..5208922 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules yarn.lock .nyc_output -dist \ No newline at end of file +dist diff --git a/tsconfig.json b/tsconfig.json index 80ba6a1..30d9578 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,4 +10,4 @@ "include": [ "source/*" ] -} \ No newline at end of file +} From daebf20a5996aca74b7eb1d790a2381fd983ceab Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Tue, 19 Nov 2019 11:58:24 +0000 Subject: [PATCH 03/13] fix: allow dynamic import in tests to work --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e630017..9a96825 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ }, "scripts": { "build": "del dist && tsc", - "test": "xo && npm run build && nyc ava", + "test": "npm run build && xo && nyc ava", "bench": "ts-node bench.ts", "prepublishOnly": "npm run build" }, From ed72882797aac361f35d0c3f567d95d246740b26 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Tue, 19 Nov 2019 15:50:45 +0000 Subject: [PATCH 04/13] fix: typescript not uses proper imports --- package.json | 2 +- source/index.ts | 73 +++++++++++++++++++++++++++---------------------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 9a96825..c61713e 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ }, "devDependencies": { "@sindresorhus/tsconfig": "^0.4.0", - "@types/node": "^12.7.4", + "@types/node": "^12.12.9", "@types/semver": "^6.2.0", "@types/write-file-atomic": "^2.1.2", "@typescript-eslint/eslint-plugin": "^1.11.0", diff --git a/source/index.ts b/source/index.ts index 3d9054f..fb991e0 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,17 +1,17 @@ -import fs = require('fs'); -import path = require('path'); -import crypto = require('crypto'); -import assert = require('assert'); -import EventEmitter = require('events'); -import dotProp = require('dot-prop'); -import makeDir = require('make-dir'); -import pkgUp = require('pkg-up'); -import envPaths = require('env-paths'); -import writeFileAtomic = require('write-file-atomic'); -import Ajv = require('ajv'); -import debounceFn = require('debounce-fn'); -import semver = require('semver'); -import onetime = require('onetime'); +import * as fs from 'fs'; +import * as path from 'path'; +import * as crypto from 'crypto'; +import * as assert from 'assert'; +import * as EventEmitter from 'events'; +import * as dotProp from 'dot-prop'; +import * as makeDir from 'make-dir'; +import * as pkgUp from 'pkg-up'; +import * as envPaths from 'env-paths'; +import * as writeFileAtomic from 'write-file-atomic'; +import * as Ajv from 'ajv'; +import * as debounceFn from 'debounce-fn'; +import * as semver from 'semver'; +import onetime from 'onetime'; const plainObject: () => object = () => Object.create(null); const encryptionAlgorithm = 'aes-256-cbc'; @@ -163,7 +163,7 @@ export default class Conf { const store = Object.assign(plainObject(), options.defaults, fileStore); this._validate(store); try { - assert.deepEqual(fileStore, store); + assert.deepStrictEqual(fileStore, store); } catch (_) { this.store = store; } @@ -440,7 +440,7 @@ export default class Conf { try { // TODO: Use `util.isDeepStrictEqual` when targeting Node.js 10 - assert.deepEqual(newValue, oldValue); + assert.deepStrictEqual(newValue, oldValue); } catch (_) { currentValue = newValue; callback.call(this, newValue, oldValue); @@ -451,6 +451,30 @@ export default class Conf { return () => this.events?.removeListener('change', onChange); } + encryptData(data: any): Buffer | undefined { + if (!this.encryptionKey) { + return data; + } + + try { + // Check if an initialization vector has been used to encrypt the data + if (data.slice(16, 17).toString() === ':') { + const initializationVector = data.slice(0, 16); + const password = crypto.pbkdf2Sync(this.encryptionKey, initializationVector.toString(), 10000, 32, 'sha512'); + const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector); + return Buffer.concat([decipher.update(data.slice(17)), decipher.final()]); + } + + // Legacy decryption without initialization vector + // tslint:disable-next-line + // eslint-disable-next-line node/no-deprecated-api + const decipher = crypto.createDecipher(encryptionAlgorithm, this.encryptionKey); + return Buffer.concat([decipher.update(data), decipher.final()]); + } catch (_) { + return data; + } + } + get size(): number { return Object.keys(this.store).length; } @@ -458,22 +482,7 @@ export default class Conf { get store(): StoreValue { try { let data: any = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8'); - - if (this.encryptionKey) { - try { - // Check if an initialization vector has been used to encrypt the data - if (data.slice(16, 17).toString() === ':') { - const initializationVector = data.slice(0, 16); - const password = crypto.pbkdf2Sync(this.encryptionKey, initializationVector.toString(), 10000, 32, 'sha512'); - const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector); - data = Buffer.concat([decipher.update(data.slice(17)), decipher.final()]); - } else { - const decipher = crypto.createDecipher(encryptionAlgorithm, this.encryptionKey); - data = Buffer.concat([decipher.update(data), decipher.final()]); - } - } catch (_) {} - } - + data = this.encryptData(data); data = this.deserialize && this.deserialize(data); this._validate(data); return Object.assign(plainObject(), data); From 9345d573701ab07d6dd045b8c5b44b3bcd5dc36e Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Fri, 22 Nov 2019 18:43:05 +0000 Subject: [PATCH 05/13] fix: change typescript typings --- package.json | 14 ++--- source/index.ts | 137 +++++++++++++++++++++++++++++------------------- 2 files changed, 91 insertions(+), 60 deletions(-) diff --git a/package.json b/package.json index c61713e..d341336 100644 --- a/package.json +++ b/package.json @@ -71,22 +71,22 @@ "dependencies": { "ajv": "^6.10.2", "debounce-fn": "^3.0.1", - "dot-prop": "^5.0.0", + "dot-prop": "^5.2.0", "env-paths": "^2.2.0", - "json-schema-typed": "^7.0.1", + "json-schema-typed": "^7.0.2", "make-dir": "^3.0.0", "onetime": "^5.1.0", "pkg-up": "^3.0.1", "semver": "^6.2.0", - "write-file-atomic": "^3.0.0" + "write-file-atomic": "^3.0.1" }, "devDependencies": { "@sindresorhus/tsconfig": "^0.4.0", - "@types/node": "^12.12.9", + "@types/node": "^12.12.11", "@types/semver": "^6.2.0", "@types/write-file-atomic": "^2.1.2", - "@typescript-eslint/eslint-plugin": "^1.11.0", - "@typescript-eslint/parser": "^1.11.0", + "@typescript-eslint/eslint-plugin": "^1.13.0", + "@typescript-eslint/parser": "^1.13.0", "ava": "^2.3.0", "clear-module": "^4.0.0", "del": "^5.1.0", @@ -96,7 +96,7 @@ "nyc": "^14.1.1", "p-event": "^4.1.0", "tempy": "^0.3.0", - "ts-node": "^8.3.0", + "ts-node": "^8.5.2", "tsd": "^0.7.4", "typescript": "^3.7.2", "xo": "^0.24.0" diff --git a/source/index.ts b/source/index.ts index fb991e0..f0e8b51 100644 --- a/source/index.ts +++ b/source/index.ts @@ -20,7 +20,7 @@ const encryptionAlgorithm = 'aes-256-cbc'; delete require.cache[__filename]; const parentDir = path.dirname((module.parent && module.parent.filename) || '.'); -const checkValueType = (key: string, value: StoreValue): void => { +const checkValueType = (key: string, value: unknown): void => { const nonJsonTypes = [ 'undefined', 'symbol', @@ -37,19 +37,46 @@ const checkValueType = (key: string, value: StoreValue): void => { const INTERNAL_KEY = '__internal__'; const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`; -type ConfSerializer = (args: any) => ArrayBuffer | Buffer | string; -type ConfDeserializer = (args: any) => any; -type ConfDefaultValues = { +export type Serializer = (...args: unknown[]) => string; +export type Deserializer = (arg: string | Buffer) => unknown; +export type DefaultValues = { [key: string]: object; }; -type ConfMigrations = { +export type Migrations = { [key: string]: (store: Conf) => void; }; -type GenericCallback = () => any; -type GenericVoidCallback = (...args: any) => void; -type StoreValue = any; -type ConfOptions = { +/** +[JSON Schema](https://json-schema.org) to validate your config data. +Under the hood, the JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats) +You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties) +@example +``` +import Conf = require('conf'); +const schema = { + foo: { + type: 'number', + maximum: 100, + minimum: 1, + default: 50 + }, + bar: { + type: 'string', + format: 'url' + } +}; +const config = new Conf({schema}); +console.log(config.get('foo')); +//=> 50 +config.set('foo', '1'); +// [Error: Config schema violation: `foo` should be number] +``` +**Note:** The `default` value will be overwritten by the `defaults` option if set. +*/ + +export type Schema = object | boolean; + +export type Options = { accessPropertiesByDotNotation?: boolean; clearInvalidConfig?: boolean; configName?: string; @@ -57,20 +84,23 @@ type ConfOptions = { defaults?: object; encryptionKey?: string; fileExtension?: string; - migrations?: ConfMigrations; + migrations?: Migrations; projectName?: string; projectSuffix?: string; projectVersion?: string; - schema?: object; + /** + * @type Schema + */ + schema?: Schema; watch?: boolean; - serialize?: ConfSerializer; - deserialize?: ConfDeserializer; + serialize: Serializer; + deserialize?: Deserializer; }; export default class Conf { - _options: ConfOptions; + _options: Options; - _defaultValues: ConfDefaultValues = {}; + _defaultValues: DefaultValues = {}; _validator?: Ajv.ValidateFunction; @@ -78,22 +108,22 @@ export default class Conf { events?: EventEmitter; - serialize?: ConfSerializer; + serialize: Serializer; - deserialize?: ConfDeserializer; + deserialize?: Deserializer; path: string; - constructor(options?: ConfOptions) { - options = { + constructor(partialOptions?: Partial) { + const options: Options = { configName: 'config', fileExtension: 'json', projectSuffix: 'nodejs', clearInvalidConfig: true, - serialize: (value: object) => JSON.stringify(value, null, '\t'), - deserialize: JSON.parse, + serialize: (value: unknown) => JSON.stringify(value, null, '\t'), + deserialize: (arg: string | Buffer) => JSON.parse(arg.toString()), accessPropertiesByDotNotation: true, - ...options + ...partialOptions }; const getPackageData = onetime(() => { @@ -185,7 +215,7 @@ export default class Conf { } } - _validate(data: any): boolean { + _validate(data: unknown): boolean { if (!this._validator) { return false; } @@ -210,8 +240,8 @@ export default class Conf { makeDir.sync(path.dirname(this.path)); } - _write(value: StoreValue): void { - let data: any = this.serialize && this.serialize(value); + _write(value: unknown): void { + let data: string | Buffer = this.serialize && this.serialize(value); if (this.encryptionKey) { const initializationVector = crypto.randomBytes(16); @@ -242,10 +272,10 @@ export default class Conf { }, {wait: 100})); } - _migrate(migrations: ConfMigrations, versionToMigrate: string): void { - let previousMigratedVersion = this._get(MIGRATION_KEY, '0.0.0'); + _migrate(migrations: Migrations, versionToMigrate: string): void { + let previousMigratedVersion: string = this._get(MIGRATION_KEY, '0.0.0'); - const newerVersions = Object.keys(migrations) + const newerVersions: string[] = Object.keys(migrations) .filter(candidateVersion => this._shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate)); let storeBackup = {...this.store}; @@ -273,7 +303,7 @@ export default class Conf { } } - _containsReservedKey(key: string): boolean { + _containsReservedKey(key: string | object): boolean { if (typeof key === 'object') { const firstKey = Object.keys(key)[0]; @@ -321,18 +351,18 @@ export default class Conf { return true; } - _get(key: string, defaultValue?: StoreValue): any { + _get(key: string, defaultValue?: unknown): any { return dotProp.get(this.store, key, defaultValue); } - _set(key: string, value?: StoreValue): void { + _set(key: string, value?: unknown): void { const {store} = this; dotProp.set(store, key, value); this.store = store; } - get(key: string, defaultValue?: any): any { + get(key: string, defaultValue?: unknown): unknown { if (this._options.accessPropertiesByDotNotation) { return dotProp.get(this.store, key, defaultValue); } @@ -340,7 +370,7 @@ export default class Conf { return key in this.store ? this.store[key] : defaultValue; } - set(key: any, value?: StoreValue): void { + set(key: string | object, value?: unknown): void { if (typeof key !== 'string' && typeof key !== 'object') { throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`); } @@ -355,7 +385,7 @@ export default class Conf { const {store} = this; - const set = (key: string, value: StoreValue): void => { + const set = (key: string, value: unknown): void => { checkValueType(key, value); if (this._options.accessPropertiesByDotNotation) { dotProp.set(store, key, value); @@ -384,7 +414,7 @@ export default class Conf { return key in this.store; } - reset(...keys: any): void { + reset(...keys: string[]): void { for (const key of keys) { if (this._defaultValues[key]) { this.set(key, this._defaultValues[key]); @@ -407,7 +437,7 @@ export default class Conf { this.store = plainObject(); } - onDidChange(key: string, callback: GenericVoidCallback): GenericCallback { + onDidChange(key: string, callback: (...args: unknown[]) => void): () => unknown { if (typeof key !== 'string') { throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof key}`); } @@ -416,25 +446,25 @@ export default class Conf { throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); } - const getter: GenericCallback = () => this.get(key); + const getter: () => unknown = () => this.get(key); return this.handleChange(getter, callback); } - onDidAnyChange(callback: GenericCallback): GenericCallback { + onDidAnyChange(callback: () => unknown): () => unknown { if (typeof callback !== 'function') { throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); } - const getter: GenericCallback = () => this.store; + const getter: () => unknown = () => this.store; return this.handleChange(getter, callback); } - handleChange(getter: GenericCallback, callback: GenericVoidCallback): () => void { + handleChange(getter: () => unknown, callback: (newValue: unknown, oldValue: unknown) => void): () => void { let currentValue = getter(); - const onChange: GenericCallback = () => { + const onChange: () => unknown = () => { const oldValue = currentValue; const newValue = getter(); @@ -451,7 +481,7 @@ export default class Conf { return () => this.events?.removeListener('change', onChange); } - encryptData(data: any): Buffer | undefined { + encryptData(data: string | Buffer): string | Buffer { if (!this.encryptionKey) { return data; } @@ -462,14 +492,15 @@ export default class Conf { const initializationVector = data.slice(0, 16); const password = crypto.pbkdf2Sync(this.encryptionKey, initializationVector.toString(), 10000, 32, 'sha512'); const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector); - return Buffer.concat([decipher.update(data.slice(17)), decipher.final()]); + const slicedData: any = data.slice(17); + return Buffer.concat([decipher.update(slicedData), decipher.final()]); } // Legacy decryption without initialization vector - // tslint:disable-next-line // eslint-disable-next-line node/no-deprecated-api const decipher = crypto.createDecipher(encryptionAlgorithm, this.encryptionKey); - return Buffer.concat([decipher.update(data), decipher.final()]); + const legacyData: any = data; + return Buffer.concat([decipher.update(legacyData), decipher.final()]); } catch (_) { return data; } @@ -479,13 +510,13 @@ export default class Conf { return Object.keys(this.store).length; } - get store(): StoreValue { + get store(): any { try { - let data: any = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8'); - data = this.encryptData(data); - data = this.deserialize && this.deserialize(data); - this._validate(data); - return Object.assign(plainObject(), data); + const data: string | Buffer = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8'); + const dataString: string | Buffer = this.encryptData(data); + const deserializedData = this.deserialize && this.deserialize(dataString); + this._validate(deserializedData); + return Object.assign(plainObject(), deserializedData); } catch (error) { if (error.code === 'ENOENT') { this._ensureDirectory(); @@ -500,7 +531,7 @@ export default class Conf { } } - set store(value: StoreValue) { + set store(value: any) { this._ensureDirectory(); this._validate(value); @@ -509,7 +540,7 @@ export default class Conf { this.events?.emit('change'); } - * [Symbol.iterator](): any { + * [Symbol.iterator](): unknown { for (const [key, value] of Object.entries(this.store)) { yield [key, value]; } From 76f70abee173ddc52784d364e9f9ca57fffa9929 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Fri, 22 Nov 2019 18:45:05 +0000 Subject: [PATCH 06/13] fix: update import syntax --- source/index.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/source/index.ts b/source/index.ts index f0e8b51..c9ee03c 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,16 +1,16 @@ -import * as fs from 'fs'; -import * as path from 'path'; -import * as crypto from 'crypto'; -import * as assert from 'assert'; -import * as EventEmitter from 'events'; -import * as dotProp from 'dot-prop'; -import * as makeDir from 'make-dir'; -import * as pkgUp from 'pkg-up'; -import * as envPaths from 'env-paths'; -import * as writeFileAtomic from 'write-file-atomic'; -import * as Ajv from 'ajv'; -import * as debounceFn from 'debounce-fn'; -import * as semver from 'semver'; +import fs = require('fs'); +import path = require('path'); +import crypto = require('crypto'); +import assert = require('assert'); +import EventEmitter = require('events'); +import dotProp = require('dot-prop'); +import makeDir = require('make-dir'); +import pkgUp = require('pkg-up'); +import envPaths = require('env-paths'); +import writeFileAtomic = require('write-file-atomic'); +import Ajv = require('ajv'); +import debounceFn = require('debounce-fn'); +import semver = require('semver'); import onetime from 'onetime'; const plainObject: () => object = () => Object.create(null); @@ -497,7 +497,6 @@ export default class Conf { } // Legacy decryption without initialization vector - // eslint-disable-next-line node/no-deprecated-api const decipher = crypto.createDecipher(encryptionAlgorithm, this.encryptionKey); const legacyData: any = data; return Buffer.concat([decipher.update(legacyData), decipher.final()]); From 0b0a98da29579b5a6758e2ad0206fc24758cd7d3 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Mon, 25 Nov 2019 10:46:22 +0000 Subject: [PATCH 07/13] fix: add docs and refactor types --- source/index.ts | 329 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 264 insertions(+), 65 deletions(-) diff --git a/source/index.ts b/source/index.ts index c9ee03c..71d7b89 100644 --- a/source/index.ts +++ b/source/index.ts @@ -37,67 +37,7 @@ const checkValueType = (key: string, value: unknown): void => { const INTERNAL_KEY = '__internal__'; const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`; -export type Serializer = (...args: unknown[]) => string; -export type Deserializer = (arg: string | Buffer) => unknown; -export type DefaultValues = { - [key: string]: object; -}; -export type Migrations = { - [key: string]: (store: Conf) => void; -}; - -/** -[JSON Schema](https://json-schema.org) to validate your config data. -Under the hood, the JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats) -You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties) -@example -``` -import Conf = require('conf'); -const schema = { - foo: { - type: 'number', - maximum: 100, - minimum: 1, - default: 50 - }, - bar: { - type: 'string', - format: 'url' - } -}; -const config = new Conf({schema}); -console.log(config.get('foo')); -//=> 50 -config.set('foo', '1'); -// [Error: Config schema violation: `foo` should be number] -``` -**Note:** The `default` value will be overwritten by the `defaults` option if set. -*/ - -export type Schema = object | boolean; - -export type Options = { - accessPropertiesByDotNotation?: boolean; - clearInvalidConfig?: boolean; - configName?: string; - cwd?: string; - defaults?: object; - encryptionKey?: string; - fileExtension?: string; - migrations?: Migrations; - projectName?: string; - projectSuffix?: string; - projectVersion?: string; - /** - * @type Schema - */ - schema?: Schema; - watch?: boolean; - serialize: Serializer; - deserialize?: Deserializer; -}; - -export default class Conf { +export default class Conf implements Iterable<[string, T]> { _options: Options; _defaultValues: DefaultValues = {}; @@ -114,6 +54,9 @@ export default class Conf { path: string; + /** + Simple config handling for your app or module. + */ constructor(partialOptions?: Partial) { const options: Options = { configName: 'config', @@ -193,7 +136,7 @@ export default class Conf { const store = Object.assign(plainObject(), options.defaults, fileStore); this._validate(store); try { - assert.deepStrictEqual(fileStore, store); + assert.deepEqual(fileStore, store); } catch (_) { this.store = store; } @@ -362,6 +305,12 @@ export default class Conf { this.store = store; } + /** + Get an item. + + @param key - The key of the item to get. + @param defaultValue - The default value if the item does not exist. + */ get(key: string, defaultValue?: unknown): unknown { if (this._options.accessPropertiesByDotNotation) { return dotProp.get(this.store, key, defaultValue); @@ -370,6 +319,12 @@ export default class Conf { return key in this.store ? this.store[key] : defaultValue; } + /** + Set an item. + + @param key - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. + @param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`. + */ set(key: string | object, value?: unknown): void { if (typeof key !== 'string' && typeof key !== 'object') { throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`); @@ -406,6 +361,11 @@ export default class Conf { this.store = store; } + /** + Check if an item exists. + + @param key - The key of the item to check. + */ has(key: string): boolean { if (this._options.accessPropertiesByDotNotation) { return dotProp.has(this.store, key); @@ -414,6 +374,11 @@ export default class Conf { return key in this.store; } + /** + Reset items to their default values, as defined by the `defaults` or `schema` option. + + @param keys - The keys of the items to reset. + */ reset(...keys: string[]): void { for (const key of keys) { if (this._defaultValues[key]) { @@ -422,6 +387,11 @@ export default class Conf { } } + /** + Delete an item. + + @param key - The key of the item to delete. + */ delete(key: string): void { const {store} = this; if (this._options.accessPropertiesByDotNotation) { @@ -433,10 +403,19 @@ export default class Conf { this.store = store; } + /** + Delete all items. + */ clear(): void{ this.store = plainObject(); } + /** + Watches the given `key`, calling `callback` on any changes. + + @param key - The key wo watch. + @param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`. + */ onDidChange(key: string, callback: (...args: unknown[]) => void): () => unknown { if (typeof key !== 'string') { throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof key}`); @@ -451,6 +430,11 @@ export default class Conf { return this.handleChange(getter, callback); } + /** + Watches the whole config object, calling `callback` on any changes. + + @param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`. + */ onDidAnyChange(callback: () => unknown): () => unknown { if (typeof callback !== 'function') { throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); @@ -469,8 +453,8 @@ export default class Conf { const newValue = getter(); try { - // TODO: Use `util.isDeepStrictEqual` when targeting Node.js 10 - assert.deepStrictEqual(newValue, oldValue); + // TODO: Use `util.isdeepEqual` when targeting Node.js 10 + assert.deepEqual(newValue, oldValue); } catch (_) { currentValue = newValue; callback.call(this, newValue, oldValue); @@ -539,9 +523,224 @@ export default class Conf { this.events?.emit('change'); } - * [Symbol.iterator](): unknown { + * [Symbol.iterator](): IterableIterator<[string, any]> { for (const [key, value] of Object.entries(this.store)) { yield [key, value]; } } } + +export type Serializer = (...args: unknown[]) => string; +export type Deserializer = (arg: string | Buffer) => unknown; +export type DefaultValues = { + [key: string]: object; +}; +export type Migrations = { + [key: string]: (store: Conf) => void; +}; + +export type Schema = object | boolean; + +export type Options = { + /** + Access nested properties by dot notation. + + @default true + + @example + ``` + const config = new Conf(); + + config.set({ + foo: { + bar: { + foobar: '🦄' + } + } + }); + + console.log(config.get('foo.bar.foobar')); + //=> '🦄' + ``` + + Alternatively, you can set this option to `false` so the whole string would be treated as one key. + + @example + ``` + const config = new Conf({accessPropertiesByDotNotation: false}); + + config.set({ + `foo.bar.foobar`: '🦄' + }); + + console.log(config.get('foo.bar.foobar')); + //=> '🦄' + ``` + + */ + accessPropertiesByDotNotation?: boolean; + + /** + The config is cleared if reading the config file causes a `SyntaxError`. This is a good default, as the config file is not intended to be hand-edited, so it usually means the config is corrupt and there's nothing the user can do about it anyway. However, if you let the user edit the config file directly, mistakes might happen and it could be more useful to throw an error when the config is invalid instead of clearing. Disabling this option will make it throw a `SyntaxError` on invalid config instead of clearing. + + @default true + */ + clearInvalidConfig?: boolean; + /** + Name of the config file (without extension). + + Useful if you need multiple config files for your app or module. For example, different config files between two major versions. + + @default 'config' + */ + configName?: string; + + /** + __You most likely don't need this. Please don't use it unless you really have to.__ + + The only use-case I can think of is having the config located in the app directory or on some external storage. Default: System default user [config directory](https://github.com/sindresorhus/env-paths#pathsconfig). + */ + cwd?: string; + + /** + Config used if there are no existing config. + ** Note: The values in `defaults` will overwrite the `default` key in the `schema` option. + */ + defaults?: Readonly; + + /** + Note that this is __not intended for security purposes__, since the encryption key would be easily found inside a plain-text Node.js app. + + Its main use is for obscurity. If a user looks through the config directory and finds the config file, since it's just a JSON file, they may be tempted to modify it. By providing an encryption key, the file will be obfuscated, which should hopefully deter any users from doing so. + + It also has the added bonus of ensuring the config file's integrity. If the file is changed in any way, the decryption will not work, in which case the store will just reset back to its default state. + + When specified, the store will be encrypted using the [`aes-256-cbc`](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation) encryption algorithm. + */ + encryptionKey?: string; + + /** + Extension of the config file. + + You would usually not need this, but could be useful if you want to interact with a file with a custom file extension that can be associated with your app. These might be simple save/export/preference files that are intended to be shareable or saved outside of the app. + + @default 'json' + */ + fileExtension?: string; + + /* + _Don't use this feature until [this issue](https://github.com/sindresorhus/conf/issues/92) has been fixed._ + + You can use migrations to perform operations to the store whenever a version is changed. + + The `migrations` object should consist of a key-value pair of `'version': handler`. The `version` can also be a [semver range](https://github.com/npm/node-semver#ranges). + + @example + ``` + import Conf = require('conf'); + + const store = new Conf({ + migrations: { + '0.0.1': store => { + store.set('debugPhase', true); + }, + '1.0.0': store => { + store.delete('debugPhase'); + store.set('phase', '1.0.0'); + }, + '1.0.2': store => { + store.set('phase', '1.0.2'); + }, + '>=2.0.0': store => { + store.set('phase', '>=2.0.0'); + } + } + }); + ``` + */ + migrations?: Migrations; + + /** + You only need to specify this if you don't have a package.json file in your project or if it doesn't have a name defined within it. + + Default: The name field in the `package.json` closest to where `conf` is imported. + */ + projectName?: string; + + /** + __You most likely don't need this. Please don't use it unless you really have to.__ + + Suffix appended to `projectName` during config file creation to avoid name conflicts with native apps. + + You can pass an empty string to remove the suffix. + + For example, on macOS, the config file will be stored in the `~/Library/Preferences/foo-nodejs` directory, where `foo` is the `projectName`. + + @default 'nodejs' + */ + projectSuffix?: string; + + /** + You only need to specify this if you don't have a package.json file in your project or if it doesn't have a version defined within it. + + Default: The name field in the `package.json` closest to where `conf` is imported. + */ + projectVersion?: string; + + /** + * [JSON Schema](https://json-schema.org) to validate your config data. + * Under the hood, the JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats) + * You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties) + + @example + ``` + import Conf = require('conf'); + const schema = { + foo: { + type: 'number', + maximum: 100, + minimum: 1, + default: 50 + }, + bar: { + type: 'string', + format: 'url' + } + }; + const config = new Conf({schema}); + console.log(config.get('foo')); + //=> 50 + config.set('foo', '1'); + // [Error: Config schema violation: `foo` should be number] + ``` + ** Note: The `default` value will be overwritten by the `defaults` option if set. + */ + schema?: Schema; + + /** + Watch for any changes in the config file and call the callback for `onDidChange` if set. This is useful if there are multiple processes changing the same config file. + + __Currently this option doesn't work on Node.js 8 on macOS.__ + + @default false + */ + watch?: boolean; + + /** + Function to serialize the config object to a UTF-8 string when writing the config file. + + You would usually not need this, but it could be useful if you want to use a format other than JSON. + + @default value => JSON.stringify(value, null, '\t') + */ + serialize: Serializer; + + /** + Function to deserialize the config object from a UTF-8 string when reading the config file. + + You would usually not need this, but it could be useful if you want to use a format other than JSON. + + @default JSON.parse + */ + deserialize?: Deserializer; +}; From 42c900ec356156d28c035f03a79182cf5fe25ca8 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Mon, 25 Nov 2019 10:48:25 +0000 Subject: [PATCH 08/13] chore: fix reserved keys typing --- source/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/index.ts b/source/index.ts index 71d7b89..2099f55 100644 --- a/source/index.ts +++ b/source/index.ts @@ -246,7 +246,7 @@ export default class Conf implements Iterable<[string, T]> { } } - _containsReservedKey(key: string | object): boolean { + _containsReservedKey(key: string | {[key: string]: unknown}): boolean { if (typeof key === 'object') { const firstKey = Object.keys(key)[0]; @@ -325,7 +325,7 @@ export default class Conf implements Iterable<[string, T]> { @param key - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. @param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`. */ - set(key: string | object, value?: unknown): void { + set(key: string | {[key: string]: unknown}, value?: unknown): void { if (typeof key !== 'string' && typeof key !== 'object') { throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`); } From d655983fe18f632e005a5f37fb2c29fb30039b66 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Mon, 25 Nov 2019 10:51:03 +0000 Subject: [PATCH 09/13] fix: test typings --- test/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/index.ts b/test/index.ts index abfceef..8b913c7 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,5 +1,5 @@ /* eslint-disable no-new */ -/* eslint-disable ava/no-ignored-test-files */ +/* eslint-disable ava/use-test */ import fs = require('fs'); import path = require('path'); import tempy = require('tempy'); @@ -8,9 +8,10 @@ import pkgUp = require('pkg-up'); import clearModule = require('clear-module'); import pEvent = require('p-event'); import delay = require('delay'); -import test from 'ava'; +import anyTest, {TestInterface} from 'ava'; import Conf from '../source'; +const test = anyTest as TestInterface<{T: string}>; const fixture = '🦄'; test.beforeEach((t: any) => { From 846e9e919d26bcbdf291df6679958082220931e0 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Mon, 25 Nov 2019 10:54:14 +0000 Subject: [PATCH 10/13] fix: optional params --- source/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/index.ts b/source/index.ts index 2099f55..f2437a6 100644 --- a/source/index.ts +++ b/source/index.ts @@ -44,13 +44,13 @@ export default class Conf implements Iterable<[string, T]> { _validator?: Ajv.ValidateFunction; - encryptionKey?: string; + encryptionKey: string | undefined; - events?: EventEmitter; + events: EventEmitter; serialize: Serializer; - deserialize?: Deserializer; + deserialize: Deserializer | undefined; path: string; From b09a636a7c1fa420e8c46ab069e97f8346c8ec24 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Mon, 25 Nov 2019 19:40:46 +0000 Subject: [PATCH 11/13] fix: update test typing --- source/index.ts | 4 +- test/index.ts | 141 ++++++++++++++++++++++++------------------------ 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/source/index.ts b/source/index.ts index f2437a6..7026539 100644 --- a/source/index.ts +++ b/source/index.ts @@ -435,7 +435,7 @@ export default class Conf implements Iterable<[string, T]> { @param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`. */ - onDidAnyChange(callback: () => unknown): () => unknown { + onDidAnyChange(callback: (newValue: unknown, oldValue: unknown) => void): () => unknown { if (typeof callback !== 'function') { throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); } @@ -445,7 +445,7 @@ export default class Conf implements Iterable<[string, T]> { return this.handleChange(getter, callback); } - handleChange(getter: () => unknown, callback: (newValue: unknown, oldValue: unknown) => void): () => void { + handleChange(getter: () => unknown, callback: (newValue: unknown, oldValue: unknown) => void): () => unknown { let currentValue = getter(); const onChange: () => unknown = () => { diff --git a/test/index.ts b/test/index.ts index 8b913c7..284cd19 100644 --- a/test/index.ts +++ b/test/index.ts @@ -11,29 +11,29 @@ import delay = require('delay'); import anyTest, {TestInterface} from 'ava'; import Conf from '../source'; -const test = anyTest as TestInterface<{T: string}>; +const test = anyTest as TestInterface<{config: Conf; configWithoutDotNotation: Conf; configWithSchema: Conf; configWithDefaults: Conf}>; const fixture = '🦄'; -test.beforeEach((t: any) => { +test.beforeEach(t => { t.context.config = new Conf({cwd: tempy.directory()}); t.context.configWithoutDotNotation = new Conf({cwd: tempy.directory(), accessPropertiesByDotNotation: false}); }); -test('.get()', (t: any) => { +test('.get()', t => { t.is(t.context.config.get('foo'), undefined); t.is(t.context.config.get('foo', '🐴'), '🐴'); t.context.config.set('foo', fixture); t.is(t.context.config.get('foo'), fixture); }); -test('.set()', (t: any) => { +test('.set()', t => { t.context.config.set('foo', fixture); t.context.config.set('baz.boo', fixture); t.is(t.context.config.get('foo'), fixture); t.is(t.context.config.get('baz.boo'), fixture); }); -test('.set() - with object', (t: any) => { +test('.set() - with object', t => { t.context.config.set({ foo1: 'bar1', foo2: 'bar2', @@ -52,13 +52,13 @@ test('.set() - with object', (t: any) => { t.is(t.context.config.get('baz.foo.bar'), 'baz'); }); -test('.set() - with undefined', (t: any) => { +test('.set() - with undefined', t => { t.throws(() => { t.context.config.set('foo', undefined); }, 'Use `delete()` to clear values'); }); -test('.set() - with unsupported values', (t: any) => { +test('.set() - with unsupported values', t => { t.throws(() => { t.context.config.set('a', () => {}); }, /not supported by JSON/); @@ -86,13 +86,14 @@ test('.set() - with unsupported values', (t: any) => { }, /not supported by JSON/); }); -test('.set() - invalid key', (t: any) => { +test('.set() - invalid key', t => { t.throws(() => { + // @ts-ignore t.context.config.set(1, 'unicorn'); }, 'Expected `key` to be of type `string` or `object`, got number'); }); -test('.has()', (t: any) => { +test('.has()', t => { t.context.config.set('foo', fixture); t.context.config.set('baz.boo', fixture); t.true(t.context.config.has('foo')); @@ -100,7 +101,7 @@ test('.has()', (t: any) => { t.false(t.context.config.has('missing')); }); -test('.reset()', (t: any) => { +test('.reset()', t => { t.context.configWithSchema = new Conf({ cwd: tempy.directory(), schema: { @@ -134,7 +135,7 @@ test('.reset()', (t: any) => { t.is(t.context.configWithDefaults.get('bar'), 99); }); -test('.delete()', (t: any) => { +test('.delete()', t => { const {config} = t.context; config.set('foo', 'bar'); config.set('baz.boo', true); @@ -151,7 +152,7 @@ test('.delete()', (t: any) => { t.is(config.get('foo.bar.zoo.awesome'), 'redpanda'); }); -test('.clear()', (t: any) => { +test('.clear()', t => { t.context.config.set('foo', 'bar'); t.context.config.set('foo1', 'bar1'); t.context.config.set('baz.boo', true); @@ -159,12 +160,12 @@ test('.clear()', (t: any) => { t.is(t.context.config.size, 0); }); -test('.size', (t: any) => { +test('.size', t => { t.context.config.set('foo', 'bar'); t.is(t.context.config.size, 1); }); -test('.store', (t: any) => { +test('.store', t => { t.context.config.set('foo', 'bar'); t.context.config.set('baz.boo', true); t.deepEqual(t.context.config.store, { @@ -175,7 +176,7 @@ test('.store', (t: any) => { }); }); -test('`defaults` option', (t: any) => { +test('`defaults` option', t => { const config = new Conf({ cwd: tempy.directory(), defaults: { @@ -186,7 +187,7 @@ test('`defaults` option', (t: any) => { t.is(config.get('foo'), 'bar'); }); -test('`configName` option', (t: any) => { +test('`configName` option', t => { const configName = 'alt-config'; const config = new Conf({ cwd: tempy.directory(), @@ -198,12 +199,12 @@ test('`configName` option', (t: any) => { t.is(path.basename(config.path, '.json'), configName); }); -test('no `suffix` option', (t: any) => { +test('no `suffix` option', t => { const config = new Conf(); t.true(config.path.includes('-nodejs')); }); -test('with `suffix` option set to empty string', (t: any) => { +test('with `suffix` option set to empty string', t => { const projectSuffix = ''; const projectName = 'conf-temp1-project'; const config = new Conf({projectSuffix, projectName}); @@ -212,7 +213,7 @@ test('with `suffix` option set to empty string', (t: any) => { t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length); }); -test('with `projectSuffix` option set to non-empty string', (t: any) => { +test('with `projectSuffix` option set to non-empty string', t => { const projectSuffix = 'new-projectSuffix'; const projectName = 'conf-temp2-project'; const config = new Conf({projectSuffix, projectName}); @@ -222,7 +223,7 @@ test('with `projectSuffix` option set to non-empty string', (t: any) => { t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length); }); -test('`fileExtension` option', (t: any) => { +test('`fileExtension` option', t => { const fileExtension = 'alt-ext'; const config = new Conf({ cwd: tempy.directory(), @@ -234,7 +235,7 @@ test('`fileExtension` option', (t: any) => { t.is(path.extname(config.path), `.${fileExtension}`); }); -test('`fileExtension` option = empty string', (t: any) => { +test('`fileExtension` option = empty string', t => { const configName = 'unicorn'; const config = new Conf({ cwd: tempy.directory(), @@ -244,7 +245,7 @@ test('`fileExtension` option = empty string', (t: any) => { t.is(path.basename(config.path), configName); }); -test('`serialize` and `deserialize` options', (t: any) => { +test('`serialize` and `deserialize` options', t => { t.plan(4); const serialized = `foo:${fixture}`; const deserialized = {foo: fixture}; @@ -268,7 +269,7 @@ test('`serialize` and `deserialize` options', (t: any) => { t.deepEqual(config.store, deserialized); }); -test('`projectName` option', (t: any) => { +test('`projectName` option', t => { const projectName = 'conf-fixture-project-name'; const config = new Conf({projectName}); t.is(config.get('foo'), undefined); @@ -278,7 +279,7 @@ test('`projectName` option', (t: any) => { del.sync(config.path, {force: true}); }); -test('ensure `.store` is always an object', (t: any) => { +test('ensure `.store` is always an object', t => { const cwd = tempy.directory(); const config = new Conf({cwd}); @@ -289,7 +290,7 @@ test('ensure `.store` is always an object', (t: any) => { }); }); -test('instance is iterable', (t: any) => { +test('instance is iterable', t => { t.context.config.set({ foo: fixture, bar: fixture @@ -300,7 +301,7 @@ test('instance is iterable', (t: any) => { ); }); -test('automatic `projectName` inference', (t: any) => { +test('automatic `projectName` inference', t => { const config = new Conf(); config.set('foo', fixture); t.is(config.get('foo'), fixture); @@ -308,7 +309,7 @@ test('automatic `projectName` inference', (t: any) => { del.sync(config.path, {force: true}); }); -test('`cwd` option overrides `projectName` option', (t: any) => { +test('`cwd` option overrides `projectName` option', t => { const cwd = tempy.directory(); let config: any; @@ -323,7 +324,7 @@ test('`cwd` option overrides `projectName` option', (t: any) => { del.sync(config.path, {force: true}); }); -test('safely handle missing package.json', (t: any) => { +test('safely handle missing package.json', t => { const pkgUpSyncOrig = pkgUp.sync; pkgUp.sync = () => null; @@ -336,7 +337,7 @@ test('safely handle missing package.json', (t: any) => { pkgUp.sync = pkgUpSyncOrig; }); -test('handle `cwd` being set and `projectName` not being set', (t: any) => { +test('handle `cwd` being set and `projectName` not being set', t => { const pkgUpSyncOrig = pkgUp.sync; pkgUp.sync = () => null; @@ -350,7 +351,7 @@ test('handle `cwd` being set and `projectName` not being set', (t: any) => { }); // See #11 -test('fallback to cwd if `module.filename` is `null`', (t: any) => { +test('fallback to cwd if `module.filename` is `null`', t => { const preservedFilename: string = module.filename; const filename: any = null; module.filename = filename; @@ -366,7 +367,7 @@ test('fallback to cwd if `module.filename` is `null`', (t: any) => { del.sync(path.dirname(config.path)); }); -test('encryption', (t: any) => { +test('encryption', t => { const config = new Conf({cwd: tempy.directory(), encryptionKey: 'abc123'}); t.is(config.get('foo'), undefined); t.is(config.get('foo', '🐴'), '🐴'); @@ -376,7 +377,7 @@ test('encryption', (t: any) => { t.is(config.get('baz.boo'), fixture); }); -test('encryption - upgrade', (t: any) => { +test('encryption - upgrade', t => { const cwd = tempy.directory(); const before = new Conf({cwd}); @@ -387,7 +388,7 @@ test('encryption - upgrade', (t: any) => { t.is(after.get('foo'), fixture); }); -test('encryption - corrupt file', (t: any) => { +test('encryption - corrupt file', t => { const cwd = tempy.directory(); const before = new Conf({cwd, encryptionKey: 'abc123'}); @@ -400,7 +401,7 @@ test('encryption - corrupt file', (t: any) => { t.is(after.get('foo'), undefined); }); -test('decryption - migration to initialization vector', (t: any) => { +test('decryption - migration to initialization vector', t => { // The `test/config-encrypted-with-conf-4-1-0.json` file contains `{"unicorn": "🦄"}` JSON data which is encrypted with conf@4.1.0 and password `abcd1234` const config = new Conf({ cwd: 'test', @@ -411,7 +412,7 @@ test('decryption - migration to initialization vector', (t: any) => { t.deepEqual(config.store, {unicorn: '🦄'}); }); -test('onDidChange()', (t: any) => { +test('onDidChange()', t => { const {config} = t.context; t.plan(8); @@ -457,7 +458,7 @@ test('onDidChange()', (t: any) => { config.set('foo', fixture); }); -test('onDidAnyChange()', (t: any) => { +test('onDidAnyChange()', t => { const {config} = t.context; t.plan(8); @@ -522,7 +523,7 @@ test('onDidAnyChange()', (t: any) => { }); // See #32 -test('doesn\'t write to disk upon instanciation if and only if the store didn\'t change', (t: any) => { +test('doesn\'t write to disk upon instanciation if and only if the store didn\'t change', t => { let exists = fs.existsSync(t.context.config.path); t.is(exists, false); @@ -536,7 +537,7 @@ test('doesn\'t write to disk upon instanciation if and only if the store didn\'t t.is(exists, true); }); -test('`clearInvalidConfig` option - invalid data', (t: any) => { +test('`clearInvalidConfig` option - invalid data', t => { const config = new Conf({cwd: tempy.directory(), clearInvalidConfig: false}); fs.writeFileSync(config.path, '🦄'); @@ -545,20 +546,20 @@ test('`clearInvalidConfig` option - invalid data', (t: any) => { }, {instanceOf: SyntaxError}); }); -test('`clearInvalidConfig` option - valid data', (t: any) => { +test('`clearInvalidConfig` option - valid data', t => { const config = new Conf({cwd: tempy.directory(), clearInvalidConfig: false}); config.set('foo', 'bar'); t.deepEqual(config.store, {foo: 'bar'}); }); -test('schema - should be an object', (t: any) => { +test('schema - should be an object', t => { const schema: any = 'object'; t.throws(() => { new Conf({cwd: tempy.directory(), schema}); }, 'The `schema` option must be an object.'); }); -test('schema - valid set', (t: any) => { +test('schema - valid set', t => { const schema = { foo: { type: 'object', @@ -579,7 +580,7 @@ test('schema - valid set', (t: any) => { }); }); -test('schema - one violation', (t: any) => { +test('schema - one violation', t => { const schema = { foo: { type: 'string' @@ -591,7 +592,7 @@ test('schema - one violation', (t: any) => { }, 'Config schema violation: `foo` should be string'); }); -test('schema - multiple violations', (t: any) => { +test('schema - multiple violations', t => { const schema = { foo: { type: 'object', @@ -612,7 +613,7 @@ test('schema - multiple violations', (t: any) => { }, 'Config schema violation: `foo.bar` should be number; `foo.foobar` should be <= 100'); }); -test('schema - complex schema', (t: any) => { +test('schema - complex schema', t => { const schema = { foo: { type: 'string', @@ -637,7 +638,7 @@ test('schema - complex schema', (t: any) => { }, 'Config schema violation: `bar` should NOT have more than 3 items; `bar[3]` should be integer; `bar` should NOT have duplicate items (items ## 1 and 0 are identical)'); }); -test('schema - invalid write to config file', (t: any) => { +test('schema - invalid write to config file', t => { const schema = { foo: { type: 'string' @@ -652,7 +653,7 @@ test('schema - invalid write to config file', (t: any) => { }, 'Config schema violation: `foo` should be string'); }); -test('schema - default', (t: any) => { +test('schema - default', t => { const schema = { foo: { type: 'string', @@ -666,7 +667,7 @@ test('schema - default', (t: any) => { t.is(config.get('foo'), 'bar'); }); -test('schema - Conf defaults overwrites schema default', (t: any) => { +test('schema - Conf defaults overwrites schema default', t => { const schema = { foo: { type: 'string', @@ -683,7 +684,7 @@ test('schema - Conf defaults overwrites schema default', (t: any) => { t.is(config.get('foo'), 'foo'); }); -test('schema - validate Conf default', (t: any) => { +test('schema - validate Conf default', t => { const schema = { foo: { type: 'string' @@ -700,21 +701,21 @@ test('schema - validate Conf default', (t: any) => { }, 'Config schema violation: `foo` should be string'); }); -test('.get() - without dot notation', (t: any) => { +test('.get() - without dot notation', t => { t.is(t.context.configWithoutDotNotation.get('foo'), undefined); t.is(t.context.configWithoutDotNotation.get('foo', '🐴'), '🐴'); t.context.configWithoutDotNotation.set('foo', fixture); t.is(t.context.configWithoutDotNotation.get('foo'), fixture); }); -test('.set() - without dot notation', (t: any) => { +test('.set() - without dot notation', t => { t.context.configWithoutDotNotation.set('foo', fixture); t.context.configWithoutDotNotation.set('baz.boo', fixture); t.is(t.context.configWithoutDotNotation.get('foo'), fixture); t.is(t.context.configWithoutDotNotation.get('baz.boo'), fixture); }); -test('.set() - with object - without dot notation', (t: any) => { +test('.set() - with object - without dot notation', t => { t.context.configWithoutDotNotation.set({ foo1: 'bar1', foo2: 'bar2', @@ -732,7 +733,7 @@ test('.set() - with object - without dot notation', (t: any) => { t.is(t.context.configWithoutDotNotation.get('baz.foo.bar'), undefined); }); -test('.has() - without dot notation', (t: any) => { +test('.has() - without dot notation', t => { t.context.configWithoutDotNotation.set('foo', fixture); t.context.configWithoutDotNotation.set('baz.boo', fixture); t.true(t.context.configWithoutDotNotation.has('foo')); @@ -740,7 +741,7 @@ test('.has() - without dot notation', (t: any) => { t.false(t.context.configWithoutDotNotation.has('missing')); }); -test('.delete() - without dot notation', (t: any) => { +test('.delete() - without dot notation', t => { const {configWithoutDotNotation} = t.context; configWithoutDotNotation.set('foo', 'bar'); configWithoutDotNotation.set('baz.boo', true); @@ -757,7 +758,7 @@ test('.delete() - without dot notation', (t: any) => { t.deepEqual(configWithoutDotNotation.get('foo.bar.zoo'), {awesome: 'redpanda'}); }); -test('`watch` option watches for config file changes by another process', async (t: any) => { +test('`watch` option watches for config file changes by another process', async t => { if (process.platform === 'darwin' && process.version.split('.')[0] === 'v8') { t.plan(0); return; @@ -789,7 +790,7 @@ test('`watch` option watches for config file changes by another process', async await pEvent(events, 'change'); }); -test('`watch` option watches for config file changes by file write', async (t: any) => { +test('`watch` option watches for config file changes by file write', async t => { // TODO: Remove this when targeting Node.js 10. if (process.platform === 'darwin' && process.version.split('.')[0] === 'v8') { t.plan(0); @@ -819,7 +820,7 @@ test('`watch` option watches for config file changes by file write', async (t: a await pEvent(events, 'change'); }); -test('migrations - should save the project version as the initial migrated version', (t: any) => { +test('migrations - should save the project version as the initial migrated version', t => { const cwd = tempy.directory(); const conf = new Conf({cwd, projectVersion: '0.0.2', migrations: {}}); @@ -827,7 +828,7 @@ test('migrations - should save the project version as the initial migrated versi t.is(conf._get('__internal__.migrations.version'), '0.0.2'); }); -test('migrations - should save the project version when a migration occurs', (t: any) => { +test('migrations - should save the project version when a migration occurs', t => { const cwd = tempy.directory(); const migrations = { @@ -846,7 +847,7 @@ test('migrations - should save the project version when a migration occurs', (t: t.is(conf2.get('foo'), 'cool stuff'); }); -test('migrations - should NOT run the migration when the version doesn\'t change', (t: any) => { +test('migrations - should NOT run the migration when the version doesn\'t change', t => { const cwd = tempy.directory(); const migrations = { @@ -865,7 +866,7 @@ test('migrations - should NOT run the migration when the version doesn\'t change t.false(conf2.has('foo')); }); -test('migrations - should run the migration when the version changes', (t: any) => { +test('migrations - should run the migration when the version changes', t => { const cwd = tempy.directory(); const migrations = { @@ -885,7 +886,7 @@ test('migrations - should run the migration when the version changes', (t: any) t.is(conf2.get('foo'), 'cool stuff'); }); -test('migrations - should run the migration when the version uses semver comparisons', (t: any) => { +test('migrations - should run the migration when the version uses semver comparisons', t => { const cwd = tempy.directory(); const migrations = { '>=1.0': (store: any) => { @@ -898,7 +899,7 @@ test('migrations - should run the migration when the version uses semver compari t.is(conf.get('foo'), 'cool stuff'); }); -test('migrations - should run the migration when the version uses multiple semver comparisons', (t: any) => { +test('migrations - should run the migration when the version uses multiple semver comparisons', t => { const cwd = tempy.directory(); const migrations = { '>=1.0': (store: any) => { @@ -918,7 +919,7 @@ test('migrations - should run the migration when the version uses multiple semve t.is(conf2.get('foo'), 'modern cool stuff'); }); -test('migrations - should run all valid migrations when the version uses multiple semver comparisons', (t: any) => { +test('migrations - should run all valid migrations when the version uses multiple semver comparisons', t => { const cwd = tempy.directory(); const migrations = { '>=1.0': (store: any) => { @@ -942,7 +943,7 @@ test('migrations - should run all valid migrations when the version uses multipl t.is(conf.get('heart'), '❤'); }); -test('migrations - should cleanup migrations with non-numeric values', (t: any) => { +test('migrations - should cleanup migrations with non-numeric values', t => { const cwd = tempy.directory(); const migrations = { '1.0.1-alpha': (store: any) => { @@ -966,7 +967,7 @@ test('migrations - should cleanup migrations with non-numeric values', (t: any) t.is(conf.get('heart'), '❤'); }); -test('migrations - should infer the applicationVersion from the package.json when it isn\'t specified', (t: any) => { +test('migrations - should infer the applicationVersion from the package.json when it isn\'t specified', t => { const cwd = tempy.directory(); const conf = new Conf({cwd, migrations: { @@ -979,7 +980,7 @@ test('migrations - should infer the applicationVersion from the package.json whe t.is(conf._get('__internal__.migrations.version'), require('../package.json').version); }); -test('migrations - should NOT throw an error when project version is unspecified but there are no migrations', (t: any) => { +test('migrations - should NOT throw an error when project version is unspecified but there are no migrations', t => { const cwd = tempy.directory(); t.notThrows(() => { @@ -988,14 +989,14 @@ test('migrations - should NOT throw an error when project version is unspecified }); }); -test('migrations - should not create the previous migration key if the migrations aren\'t needed', (t: any) => { +test('migrations - should not create the previous migration key if the migrations aren\'t needed', t => { const cwd = tempy.directory(); const conf = new Conf({cwd}); t.false(conf.has('__internal__.migrations.version')); }); -test('migrations error handling - should rollback changes if a migration failed', (t: any) => { +test('migrations error handling - should rollback changes if a migration failed', t => { const cwd = tempy.directory(); const failingMigrations = { @@ -1029,7 +1030,7 @@ test('migrations error handling - should rollback changes if a migration failed' t.is(conf.get('foo'), 'initial update'); }); -test('__internal__ keys - should not be accessible by the user', (t: any) => { +test('__internal__ keys - should not be accessible by the user', t => { const cwd = tempy.directory(); const conf = new Conf({cwd}); @@ -1039,7 +1040,7 @@ test('__internal__ keys - should not be accessible by the user', (t: any) => { }, /Please don't use the __internal__ key/); }); -test('__internal__ keys - should not be accessible by the user even without dot notation', (t: any) => { +test('__internal__ keys - should not be accessible by the user even without dot notation', t => { const cwd = tempy.directory(); const conf = new Conf({cwd, accessPropertiesByDotNotation: false}); @@ -1053,7 +1054,7 @@ test('__internal__ keys - should not be accessible by the user even without dot }, /Please don't use the __internal__ key/); }); -test('__internal__ keys - should only match specific "__internal__" entry', (t: any) => { +test('__internal__ keys - should only match specific "__internal__" entry', t => { const cwd = tempy.directory(); const conf = new Conf({cwd}); From e32335f6990244dcddc03c40f10e11459ffba6e5 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Wed, 4 Dec 2019 14:43:47 +0000 Subject: [PATCH 12/13] fix: improve typescript typings and documentation --- package.json | 20 ++++---- source/index.ts | 86 ++++++++++++++++---------------- test/{index.ts => index.spec.ts} | 6 +-- tsconfig.json | 5 +- 4 files changed, 60 insertions(+), 57 deletions(-) rename test/{index.ts => index.spec.ts} (99%) diff --git a/package.json b/package.json index d341336..9f6ec02 100644 --- a/package.json +++ b/package.json @@ -66,10 +66,12 @@ "nyc": { "extension": [ ".ts" - ] + ], + "include": ["source/**"] }, "dependencies": { "ajv": "^6.10.2", + "crypto": "^1.0.1", "debounce-fn": "^3.0.1", "dot-prop": "^5.2.0", "env-paths": "^2.2.0", @@ -81,24 +83,24 @@ "write-file-atomic": "^3.0.1" }, "devDependencies": { - "@sindresorhus/tsconfig": "^0.4.0", - "@types/node": "^12.12.11", + "@sindresorhus/tsconfig": "^0.6.0", + "@types/node": "^12.12.12", "@types/semver": "^6.2.0", "@types/write-file-atomic": "^2.1.2", - "@typescript-eslint/eslint-plugin": "^1.13.0", - "@typescript-eslint/parser": "^1.13.0", - "ava": "^2.3.0", + "@typescript-eslint/eslint-plugin": "^2.9.0", + "@typescript-eslint/parser": "^2.9.0", + "ava": "^2.4.0", "clear-module": "^4.0.0", "del": "^5.1.0", "del-cli": "^3.0.0", "delay": "^4.3.0", - "eslint-config-xo-typescript": "^0.15.0", + "eslint-config-xo-typescript": "^0.22.0", "nyc": "^14.1.1", "p-event": "^4.1.0", "tempy": "^0.3.0", "ts-node": "^8.5.2", - "tsd": "^0.7.4", + "tsd": "^0.11.0", "typescript": "^3.7.2", - "xo": "^0.24.0" + "xo": "^0.25.3" } } diff --git a/source/index.ts b/source/index.ts index 7026539..efcc06c 100644 --- a/source/index.ts +++ b/source/index.ts @@ -11,12 +11,12 @@ import writeFileAtomic = require('write-file-atomic'); import Ajv = require('ajv'); import debounceFn = require('debounce-fn'); import semver = require('semver'); -import onetime from 'onetime'; +import onetime = require('onetime'); -const plainObject: () => object = () => Object.create(null); const encryptionAlgorithm = 'aes-256-cbc'; // Prevent caching of this module so module.parent is always accurate +// eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete require.cache[__filename]; const parentDir = path.dirname((module.parent && module.parent.filename) || '.'); @@ -37,10 +37,10 @@ const checkValueType = (key: string, value: unknown): void => { const INTERNAL_KEY = '__internal__'; const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`; -export default class Conf implements Iterable<[string, T]> { - _options: Options; +export default class Conf implements Iterable<[string, T]> { + _options: Partial>; - _defaultValues: DefaultValues = {}; + _defaultValues: {[key: string]: T} = {}; _validator?: Ajv.ValidateFunction; @@ -48,23 +48,21 @@ export default class Conf implements Iterable<[string, T]> { events: EventEmitter; - serialize: Serializer; + serialize: (value: {[key: string]: T}) => string; - deserialize: Deserializer | undefined; + deserialize: (text: string) => T; path: string; /** Simple config handling for your app or module. */ - constructor(partialOptions?: Partial) { - const options: Options = { + constructor(partialOptions?: Partial>) { + const options: Partial> = { configName: 'config', fileExtension: 'json', projectSuffix: 'nodejs', clearInvalidConfig: true, - serialize: (value: unknown) => JSON.stringify(value, null, '\t'), - deserialize: (arg: string | Buffer) => JSON.parse(arg.toString()), accessPropertiesByDotNotation: true, ...partialOptions }; @@ -126,15 +124,16 @@ export default class Conf implements Iterable<[string, T]> { this.events = new EventEmitter(); this.encryptionKey = options.encryptionKey; - this.serialize = options.serialize; - this.deserialize = options.deserialize; + this.serialize = options.serialize ? options.serialize : value => JSON.stringify(value, null, '\t'); + this.deserialize = options.deserialize ? options.deserialize : (arg: string): T => JSON.parse(arg); const fileExtension = options.fileExtension ? `.${options.fileExtension}` : ''; - this.path = path.resolve(options.cwd, `${options.configName}${fileExtension}`); + this.path = path.resolve(options.cwd, `${options.configName || 'config'}${fileExtension}`); const fileStore = this.store; - const store = Object.assign(plainObject(), options.defaults, fileStore); + const store = Object.assign(this.createPlainObject(), options.defaults, fileStore); this._validate(store); + try { assert.deepEqual(fileStore, store); } catch (_) { @@ -172,7 +171,7 @@ export default class Conf implements Iterable<[string, T]> { return false; } - const errors = this._validator.errors.reduce((error, {dataPath, message}) => + const errors = this._validator.errors.reduce((error, {dataPath, message = ''}) => error + ` \`${dataPath.slice(1)}\` ${message};`, ''); throw new Error('Config schema violation:' + errors.slice(0, -1)); } @@ -183,8 +182,8 @@ export default class Conf implements Iterable<[string, T]> { makeDir.sync(path.dirname(this.path)); } - _write(value: unknown): void { - let data: string | Buffer = this.serialize && this.serialize(value); + _write(value: {[key: string]: T}): void { + let data: string | Buffer = this.serialize(value); if (this.encryptionKey) { const initializationVector = crypto.randomBytes(16); @@ -216,7 +215,7 @@ export default class Conf implements Iterable<[string, T]> { } _migrate(migrations: Migrations, versionToMigrate: string): void { - let previousMigratedVersion: string = this._get(MIGRATION_KEY, '0.0.0'); + let previousMigratedVersion: string = this._get(MIGRATION_KEY, '0.0.0') as string; const newerVersions: string[] = Object.keys(migrations) .filter(candidateVersion => this._shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate)); @@ -236,7 +235,7 @@ export default class Conf implements Iterable<[string, T]> { this.store = storeBackup; throw new Error( - `Something went wrong during the migration! Changes applied to the store until this failed migration will be restored. ${error}` + `Something went wrong during the migration! Changes applied to the store until this failed migration will be restored. ${error as string}` ); } } @@ -294,7 +293,7 @@ export default class Conf implements Iterable<[string, T]> { return true; } - _get(key: string, defaultValue?: unknown): any { + _get(key: string, defaultValue?: unknown): unknown { return dotProp.get(this.store, key, defaultValue); } @@ -325,7 +324,7 @@ export default class Conf implements Iterable<[string, T]> { @param key - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. @param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`. */ - set(key: string | {[key: string]: unknown}, value?: unknown): void { + set(key: string | {[key: string]: T}, value?: T): void { if (typeof key !== 'string' && typeof key !== 'object') { throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`); } @@ -340,7 +339,7 @@ export default class Conf implements Iterable<[string, T]> { const {store} = this; - const set = (key: string, value: unknown): void => { + const set = (key: string, value: T): void => { checkValueType(key, value); if (this._options.accessPropertiesByDotNotation) { dotProp.set(store, key, value); @@ -355,7 +354,7 @@ export default class Conf implements Iterable<[string, T]> { set(key, value); } } else { - set(key, value); + set(key, value as T); } this.store = store; @@ -397,6 +396,7 @@ export default class Conf implements Iterable<[string, T]> { if (this._options.accessPropertiesByDotNotation) { dotProp.delete(store, key); } else { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete store[key]; } @@ -407,7 +407,7 @@ export default class Conf implements Iterable<[string, T]> { Delete all items. */ clear(): void{ - this.store = plainObject(); + this.store = this.createPlainObject(); } /** @@ -489,32 +489,36 @@ export default class Conf implements Iterable<[string, T]> { } } + createPlainObject(): {[key: string]: T} { + return Object.create(null); + } + get size(): number { return Object.keys(this.store).length; } - get store(): any { + get store(): {[key: string]: T} { try { const data: string | Buffer = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8'); - const dataString: string | Buffer = this.encryptData(data); + const dataString = this.encryptData(data) as string; const deserializedData = this.deserialize && this.deserialize(dataString); this._validate(deserializedData); - return Object.assign(plainObject(), deserializedData); + return Object.assign(this.createPlainObject(), deserializedData); } catch (error) { if (error.code === 'ENOENT') { this._ensureDirectory(); - return plainObject(); + return this.createPlainObject(); } if (this._options.clearInvalidConfig && error.name === 'SyntaxError') { - return plainObject(); + return this.createPlainObject(); } throw error; } } - set store(value: any) { + set store(value: {[key: string]: T}) { this._ensureDirectory(); this._validate(value); @@ -523,25 +527,20 @@ export default class Conf implements Iterable<[string, T]> { this.events?.emit('change'); } - * [Symbol.iterator](): IterableIterator<[string, any]> { + * [Symbol.iterator](): IterableIterator<[string, T]> { for (const [key, value] of Object.entries(this.store)) { yield [key, value]; } } } -export type Serializer = (...args: unknown[]) => string; -export type Deserializer = (arg: string | Buffer) => unknown; -export type DefaultValues = { - [key: string]: object; -}; export type Migrations = { - [key: string]: (store: Conf) => void; + [key: string]: (store: unknown) => void; }; export type Schema = object | boolean; -export type Options = { +export interface Options { /** Access nested properties by dot notation. @@ -586,6 +585,7 @@ export type Options = { @default true */ clearInvalidConfig?: boolean; + /** Name of the config file (without extension). @@ -593,7 +593,7 @@ export type Options = { @default 'config' */ - configName?: string; + configName: string; /** __You most likely don't need this. Please don't use it unless you really have to.__ @@ -733,7 +733,7 @@ export type Options = { @default value => JSON.stringify(value, null, '\t') */ - serialize: Serializer; + serialize?: (value: {[key: string]: T}) => string; /** Function to deserialize the config object from a UTF-8 string when reading the config file. @@ -742,5 +742,5 @@ export type Options = { @default JSON.parse */ - deserialize?: Deserializer; -}; + deserialize?: (text: string) => T; +} diff --git a/test/index.ts b/test/index.spec.ts similarity index 99% rename from test/index.ts rename to test/index.spec.ts index 284cd19..87ab119 100644 --- a/test/index.ts +++ b/test/index.spec.ts @@ -1,5 +1,4 @@ -/* eslint-disable no-new */ -/* eslint-disable ava/use-test */ +/* eslint-disable no-new, @typescript-eslint/no-empty-function, ava/use-test */ import fs = require('fs'); import path = require('path'); import tempy = require('tempy'); @@ -542,7 +541,8 @@ test('`clearInvalidConfig` option - invalid data', t => { fs.writeFileSync(config.path, '🦄'); t.throws(() => { - config.store; // eslint-disable-line no-unused-expressions + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + config.store; }, {instanceOf: SyntaxError}); }); diff --git a/tsconfig.json b/tsconfig.json index 30d9578..144f348 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ ] }, "include": [ - "source/*" - ] + "source/*.ts" + ], + "exclude": ["node_modules", "dist"], } From c08e4d41b9ac94617f85f1f40d7dfc83ac3a33dd Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Wed, 4 Dec 2019 15:08:13 +0000 Subject: [PATCH 13/13] fix: test typings --- package.json | 11 +++--- source/index.ts | 16 ++++---- test/index.spec.ts | 93 ++++++++++++++++++++++------------------------ 3 files changed, 56 insertions(+), 64 deletions(-) diff --git a/package.json b/package.json index 9f6ec02..b8b5add 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,7 @@ }, "scripts": { "build": "del dist && tsc", - "test": "npm run build && xo && nyc ava", - "bench": "ts-node bench.ts", + "test": "npm run build && xo source/** && nyc ava", "prepublishOnly": "npm run build" }, "files": [ @@ -58,16 +57,16 @@ "ts" ], "rules": { - "import/first": "off", - "import/newline-after-import": "off", - "@typescript-eslint/member-ordering": "off" + "@typescript-eslint/no-dynamic-delete": "off" } }, "nyc": { "extension": [ ".ts" ], - "include": ["source/**"] + "include": [ + "source/**" + ] }, "dependencies": { "ajv": "^6.10.2", diff --git a/source/index.ts b/source/index.ts index efcc06c..fb9d954 100644 --- a/source/index.ts +++ b/source/index.ts @@ -16,7 +16,6 @@ import onetime = require('onetime'); const encryptionAlgorithm = 'aes-256-cbc'; // Prevent caching of this module so module.parent is always accurate -// eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete require.cache[__filename]; const parentDir = path.dirname((module.parent && module.parent.filename) || '.'); @@ -214,7 +213,7 @@ export default class Conf implements Iterable<[string, T]> { }, {wait: 100})); } - _migrate(migrations: Migrations, versionToMigrate: string): void { + _migrate(migrations: Migrations, versionToMigrate: string): void { let previousMigratedVersion: string = this._get(MIGRATION_KEY, '0.0.0') as string; const newerVersions: string[] = Object.keys(migrations) @@ -310,7 +309,7 @@ export default class Conf implements Iterable<[string, T]> { @param key - The key of the item to get. @param defaultValue - The default value if the item does not exist. */ - get(key: string, defaultValue?: unknown): unknown { + get(key: string, defaultValue?: T): T | undefined { if (this._options.accessPropertiesByDotNotation) { return dotProp.get(this.store, key, defaultValue); } @@ -396,7 +395,6 @@ export default class Conf implements Iterable<[string, T]> { if (this._options.accessPropertiesByDotNotation) { dotProp.delete(store, key); } else { - // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete store[key]; } @@ -465,7 +463,7 @@ export default class Conf implements Iterable<[string, T]> { return () => this.events?.removeListener('change', onChange); } - encryptData(data: string | Buffer): string | Buffer { + _encryptData(data: string | Buffer): string | Buffer { if (!this.encryptionKey) { return data; } @@ -500,7 +498,7 @@ export default class Conf implements Iterable<[string, T]> { get store(): {[key: string]: T} { try { const data: string | Buffer = fs.readFileSync(this.path, this.encryptionKey ? null : 'utf8'); - const dataString = this.encryptData(data) as string; + const dataString = this._encryptData(data) as string; const deserializedData = this.deserialize && this.deserialize(dataString); this._validate(deserializedData); return Object.assign(this.createPlainObject(), deserializedData); @@ -534,8 +532,8 @@ export default class Conf implements Iterable<[string, T]> { } } -export type Migrations = { - [key: string]: (store: unknown) => void; +export type Migrations = { + [key: string]: (store: Conf) => void; }; export type Schema = object | boolean; @@ -658,7 +656,7 @@ export interface Options { }); ``` */ - migrations?: Migrations; + migrations?: Migrations; /** You only need to specify this if you don't have a package.json file in your project or if it doesn't have a name defined within it. diff --git a/test/index.spec.ts b/test/index.spec.ts index 87ab119..42b64cf 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -248,12 +248,12 @@ test('`serialize` and `deserialize` options', t => { t.plan(4); const serialized = `foo:${fixture}`; const deserialized = {foo: fixture}; - const serialize = (value: any): string => { + const serialize = (value: unknown): string => { t.is(value, deserialized); return serialized; }; - const deserialize = (value: any): any => { + const deserialize = (value: unknown): unknown => { t.is(value, serialized); return deserialized; }; @@ -311,28 +311,26 @@ test('automatic `projectName` inference', t => { test('`cwd` option overrides `projectName` option', t => { const cwd = tempy.directory(); - let config: any; t.notThrows(() => { - config = new Conf({cwd, projectName: ''}); + const config: Conf = new Conf({cwd, projectName: ''}); + t.true(config.path.startsWith(cwd)); + t.is(config.get('foo'), undefined); + config.set('foo', fixture); + t.is(config.get('foo'), fixture); + del.sync(config.path, {force: true}); }); - - t.true(config.path.startsWith(cwd)); - t.is(config.get('foo'), undefined); - config.set('foo', fixture); - t.is(config.get('foo'), fixture); - del.sync(config.path, {force: true}); }); test('safely handle missing package.json', t => { const pkgUpSyncOrig = pkgUp.sync; pkgUp.sync = () => null; - let config: any; + let config: Conf; t.notThrows(() => { config = new Conf({projectName: 'conf-fixture-project-name'}); + del.sync(config.path, {force: true}); }); - del.sync(config.path, {force: true}); pkgUp.sync = pkgUpSyncOrig; }); @@ -340,30 +338,27 @@ test('handle `cwd` being set and `projectName` not being set', t => { const pkgUpSyncOrig = pkgUp.sync; pkgUp.sync = () => null; - let config: any; + let config: Conf; t.notThrows(() => { config = new Conf({cwd: 'conf-fixture-cwd'}); + del.sync(path.dirname(config.path)); }); - del.sync(path.dirname(config.path)); pkgUp.sync = pkgUpSyncOrig; }); // See #11 test('fallback to cwd if `module.filename` is `null`', t => { const preservedFilename: string = module.filename; - const filename: any = null; - module.filename = filename; + module.filename = ''; clearModule('.'); - let config: any; t.notThrows(() => { const Conf = require('../dist').default; - config = new Conf({cwd: 'conf-fixture-fallback-module-filename-null'}); + const config: Conf = new Conf({cwd: 'conf-fixture-fallback-module-filename-null'}); + del.sync(path.dirname(config.path)); + module.filename = preservedFilename; }); - - module.filename = preservedFilename; - del.sync(path.dirname(config.path)); }); test('encryption', t => { @@ -416,12 +411,12 @@ test('onDidChange()', t => { t.plan(8); - const checkFoo = (newValue: any, oldValue: any): void => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🐴'); t.is(oldValue, fixture); }; - const checkBaz = (newValue: any, oldValue: any): void => { + const checkBaz = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🐴'); t.is(oldValue, fixture); }; @@ -438,12 +433,12 @@ test('onDidChange()', t => { unsubscribe(); config.set('baz.boo', fixture); - const checkUndefined = (newValue: any, oldValue: any): void => { + const checkUndefined = (newValue: unknown, oldValue: unknown): void => { t.is(oldValue, fixture); t.is(newValue, undefined); }; - const checkSet = (newValue: any, oldValue: any): void => { + const checkSet = (newValue: unknown, oldValue: unknown): void => { t.is(oldValue, undefined); t.is(newValue, '🐴'); }; @@ -462,12 +457,12 @@ test('onDidAnyChange()', t => { t.plan(8); - const checkFoo = (newValue: any, oldValue: any): void => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(newValue, {foo: '🐴'}); t.deepEqual(oldValue, {foo: fixture}); }; - const checkBaz = (newValue: any, oldValue: any): void => { + const checkBaz = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(newValue, { foo: fixture, baz: {boo: '🐴'} @@ -490,7 +485,7 @@ test('onDidAnyChange()', t => { unsubscribe(); config.set('baz.boo', fixture); - const checkUndefined = (newValue: any, oldValue: any): void => { + const checkUndefined = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(oldValue, { foo: '🦄', baz: {boo: '🦄'} @@ -501,7 +496,7 @@ test('onDidAnyChange()', t => { }); }; - const checkSet = (newValue: any, oldValue: any): void => { + const checkSet = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(oldValue, { baz: {boo: fixture} }); @@ -771,7 +766,7 @@ test('`watch` option watches for config file changes by another process', async t.plan(4); - const checkFoo = (newValue: any, oldValue: any): void => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🐴'); t.is(oldValue, '👾'); }; @@ -785,7 +780,7 @@ test('`watch` option watches for config file changes by another process', async conf2.set('foo', '🐴'); })(); - const {events}: any = conf1; + const {events} = conf1; await pEvent(events, 'change'); }); @@ -803,7 +798,7 @@ test('`watch` option watches for config file changes by file write', async t => t.plan(2); - const checkFoo = (newValue: any, oldValue: any): void => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🦄'); t.is(oldValue, '🐴'); }; @@ -815,7 +810,7 @@ test('`watch` option watches for config file changes by file write', async t => fs.writeFileSync(path.join(cwd, 'config.json'), JSON.stringify({foo: '🦄'})); })(); - const {events}: any = conf; + const {events} = conf; await pEvent(events, 'change'); }); @@ -832,7 +827,7 @@ test('migrations - should save the project version when a migration occurs', t = const cwd = tempy.directory(); const migrations = { - '0.0.3': (store: any) => { + '0.0.3': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -851,7 +846,7 @@ test('migrations - should NOT run the migration when the version doesn\'t change const cwd = tempy.directory(); const migrations = { - '1.0.0': (store: any) => { + '1.0.0': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -870,7 +865,7 @@ test('migrations - should run the migration when the version changes', t => { const cwd = tempy.directory(); const migrations = { - '1.0.0': (store: any) => { + '1.0.0': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -889,7 +884,7 @@ test('migrations - should run the migration when the version changes', t => { test('migrations - should run the migration when the version uses semver comparisons', t => { const cwd = tempy.directory(); const migrations = { - '>=1.0': (store: any) => { + '>=1.0': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -902,10 +897,10 @@ test('migrations - should run the migration when the version uses semver compari test('migrations - should run the migration when the version uses multiple semver comparisons', t => { const cwd = tempy.directory(); const migrations = { - '>=1.0': (store: any) => { + '>=1.0': (store: Conf) => { store.set('foo', 'cool stuff'); }, - '>2.0.0': (store: any) => { + '>2.0.0': (store: Conf) => { store.set('foo', 'modern cool stuff'); } }; @@ -922,14 +917,14 @@ test('migrations - should run the migration when the version uses multiple semve test('migrations - should run all valid migrations when the version uses multiple semver comparisons', t => { const cwd = tempy.directory(); const migrations = { - '>=1.0': (store: any) => { + '>=1.0': (store: Conf) => { store.set('foo', 'cool stuff'); }, - '>2.0.0': (store: any) => { + '>2.0.0': (store: Conf) => { store.set('woof', 'oof'); store.set('medium', 'yes'); }, - '<3.0.0': (store: any) => { + '<3.0.0': (store: Conf) => { store.set('woof', 'woof'); store.set('heart', '❤'); } @@ -946,14 +941,14 @@ test('migrations - should run all valid migrations when the version uses multipl test('migrations - should cleanup migrations with non-numeric values', t => { const cwd = tempy.directory(); const migrations = { - '1.0.1-alpha': (store: any) => { + '1.0.1-alpha': (store: Conf) => { store.set('foo', 'cool stuff'); }, - '>2.0.0-beta': (store: any) => { + '>2.0.0-beta': (store: Conf) => { store.set('woof', 'oof'); store.set('medium', 'yes'); }, - '<3.0.0': (store: any) => { + '<3.0.0': (store: Conf) => { store.set('woof', 'woof'); store.set('heart', '❤'); } @@ -971,7 +966,7 @@ test('migrations - should infer the applicationVersion from the package.json whe const cwd = tempy.directory(); const conf = new Conf({cwd, migrations: { - '2000.0.0': (store: any) => { + '2000.0.0': (store: Conf) => { store.set('foo', 'bar'); } }}); @@ -1000,10 +995,10 @@ test('migrations error handling - should rollback changes if a migration failed' const cwd = tempy.directory(); const failingMigrations = { - '1.0.0': (store: any) => { + '1.0.0': (store: Conf) => { store.set('foo', 'initial update'); }, - '1.0.1': (store: any) => { + '1.0.1': (store: Conf) => { store.set('foo', 'updated before crash'); throw new Error('throw the migration and rollback'); @@ -1014,7 +1009,7 @@ test('migrations error handling - should rollback changes if a migration failed' }; const passingMigrations = { - '1.0.0': (store: any) => { + '1.0.0': (store: Conf) => { store.set('foo', 'initial update'); } };