@tikkhun/version 2025.7.7 → 2026.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +179 -5
- package/dist/cli/date-version.js +4 -3
- package/dist/cli/date-version.mjs +4 -3
- package/dist/cli/node-version.js +4 -3
- package/dist/cli/node-version.mjs +4 -3
- package/dist/cli/semantic-version.js +4 -3
- package/dist/cli/semantic-version.mjs +4 -3
- package/dist/cli/shared.d.mts +130 -0
- package/dist/cli/shared.d.ts +130 -0
- package/dist/cli/shared.js +6 -0
- package/dist/cli/shared.mjs +6 -0
- package/dist/lib/VersionManager.d.mts +49 -14
- package/dist/lib/VersionManager.d.ts +49 -14
- package/dist/lib/VersionManager.js +4 -3
- package/dist/lib/VersionManager.mjs +4 -3
- package/dist/lib/VersionUpdateResult.d.mts +36 -24
- package/dist/lib/VersionUpdateResult.d.ts +36 -24
- package/dist/lib/VersionUpdateResult.js +2 -2
- package/dist/lib/VersionUpdateResult.mjs +2 -2
- package/dist/lib/getter/NodeVersionGetter.js +1 -1
- package/dist/lib/getter/NodeVersionGetter.mjs +1 -1
- package/dist/lib/getter/SemanticVersionGetter.js +1 -1
- package/dist/lib/getter/SemanticVersionGetter.mjs +1 -1
- package/dist/lib/getter/index.js +1 -1
- package/dist/lib/getter/index.mjs +1 -1
- package/dist/lib/index.d.mts +4 -1
- package/dist/lib/index.d.ts +4 -1
- package/dist/lib/index.js +4 -3
- package/dist/lib/index.mjs +4 -3
- package/dist/lib/store/ConfigStore.d.mts +69 -0
- package/dist/lib/store/ConfigStore.d.ts +69 -0
- package/dist/lib/store/ConfigStore.js +6 -0
- package/dist/lib/store/ConfigStore.mjs +6 -0
- package/dist/lib/store/JsonStore.d.mts +32 -9
- package/dist/lib/store/JsonStore.d.ts +32 -9
- package/dist/lib/store/JsonStore.js +4 -3
- package/dist/lib/store/JsonStore.mjs +4 -3
- package/dist/lib/store/Store.d.mts +182 -4
- package/dist/lib/store/Store.d.ts +182 -4
- package/dist/lib/store/StoreFactory.d.mts +14 -0
- package/dist/lib/store/StoreFactory.d.ts +14 -0
- package/dist/lib/store/StoreFactory.js +6 -0
- package/dist/lib/store/StoreFactory.mjs +6 -0
- package/dist/lib/store/TextStore.d.mts +62 -6
- package/dist/lib/store/TextStore.d.ts +62 -6
- package/dist/lib/store/TextStore.js +3 -3
- package/dist/lib/store/TextStore.mjs +3 -3
- package/dist/lib/store/index.d.mts +3 -1
- package/dist/lib/store/index.d.ts +3 -1
- package/dist/lib/store/index.js +4 -3
- package/dist/lib/store/index.mjs +4 -3
- package/dist/lib/utils.js +1 -1
- package/dist/lib/utils.mjs +1 -1
- package/package.json +10 -6
|
@@ -1,28 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
interface
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { StoreResult, VersionValue, StoreGetResult, StoreSetResult } from './store/Store.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description store 聚合结果.
|
|
5
|
+
*/
|
|
6
|
+
interface VersionManagerResult<T extends StoreResult = StoreResult> {
|
|
7
|
+
/**
|
|
8
|
+
* @description 当前操作是否全部成功.
|
|
9
|
+
*/
|
|
10
|
+
success: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* @description 本次操作处理的版本值.
|
|
13
|
+
*/
|
|
14
|
+
value?: VersionValue;
|
|
15
|
+
/**
|
|
16
|
+
* @description 每个 store 的执行结果.
|
|
17
|
+
*/
|
|
18
|
+
results: T[];
|
|
10
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* @description 读取聚合结果.
|
|
22
|
+
*/
|
|
23
|
+
type VersionReadResult = VersionManagerResult<StoreGetResult>;
|
|
24
|
+
/**
|
|
25
|
+
* @description 写入聚合结果.
|
|
26
|
+
*/
|
|
27
|
+
type VersionSetResult = VersionManagerResult<StoreSetResult>;
|
|
28
|
+
/**
|
|
29
|
+
* @description 结果工厂.
|
|
30
|
+
*/
|
|
11
31
|
declare class VersionUpdateResult {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
static error(opts: ErrorOptions): {
|
|
20
|
-
oldVersion: string;
|
|
21
|
-
newVersion?: string;
|
|
22
|
-
message: string;
|
|
23
|
-
isSuccess: boolean;
|
|
24
|
-
isError: boolean;
|
|
25
|
-
};
|
|
32
|
+
/**
|
|
33
|
+
* @description 聚合多个 store 结果.
|
|
34
|
+
* @param results store 结果列表.
|
|
35
|
+
* @param value 当前版本值.
|
|
36
|
+
*/
|
|
37
|
+
static create<T extends StoreResult>(results: T[], value?: VersionValue): VersionManagerResult<T>;
|
|
26
38
|
}
|
|
27
39
|
|
|
28
|
-
export { type
|
|
40
|
+
export { type VersionManagerResult, type VersionReadResult, type VersionSetResult, VersionUpdateResult };
|
|
@@ -1,28 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
interface
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { StoreResult, VersionValue, StoreGetResult, StoreSetResult } from './store/Store.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description store 聚合结果.
|
|
5
|
+
*/
|
|
6
|
+
interface VersionManagerResult<T extends StoreResult = StoreResult> {
|
|
7
|
+
/**
|
|
8
|
+
* @description 当前操作是否全部成功.
|
|
9
|
+
*/
|
|
10
|
+
success: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* @description 本次操作处理的版本值.
|
|
13
|
+
*/
|
|
14
|
+
value?: VersionValue;
|
|
15
|
+
/**
|
|
16
|
+
* @description 每个 store 的执行结果.
|
|
17
|
+
*/
|
|
18
|
+
results: T[];
|
|
10
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* @description 读取聚合结果.
|
|
22
|
+
*/
|
|
23
|
+
type VersionReadResult = VersionManagerResult<StoreGetResult>;
|
|
24
|
+
/**
|
|
25
|
+
* @description 写入聚合结果.
|
|
26
|
+
*/
|
|
27
|
+
type VersionSetResult = VersionManagerResult<StoreSetResult>;
|
|
28
|
+
/**
|
|
29
|
+
* @description 结果工厂.
|
|
30
|
+
*/
|
|
11
31
|
declare class VersionUpdateResult {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
static error(opts: ErrorOptions): {
|
|
20
|
-
oldVersion: string;
|
|
21
|
-
newVersion?: string;
|
|
22
|
-
message: string;
|
|
23
|
-
isSuccess: boolean;
|
|
24
|
-
isError: boolean;
|
|
25
|
-
};
|
|
32
|
+
/**
|
|
33
|
+
* @description 聚合多个 store 结果.
|
|
34
|
+
* @param results store 结果列表.
|
|
35
|
+
* @param value 当前版本值.
|
|
36
|
+
*/
|
|
37
|
+
static create<T extends StoreResult>(results: T[], value?: VersionValue): VersionManagerResult<T>;
|
|
26
38
|
}
|
|
27
39
|
|
|
28
|
-
export { type
|
|
40
|
+
export { type VersionManagerResult, type VersionReadResult, type VersionSetResult, VersionUpdateResult };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use strict';/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var t=class{static create(e,s){return {success:e.every(r=>r.success),value:s,results:e}}};exports.VersionUpdateResult=t;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var t=class{static create(e,s){return {success:e.every(r=>r.success),value:s,results:e}}};export{t as VersionUpdateResult};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use strict';var fsExtra=require('fs-extra');/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
5
|
var a=(p,r,e)=>new Promise((t,i)=>{var f=o=>{try{n(e.next(o));}catch(s){i(s);}},c=o=>{try{n(e.throw(o));}catch(s){i(s);}},n=o=>o.done?t(o.value):Promise.resolve(o.value).then(f,c);n((e=e.apply(p,r)).next());});var u=(t=>(t[t.major=0]="major",t[t.minor=1]="minor",t[t.patch=2]="patch",t))(u||{}),g={file:"package.json",position:2,key:"version"},m=class{constructor(r){this.opts=Object.assign({},g,r);}get(){return a(this,null,function*(){if(!(yield fsExtra.pathExists(this.opts.file)))throw new Error("get new version,but error: the file is not found");let{[this.opts.key]:r}=yield fsExtra.readJson(this.opts.file);if(!r)throw new Error("get new version, but error: the old version is not found");if(typeof r!="string")throw new Error(`get new version, but error: the old version is not string,old version: ${JSON.stringify(r)}`);let[e,t,i]=r.split(".");return this.opts.position===0?[Number(e)+1,t,i].join("."):this.opts.position===1?[e,Number(t)+1,i].join("."):[e,t,Number(i)+1].join(".")})}};exports.DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS=g;exports.Positions=u;exports.SemanticVersionGetter=m;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {pathExists,readJson}from'fs-extra';/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
5
|
var a=(p,r,e)=>new Promise((t,i)=>{var f=o=>{try{n(e.next(o));}catch(s){i(s);}},c=o=>{try{n(e.throw(o));}catch(s){i(s);}},n=o=>o.done?t(o.value):Promise.resolve(o.value).then(f,c);n((e=e.apply(p,r)).next());});var u=(t=>(t[t.major=0]="major",t[t.minor=1]="minor",t[t.patch=2]="patch",t))(u||{}),g={file:"package.json",position:2,key:"version"},m=class{constructor(r){this.opts=Object.assign({},g,r);}get(){return a(this,null,function*(){if(!(yield pathExists(this.opts.file)))throw new Error("get new version,but error: the file is not found");let{[this.opts.key]:r}=yield readJson(this.opts.file);if(!r)throw new Error("get new version, but error: the old version is not found");if(typeof r!="string")throw new Error(`get new version, but error: the old version is not string,old version: ${JSON.stringify(r)}`);let[e,t,i]=r.split(".");return this.opts.position===0?[Number(e)+1,t,i].join("."):this.opts.position===1?[e,Number(t)+1,i].join("."):[e,t,Number(i)+1].join(".")})}};export{g as DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS,u as Positions,m as SemanticVersionGetter};
|
package/dist/lib/getter/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use strict';var utils=require('@tikkhun/utils'),fsExtra=require('fs-extra');/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
5
|
var m=(n,r,e)=>new Promise((t,i)=>{var c=o=>{try{s(e.next(o));}catch(p){i(p);}},h=o=>{try{s(e.throw(o));}catch(p){i(p);}},s=o=>o.done?t(o.value):Promise.resolve(o.value).then(c,h);s((e=e.apply(n,r)).next());});var a=class{get(){return utils.currentNodeVersion}};var w=(t=>(t[t.major=0]="major",t[t.minor=1]="minor",t[t.patch=2]="patch",t))(w||{}),x={file:"package.json",position:2,key:"version"},f=class{constructor(r){this.opts=Object.assign({},x,r);}get(){return m(this,null,function*(){if(!(yield fsExtra.pathExists(this.opts.file)))throw new Error("get new version,but error: the file is not found");let{[this.opts.key]:r}=yield fsExtra.readJson(this.opts.file);if(!r)throw new Error("get new version, but error: the old version is not found");if(typeof r!="string")throw new Error(`get new version, but error: the old version is not string,old version: ${JSON.stringify(r)}`);let[e,t,i]=r.split(".");return this.opts.position===0?[Number(e)+1,t,i].join("."):this.opts.position===1?[e,Number(t)+1,i].join("."):[e,t,Number(i)+1].join(".")})}};exports.DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS=x;exports.NodeVersionGetter=a;exports.Positions=w;exports.SemanticVersionGetter=f;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {currentNodeVersion}from'@tikkhun/utils';import {pathExists,readJson}from'fs-extra';/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
5
|
var m=(n,r,e)=>new Promise((t,i)=>{var c=o=>{try{s(e.next(o));}catch(p){i(p);}},h=o=>{try{s(e.throw(o));}catch(p){i(p);}},s=o=>o.done?t(o.value):Promise.resolve(o.value).then(c,h);s((e=e.apply(n,r)).next());});var a=class{get(){return currentNodeVersion}};var w=(t=>(t[t.major=0]="major",t[t.minor=1]="minor",t[t.patch=2]="patch",t))(w||{}),x={file:"package.json",position:2,key:"version"},f=class{constructor(r){this.opts=Object.assign({},x,r);}get(){return m(this,null,function*(){if(!(yield pathExists(this.opts.file)))throw new Error("get new version,but error: the file is not found");let{[this.opts.key]:r}=yield readJson(this.opts.file);if(!r)throw new Error("get new version, but error: the old version is not found");if(typeof r!="string")throw new Error(`get new version, but error: the old version is not string,old version: ${JSON.stringify(r)}`);let[e,t,i]=r.split(".");return this.opts.position===0?[Number(e)+1,t,i].join("."):this.opts.position===1?[e,Number(t)+1,i].join("."):[e,t,Number(i)+1].join(".")})}};export{x as DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS,a as NodeVersionGetter,w as Positions,f as SemanticVersionGetter};
|
package/dist/lib/index.d.mts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export { DEFAULT_VERSION_MANAGER_OPTIONS, VersionManager, VersionManagerOptions } from './VersionManager.mjs';
|
|
2
|
+
export { VersionManagerResult, VersionReadResult, VersionSetResult, VersionUpdateResult } from './VersionUpdateResult.mjs';
|
|
2
3
|
export { workspace } from './utils.mjs';
|
|
4
|
+
export { ConfigStore } from './store/ConfigStore.mjs';
|
|
3
5
|
export { DEFAULT_JSON_STORE_OPTIONS, JsonStore, JsonStoreOptions } from './store/JsonStore.mjs';
|
|
4
|
-
export { Store,
|
|
6
|
+
export { BaseVersionTarget, CustomVersionTarget, Store, StoreGetResult, StoreResult, StoreSetResult, StructuredVersionTarget, StructuredVersionTargetType, TextStoreMode, TextVersionTarget, VersionTarget, VersionValue } from './store/Store.mjs';
|
|
7
|
+
export { StoreFactory } from './store/StoreFactory.mjs';
|
|
5
8
|
export { DEFAULT_TEXT_STORE_OPTIONS, TextStore, TextStoreOptions } from './store/TextStore.mjs';
|
|
6
9
|
export { NodeVersionGetter } from './getter/NodeVersionGetter.mjs';
|
|
7
10
|
export { DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS, Positions, SemanticVersionGetter, SemanticVersionGetterOptions } from './getter/SemanticVersionGetter.mjs';
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export { DEFAULT_VERSION_MANAGER_OPTIONS, VersionManager, VersionManagerOptions } from './VersionManager.js';
|
|
2
|
+
export { VersionManagerResult, VersionReadResult, VersionSetResult, VersionUpdateResult } from './VersionUpdateResult.js';
|
|
2
3
|
export { workspace } from './utils.js';
|
|
4
|
+
export { ConfigStore } from './store/ConfigStore.js';
|
|
3
5
|
export { DEFAULT_JSON_STORE_OPTIONS, JsonStore, JsonStoreOptions } from './store/JsonStore.js';
|
|
4
|
-
export { Store,
|
|
6
|
+
export { BaseVersionTarget, CustomVersionTarget, Store, StoreGetResult, StoreResult, StoreSetResult, StructuredVersionTarget, StructuredVersionTargetType, TextStoreMode, TextVersionTarget, VersionTarget, VersionValue } from './store/Store.js';
|
|
7
|
+
export { StoreFactory } from './store/StoreFactory.js';
|
|
5
8
|
export { DEFAULT_TEXT_STORE_OPTIONS, TextStore, TextStoreOptions } from './store/TextStore.js';
|
|
6
9
|
export { NodeVersionGetter } from './getter/NodeVersionGetter.js';
|
|
7
10
|
export { DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS, Positions, SemanticVersionGetter, SemanticVersionGetterOptions } from './getter/SemanticVersionGetter.js';
|
package/dist/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
'use strict';var logger=require('@tikkhun/logger'),fsExtra=require('fs-extra'),
|
|
1
|
+
'use strict';var logger=require('@tikkhun/logger'),fsExtra=require('fs-extra'),fastXmlParser=require('fast-xml-parser'),R=require('js-yaml'),lodash=require('lodash'),smolToml=require('smol-toml'),dateVersion=require('@tikkhun/date-version'),utils=require('@tikkhun/utils');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var R__default=/*#__PURE__*/_interopDefault(R);/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var b=Object.defineProperty,j=Object.defineProperties;var A=Object.getOwnPropertyDescriptors;var V=Object.getOwnPropertySymbols;var M=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var E=(n,t,e)=>t in n?b(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e,u=(n,t)=>{for(var e in t||(t={}))M.call(t,e)&&E(n,e,t[e]);if(V)for(var e of V(t))k.call(t,e)&&E(n,e,t[e]);return n},p=(n,t)=>j(n,A(t));var o=(n,t,e)=>new Promise((r,s)=>{var i=a=>{try{g(e.next(a));}catch(S){s(S);}},c=a=>{try{g(e.throw(a));}catch(S){s(S);}},g=a=>a.done?r(a.value):Promise.resolve(a.value).then(i,c);g((e=e.apply(n,t)).next());});var l=class{constructor(t){this.target=t;}get(){return o(this,null,function*(){var t;try{if(!(yield fsExtra.pathExists(this.target.file)))return this.createGetError(`file is not found: ${this.target.file}`);let e=yield this.loadStructuredConfig(),r=this.target.path?lodash.get(e,this.target.path):e;return r===void 0?this.createGetError(`path is not found: ${(t=this.target.path)!=null?t:"<root>"}`):typeof r!="string"&&typeof r!="number"?this.createGetError(`value is not string or number: ${JSON.stringify(r)}`):{action:"get",success:!0,file:this.target.file,type:this.target.type,value:r}}catch(e){return this.createGetError(this.getErrorMessage(e))}})}set(t){return o(this,null,function*(){try{let e=yield this.get(),r=e.success?e.value:void 0,s=yield fsExtra.pathExists(this.target.file);if(!s){if(!this.target.createIfNotExists)return this.createSetError(t,`file is not found: ${this.target.file}`,r,"set");yield fsExtra.ensureFile(this.target.file);}let i=s?yield this.loadStructuredConfig():{};if(this.target.path)lodash.set(i,this.target.path,t),yield this.saveStructuredConfig(i);else {if(typeof i=="object"&&i!==null&&!Array.isArray(i))return this.createSetError(t,"path is required for structured target",r,"set");yield this.saveStructuredConfig(t);}return {action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:r,newValue:t}}catch(e){return this.createSetError(t,this.getErrorMessage(e),void 0,"set")}})}update(t){return o(this,null,function*(){let e=yield this.set(t);return p(u({},e),{action:"update"})})}createGetError(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t}}createSetError(t,e,r,s="set"){return {action:s,success:false,file:this.target.file,type:this.target.type,oldValue:r,newValue:t,message:e}}getErrorMessage(t){return t instanceof Error?t.message:String(t)}loadStructuredConfig(){return o(this,null,function*(){var e;let t=yield fsExtra.readFile(this.target.file,(e=this.target.encoding)!=null?e:"utf8");switch(this.target.type){case "json":return JSON.parse(t);case "yaml":return R__default.default.load(t);case "toml":return smolToml.parse(t);case "xml":return new fastXmlParser.XMLParser({ignoreAttributes:true}).parse(t);case "env":return this.parseEnvContent(t);default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}saveStructuredConfig(t){return o(this,null,function*(){var r;let e=(r=this.target.encoding)!=null?r:"utf8";switch(this.target.type){case "json":yield fsExtra.writeFile(this.target.file,JSON.stringify(t,null,2),e);return;case "yaml":yield fsExtra.writeFile(this.target.file,R__default.default.dump(t),e);return;case "toml":yield fsExtra.writeFile(this.target.file,smolToml.stringify(t),e);return;case "xml":yield fsExtra.writeFile(this.target.file,new fastXmlParser.XMLBuilder({format:true,ignoreAttributes:true}).build(t),e);return;case "env":yield fsExtra.writeFile(this.target.file,this.stringifyEnvContent(t),e);return;default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}parseEnvContent(t){let e={},r=t.split(/\r?\n/);for(let s of r){let i=s.trim();if(!i||i.startsWith("#"))continue;let c=i.indexOf("=");if(c<0)continue;let g=i.slice(0,c).trim(),a=i.slice(c+1).trim();lodash.set(e,g.replace(/__/g,"."),a);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[g,a]of Object.entries(s))r(a,[...i,g]);return}let c=i.join("__").toUpperCase();e.push(`${c}=${s!=null?s:""}`);};for(let[s,i]of Object.entries(t))r(i,[s]);return e.join(`
|
|
6
|
+
`)}};var D={file:"package.json",key:"version"},m=class extends l{constructor(t){var s;let e=Object.assign({},D,t),r={type:"json",file:e.file,path:(s=e.path)!=null?s:e.key,createIfNotExists:e.createIfNotExists};super(r);}};var X={file:"",mode:"raw",trim:true,replacement:"{{version}}"},d=class{constructor(t){this.target=u(u({type:"text"},X),t);}get(){return o(this,null,function*(){var t;try{if(!(yield fsExtra.pathExists(this.target.file)))return this.createGetError(`file is not found: ${this.target.file}`);let e=yield fsExtra.readFile(this.target.file,(t=this.target.encoding)!=null?t:"utf8"),r=this.resolveValueFromText(e);return r===void 0?this.createGetError("version is not found in text content"):{action:"get",success:!0,file:this.target.file,type:this.target.type,value:r}}catch(e){return this.createGetError(this.getErrorMessage(e))}})}set(t){return o(this,null,function*(){try{let e=yield this.get(),r=e.success?e.value:void 0,s=yield this.createNextText(t);return yield this.writeText(s),{action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:r,newValue:t}}catch(e){return {action:"set",success:false,file:this.target.file,type:this.target.type,newValue:t,message:this.getErrorMessage(e)}}})}update(t){return o(this,null,function*(){let e=yield this.set(t);return p(u({},e),{action:"update"})})}resolveValueFromText(t){var e;if(this.target.mode==="replace"){let s=this.createPattern().exec(t);if(!s)return;let i=(e=s.groups)==null?void 0:e.version;return i!==void 0?i:s[1]!==void 0?s[1]:s[0]}return this.target.trim?t.trim():t}createNextText(t){return o(this,null,function*(){var e,r;if(this.target.mode==="replace"){if(!(yield fsExtra.pathExists(this.target.file)))throw new Error(`file is not found: ${this.target.file}`);let s=yield fsExtra.readFile(this.target.file,(e=this.target.encoding)!=null?e:"utf8");if(!this.createPattern().test(s))throw new Error("pattern is not matched");let c=((r=this.target.replacement)!=null?r:"{{version}}").replaceAll("{{version}}",String(t));return s.replace(this.createPattern(),c)}return String(t)})}writeText(t){return o(this,null,function*(){var e;if(!(yield fsExtra.pathExists(this.target.file))){if(!this.target.createIfNotExists)throw new Error(`file is not found: ${this.target.file}`);yield fsExtra.ensureFile(this.target.file);}yield fsExtra.writeFile(this.target.file,t,(e=this.target.encoding)!=null?e:"utf8");})}createPattern(){if(!this.target.pattern)throw new Error("pattern is required when text mode is replace");return new RegExp(this.target.pattern,this.target.flags)}createGetError(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t}}getErrorMessage(t){return t instanceof Error?t.message:String(t)}};var y=class{static create(t){return t.type==="text"?new d(t):t.type==="custom"?new x(t):new l(t)}},x=class{constructor(t){this.target=t;}get(){return o(this,null,function*(){if(!this.target.get)return {action:"get",success:false,file:this.target.file,type:this.target.type,message:"custom get is not implemented"};try{let t=yield this.target.get(this.target.file);return {action:"get",success:!0,file:this.target.file,type:this.target.type,value:t}}catch(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t instanceof Error?t.message:String(t)}}})}set(t){return o(this,null,function*(){try{let e=yield this.get();return this.target.set?(yield this.target.set(t,this.target.file),{action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:e.success?e.value:void 0,newValue:t}):{action:"set",success:!1,file:this.target.file,type:this.target.type,oldValue:e.success?e.value:void 0,newValue:t,message:"custom set is not implemented"}}catch(e){return {action:"set",success:false,file:this.target.file,type:this.target.type,newValue:t,message:e instanceof Error?e.message:String(e)}}})}update(t){return o(this,null,function*(){let e=yield this.set(t);return p(u({},e),{action:"update"})})}};var f=class{static create(t,e){return {success:t.every(r=>r.success),value:e,results:t}}};var W={getter:{get(){return dateVersion.getDateVersion()}},store:new m,targets:[]},G=class{constructor(t){this.logger=new logger.Logger("VersionManager");this.opts=Object.assign({},W,t);}get(){return this.opts.getter.get()}read(){return o(this,null,function*(){let t=this.resolveStores(),e=[];for(let r of t)e.push(yield r.get());return f.create(e)})}set(t){return o(this,null,function*(){this.logger.log("set version: "+t);let e=this.resolveStores(),r=[];for(let s of e)r.push(yield s.set(t));return f.create(r,t)})}update(){return o(this,null,function*(){let t=yield this.get();this.logger.log("new version: "+t);let e=this.resolveStores(),r=[];for(let s of e)r.push(yield s.update(t));return f.create(r,t)})}resolveStores(){var t;return (t=this.opts.targets)!=null&&t.length?this.opts.targets.map(e=>y.create(e)):Array.isArray(this.opts.store)?this.opts.store:[this.opts.store]}};var _t=process.cwd();var P=class{get(){return utils.currentNodeVersion}};var Y=(r=>(r[r.major=0]="major",r[r.minor=1]="minor",r[r.patch=2]="patch",r))(Y||{}),Z={file:"package.json",position:2,key:"version"},N=class{constructor(t){this.opts=Object.assign({},Z,t);}get(){return o(this,null,function*(){if(!(yield fsExtra.pathExists(this.opts.file)))throw new Error("get new version,but error: the file is not found");let{[this.opts.key]:t}=yield fsExtra.readJson(this.opts.file);if(!t)throw new Error("get new version, but error: the old version is not found");if(typeof t!="string")throw new Error(`get new version, but error: the old version is not string,old version: ${JSON.stringify(t)}`);let[e,r,s]=t.split(".");return this.opts.position===0?[Number(e)+1,r,s].join("."):this.opts.position===1?[e,Number(r)+1,s].join("."):[e,r,Number(s)+1].join(".")})}};exports.ConfigStore=l;exports.DEFAULT_JSON_STORE_OPTIONS=D;exports.DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS=Z;exports.DEFAULT_TEXT_STORE_OPTIONS=X;exports.DEFAULT_VERSION_MANAGER_OPTIONS=W;exports.JsonStore=m;exports.NodeVersionGetter=P;exports.Positions=Y;exports.SemanticVersionGetter=N;exports.StoreFactory=y;exports.TextStore=d;exports.VersionManager=G;exports.VersionUpdateResult=f;exports.workspace=_t;
|
package/dist/lib/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {Logger}from'@tikkhun/logger';import {
|
|
1
|
+
import {Logger}from'@tikkhun/logger';import {pathExists,ensureFile,readFile,writeFile,readJson}from'fs-extra';import {XMLParser,XMLBuilder}from'fast-xml-parser';import R from'js-yaml';import {set,get}from'lodash';import {parse,stringify}from'smol-toml';import {getDateVersion}from'@tikkhun/date-version';import {currentNodeVersion}from'@tikkhun/utils';/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var b=Object.defineProperty,j=Object.defineProperties;var A=Object.getOwnPropertyDescriptors;var V=Object.getOwnPropertySymbols;var M=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var E=(n,t,e)=>t in n?b(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e,u=(n,t)=>{for(var e in t||(t={}))M.call(t,e)&&E(n,e,t[e]);if(V)for(var e of V(t))k.call(t,e)&&E(n,e,t[e]);return n},p=(n,t)=>j(n,A(t));var o=(n,t,e)=>new Promise((r,s)=>{var i=a=>{try{g(e.next(a));}catch(S){s(S);}},c=a=>{try{g(e.throw(a));}catch(S){s(S);}},g=a=>a.done?r(a.value):Promise.resolve(a.value).then(i,c);g((e=e.apply(n,t)).next());});var l=class{constructor(t){this.target=t;}get(){return o(this,null,function*(){var t;try{if(!(yield pathExists(this.target.file)))return this.createGetError(`file is not found: ${this.target.file}`);let e=yield this.loadStructuredConfig(),r=this.target.path?get(e,this.target.path):e;return r===void 0?this.createGetError(`path is not found: ${(t=this.target.path)!=null?t:"<root>"}`):typeof r!="string"&&typeof r!="number"?this.createGetError(`value is not string or number: ${JSON.stringify(r)}`):{action:"get",success:!0,file:this.target.file,type:this.target.type,value:r}}catch(e){return this.createGetError(this.getErrorMessage(e))}})}set(t){return o(this,null,function*(){try{let e=yield this.get(),r=e.success?e.value:void 0,s=yield pathExists(this.target.file);if(!s){if(!this.target.createIfNotExists)return this.createSetError(t,`file is not found: ${this.target.file}`,r,"set");yield ensureFile(this.target.file);}let i=s?yield this.loadStructuredConfig():{};if(this.target.path)set(i,this.target.path,t),yield this.saveStructuredConfig(i);else {if(typeof i=="object"&&i!==null&&!Array.isArray(i))return this.createSetError(t,"path is required for structured target",r,"set");yield this.saveStructuredConfig(t);}return {action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:r,newValue:t}}catch(e){return this.createSetError(t,this.getErrorMessage(e),void 0,"set")}})}update(t){return o(this,null,function*(){let e=yield this.set(t);return p(u({},e),{action:"update"})})}createGetError(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t}}createSetError(t,e,r,s="set"){return {action:s,success:false,file:this.target.file,type:this.target.type,oldValue:r,newValue:t,message:e}}getErrorMessage(t){return t instanceof Error?t.message:String(t)}loadStructuredConfig(){return o(this,null,function*(){var e;let t=yield readFile(this.target.file,(e=this.target.encoding)!=null?e:"utf8");switch(this.target.type){case "json":return JSON.parse(t);case "yaml":return R.load(t);case "toml":return parse(t);case "xml":return new XMLParser({ignoreAttributes:true}).parse(t);case "env":return this.parseEnvContent(t);default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}saveStructuredConfig(t){return o(this,null,function*(){var r;let e=(r=this.target.encoding)!=null?r:"utf8";switch(this.target.type){case "json":yield writeFile(this.target.file,JSON.stringify(t,null,2),e);return;case "yaml":yield writeFile(this.target.file,R.dump(t),e);return;case "toml":yield writeFile(this.target.file,stringify(t),e);return;case "xml":yield writeFile(this.target.file,new XMLBuilder({format:true,ignoreAttributes:true}).build(t),e);return;case "env":yield writeFile(this.target.file,this.stringifyEnvContent(t),e);return;default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}parseEnvContent(t){let e={},r=t.split(/\r?\n/);for(let s of r){let i=s.trim();if(!i||i.startsWith("#"))continue;let c=i.indexOf("=");if(c<0)continue;let g=i.slice(0,c).trim(),a=i.slice(c+1).trim();set(e,g.replace(/__/g,"."),a);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[g,a]of Object.entries(s))r(a,[...i,g]);return}let c=i.join("__").toUpperCase();e.push(`${c}=${s!=null?s:""}`);};for(let[s,i]of Object.entries(t))r(i,[s]);return e.join(`
|
|
6
|
+
`)}};var D={file:"package.json",key:"version"},m=class extends l{constructor(t){var s;let e=Object.assign({},D,t),r={type:"json",file:e.file,path:(s=e.path)!=null?s:e.key,createIfNotExists:e.createIfNotExists};super(r);}};var X={file:"",mode:"raw",trim:true,replacement:"{{version}}"},d=class{constructor(t){this.target=u(u({type:"text"},X),t);}get(){return o(this,null,function*(){var t;try{if(!(yield pathExists(this.target.file)))return this.createGetError(`file is not found: ${this.target.file}`);let e=yield readFile(this.target.file,(t=this.target.encoding)!=null?t:"utf8"),r=this.resolveValueFromText(e);return r===void 0?this.createGetError("version is not found in text content"):{action:"get",success:!0,file:this.target.file,type:this.target.type,value:r}}catch(e){return this.createGetError(this.getErrorMessage(e))}})}set(t){return o(this,null,function*(){try{let e=yield this.get(),r=e.success?e.value:void 0,s=yield this.createNextText(t);return yield this.writeText(s),{action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:r,newValue:t}}catch(e){return {action:"set",success:false,file:this.target.file,type:this.target.type,newValue:t,message:this.getErrorMessage(e)}}})}update(t){return o(this,null,function*(){let e=yield this.set(t);return p(u({},e),{action:"update"})})}resolveValueFromText(t){var e;if(this.target.mode==="replace"){let s=this.createPattern().exec(t);if(!s)return;let i=(e=s.groups)==null?void 0:e.version;return i!==void 0?i:s[1]!==void 0?s[1]:s[0]}return this.target.trim?t.trim():t}createNextText(t){return o(this,null,function*(){var e,r;if(this.target.mode==="replace"){if(!(yield pathExists(this.target.file)))throw new Error(`file is not found: ${this.target.file}`);let s=yield readFile(this.target.file,(e=this.target.encoding)!=null?e:"utf8");if(!this.createPattern().test(s))throw new Error("pattern is not matched");let c=((r=this.target.replacement)!=null?r:"{{version}}").replaceAll("{{version}}",String(t));return s.replace(this.createPattern(),c)}return String(t)})}writeText(t){return o(this,null,function*(){var e;if(!(yield pathExists(this.target.file))){if(!this.target.createIfNotExists)throw new Error(`file is not found: ${this.target.file}`);yield ensureFile(this.target.file);}yield writeFile(this.target.file,t,(e=this.target.encoding)!=null?e:"utf8");})}createPattern(){if(!this.target.pattern)throw new Error("pattern is required when text mode is replace");return new RegExp(this.target.pattern,this.target.flags)}createGetError(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t}}getErrorMessage(t){return t instanceof Error?t.message:String(t)}};var y=class{static create(t){return t.type==="text"?new d(t):t.type==="custom"?new x(t):new l(t)}},x=class{constructor(t){this.target=t;}get(){return o(this,null,function*(){if(!this.target.get)return {action:"get",success:false,file:this.target.file,type:this.target.type,message:"custom get is not implemented"};try{let t=yield this.target.get(this.target.file);return {action:"get",success:!0,file:this.target.file,type:this.target.type,value:t}}catch(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t instanceof Error?t.message:String(t)}}})}set(t){return o(this,null,function*(){try{let e=yield this.get();return this.target.set?(yield this.target.set(t,this.target.file),{action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:e.success?e.value:void 0,newValue:t}):{action:"set",success:!1,file:this.target.file,type:this.target.type,oldValue:e.success?e.value:void 0,newValue:t,message:"custom set is not implemented"}}catch(e){return {action:"set",success:false,file:this.target.file,type:this.target.type,newValue:t,message:e instanceof Error?e.message:String(e)}}})}update(t){return o(this,null,function*(){let e=yield this.set(t);return p(u({},e),{action:"update"})})}};var f=class{static create(t,e){return {success:t.every(r=>r.success),value:e,results:t}}};var W={getter:{get(){return getDateVersion()}},store:new m,targets:[]},G=class{constructor(t){this.logger=new Logger("VersionManager");this.opts=Object.assign({},W,t);}get(){return this.opts.getter.get()}read(){return o(this,null,function*(){let t=this.resolveStores(),e=[];for(let r of t)e.push(yield r.get());return f.create(e)})}set(t){return o(this,null,function*(){this.logger.log("set version: "+t);let e=this.resolveStores(),r=[];for(let s of e)r.push(yield s.set(t));return f.create(r,t)})}update(){return o(this,null,function*(){let t=yield this.get();this.logger.log("new version: "+t);let e=this.resolveStores(),r=[];for(let s of e)r.push(yield s.update(t));return f.create(r,t)})}resolveStores(){var t;return (t=this.opts.targets)!=null&&t.length?this.opts.targets.map(e=>y.create(e)):Array.isArray(this.opts.store)?this.opts.store:[this.opts.store]}};var _t=process.cwd();var P=class{get(){return currentNodeVersion}};var Y=(r=>(r[r.major=0]="major",r[r.minor=1]="minor",r[r.patch=2]="patch",r))(Y||{}),Z={file:"package.json",position:2,key:"version"},N=class{constructor(t){this.opts=Object.assign({},Z,t);}get(){return o(this,null,function*(){if(!(yield pathExists(this.opts.file)))throw new Error("get new version,but error: the file is not found");let{[this.opts.key]:t}=yield readJson(this.opts.file);if(!t)throw new Error("get new version, but error: the old version is not found");if(typeof t!="string")throw new Error(`get new version, but error: the old version is not string,old version: ${JSON.stringify(t)}`);let[e,r,s]=t.split(".");return this.opts.position===0?[Number(e)+1,r,s].join("."):this.opts.position===1?[e,Number(r)+1,s].join("."):[e,r,Number(s)+1].join(".")})}};export{l as ConfigStore,D as DEFAULT_JSON_STORE_OPTIONS,Z as DEFAULT_SEMANTIC_VERSION_GETTER_OPTIONS,X as DEFAULT_TEXT_STORE_OPTIONS,W as DEFAULT_VERSION_MANAGER_OPTIONS,m as JsonStore,P as NodeVersionGetter,Y as Positions,N as SemanticVersionGetter,y as StoreFactory,d as TextStore,G as VersionManager,f as VersionUpdateResult,_t as workspace};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Store, StructuredVersionTarget, StoreGetResult, VersionValue, StoreSetResult } from './Store.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description 结构化配置 store.
|
|
5
|
+
*/
|
|
6
|
+
declare class ConfigStore implements Store {
|
|
7
|
+
/**
|
|
8
|
+
* @description 当前 store 绑定的目标.
|
|
9
|
+
*/
|
|
10
|
+
target: StructuredVersionTarget;
|
|
11
|
+
/**
|
|
12
|
+
* @description 初始化结构化配置 store.
|
|
13
|
+
* @param target 目标配置.
|
|
14
|
+
*/
|
|
15
|
+
constructor(target: StructuredVersionTarget);
|
|
16
|
+
/**
|
|
17
|
+
* @description 读取结构化文件里的版本值.
|
|
18
|
+
*/
|
|
19
|
+
get(): Promise<StoreGetResult>;
|
|
20
|
+
/**
|
|
21
|
+
* @description 写入结构化文件里的版本值.
|
|
22
|
+
* @param value 版本值.
|
|
23
|
+
*/
|
|
24
|
+
set(value: VersionValue): Promise<StoreSetResult>;
|
|
25
|
+
/**
|
|
26
|
+
* @description 更新结构化文件里的版本值.
|
|
27
|
+
* @param value 版本值.
|
|
28
|
+
*/
|
|
29
|
+
update(value: VersionValue): Promise<StoreSetResult>;
|
|
30
|
+
/**
|
|
31
|
+
* @description 创建读取失败结果.
|
|
32
|
+
* @param message 错误消息.
|
|
33
|
+
*/
|
|
34
|
+
protected createGetError(message: string): StoreGetResult;
|
|
35
|
+
/**
|
|
36
|
+
* @description 创建写入失败结果.
|
|
37
|
+
* @param value 目标版本值.
|
|
38
|
+
* @param message 错误消息.
|
|
39
|
+
* @param oldValue 旧版本值.
|
|
40
|
+
* @param action 操作名称.
|
|
41
|
+
*/
|
|
42
|
+
protected createSetError(value: VersionValue, message: string, oldValue?: VersionValue, action?: 'set' | 'update'): StoreSetResult;
|
|
43
|
+
/**
|
|
44
|
+
* @description 提取异常消息.
|
|
45
|
+
* @param error 异常对象.
|
|
46
|
+
*/
|
|
47
|
+
protected getErrorMessage(error: unknown): string;
|
|
48
|
+
/**
|
|
49
|
+
* @description 读取结构化配置.
|
|
50
|
+
*/
|
|
51
|
+
protected loadStructuredConfig(): Promise<any>;
|
|
52
|
+
/**
|
|
53
|
+
* @description 保存结构化配置.
|
|
54
|
+
* @param config 配置对象.
|
|
55
|
+
*/
|
|
56
|
+
protected saveStructuredConfig(config: any): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* @description 解析 env 文本.
|
|
59
|
+
* @param fileContent 文件内容.
|
|
60
|
+
*/
|
|
61
|
+
protected parseEnvContent(fileContent: string): Record<string, unknown>;
|
|
62
|
+
/**
|
|
63
|
+
* @description 序列化 env 对象.
|
|
64
|
+
* @param config 配置对象.
|
|
65
|
+
*/
|
|
66
|
+
protected stringifyEnvContent(config: Record<string, unknown>): string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { ConfigStore };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Store, StructuredVersionTarget, StoreGetResult, VersionValue, StoreSetResult } from './Store.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description 结构化配置 store.
|
|
5
|
+
*/
|
|
6
|
+
declare class ConfigStore implements Store {
|
|
7
|
+
/**
|
|
8
|
+
* @description 当前 store 绑定的目标.
|
|
9
|
+
*/
|
|
10
|
+
target: StructuredVersionTarget;
|
|
11
|
+
/**
|
|
12
|
+
* @description 初始化结构化配置 store.
|
|
13
|
+
* @param target 目标配置.
|
|
14
|
+
*/
|
|
15
|
+
constructor(target: StructuredVersionTarget);
|
|
16
|
+
/**
|
|
17
|
+
* @description 读取结构化文件里的版本值.
|
|
18
|
+
*/
|
|
19
|
+
get(): Promise<StoreGetResult>;
|
|
20
|
+
/**
|
|
21
|
+
* @description 写入结构化文件里的版本值.
|
|
22
|
+
* @param value 版本值.
|
|
23
|
+
*/
|
|
24
|
+
set(value: VersionValue): Promise<StoreSetResult>;
|
|
25
|
+
/**
|
|
26
|
+
* @description 更新结构化文件里的版本值.
|
|
27
|
+
* @param value 版本值.
|
|
28
|
+
*/
|
|
29
|
+
update(value: VersionValue): Promise<StoreSetResult>;
|
|
30
|
+
/**
|
|
31
|
+
* @description 创建读取失败结果.
|
|
32
|
+
* @param message 错误消息.
|
|
33
|
+
*/
|
|
34
|
+
protected createGetError(message: string): StoreGetResult;
|
|
35
|
+
/**
|
|
36
|
+
* @description 创建写入失败结果.
|
|
37
|
+
* @param value 目标版本值.
|
|
38
|
+
* @param message 错误消息.
|
|
39
|
+
* @param oldValue 旧版本值.
|
|
40
|
+
* @param action 操作名称.
|
|
41
|
+
*/
|
|
42
|
+
protected createSetError(value: VersionValue, message: string, oldValue?: VersionValue, action?: 'set' | 'update'): StoreSetResult;
|
|
43
|
+
/**
|
|
44
|
+
* @description 提取异常消息.
|
|
45
|
+
* @param error 异常对象.
|
|
46
|
+
*/
|
|
47
|
+
protected getErrorMessage(error: unknown): string;
|
|
48
|
+
/**
|
|
49
|
+
* @description 读取结构化配置.
|
|
50
|
+
*/
|
|
51
|
+
protected loadStructuredConfig(): Promise<any>;
|
|
52
|
+
/**
|
|
53
|
+
* @description 保存结构化配置.
|
|
54
|
+
* @param config 配置对象.
|
|
55
|
+
*/
|
|
56
|
+
protected saveStructuredConfig(config: any): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* @description 解析 env 文本.
|
|
59
|
+
* @param fileContent 文件内容.
|
|
60
|
+
*/
|
|
61
|
+
protected parseEnvContent(fileContent: string): Record<string, unknown>;
|
|
62
|
+
/**
|
|
63
|
+
* @description 序列化 env 对象.
|
|
64
|
+
* @param config 配置对象.
|
|
65
|
+
*/
|
|
66
|
+
protected stringifyEnvContent(config: Record<string, unknown>): string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { ConfigStore };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
'use strict';var fsExtra=require('fs-extra'),fastXmlParser=require('fast-xml-parser'),m=require('js-yaml'),lodash=require('lodash'),smolToml=require('smol-toml');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var m__default=/*#__PURE__*/_interopDefault(m);/**
|
|
2
|
+
@tikkhun/version
|
|
3
|
+
2026.4.13
|
|
4
|
+
*/
|
|
5
|
+
var E=Object.defineProperty,V=Object.defineProperties;var C=Object.getOwnPropertyDescriptors;var l=Object.getOwnPropertySymbols;var R=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var h=(n,t,e)=>t in n?E(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e,p=(n,t)=>{for(var e in t||(t={}))R.call(t,e)&&h(n,e,t[e]);if(l)for(var e of l(t))b.call(t,e)&&h(n,e,t[e]);return n},d=(n,t)=>V(n,C(t));var c=(n,t,e)=>new Promise((r,s)=>{var i=a=>{try{u(e.next(a));}catch(f){s(f);}},o=a=>{try{u(e.throw(a));}catch(f){s(f);}},u=a=>a.done?r(a.value):Promise.resolve(a.value).then(i,o);u((e=e.apply(n,t)).next());});var S=class{constructor(t){this.target=t;}get(){return c(this,null,function*(){var t;try{if(!(yield fsExtra.pathExists(this.target.file)))return this.createGetError(`file is not found: ${this.target.file}`);let e=yield this.loadStructuredConfig(),r=this.target.path?lodash.get(e,this.target.path):e;return r===void 0?this.createGetError(`path is not found: ${(t=this.target.path)!=null?t:"<root>"}`):typeof r!="string"&&typeof r!="number"?this.createGetError(`value is not string or number: ${JSON.stringify(r)}`):{action:"get",success:!0,file:this.target.file,type:this.target.type,value:r}}catch(e){return this.createGetError(this.getErrorMessage(e))}})}set(t){return c(this,null,function*(){try{let e=yield this.get(),r=e.success?e.value:void 0,s=yield fsExtra.pathExists(this.target.file);if(!s){if(!this.target.createIfNotExists)return this.createSetError(t,`file is not found: ${this.target.file}`,r,"set");yield fsExtra.ensureFile(this.target.file);}let i=s?yield this.loadStructuredConfig():{};if(this.target.path)lodash.set(i,this.target.path,t),yield this.saveStructuredConfig(i);else {if(typeof i=="object"&&i!==null&&!Array.isArray(i))return this.createSetError(t,"path is required for structured target",r,"set");yield this.saveStructuredConfig(t);}return {action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:r,newValue:t}}catch(e){return this.createSetError(t,this.getErrorMessage(e),void 0,"set")}})}update(t){return c(this,null,function*(){let e=yield this.set(t);return d(p({},e),{action:"update"})})}createGetError(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t}}createSetError(t,e,r,s="set"){return {action:s,success:false,file:this.target.file,type:this.target.type,oldValue:r,newValue:t,message:e}}getErrorMessage(t){return t instanceof Error?t.message:String(t)}loadStructuredConfig(){return c(this,null,function*(){var e;let t=yield fsExtra.readFile(this.target.file,(e=this.target.encoding)!=null?e:"utf8");switch(this.target.type){case "json":return JSON.parse(t);case "yaml":return m__default.default.load(t);case "toml":return smolToml.parse(t);case "xml":return new fastXmlParser.XMLParser({ignoreAttributes:true}).parse(t);case "env":return this.parseEnvContent(t);default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}saveStructuredConfig(t){return c(this,null,function*(){var r;let e=(r=this.target.encoding)!=null?r:"utf8";switch(this.target.type){case "json":yield fsExtra.writeFile(this.target.file,JSON.stringify(t,null,2),e);return;case "yaml":yield fsExtra.writeFile(this.target.file,m__default.default.dump(t),e);return;case "toml":yield fsExtra.writeFile(this.target.file,smolToml.stringify(t),e);return;case "xml":yield fsExtra.writeFile(this.target.file,new fastXmlParser.XMLBuilder({format:true,ignoreAttributes:true}).build(t),e);return;case "env":yield fsExtra.writeFile(this.target.file,this.stringifyEnvContent(t),e);return;default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}parseEnvContent(t){let e={},r=t.split(/\r?\n/);for(let s of r){let i=s.trim();if(!i||i.startsWith("#"))continue;let o=i.indexOf("=");if(o<0)continue;let u=i.slice(0,o).trim(),a=i.slice(o+1).trim();lodash.set(e,u.replace(/__/g,"."),a);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[u,a]of Object.entries(s))r(a,[...i,u]);return}let o=i.join("__").toUpperCase();e.push(`${o}=${s!=null?s:""}`);};for(let[s,i]of Object.entries(t))r(i,[s]);return e.join(`
|
|
6
|
+
`)}};exports.ConfigStore=S;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import {pathExists,ensureFile,readFile,writeFile}from'fs-extra';import {XMLParser,XMLBuilder}from'fast-xml-parser';import m from'js-yaml';import {set,get}from'lodash';import {parse,stringify}from'smol-toml';/**
|
|
2
|
+
@tikkhun/version
|
|
3
|
+
2026.4.13
|
|
4
|
+
*/
|
|
5
|
+
var E=Object.defineProperty,V=Object.defineProperties;var C=Object.getOwnPropertyDescriptors;var l=Object.getOwnPropertySymbols;var R=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var h=(n,t,e)=>t in n?E(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e,p=(n,t)=>{for(var e in t||(t={}))R.call(t,e)&&h(n,e,t[e]);if(l)for(var e of l(t))b.call(t,e)&&h(n,e,t[e]);return n},d=(n,t)=>V(n,C(t));var c=(n,t,e)=>new Promise((r,s)=>{var i=a=>{try{u(e.next(a));}catch(f){s(f);}},o=a=>{try{u(e.throw(a));}catch(f){s(f);}},u=a=>a.done?r(a.value):Promise.resolve(a.value).then(i,o);u((e=e.apply(n,t)).next());});var S=class{constructor(t){this.target=t;}get(){return c(this,null,function*(){var t;try{if(!(yield pathExists(this.target.file)))return this.createGetError(`file is not found: ${this.target.file}`);let e=yield this.loadStructuredConfig(),r=this.target.path?get(e,this.target.path):e;return r===void 0?this.createGetError(`path is not found: ${(t=this.target.path)!=null?t:"<root>"}`):typeof r!="string"&&typeof r!="number"?this.createGetError(`value is not string or number: ${JSON.stringify(r)}`):{action:"get",success:!0,file:this.target.file,type:this.target.type,value:r}}catch(e){return this.createGetError(this.getErrorMessage(e))}})}set(t){return c(this,null,function*(){try{let e=yield this.get(),r=e.success?e.value:void 0,s=yield pathExists(this.target.file);if(!s){if(!this.target.createIfNotExists)return this.createSetError(t,`file is not found: ${this.target.file}`,r,"set");yield ensureFile(this.target.file);}let i=s?yield this.loadStructuredConfig():{};if(this.target.path)set(i,this.target.path,t),yield this.saveStructuredConfig(i);else {if(typeof i=="object"&&i!==null&&!Array.isArray(i))return this.createSetError(t,"path is required for structured target",r,"set");yield this.saveStructuredConfig(t);}return {action:"set",success:!0,file:this.target.file,type:this.target.type,oldValue:r,newValue:t}}catch(e){return this.createSetError(t,this.getErrorMessage(e),void 0,"set")}})}update(t){return c(this,null,function*(){let e=yield this.set(t);return d(p({},e),{action:"update"})})}createGetError(t){return {action:"get",success:false,file:this.target.file,type:this.target.type,message:t}}createSetError(t,e,r,s="set"){return {action:s,success:false,file:this.target.file,type:this.target.type,oldValue:r,newValue:t,message:e}}getErrorMessage(t){return t instanceof Error?t.message:String(t)}loadStructuredConfig(){return c(this,null,function*(){var e;let t=yield readFile(this.target.file,(e=this.target.encoding)!=null?e:"utf8");switch(this.target.type){case "json":return JSON.parse(t);case "yaml":return m.load(t);case "toml":return parse(t);case "xml":return new XMLParser({ignoreAttributes:true}).parse(t);case "env":return this.parseEnvContent(t);default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}saveStructuredConfig(t){return c(this,null,function*(){var r;let e=(r=this.target.encoding)!=null?r:"utf8";switch(this.target.type){case "json":yield writeFile(this.target.file,JSON.stringify(t,null,2),e);return;case "yaml":yield writeFile(this.target.file,m.dump(t),e);return;case "toml":yield writeFile(this.target.file,stringify(t),e);return;case "xml":yield writeFile(this.target.file,new XMLBuilder({format:true,ignoreAttributes:true}).build(t),e);return;case "env":yield writeFile(this.target.file,this.stringifyEnvContent(t),e);return;default:throw new Error(`unsupported structured target type: ${this.target.type}`)}})}parseEnvContent(t){let e={},r=t.split(/\r?\n/);for(let s of r){let i=s.trim();if(!i||i.startsWith("#"))continue;let o=i.indexOf("=");if(o<0)continue;let u=i.slice(0,o).trim(),a=i.slice(o+1).trim();set(e,u.replace(/__/g,"."),a);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[u,a]of Object.entries(s))r(a,[...i,u]);return}let o=i.join("__").toUpperCase();e.push(`${o}=${s!=null?s:""}`);};for(let[s,i]of Object.entries(t))r(i,[s]);return e.join(`
|
|
6
|
+
`)}};export{S as ConfigStore};
|
|
@@ -1,17 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ConfigStore } from './ConfigStore.mjs';
|
|
2
|
+
import './Store.mjs';
|
|
2
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @description JSON store 配置.
|
|
6
|
+
*/
|
|
3
7
|
interface JsonStoreOptions {
|
|
8
|
+
/**
|
|
9
|
+
* @description 目标文件路径.
|
|
10
|
+
*/
|
|
4
11
|
file: string;
|
|
5
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @description 兼容旧版本的 key 配置.
|
|
14
|
+
*/
|
|
15
|
+
key?: string;
|
|
16
|
+
/**
|
|
17
|
+
* @description 新版推荐的对象路径配置.
|
|
18
|
+
*/
|
|
19
|
+
path?: string;
|
|
20
|
+
/**
|
|
21
|
+
* @description 文件不存在时是否允许创建.
|
|
22
|
+
*/
|
|
23
|
+
createIfNotExists?: boolean;
|
|
6
24
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @description JSON store 默认配置.
|
|
27
|
+
*/
|
|
28
|
+
declare const DEFAULT_JSON_STORE_OPTIONS: JsonStoreOptions;
|
|
29
|
+
/**
|
|
30
|
+
* @description JSON 版本 store.
|
|
31
|
+
*/
|
|
32
|
+
declare class JsonStore extends ConfigStore {
|
|
33
|
+
/**
|
|
34
|
+
* @description 初始化 JSON store.
|
|
35
|
+
* @param options store 配置.
|
|
36
|
+
*/
|
|
13
37
|
constructor(options?: Partial<JsonStoreOptions>);
|
|
14
|
-
update(value: any): Promise<boolean>;
|
|
15
38
|
}
|
|
16
39
|
|
|
17
40
|
export { DEFAULT_JSON_STORE_OPTIONS, JsonStore, type JsonStoreOptions };
|
|
@@ -1,17 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ConfigStore } from './ConfigStore.js';
|
|
2
|
+
import './Store.js';
|
|
2
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @description JSON store 配置.
|
|
6
|
+
*/
|
|
3
7
|
interface JsonStoreOptions {
|
|
8
|
+
/**
|
|
9
|
+
* @description 目标文件路径.
|
|
10
|
+
*/
|
|
4
11
|
file: string;
|
|
5
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @description 兼容旧版本的 key 配置.
|
|
14
|
+
*/
|
|
15
|
+
key?: string;
|
|
16
|
+
/**
|
|
17
|
+
* @description 新版推荐的对象路径配置.
|
|
18
|
+
*/
|
|
19
|
+
path?: string;
|
|
20
|
+
/**
|
|
21
|
+
* @description 文件不存在时是否允许创建.
|
|
22
|
+
*/
|
|
23
|
+
createIfNotExists?: boolean;
|
|
6
24
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @description JSON store 默认配置.
|
|
27
|
+
*/
|
|
28
|
+
declare const DEFAULT_JSON_STORE_OPTIONS: JsonStoreOptions;
|
|
29
|
+
/**
|
|
30
|
+
* @description JSON 版本 store.
|
|
31
|
+
*/
|
|
32
|
+
declare class JsonStore extends ConfigStore {
|
|
33
|
+
/**
|
|
34
|
+
* @description 初始化 JSON store.
|
|
35
|
+
* @param options store 配置.
|
|
36
|
+
*/
|
|
13
37
|
constructor(options?: Partial<JsonStoreOptions>);
|
|
14
|
-
update(value: any): Promise<boolean>;
|
|
15
38
|
}
|
|
16
39
|
|
|
17
40
|
export { DEFAULT_JSON_STORE_OPTIONS, JsonStore, type JsonStoreOptions };
|