Skip to content

Commit

Permalink
Require Node.js 12
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 19, 2021
1 parent 3ef86c8 commit 4fa57ef
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ jobs:
node-version:
- 14
- 12
- 10
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"main": "dist/source",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && npm run build && nyc ava",
Expand Down Expand Up @@ -42,35 +42,34 @@
"cache"
],
"dependencies": {
"ajv": "^7.0.3",
"ajv-formats": "^1.5.1",
"ajv": "^8.1.0",
"ajv-formats": "^2.0.2",
"atomically": "^1.7.0",
"debounce-fn": "^4.0.0",
"dot-prop": "^6.0.1",
"env-paths": "^2.2.0",
"env-paths": "^2.2.1",
"json-schema-typed": "^7.0.3",
"make-dir": "^3.1.0",
"onetime": "^5.1.2",
"pkg-up": "^3.1.0",
"semver": "^7.3.4"
"semver": "^7.3.5"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@sindresorhus/tsconfig": "^0.7.0",
"@types/node": "^14.14.20",
"@types/node": "^14.14.41",
"@types/semver": "^7.3.4",
"@types/write-file-atomic": "^3.0.1",
"ava": "^3.15.0",
"clear-module": "^4.1.1",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"delay": "^4.4.0",
"delay": "^5.0.0",
"nyc": "^15.1.0",
"p-event": "^4.2.0",
"tempy": "^1.0.0",
"tempy": "^1.0.1",
"tsd": "^0.14.0",
"typescript": "4.1.3",
"xo": "^0.37.1"
"typescript": "^4.2.4",
"xo": "^0.38.2"
},
"types": "dist/source",
"ava": {
Expand Down
20 changes: 9 additions & 11 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {isDeepStrictEqual} from 'util';
import fs = require('fs');
import path = require('path');
import crypto = require('crypto');
import assert = require('assert');
import {EventEmitter} from 'events';
import dotProp = require('dot-prop');
import makeDir = require('make-dir');
import pkgUp = require('pkg-up');
import envPaths = require('env-paths');
import atomically = require('atomically');
Expand Down Expand Up @@ -411,21 +411,20 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
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);
if (isDeepStrictEqual(newValue, oldValue)) {
return;
}

currentValue = newValue;
callback.call(this, newValue, oldValue);
};

this.events.on('change', onChange);
return () => this.events.removeListener('change', onChange);
}

private readonly _deserialize: Deserialize<T> = value => JSON.parse(value);
private readonly _serialize: Serialize<T> = value => JSON.stringify(value, null, '\t');
private readonly _serialize: Serialize<T> = value => JSON.stringify(value, undefined, '\t');

private _validate(data: T | unknown): void {
if (!this.#validator) {
Expand All @@ -438,14 +437,13 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
}

const errors = this.#validator.errors
.map(({dataPath, message = ''}) => `\`${dataPath.slice(1)}\` ${message}`);
.map(({instancePath, message = ''}) => `\`${instancePath.slice(1)}\` ${message}`);
throw new Error('Config schema violation: ' + errors.join('; '));
}

private _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));
fs.mkdirSync(path.dirname(this.path), {recursive: true});
}

private _write(value: T): void {
Expand Down
2 changes: 1 addition & 1 deletion source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export interface Options<T> {

export type Migrations<T> = Record<string, (store: Conf<T>) => void>;

export type Schema<T> = { [Property in keyof T]: ValueSchema };
export type Schema<T> = {[Property in keyof T]: ValueSchema};
export type ValueSchema = TypedJSONSchema;

export type Serialize<T> = (value: T) => string;
Expand Down
18 changes: 9 additions & 9 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ test('with `suffix` option set to empty string', t => {
const projectName = 'conf-temp1-project';
const config = new Conf({projectSuffix, projectName});
const configPathSegments = config.path.split(path.sep);
const configRootIndex = configPathSegments.findIndex(segment => segment === projectName);
const configRootIndex = configPathSegments.indexOf(projectName);
t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length);
});

Expand All @@ -292,7 +292,7 @@ test('with `projectSuffix` option set to non-empty string', t => {
const config = new Conf({projectSuffix, projectName});
const configPathSegments = config.path.split(path.sep);
const expectedRootName = `${projectName}-${projectSuffix}`;
const configRootIndex = configPathSegments.findIndex(segment => segment === expectedRootName);
const configRootIndex = configPathSegments.indexOf(expectedRootName);
t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length);
});

Expand Down Expand Up @@ -674,7 +674,7 @@ test('schema - one violation', t => {
});
t.throws(() => {
config.set('foo', 1);
}, {message: 'Config schema violation: `foo` should be string'});
}, {message: 'Config schema violation: `foo` must be string'});
});

test('schema - multiple violations', t => {
Expand All @@ -695,7 +695,7 @@ test('schema - multiple violations', t => {
const config = new Conf({cwd: tempy.directory(), schema});
t.throws(() => {
config.set('foo', {bar: '1', foobar: 101});
}, {message: 'Config schema violation: `foo/bar` should be number; `foo/foobar` should be <= 100'});
}, {message: 'Config schema violation: `foo/bar` must be number; `foo/foobar` must be <= 100'});
});

test('schema - complex schema', t => {
Expand All @@ -717,10 +717,10 @@ test('schema - complex schema', t => {
const config = new Conf({cwd: tempy.directory(), schema});
t.throws(() => {
config.set('foo', 'abca');
}, {message: 'Config schema violation: `foo` should NOT have more than 3 characters; `foo` should match pattern "[def]+"'});
}, {message: 'Config schema violation: `foo` must NOT have more than 3 characters; `foo` must match pattern "[def]+"'});
t.throws(() => {
config.set('bar', [1, 1, 2, 'a']);
}, {message: '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)'});
}, {message: 'Config schema violation: `bar` must NOT have more than 3 items; `bar/3` must be integer; `bar` must NOT have duplicate items (items ## 1 and 0 are identical)'});
});

test('schema - supports formats', t => {
Expand All @@ -735,7 +735,7 @@ test('schema - supports formats', t => {
});
t.throws(() => {
config.set('foo', 'bar');
}, {message: 'Config schema violation: `foo` should match format "uri"'});
}, {message: 'Config schema violation: `foo` must match format "uri"'});
});

test('schema - invalid write to config file', t => {
Expand All @@ -750,7 +750,7 @@ test('schema - invalid write to config file', t => {
fs.writeFileSync(path.join(cwd, 'config.json'), JSON.stringify({foo: 1}));
t.throws(() => {
config.get('foo');
}, {message: 'Config schema violation: `foo` should be string'});
}, {message: 'Config schema violation: `foo` must be string'});
});

test('schema - default', t => {
Expand Down Expand Up @@ -803,7 +803,7 @@ test('schema - validate Conf default', t => {
},
schema
});
}, {message: 'Config schema violation: `foo` should be string'});
}, {message: 'Config schema violation: `foo` must be string'});
});

test('.get() - without dot notation', t => {
Expand Down

0 comments on commit 4fa57ef

Please sign in to comment.