configuration
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -9,2 +9,3 @@ "use strict"; | ||
/* JSON */ | ||
//TODO: preserve the existing path keys instead of modifying them | ||
class ProviderJSON extends file_2.default { | ||
@@ -11,0 +12,0 @@ async read() { |
@@ -32,3 +32,3 @@ "use strict"; | ||
this.data = path_prop_1.default.unflat(data); | ||
this.dataRaw = (_b = this.dataParser.stringify(data)) !== null && _b !== void 0 ? _b : this.defaultsRaw; | ||
this.dataRaw = (_b = this.dataParser.stringify(data, this.dataRaw)) !== null && _b !== void 0 ? _b : this.defaultsRaw; | ||
this.dataSchema = this.filterer(this.data); | ||
@@ -35,0 +35,0 @@ } |
@@ -17,3 +17,3 @@ import { WriteOptions } from 'atomically/dist/types'; | ||
parse: (raw: DataRaw) => Data | undefined; | ||
stringify: (data: Data) => DataRaw | undefined; | ||
stringify: (data: Data, dataRawPrev: DataRaw) => DataRaw | undefined; | ||
}; | ||
@@ -20,0 +20,0 @@ declare type Value = ValuePrimitive | ValueArray | ValueObject; |
@@ -6,4 +6,4 @@ import { Data, DataRaw } from '../types'; | ||
parse(raw: DataRaw): Data | undefined; | ||
stringify(data: Data): DataRaw | undefined; | ||
stringify(data: Data, dataRawPrev: DataRaw): DataRaw | undefined; | ||
} | ||
export default Parser; |
@@ -19,15 +19,29 @@ "use strict"; | ||
} | ||
stringify(data) { | ||
stringify(data, dataRawPrev) { | ||
const getContent = (data) => { | ||
if (type_1.default.isArray(data)) { | ||
//TODO: Publish the following code as 2 separate packages | ||
const lines = data.map(item => jsonc_simple_parser_1.default.stringify(item, undefined, ' ') | ||
.replace(/\[\s*?(?:\r?\n|\r)\s*/g, '[') | ||
.replace(/\s*?(?:\r?\n|\r)\s*]/g, ']') | ||
.replace(/{\s*?(?:\r?\n|\r)\s*/g, '{ ') | ||
.replace(/\s*?(?:\r?\n|\r)\s*}/g, ' }') | ||
.replace(/,\s*?(?:\r?\n|\r)\s*/g, ', ')); | ||
const indentation = type_1.default.isString(this.indentation) ? this.indentation : ' '.repeat(this.indentation || 0); | ||
return `[\n${indentation}${lines.join(`,\n${indentation}`)}\n]`; | ||
} | ||
else { | ||
return jsonc_simple_parser_1.default.stringify(data, undefined, this.indentation); | ||
} | ||
}; | ||
const getBackup = (dataRaw) => { | ||
const isValid = jsonc_simple_parser_1.default.validate(dataRaw); | ||
if (isValid) | ||
return ''; | ||
const timestamp = new Date().toLocaleString(), header = `// BACKUP (${timestamp})`, comments = dataRaw.trim().replace(/^/gm, '// '), backup = `\n\n${header}\n${comments}`; | ||
return backup; | ||
}; | ||
try { | ||
if (!type_1.default.isArray(data)) | ||
return jsonc_simple_parser_1.default.stringify(data, undefined, this.indentation); | ||
//TODO: Publish the following code as 2 separate packages | ||
const lines = data.map(item => jsonc_simple_parser_1.default.stringify(item, undefined, ' ') | ||
.replace(/\[\s*?(?:\r?\n|\r)\s*/g, '[') | ||
.replace(/\s*?(?:\r?\n|\r)\s*]/g, ']') | ||
.replace(/{\s*?(?:\r?\n|\r)\s*/g, '{ ') | ||
.replace(/\s*?(?:\r?\n|\r)\s*}/g, ' }') | ||
.replace(/,\s*?(?:\r?\n|\r)\s*/g, ', ')); | ||
const indentation = type_1.default.isString(this.indentation) ? this.indentation : ' '.repeat(this.indentation || 0); | ||
return `[\n${indentation}${lines.join(`,\n${indentation}`)}\n]`; | ||
const content = getContent(data), backup = getBackup(dataRawPrev); | ||
return `${content}${backup}`; | ||
} | ||
@@ -34,0 +48,0 @@ catch (_a) { } |
{ | ||
"name": "configuration", | ||
"description": "Performant and feature rich library for managing configurations/settings.", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"main": "dist/index.js", | ||
@@ -49,14 +49,14 @@ "types": "dist/index.d.ts", | ||
"dependencies": { | ||
"atomically": "^1.3.0", | ||
"atomically": "^1.7.0", | ||
"is-primitive": "^3.0.1", | ||
"jsonc-simple-parser": "^1.0.0", | ||
"path-prop": "^1.1.0", | ||
"jsonc-simple-parser": "^2.1.0", | ||
"path-prop": "^1.1.2", | ||
"plain-object-clone": "^1.1.0", | ||
"plain-object-is-equal": "^1.0.0", | ||
"plain-object-merge": "^1.0.1", | ||
"watcher": "^1.0.0" | ||
"plain-object-merge": "^1.0.2", | ||
"watcher": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"@types/json-schema": "^7.0.4", | ||
"@types/node": "^14.0.5", | ||
"@types/json-schema": "^7.0.7", | ||
"@types/node": "^14.14.35", | ||
"ajv": "^6.12.2", | ||
@@ -66,11 +66,11 @@ "ajv-filter": "^1.1.1", | ||
"ava-spec": "^1.1.1", | ||
"benchloop": "^1.3.1", | ||
"delay": "^4.3.0", | ||
"lodash": "^4.17.15", | ||
"nyc": "^15.0.1", | ||
"benchloop": "^1.3.2", | ||
"delay": "^5.0.0", | ||
"lodash": "^4.17.21", | ||
"nyc": "^15.1.0", | ||
"rimraf": "^3.0.2", | ||
"tempy": "^0.5.0", | ||
"typescript": "^3.9.3", | ||
"typescript": "^4.2.3", | ||
"typescript-transform-export-interop": "^1.0.2" | ||
} | ||
} |
@@ -12,2 +12,4 @@ | ||
//TODO: preserve the existing path keys instead of modifying them | ||
class ProviderJSON<Options extends ProviderJSONOptions = ProviderJSONOptions> extends ProviderFile<Options> { | ||
@@ -14,0 +16,0 @@ |
@@ -48,3 +48,3 @@ | ||
this.data = pp.unflat ( data ); | ||
this.dataRaw = this.dataParser.stringify ( data ) ?? this.defaultsRaw; | ||
this.dataRaw = this.dataParser.stringify ( data, this.dataRaw ) ?? this.defaultsRaw; | ||
this.dataSchema = this.filterer ( this.data ); | ||
@@ -51,0 +51,0 @@ |
@@ -27,3 +27,3 @@ | ||
parse: ( raw: DataRaw ) => Data | undefined, | ||
stringify: ( data: Data ) => DataRaw | undefined | ||
stringify: ( data: Data, dataRawPrev: DataRaw ) => DataRaw | undefined | ||
}; | ||
@@ -30,0 +30,0 @@ |
@@ -32,21 +32,51 @@ | ||
stringify ( data: Data ): DataRaw | undefined { | ||
stringify ( data: Data, dataRawPrev: DataRaw ): DataRaw | undefined { | ||
try { | ||
const getContent = ( data: Data ): DataRaw => { | ||
if ( !Type.isArray ( data ) ) return JSONC.stringify ( data, undefined, this.indentation ); | ||
if ( Type.isArray ( data ) ) { | ||
//TODO: Publish the following code as 2 separate packages | ||
//TODO: Publish the following code as 2 separate packages | ||
const lines = data.map ( item => JSONC.stringify ( item, undefined, ' ' ) | ||
.replace ( /\[\s*?(?:\r?\n|\r)\s*/g, '[' ) | ||
.replace ( /\s*?(?:\r?\n|\r)\s*]/g, ']' ) | ||
.replace ( /{\s*?(?:\r?\n|\r)\s*/g, '{ ' ) | ||
.replace ( /\s*?(?:\r?\n|\r)\s*}/g, ' }' ) | ||
.replace ( /,\s*?(?:\r?\n|\r)\s*/g, ', ' ) ); | ||
const lines = data.map ( item => JSONC.stringify ( item, undefined, ' ' ) | ||
.replace ( /\[\s*?(?:\r?\n|\r)\s*/g, '[' ) | ||
.replace ( /\s*?(?:\r?\n|\r)\s*]/g, ']' ) | ||
.replace ( /{\s*?(?:\r?\n|\r)\s*/g, '{ ' ) | ||
.replace ( /\s*?(?:\r?\n|\r)\s*}/g, ' }' ) | ||
.replace ( /,\s*?(?:\r?\n|\r)\s*/g, ', ' ) ); | ||
const indentation = Type.isString ( this.indentation ) ? this.indentation : ' '.repeat ( this.indentation || 0 ); | ||
const indentation = Type.isString ( this.indentation ) ? this.indentation : ' '.repeat ( this.indentation || 0 ); | ||
return `[\n${indentation}${lines.join ( `,\n${indentation}` )}\n]`; | ||
return `[\n${indentation}${lines.join ( `,\n${indentation}` )}\n]`; | ||
} else { | ||
return JSONC.stringify ( data, undefined, this.indentation ); | ||
} | ||
}; | ||
const getBackup = ( dataRaw: DataRaw ): DataRaw => { | ||
const isValid = JSONC.validate ( dataRaw ); | ||
if ( isValid ) return ''; | ||
const timestamp = new Date ().toLocaleString (), | ||
header = `// BACKUP (${timestamp})`, | ||
comments = dataRaw.trim ().replace ( /^/gm, '// ' ), | ||
backup = `\n\n${header}\n${comments}`; | ||
return backup; | ||
}; | ||
try { | ||
const content = getContent ( data ), | ||
backup = getBackup ( dataRawPrev ); | ||
return `${content}${backup}`; | ||
} catch {} | ||
@@ -53,0 +83,0 @@ |
@@ -546,2 +546,23 @@ | ||
it ( 'can set while preserving the current broken data string', t => { | ||
const conf = new Configuration ( Fixtures.options () ); | ||
const valuePrev = {}, | ||
valuePrevRaw = '\n\n{ "core": {},,, \n "broken": {} }\n\n', | ||
valueNext = { core: { foo: 'asd' } }, | ||
valueNextRaw = `${JSON.stringify ( valueNext, undefined, 2 )}\n\n// BACKUP (${new Date ().toLocaleString ()})\n// { "core": {},,, \n// "broken": {} }`; | ||
conf.scopes.local.writeSync ( valuePrevRaw ); | ||
t.deepEqual ( conf.scopes.local.data, valuePrev ); | ||
t.is ( conf.scopes.local.dataRaw, valuePrevRaw ); | ||
conf.set ( 'local', 'core.foo', 'asd' ); | ||
t.deepEqual ( conf.scopes.local.data, valueNext ); | ||
t.is ( conf.scopes.local.dataRaw, valueNextRaw ); | ||
}); | ||
it ( 'throws when trying to change defaults', t => { | ||
@@ -548,0 +569,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
103967
2701
50
+ Addedjsonc-simple-parser@2.2.1(transitive)
- Removedjsonc-simple-parser@1.0.1(transitive)
Updatedatomically@^1.7.0
Updatedjsonc-simple-parser@^2.1.0
Updatedpath-prop@^1.1.2
Updatedplain-object-merge@^1.0.2
Updatedwatcher@^1.1.1