conf

Delphi源码 2025-08-13

conf

简单的conf IG处理您的应用或模块

您需要关心的就是要坚持什么。该模块将处理所有沉闷的细节,例如在哪里以及如何。

它不支持为同一家商店写的多个过程。
我最初制作了此工具,让命令行工具持续一些数据。

如果需要电子,请查看electron-store

安装

npm install conf 

用法

conf '; const conf ig = new conf ({projectName: 'foo'}); conf ig.set('unicorn', '?'); console.log( conf ig.get('unicorn')); //=> '?' // Use dot-notation to access nested properties conf ig.set('foo.bar', true); console.log( conf ig.get('foo')); //=> {bar: true} conf ig.delete('unicorn'); console.log( conf ig.get('unicorn')); //=> undefined">
 import conf from ' conf ' ;

const conf ig = new conf ( { projectName : 'foo' } ) ;

conf ig . set ( 'unicorn' , '?' ) ;
console . log ( conf ig . get ( 'unicorn' ) ) ;
//=> '?'

// Use dot-notation to access nested properties
conf ig . set ( 'foo.bar' , true ) ;
console . log ( conf ig . get ( 'foo' ) ) ;
//=> {bar: true}

conf ig . delete ( 'unicorn' ) ;
console . log ( conf ig . get ( 'unicorn' ) ) ;
//=> undefined

或创建一个子类。

API

更改是原子上写给磁盘的,因此,如果该过程在写入过程中崩溃,则不会损坏现有的conf ig。

conf (选项?)

返回新实例。

选项

类型: object

默认值

类型: object

conf IG项目的默认值。

注意: defaults中的值将覆盖schema选项中的default键。

模式

类型: object

JSON模式以验证您的conf Ig数据。

这将是JSON模式的properties对象。也就是说,将schema定义为一个对象,其中每个键是数据属性的名称,每个键是用于验证该属性的JSON模式。

例子:

conf '; const schema = { foo: { type: 'number', maximum: 100, minimum: 1, default: 50 }, bar: { type: 'string', format: 'url' } }; const conf ig = new conf ({ projectName: 'foo', schema }); console.log( conf ig.get('foo')); //=> 50 conf ig.set('foo', '1'); // [Error: conf ig schema violation: `foo` should be number]">
 import conf from ' conf ' ;

const schema = {
	foo : {
		type : 'number' ,
		maximum : 100 ,
		minimum : 1 ,
		default : 50
	} ,
	bar : {
		type : 'string' ,
		format : 'url'
	}
} ;

const conf ig = new conf ( {
	projectName : 'foo' ,
	schema
} ) ;

console . log ( conf ig . get ( 'foo' ) ) ;
//=> 50

conf ig . set ( 'foo' , '1' ) ;
// [Error: conf ig schema violation: `foo` should be number]

注意:如果设置, default值将被defaults值覆盖。

Rootschema

类型: object

模式的顶级属性,不包括properties字段。

例子:

conf '; const store = new conf ({ projectName: 'foo', schema: { /* … */ }, rootSchema: { additionalProperties: false } });">
 import conf from ' conf ' ;

const store = new conf ( {
	projectName : 'foo' ,
	schema : { /* … */ } ,
	rootSchema : {
		additionalProperties : false
	}
} ) ; 

ajvoptions

类型: object

选项传递给AJV。

在引擎盖下,使用JSON模式验证器AJV来验证您的conf ig。我们使用JSON Schema Draft-2020-12并支持所有验证关键字和格式。

注意:默认情况下, allErrorsuseDefaults均设置为true ,但可以被覆盖。

例子:

conf '; const store = new conf ({ projectName: 'foo', schema: { /* … */ }, rootSchema: { additionalProperties: false }, ajvOptions: { removeAdditional: true } });">
 import conf from ' conf ' ;

const store = new conf ( {
	projectName : 'foo' ,
	schema : { /* … */ } ,
	rootSchema : {
		additionalProperties : false
	} ,
	ajvOptions : {
		removeAdditional : true
	}
} ) ; 

迁移

类型: object

重要的是:我无法为此功能提供支持。它有一些已知的错误。我没有计划进行的计划,但是欢迎拉动请求。

每当升级项目版本时,您都可以使用迁移来对商店进行操作。

migrations对象应由'version': handler 。该version也可以是SEMVER范围。

例子:

conf '; const store = new conf ({ projectName: 'foo', projectVersion: …, 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'); } } });">
 import conf from ' conf ' ;

const store = new conf ( {
	projectName : 'foo' ,
	projectVersion :  ,
	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' ) ;
		}
	}
} ) ;

注意:迁移使用的版本默认情况下是指项目版本。如果要更改此行为,请指定projectVersion Option。

移民前

类型: Function
默认值: undefined

给定的回调函数将在每个迁移步骤之前调用。

该函数将存储作为第一个参数和上下文对象作为第二个参数,其中包括以下属性:

  • fromVersion - 迁移步骤的版本正在迁移。
  • toVersion - 迁移步骤的版本正在迁移到。
  • finalVersion - 应用所有迁移后的最终版本。
  • versions - 所有版本具有迁移步骤。

这对于记录目的,准备迁移数据等可能很有用。

例子:

conf '; console.log = someLogger.log; const main conf ig = new conf ({ projectName: 'foo1', beforeEachMigration: (store, context) => { console.log(`[main- conf ig] migrate from ${context.fromVersion} → ${context.toVersion}`); }, migrations: { '0.4.0': store => { store.set('debugPhase', true); }, } }); const second conf ig = new conf ({ projectName: 'foo2', beforeEachMigration: (store, context) => { console.log(`[second- conf ig] migrate from ${context.fromVersion} → ${context.toVersion}`); }, migrations: { '1.0.1': store => { store.set('debugPhase', true); }, } });">
 import conf from ' conf ' ;

console . log = someLogger . log ;

const main conf ig = new conf ( {
	projectName : 'foo1' ,
	beforeEachMigration : ( store , context ) => {
		console . log ( `[main- conf ig] migrate from ${ context . fromVersion }${ context . toVersion } ` ) ;
	} ,
	migrations : {
		'0.4.0' : store => {
			store . set ( 'debugPhase' , true ) ;
		} ,
	}
} ) ;

const second conf ig = new conf ( {
	projectName : 'foo2' ,
	beforeEachMigration : ( store , context ) => {
		console . log ( `[second- conf ig] migrate from ${ context . fromVersion }${ context . toVersion } ` ) ;
	} ,
	migrations : {
		'1.0.1' : store => {
			store . set ( 'debugPhase' , true ) ;
		} ,
	}
} ) ; 

conf Igname

类型: string
默认值: ' conf ig'

conf Ig文件的名称(无扩展)。

如果您需要多个conf IG文件的应用程序或模块,则有用。例如,两个主要版本之间的不同conf IG文件。

ProjectName

类型: string

除非您指定cwd选项,否则需要。

您可以从package.json获取name字段。

项目列表

类型: string

如果指定migration选项,则需要。

您可以从package.json获取version字段。

CWD

类型: string
默认:系统默认用户conf IG目录

您很可能不需要这个。除非您真的必须这样做,否则请不要使用它。默认情况下,它将通过遵守系统约定选择最佳位置。您很可能会弄错这个问题,惹恼用户。

覆盖projectName

我能想到的唯一用例是在应用程序目录或某些外部存储中放置conf Ig。

加密钥匙

类型: string | Uint8Array | TypedArray | DataView
默认值: undefined

请注意,这不是用于安全目的的目的,因为加密密钥将在Plain-Text Node.js应用中很容易找到。

它的主要用途是默默无闻。如果用户通过conf IG目录查看并找到conf Ig文件,则由于它只是一个JSON文件,因此他们可能会很想修改它。通过提供加密密钥,该文件将被混淆,这应该希望阻止任何用户这样做。

指定时,将使用aes-256-cbc加密算法对存储进行加密。

fileextension

类型: string
默认值: 'json'

conf Ig文件的扩展。

通常,您不需要这个,但是如果要与可以与应用程序关联的自定义文件扩展名进行交互,则可能会有用。这些可能是简单的保存/导出/偏好文件,该文件旨在在应用程序之外共享或保存。

clearinvalid conf ig

类型: boolean
默认值: false

如果读取conf Ig文件会导致SyntaxError则清除conf ig。这对于不重要的数据是一个很好的行为,因为conf IG文件不打算手工编辑,因此通常意味着conf IG已损坏,并且用户对此无能为力。但是,如果让用户直接编辑conf Ig文件,则可能会发生错误,并且在conf Ig无效而不是清除时,丢弃错误可能会更有用。

连载

类型: Function
默认值: value => JSON.stringify(value, null, '\t')

在编写conf Ig文件时,功能将conf ig对象序列化为UTF-8字符串。

您通常不需要这个,但是如果您想使用JSON以外的其他格式,则可能会有用。

避免

类型: Function
默认值: JSON.parse

读取conf ig文件时,功能可以从UTF-8字符串中对conf Ig对象进行验证。

您通常不需要这个,但是如果您想使用JSON以外的其他格式,则可能会有用。

Projectsuffix

类型: string
默认值: 'nodejs'

您很可能不需要这个。除非您真的必须这样做,否则请不要使用它。

在conf Ig文件创建期间,附加的后缀附加到projectName ,以避免使用本机应用程序名称conf licts。

您可以传递一个空字符串以删除后缀。

例如,在macOS上, conf Ig文件将存储在~/Library/Preferences/foo-nodejs目录中,其中fooprojectName

AccessPropertiesByDotation

类型: boolean
默认值: true

通过DOT表示法访问嵌套属性。例如:

conf '; const conf ig = new conf ({projectName: 'foo'}); conf ig.set({ foo: { bar: { foobar: '?' } } }); console.log( conf ig.get('foo.bar.foobar')); //=> '?'">
 import conf from ' conf ' ;

const conf ig = new conf ( { projectName : 'foo' } ) ;

conf ig . set ( {
	foo : {
		bar : {
			foobar : '?'
		}
	}
} ) ;

console . log ( conf ig . get ( 'foo.bar.foobar' ) ) ;
//=> '?'

另外,您可以将此选项设置为false ,因此整个字符串将被视为一个键。

conf '; const conf ig = new conf ({ projectName: 'foo', accessPropertiesByDotNotation: false }); conf ig.set({ `foo.bar.foobar`: '?' }); console.log( conf ig.get('foo.bar.foobar')); //=> '?'">
 import conf from ' conf ' ;

const conf ig = new conf ( {
	projectName : 'foo' ,
	accessPropertiesByDotNotation : false
} ) ;

conf ig . set ( {
	`foo.bar.foobar` : '?'
} ) ;

console . log ( conf ig . get ( 'foo.bar.foobar' ) ) ;
//=> '?' 

手表

类型: boolean
默认值: false

请注意conf IG文件中的任何更改,并在设置onDidChangeonDidAnyChange回调中调用回调。如果有多个过程更改相同的conf Ig文件,这将很有用。

conf igfilemode

类型: number
默认值: 0o666

将用于conf Ig文件的模式。

您通常不需要这个,但是如果您要限制conf IG文件的权限,则可能会很有用。设置诸如0o600之类的权限将导致一个conf IG文件,该文件只能由运行程序的用户才能访问。

请注意,如果不同用户需要读取文件,则设置限制性权限可能会导致问题。一个常见的问题是用户在有或没有sudo的情况下运行工具,然后第二次无法访问conf ig。

实例

您可以在key中使用点通知来访问嵌套属性。

该实例是iterable ,因此您可以将其直接在for…of loop中使用。

.set(键,值)

设置一个项目。

value必须是可序列化的。尝试设置undefined类型, functionsymbol将导致typeError。

.set(对象)

一次设置多个项目。

.get(键,defaultValue?)

如果不存在该项目,请获取项目或defaultValue

提示:要获取所有项目,请参阅.store

.RESET(...键)

将项目重置为其默认值,如defaultsschema选项所定义。

使用.clear()重置所有项目。

.has(键)

检查是否存在物品。

.DELETE(键)

删除项目。

。清除()

删除所有项目。

如果由defaultsschema选项定义,则将已知项目重置为其默认值。

.didChange(键,回调)

callback :( (newValue, oldValue) => {}

观看给定的key ,对任何更改进行callback

当键首先设置时, oldValueundefined ,当键删除时, newValueundefined

返回一个可以使用的函数:

config.onDidChange(key, callback); unsubscribe();">
 const unsubscribe = conf ig . onDidChange ( key , callback ) ;

unsubscribe ( ) ; 

.didanyChange(回调)

callback :( (newValue, oldValue) => {}

观看整个conf IG对象,对任何更改进行callback

oldValuenewValue将分别是更改之前和之后的conf IG对象。您必须将oldValuenewValue进行比较,以找出发生了什么变化。

返回一个可以使用的函数:

config.onDidAnyChange(callback); unsubscribe();">
 const unsubscribe = conf ig . onDidAnyChange ( callback ) ;

unsubscribe ( ) ; 

。尺寸

获取项目计数。

。店铺

将所有conf Ig作为对象或用对象替换当前的conf Ig:

config.store); //=> {name: 'John', age: 30}">
 console . log ( conf ig . store ) ;
//=> {name: 'John', age: 30} 
config.store = { hello: 'world' };">
 conf ig . store = {
	hello : 'world'
} ; 

。小路

获取通往conf IG文件的路径。

常问问题

这与conf igstore有何不同?

我也是conf igstore的作者。虽然很好,但此时我确实很难改变一些错误。这个模块是我从制作conf igstore中学到的一切的结果。主要是存储conf Ig的地方。在conf igstore中, conf Ig存储在~/. conf ig所有系统上的~/. conf ig (主要是Linux约定),而conf conf in conf in默认用户conf ig目录中的conf ig。 ~/. conf ig事实证明, ~/. conf ig目录通常对MacOS和Windows有不正确的许可,这给用户带来了很多悲伤。

我可以使用YAML或其他序列化格式吗?

只要表示表示与utf8编码兼容, serializedeserialize选项可用于自定义conf Ig文件的格式。

使用yaml的示例:

conf '; import yaml from 'js-yaml'; const conf ig = new conf ({ projectName: 'foo', fileExtension: 'yaml', serialize: yaml.safeDump, deserialize: yaml.safeLoad });">
 import conf from ' conf ' ;
import yaml from 'js-yaml' ;

const conf ig = new conf ( {
	projectName : 'foo' ,
	fileExtension : 'yaml' ,
	serialize : yaml . safeDump ,
	deserialize : yaml . safeLoad
} ) ; 

有关的

  • 电子商店 - 电子应用或模块的简单数据持久性
  • cache- conf简单的高速缓存conf IG处理您的应用或模块
下载源码

通过命令行克隆项目:

git clone https://github.com/sindresorhus/conf.git