diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..78000bc --- /dev/null +++ b/.eslintrc @@ -0,0 +1,4 @@ +{ + "extends": ["xo", "xo-typescript"], + "parser": "@typescript-eslint/parser" +} diff --git a/.gitignore b/.gitignore index 239ecff..5208922 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules yarn.lock +.nyc_output +dist 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.js b/index.js deleted file mode 100644 index c4daca9..0000000 --- a/index.js +++ /dev/null @@ -1,461 +0,0 @@ -/* 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); -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 nonJsonTypes = [ - 'undefined', - 'symbol', - 'function' - ]; - - const type = typeof value; - - if (nonJsonTypes.includes(type)) { - throw new TypeError(`Setting a value of type \`${type}\` for key \`${key}\` is not allowed as it's not supported by JSON`); - } -}; - -const INTERNAL_KEY = '__internal__'; -const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`; - -class Conf { - constructor(options) { - options = { - configName: 'config', - fileExtension: 'json', - projectSuffix: 'nodejs', - clearInvalidConfig: true, - serialize: value => JSON.stringify(value, null, '\t'), - deserialize: JSON.parse, - accessPropertiesByDotNotation: true, - ...options - }; - - const getPackageData = onetime(() => { - const packagePath = pkgUp.sync(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')); - - return packageData || {}; - }); - - if (!options.cwd) { - if (!options.projectName) { - options.projectName = getPackageData().name; - } - - if (!options.projectName) { - throw new Error('Project name could not be inferred. Please specify the `projectName` option.'); - } - - options.cwd = envPaths(options.projectName, {suffix: options.projectSuffix}).config; - } - - this._options = options; - this._defaultValues = {}; - - if (options.schema) { - if (typeof options.schema !== 'object') { - throw new TypeError('The `schema` option must be an object.'); - } - - const ajv = new Ajv({ - allErrors: true, - format: 'full', - useDefaults: true, - errorDataPath: 'property' - }); - const schema = { - type: 'object', - properties: options.schema - }; - - this._validator = ajv.compile(schema); - - for (const [key, value] of Object.entries(options.schema)) { - if (value && value.default) { - this._defaultValues[key] = value.default; - } - } - } - - if (options.defaults) { - this._defaultValues = { - ...this._defaultValues, - ...options.defaults - }; - } - - this.events = new EventEmitter(); - this.encryptionKey = options.encryptionKey; - this.serialize = options.serialize; - this.deserialize = options.deserialize; - - const fileExtension = options.fileExtension ? `.${options.fileExtension}` : ''; - this.path = path.resolve(options.cwd, `${options.configName}${fileExtension}`); - - const fileStore = this.store; - const store = Object.assign(plainObject(), options.defaults, fileStore); - this._validate(store); - try { - assert.deepEqual(fileStore, store); - } catch (_) { - this.store = store; - } - - if (options.watch) { - this._watch(); - } - - if (options.migrations) { - if (!options.projectVersion) { - options.projectVersion = getPackageData().version; - } - - if (!options.projectVersion) { - throw new Error('Project version could not be inferred. Please specify the `projectVersion` option.'); - } - - this._migrate(options.migrations, options.projectVersion); - } - } - - _validate(data) { - if (!this._validator) { - return; - } - - 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)); - } - } - - _ensureDirectory() { - // 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); - - if (this.encryptionKey) { - const initializationVector = crypto.randomBytes(16); - const password = crypto.pbkdf2Sync(this.encryptionKey, initializationVector.toString(), 10000, 32, 'sha512'); - const cipher = crypto.createCipheriv(encryptionAlgorithm, password, initializationVector); - data = Buffer.concat([initializationVector, Buffer.from(':'), cipher.update(Buffer.from(data)), cipher.final()]); - } - - // Temporary workaround for Conf being packaged in a Ubuntu Snap app. - // See https://github.com/sindresorhus/conf/pull/82 - if (process.env.SNAP) { - fs.writeFileSync(this.path, data); - } else { - writeFileAtomic.sync(this.path, data); - } - } - - _watch() { - this._ensureDirectory(); - - if (!fs.existsSync(this.path)) { - this._write({}); - } - - 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'); - }, {wait: 100})); - } - - _migrate(migrations, versionToMigrate) { - let previousMigratedVersion = this._get(MIGRATION_KEY, '0.0.0'); - - const newerVersions = Object.keys(migrations) - .filter(candidateVersion => this._shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate)); - - let storeBackup = {...this.store}; - - for (const version of newerVersions) { - try { - const migration = migrations[version]; - migration(this); - - this._set(MIGRATION_KEY, version); - - previousMigratedVersion = version; - storeBackup = {...this.store}; - } catch (error) { - 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}` - ); - } - } - - if (this._isVersionInRangeFormat(previousMigratedVersion) || !semver.eq(previousMigratedVersion, versionToMigrate)) { - this._set(MIGRATION_KEY, versionToMigrate); - } - } - - _containsReservedKey(key) { - if (typeof key === 'object') { - const firstKey = Object.keys(key)[0]; - - if (firstKey === INTERNAL_KEY) { - return true; - } - } - - if (typeof key !== 'string') { - return false; - } - - if (this._options.accessPropertiesByDotNotation) { - if (key.startsWith(`${INTERNAL_KEY}.`)) { - return true; - } - - return false; - } - - return false; - } - - _isVersionInRangeFormat(version) { - return semver.clean(version) === null; - } - - _shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate) { - if (this._isVersionInRangeFormat(candidateVersion)) { - if (previousMigratedVersion !== '0.0.0' && semver.satisfies(previousMigratedVersion, candidateVersion)) { - return false; - } - - return semver.satisfies(versionToMigrate, candidateVersion); - } - - if (semver.lte(candidateVersion, previousMigratedVersion)) { - return false; - } - - if (semver.gt(candidateVersion, versionToMigrate)) { - return false; - } - - return true; - } - - _get(key, defaultValue) { - return dotProp.get(this.store, key, defaultValue); - } - - _set(key, value) { - const {store} = this; - dotProp.set(store, key, value); - - this.store = store; - } - - get(key, defaultValue) { - if (this._options.accessPropertiesByDotNotation) { - return dotProp.get(this.store, key, defaultValue); - } - - return key in this.store ? this.store[key] : defaultValue; - } - - set(key, value) { - if (typeof key !== 'string' && typeof key !== 'object') { - throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`); - } - - if (typeof key !== 'object' && value === undefined) { - throw new TypeError('Use `delete()` to clear values'); - } - - if (this._containsReservedKey(key)) { - throw new TypeError(`Please don't use the ${INTERNAL_KEY} key, as it's used to manage this module internal operations.`); - } - - const {store} = this; - - const set = (key, value) => { - checkValueType(key, value); - if (this._options.accessPropertiesByDotNotation) { - dotProp.set(store, key, value); - } else { - store[key] = value; - } - }; - - if (typeof key === 'object') { - const object = key; - for (const [key, value] of Object.entries(object)) { - set(key, value); - } - } else { - set(key, value); - } - - this.store = store; - } - - has(key) { - if (this._options.accessPropertiesByDotNotation) { - return dotProp.has(this.store, key); - } - - return key in this.store; - } - - reset(...keys) { - for (const key of keys) { - if (this._defaultValues[key]) { - this.set(key, this._defaultValues[key]); - } - } - } - - delete(key) { - const {store} = this; - if (this._options.accessPropertiesByDotNotation) { - dotProp.delete(store, key); - } else { - delete store[key]; - } - - this.store = store; - } - - clear() { - this.store = plainObject(); - } - - onDidChange(key, callback) { - if (typeof key !== 'string') { - throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof key}`); - } - - if (typeof callback !== 'function') { - throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); - } - - const getter = () => this.get(key); - - return this.handleChange(getter, callback); - } - - onDidAnyChange(callback) { - if (typeof callback !== 'function') { - throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); - } - - const getter = () => this.store; - - return this.handleChange(getter, callback); - } - - handleChange(getter, callback) { - let currentValue = getter(); - - const onChange = () => { - const oldValue = currentValue; - const newValue = getter(); - - try { - // TODO: Use `util.isDeepStrictEqual` when targeting Node.js 10 - assert.deepEqual(newValue, oldValue); - } catch (_) { - currentValue = newValue; - callback.call(this, newValue, oldValue); - } - }; - - this.events.on('change', onChange); - return () => this.events.removeListener('change', onChange); - } - - get size() { - return Object.keys(this.store).length; - } - - get store() { - try { - let data = 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.deserialize(data); - this._validate(data); - return Object.assign(plainObject(), data); - } catch (error) { - if (error.code === 'ENOENT') { - this._ensureDirectory(); - return plainObject(); - } - - if (this._options.clearInvalidConfig && error.name === 'SyntaxError') { - return plainObject(); - } - - throw error; - } - } - - set store(value) { - this._ensureDirectory(); - - this._validate(value); - this._write(value); - - this.events.emit('change'); - } - - * [Symbol.iterator]() { - for (const [key, value] of Object.entries(this.store)) { - yield [key, value]; - } - } -} - -module.exports = 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..b8b5add 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,14 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava && tsd" + "build": "del dist && tsc", + "test": "npm run build && xo source/** && nyc ava", + "prepublishOnly": "npm run build" }, "files": [ - "index.js", - "index.d.ts" + "dist" ], + "types": "dist", "keywords": [ "config", "store", @@ -38,27 +41,65 @@ "write", "cache" ], + "ava": { + "babel": false, + "compileEnhancements": false, + "extensions": [ + "ts" + ], + "require": [ + "ts-node/register" + ] + }, + "xo": { + "extends": "xo-typescript", + "extensions": [ + "ts" + ], + "rules": { + "@typescript-eslint/no-dynamic-delete": "off" + } + }, + "nyc": { + "extension": [ + ".ts" + ], + "include": [ + "source/**" + ] + }, "dependencies": { "ajv": "^6.10.2", + "crypto": "^1.0.1", "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": { - "@types/node": "^12.7.4", - "ava": "^2.3.0", + "@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": "^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.22.0", + "nyc": "^14.1.1", "p-event": "^4.1.0", "tempy": "^0.3.0", - "tsd": "^0.7.4", - "xo": "^0.24.0" + "ts-node": "^8.5.2", + "tsd": "^0.11.0", + "typescript": "^3.7.2", + "xo": "^0.25.3" } } diff --git a/source/index.ts b/source/index.ts new file mode 100644 index 0000000..fb9d954 --- /dev/null +++ b/source/index.ts @@ -0,0 +1,744 @@ +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 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: string, value: unknown): void => { + const nonJsonTypes = [ + 'undefined', + 'symbol', + 'function' + ]; + + const type = typeof value; + + if (nonJsonTypes.includes(type)) { + throw new TypeError(`Setting a value of type \`${type}\` for key \`${key}\` is not allowed as it's not supported by JSON`); + } +}; + +const INTERNAL_KEY = '__internal__'; +const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`; + +export default class Conf implements Iterable<[string, T]> { + _options: Partial>; + + _defaultValues: {[key: string]: T} = {}; + + _validator?: Ajv.ValidateFunction; + + encryptionKey: string | undefined; + + events: EventEmitter; + + serialize: (value: {[key: string]: T}) => string; + + deserialize: (text: string) => T; + + path: string; + + /** + Simple config handling for your app or module. + */ + constructor(partialOptions?: Partial>) { + const options: Partial> = { + configName: 'config', + fileExtension: 'json', + projectSuffix: 'nodejs', + clearInvalidConfig: true, + accessPropertiesByDotNotation: true, + ...partialOptions + }; + + const getPackageData = onetime(() => { + 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')); + + return packageData || {}; + }); + + if (!options.cwd) { + if (!options.projectName) { + options.projectName = getPackageData().name; + } + + if (!options.projectName) { + throw new Error('Project name could not be inferred. Please specify the `projectName` option.'); + } + + options.cwd = envPaths(options.projectName, {suffix: options.projectSuffix}).config; + } + + this._options = options; + + if (options.schema) { + if (typeof options.schema !== 'object') { + throw new TypeError('The `schema` option must be an object.'); + } + + const ajv = new Ajv({ + allErrors: true, + format: 'full', + useDefaults: true, + errorDataPath: 'property' + }); + const schema = { + type: 'object', + properties: options.schema + }; + + this._validator = ajv.compile(schema); + + for (const [key, value] of Object.entries(options.schema)) { + if (value && value.default) { + this._defaultValues[key] = value.default; + } + } + } + + if (options.defaults) { + this._defaultValues = { + ...this._defaultValues, + ...options.defaults + }; + } + + this.events = new EventEmitter(); + this.encryptionKey = options.encryptionKey; + 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 || 'config'}${fileExtension}`); + + const fileStore = this.store; + const store = Object.assign(this.createPlainObject(), options.defaults, fileStore); + this._validate(store); + + try { + assert.deepEqual(fileStore, store); + } catch (_) { + this.store = store; + } + + if (options.watch) { + this._watch(); + } + + if (options.migrations) { + if (!options.projectVersion) { + options.projectVersion = getPackageData().version; + } + + if (!options.projectVersion) { + throw new Error('Project version could not be inferred. Please specify the `projectVersion` option.'); + } + + this._migrate(options.migrations, options.projectVersion); + } + } + + _validate(data: unknown): boolean { + if (!this._validator) { + return false; + } + + const valid = this._validator(data); + 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(): 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: {[key: string]: T}): void { + let data: string | Buffer = this.serialize(value); + + if (this.encryptionKey) { + const initializationVector = crypto.randomBytes(16); + const password = crypto.pbkdf2Sync(this.encryptionKey, initializationVector.toString(), 10000, 32, 'sha512'); + const cipher = crypto.createCipheriv(encryptionAlgorithm, password, initializationVector); + data = Buffer.concat([initializationVector, Buffer.from(':'), cipher.update(Buffer.from(data)), cipher.final()]); + } + + // Temporary workaround for Conf being packaged in a Ubuntu Snap app. + // See https://github.com/sindresorhus/conf/pull/82 + if (process.env.SNAP) { + fs.writeFileSync(this.path, data); + } else { + writeFileAtomic.sync(this.path, data); + } + } + + _watch(): void { + this._ensureDirectory(); + + if (!fs.existsSync(this.path)) { + this._write({}); + } + + 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'); + }, {wait: 100})); + } + + _migrate(migrations: Migrations, versionToMigrate: string): void { + 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)); + + let storeBackup = {...this.store}; + + for (const version of newerVersions) { + try { + const migration = migrations[version]; + migration(this); + + this._set(MIGRATION_KEY, version); + + previousMigratedVersion = version; + storeBackup = {...this.store}; + } catch (error) { + 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 as string}` + ); + } + } + + if (this._isVersionInRangeFormat(previousMigratedVersion) || !semver.eq(previousMigratedVersion, versionToMigrate)) { + this._set(MIGRATION_KEY, versionToMigrate); + } + } + + _containsReservedKey(key: string | {[key: string]: unknown}): boolean { + if (typeof key === 'object') { + const firstKey = Object.keys(key)[0]; + + if (firstKey === INTERNAL_KEY) { + return true; + } + } + + if (typeof key !== 'string') { + return false; + } + + if (this._options.accessPropertiesByDotNotation) { + if (key.startsWith(`${INTERNAL_KEY}.`)) { + return true; + } + + return false; + } + + return false; + } + + _isVersionInRangeFormat(version: string): boolean { + return semver.clean(version) === null; + } + + _shouldPerformMigration(candidateVersion: string, previousMigratedVersion: string, versionToMigrate: string): boolean { + if (this._isVersionInRangeFormat(candidateVersion)) { + if (previousMigratedVersion !== '0.0.0' && semver.satisfies(previousMigratedVersion, candidateVersion)) { + return false; + } + + return semver.satisfies(versionToMigrate, candidateVersion); + } + + if (semver.lte(candidateVersion, previousMigratedVersion)) { + return false; + } + + if (semver.gt(candidateVersion, versionToMigrate)) { + return false; + } + + return true; + } + + _get(key: string, defaultValue?: unknown): unknown { + return dotProp.get(this.store, key, defaultValue); + } + + _set(key: string, value?: unknown): void { + const {store} = this; + dotProp.set(store, key, value); + + 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?: T): T | undefined { + if (this._options.accessPropertiesByDotNotation) { + return dotProp.get(this.store, key, defaultValue); + } + + 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 | {[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}`); + } + + if (typeof key !== 'object' && value === undefined) { + throw new TypeError('Use `delete()` to clear values'); + } + + if (this._containsReservedKey(key)) { + throw new TypeError(`Please don't use the ${INTERNAL_KEY} key, as it's used to manage this module internal operations.`); + } + + const {store} = this; + + const set = (key: string, value: T): void => { + checkValueType(key, value); + if (this._options.accessPropertiesByDotNotation) { + dotProp.set(store, key, value); + } else { + store[key] = value; + } + }; + + if (typeof key === 'object') { + const object = key; + for (const [key, value] of Object.entries(object)) { + set(key, value); + } + } else { + set(key, value as T); + } + + 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); + } + + 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]) { + this.set(key, this._defaultValues[key]); + } + } + } + + /** + Delete an item. + + @param key - The key of the item to delete. + */ + delete(key: string): void { + const {store} = this; + if (this._options.accessPropertiesByDotNotation) { + dotProp.delete(store, key); + } else { + delete store[key]; + } + + this.store = store; + } + + /** + Delete all items. + */ + clear(): void{ + this.store = this.createPlainObject(); + } + + /** + 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}`); + } + + if (typeof callback !== 'function') { + throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); + } + + const getter: () => unknown = () => this.get(key); + + 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: (newValue: unknown, oldValue: unknown) => void): () => unknown { + if (typeof callback !== 'function') { + throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); + } + + const getter: () => unknown = () => this.store; + + return this.handleChange(getter, callback); + } + + handleChange(getter: () => unknown, callback: (newValue: unknown, oldValue: unknown) => void): () => unknown { + let currentValue = getter(); + + const onChange: () => unknown = () => { + const oldValue = currentValue; + const newValue = getter(); + + try { + // TODO: Use `util.isdeepEqual` when targeting Node.js 10 + assert.deepEqual(newValue, oldValue); + } catch (_) { + currentValue = newValue; + callback.call(this, newValue, oldValue); + } + }; + + this.events?.on('change', onChange); + return () => this.events?.removeListener('change', onChange); + } + + _encryptData(data: string | Buffer): string | Buffer { + 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); + const slicedData: any = data.slice(17); + return Buffer.concat([decipher.update(slicedData), decipher.final()]); + } + + // Legacy decryption without initialization vector + const decipher = crypto.createDecipher(encryptionAlgorithm, this.encryptionKey); + const legacyData: any = data; + return Buffer.concat([decipher.update(legacyData), decipher.final()]); + } catch (_) { + return data; + } + } + + createPlainObject(): {[key: string]: T} { + return Object.create(null); + } + + get size(): number { + return Object.keys(this.store).length; + } + + 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 deserializedData = this.deserialize && this.deserialize(dataString); + this._validate(deserializedData); + return Object.assign(this.createPlainObject(), deserializedData); + } catch (error) { + if (error.code === 'ENOENT') { + this._ensureDirectory(); + return this.createPlainObject(); + } + + if (this._options.clearInvalidConfig && error.name === 'SyntaxError') { + return this.createPlainObject(); + } + + throw error; + } + } + + set store(value: {[key: string]: T}) { + this._ensureDirectory(); + + this._validate(value); + this._write(value); + + this.events?.emit('change'); + } + + * [Symbol.iterator](): IterableIterator<[string, T]> { + for (const [key, value] of Object.entries(this.store)) { + yield [key, value]; + } + } +} + +export type Migrations = { + [key: string]: (store: Conf) => void; +}; + +export type Schema = object | boolean; + +export interface 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?: (value: {[key: string]: 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 + */ + deserialize?: (text: string) => T; +} diff --git a/test.js b/test/index.spec.ts similarity index 89% rename from test.js rename to test/index.spec.ts index 2682ad1..42b64cf 100644 --- a/test.js +++ b/test/index.spec.ts @@ -1,14 +1,16 @@ -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, @typescript-eslint/no-empty-function, ava/use-test */ +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 anyTest, {TestInterface} from 'ava'; +import Conf from '../source'; + +const test = anyTest as TestInterface<{config: Conf; configWithoutDotNotation: Conf; configWithSchema: Conf; configWithDefaults: Conf}>; const fixture = '🦄'; test.beforeEach(t => { @@ -85,6 +87,7 @@ test('.set() - with unsupported values', t => { 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'); }); @@ -245,12 +248,12 @@ test('`serialize` and `deserialize` options', t => { t.plan(4); const serialized = `foo:${fixture}`; const deserialized = {foo: fixture}; - const serialize = value => { + const serialize = (value: unknown): string => { t.is(value, deserialized); return serialized; }; - const deserialize = value => { + const deserialize = (value: unknown): unknown => { t.is(value, serialized); return deserialized; }; @@ -308,28 +311,26 @@ test('automatic `projectName` inference', t => { test('`cwd` option overrides `projectName` option', t => { const cwd = tempy.directory(); - let config; 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; + 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; }); @@ -337,29 +338,27 @@ test('handle `cwd` being set and `projectName` not being set', t => { const pkgUpSyncOrig = pkgUp.sync; pkgUp.sync = () => null; - let config; + 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 = module.filename; - module.filename = null; + const preservedFilename: string = module.filename; + module.filename = ''; clearModule('.'); - let config; t.notThrows(() => { - const Conf = require('.'); - config = new Conf({cwd: 'conf-fixture-fallback-module-filename-null'}); + const Conf = require('../dist').default; + 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 => { @@ -412,12 +411,12 @@ test('onDidChange()', t => { t.plan(8); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🐴'); t.is(oldValue, fixture); }; - const checkBaz = (newValue, oldValue) => { + const checkBaz = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🐴'); t.is(oldValue, fixture); }; @@ -434,12 +433,12 @@ test('onDidChange()', t => { unsubscribe(); config.set('baz.boo', fixture); - const checkUndefined = (newValue, oldValue) => { + const checkUndefined = (newValue: unknown, oldValue: unknown): void => { t.is(oldValue, fixture); t.is(newValue, undefined); }; - const checkSet = (newValue, oldValue) => { + const checkSet = (newValue: unknown, oldValue: unknown): void => { t.is(oldValue, undefined); t.is(newValue, '🐴'); }; @@ -458,12 +457,12 @@ test('onDidAnyChange()', t => { t.plan(8); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(newValue, {foo: '🐴'}); t.deepEqual(oldValue, {foo: fixture}); }; - const checkBaz = (newValue, oldValue) => { + const checkBaz = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(newValue, { foo: fixture, baz: {boo: '🐴'} @@ -486,7 +485,7 @@ test('onDidAnyChange()', t => { unsubscribe(); config.set('baz.boo', fixture); - const checkUndefined = (newValue, oldValue) => { + const checkUndefined = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(oldValue, { foo: '🦄', baz: {boo: '🦄'} @@ -497,7 +496,7 @@ test('onDidAnyChange()', t => { }); }; - const checkSet = (newValue, oldValue) => { + const checkSet = (newValue: unknown, oldValue: unknown): void => { t.deepEqual(oldValue, { baz: {boo: fixture} }); @@ -537,7 +536,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}); }); @@ -548,9 +548,9 @@ test('`clearInvalidConfig` option - valid data', t => { }); test('schema - should be an object', t => { - const schema = 'object'; + 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.'); }); @@ -686,7 +686,7 @@ test('schema - validate Conf default', t => { } }; t.throws(() => { - new Conf({ // eslint-disable-line no-new + new Conf({ cwd: tempy.directory(), defaults: { foo: 1 @@ -766,7 +766,7 @@ test('`watch` option watches for config file changes by another process', async t.plan(4); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🐴'); t.is(oldValue, '👾'); }; @@ -780,7 +780,9 @@ test('`watch` option watches for config file changes by another process', async conf2.set('foo', '🐴'); })(); - await pEvent(conf1.events, 'change'); + const {events} = conf1; + + await pEvent(events, 'change'); }); test('`watch` option watches for config file changes by file write', async t => { @@ -796,7 +798,7 @@ test('`watch` option watches for config file changes by file write', async t => t.plan(2); - const checkFoo = (newValue, oldValue) => { + const checkFoo = (newValue: unknown, oldValue: unknown): void => { t.is(newValue, '🦄'); t.is(oldValue, '🐴'); }; @@ -808,7 +810,9 @@ 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} = conf; + + await pEvent(events, 'change'); }); test('migrations - should save the project version as the initial migrated version', t => { @@ -823,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 => { + '0.0.3': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -842,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 => { + '1.0.0': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -861,7 +865,7 @@ test('migrations - should run the migration when the version changes', t => { const cwd = tempy.directory(); const migrations = { - '1.0.0': store => { + '1.0.0': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -880,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 => { + '>=1.0': (store: Conf) => { store.set('foo', 'cool stuff'); } }; @@ -893,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 => { + '>=1.0': (store: Conf) => { store.set('foo', 'cool stuff'); }, - '>2.0.0': store => { + '>2.0.0': (store: Conf) => { store.set('foo', 'modern cool stuff'); } }; @@ -913,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 => { + '>=1.0': (store: Conf) => { store.set('foo', 'cool stuff'); }, - '>2.0.0': store => { + '>2.0.0': (store: Conf) => { store.set('woof', 'oof'); store.set('medium', 'yes'); }, - '<3.0.0': store => { + '<3.0.0': (store: Conf) => { store.set('woof', 'woof'); store.set('heart', '❤'); } @@ -937,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 => { + '1.0.1-alpha': (store: Conf) => { store.set('foo', 'cool stuff'); }, - '>2.0.0-beta': store => { + '>2.0.0-beta': (store: Conf) => { store.set('woof', 'oof'); store.set('medium', 'yes'); }, - '<3.0.0': store => { + '<3.0.0': (store: Conf) => { store.set('woof', 'woof'); store.set('heart', '❤'); } @@ -962,13 +966,13 @@ 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 => { + '2000.0.0': (store: Conf) => { 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 => { @@ -991,10 +995,10 @@ test('migrations error handling - should rollback changes if a migration failed' const cwd = tempy.directory(); const failingMigrations = { - '1.0.0': store => { + '1.0.0': (store: Conf) => { store.set('foo', 'initial update'); }, - '1.0.1': store => { + '1.0.1': (store: Conf) => { store.set('foo', 'updated before crash'); throw new Error('throw the migration and rollback'); @@ -1005,7 +1009,7 @@ test('migrations error handling - should rollback changes if a migration failed' }; const passingMigrations = { - '1.0.0': store => { + '1.0.0': (store: Conf) => { store.set('foo', 'initial update'); } }; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..144f348 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@sindresorhus/tsconfig", + "compilerOptions": { + "outDir": "dist", + "target": "es2017", + "lib": [ + "es2017" + ] + }, + "include": [ + "source/*.ts" + ], + "exclude": ["node_modules", "dist"], +}