@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,5 +1,6 @@
|
|
|
1
|
-
'use strict';var fsExtra=require('fs-extra');/**
|
|
1
|
+
'use strict';var fsExtra=require('fs-extra'),fastXmlParser=require('fast-xml-parser'),S=require('js-yaml'),lodash=require('lodash'),smolToml=require('smol-toml');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var S__default=/*#__PURE__*/_interopDefault(S);/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var V=Object.defineProperty,x=Object.defineProperties;var O=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var h=(n,t,e)=>t in n?V(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e,d=(n,t)=>{for(var e in t||(t={}))C.call(t,e)&&h(n,e,t[e]);if(p)for(var e of p(t))k.call(t,e)&&h(n,e,t[e]);return n},y=(n,t)=>x(n,O(t));var u=(n,t,e)=>new Promise((r,s)=>{var i=o=>{try{c(e.next(o));}catch(l){s(l);}},a=o=>{try{c(e.throw(o));}catch(l){s(l);}},c=o=>o.done?r(o.value):Promise.resolve(o.value).then(i,a);c((e=e.apply(n,t)).next());});var f=class{constructor(t){this.target=t;}get(){return u(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 u(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 u(this,null,function*(){let e=yield this.set(t);return y(d({},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 u(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 S__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 u(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,S__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 a=i.indexOf("=");if(a<0)continue;let c=i.slice(0,a).trim(),o=i.slice(a+1).trim();lodash.set(e,c.replace(/__/g,"."),o);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[c,o]of Object.entries(s))r(o,[...i,c]);return}let a=i.join("__").toUpperCase();e.push(`${a}=${s!=null?s:""}`);};for(let[s,i]of Object.entries(t))r(i,[s]);return e.join(`
|
|
6
|
+
`)}};var $={file:"package.json",key:"version"},E=class extends f{constructor(t){var s;let e=Object.assign({},$,t),r={type:"json",file:e.file,path:(s=e.path)!=null?s:e.key,createIfNotExists:e.createIfNotExists};super(r);}};exports.DEFAULT_JSON_STORE_OPTIONS=$;exports.JsonStore=E;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {pathExists,ensureFile,readFile,writeFile}from'fs-extra';import {XMLParser,XMLBuilder}from'fast-xml-parser';import S from'js-yaml';import {set,get}from'lodash';import {parse,stringify}from'smol-toml';/**
|
|
2
2
|
@tikkhun/version
|
|
3
|
-
|
|
3
|
+
2026.4.13
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var V=Object.defineProperty,x=Object.defineProperties;var O=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var h=(n,t,e)=>t in n?V(n,t,{enumerable:true,configurable:true,writable:true,value:e}):n[t]=e,d=(n,t)=>{for(var e in t||(t={}))C.call(t,e)&&h(n,e,t[e]);if(p)for(var e of p(t))k.call(t,e)&&h(n,e,t[e]);return n},y=(n,t)=>x(n,O(t));var u=(n,t,e)=>new Promise((r,s)=>{var i=o=>{try{c(e.next(o));}catch(l){s(l);}},a=o=>{try{c(e.throw(o));}catch(l){s(l);}},c=o=>o.done?r(o.value):Promise.resolve(o.value).then(i,a);c((e=e.apply(n,t)).next());});var f=class{constructor(t){this.target=t;}get(){return u(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 u(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 u(this,null,function*(){let e=yield this.set(t);return y(d({},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 u(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 S.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 u(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,S.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 a=i.indexOf("=");if(a<0)continue;let c=i.slice(0,a).trim(),o=i.slice(a+1).trim();set(e,c.replace(/__/g,"."),o);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[c,o]of Object.entries(s))r(o,[...i,c]);return}let a=i.join("__").toUpperCase();e.push(`${a}=${s!=null?s:""}`);};for(let[s,i]of Object.entries(t))r(i,[s]);return e.join(`
|
|
6
|
+
`)}};var $={file:"package.json",key:"version"},E=class extends f{constructor(t){var s;let e=Object.assign({},$,t),r={type:"json",file:e.file,path:(s=e.path)!=null?s:e.key,createIfNotExists:e.createIfNotExists};super(r);}};export{$ as DEFAULT_JSON_STORE_OPTIONS,E as JsonStore};
|
|
@@ -1,8 +1,186 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @description 版本值类型.
|
|
3
|
+
*/
|
|
4
|
+
type VersionValue = string | number;
|
|
5
|
+
/**
|
|
6
|
+
* @description 结构化文件类型.
|
|
7
|
+
*/
|
|
8
|
+
type StructuredVersionTargetType = 'json' | 'yaml' | 'toml' | 'xml' | 'env';
|
|
9
|
+
/**
|
|
10
|
+
* @description 文本文件更新模式.
|
|
11
|
+
*/
|
|
12
|
+
type TextStoreMode = 'raw' | 'replace';
|
|
13
|
+
/**
|
|
14
|
+
* @description 版本目标基础配置.
|
|
15
|
+
*/
|
|
16
|
+
interface BaseVersionTarget {
|
|
17
|
+
/**
|
|
18
|
+
* @description 目标文件路径.
|
|
19
|
+
*/
|
|
20
|
+
file: string;
|
|
21
|
+
/**
|
|
22
|
+
* @description 文件编码.
|
|
23
|
+
*/
|
|
24
|
+
encoding?: BufferEncoding;
|
|
25
|
+
/**
|
|
26
|
+
* @description 文件不存在时是否允许创建.
|
|
27
|
+
*/
|
|
28
|
+
createIfNotExists?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @description 结构化配置文件目标.
|
|
32
|
+
*/
|
|
33
|
+
interface StructuredVersionTarget extends BaseVersionTarget {
|
|
34
|
+
/**
|
|
35
|
+
* @description 目标文件类型.
|
|
36
|
+
*/
|
|
37
|
+
type: StructuredVersionTargetType;
|
|
38
|
+
/**
|
|
39
|
+
* @description 配置路径. 例如 version 或 build.app.version.
|
|
40
|
+
*/
|
|
41
|
+
path?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @description 文本文件目标.
|
|
45
|
+
*/
|
|
46
|
+
interface TextVersionTarget extends BaseVersionTarget {
|
|
47
|
+
/**
|
|
48
|
+
* @description 文本存储类型.
|
|
49
|
+
*/
|
|
50
|
+
type: 'text';
|
|
51
|
+
/**
|
|
52
|
+
* @description 文本更新模式.
|
|
53
|
+
*/
|
|
54
|
+
mode?: TextStoreMode;
|
|
55
|
+
/**
|
|
56
|
+
* @description 读取文本时是否需要 trim.
|
|
57
|
+
*/
|
|
58
|
+
trim?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* @description 替换模式的正则表达式.
|
|
61
|
+
*/
|
|
62
|
+
pattern?: string;
|
|
63
|
+
/**
|
|
64
|
+
* @description 替换模式的正则标记.
|
|
65
|
+
*/
|
|
66
|
+
flags?: string;
|
|
67
|
+
/**
|
|
68
|
+
* @description 替换模板. 支持 {{version}} 占位符.
|
|
69
|
+
*/
|
|
70
|
+
replacement?: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @description 自定义目标配置.
|
|
74
|
+
*/
|
|
75
|
+
interface CustomVersionTarget extends BaseVersionTarget {
|
|
76
|
+
/**
|
|
77
|
+
* @description 自定义类型.
|
|
78
|
+
*/
|
|
79
|
+
type: 'custom';
|
|
80
|
+
/**
|
|
81
|
+
* @description 自定义读取函数.
|
|
82
|
+
* @param file 文件路径.
|
|
83
|
+
*/
|
|
84
|
+
get?: (file: string) => Promise<VersionValue> | VersionValue;
|
|
85
|
+
/**
|
|
86
|
+
* @description 自定义写入函数.
|
|
87
|
+
* @param value 版本值.
|
|
88
|
+
* @param file 文件路径.
|
|
89
|
+
*/
|
|
90
|
+
set?: (value: VersionValue, file: string) => Promise<void> | void;
|
|
3
91
|
}
|
|
4
|
-
|
|
92
|
+
/**
|
|
93
|
+
* @description 支持的版本目标.
|
|
94
|
+
*/
|
|
95
|
+
type VersionTarget = StructuredVersionTarget | TextVersionTarget | CustomVersionTarget;
|
|
96
|
+
/**
|
|
97
|
+
* @description store 读取结果.
|
|
98
|
+
*/
|
|
99
|
+
interface StoreGetResult {
|
|
100
|
+
/**
|
|
101
|
+
* @description 操作名称.
|
|
102
|
+
*/
|
|
103
|
+
action: 'get';
|
|
104
|
+
/**
|
|
105
|
+
* @description 是否成功.
|
|
106
|
+
*/
|
|
107
|
+
success: boolean;
|
|
108
|
+
/**
|
|
109
|
+
* @description 目标文件路径.
|
|
110
|
+
*/
|
|
5
111
|
file: string;
|
|
112
|
+
/**
|
|
113
|
+
* @description 目标文件类型.
|
|
114
|
+
*/
|
|
115
|
+
type: VersionTarget['type'];
|
|
116
|
+
/**
|
|
117
|
+
* @description 当前读取到的值.
|
|
118
|
+
*/
|
|
119
|
+
value?: VersionValue;
|
|
120
|
+
/**
|
|
121
|
+
* @description 附加消息.
|
|
122
|
+
*/
|
|
123
|
+
message?: string;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @description store 写入结果.
|
|
127
|
+
*/
|
|
128
|
+
interface StoreSetResult {
|
|
129
|
+
/**
|
|
130
|
+
* @description 操作名称.
|
|
131
|
+
*/
|
|
132
|
+
action: 'set' | 'update';
|
|
133
|
+
/**
|
|
134
|
+
* @description 是否成功.
|
|
135
|
+
*/
|
|
136
|
+
success: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* @description 目标文件路径.
|
|
139
|
+
*/
|
|
140
|
+
file: string;
|
|
141
|
+
/**
|
|
142
|
+
* @description 目标文件类型.
|
|
143
|
+
*/
|
|
144
|
+
type: VersionTarget['type'];
|
|
145
|
+
/**
|
|
146
|
+
* @description 写入前的旧值.
|
|
147
|
+
*/
|
|
148
|
+
oldValue?: VersionValue;
|
|
149
|
+
/**
|
|
150
|
+
* @description 写入后的新值.
|
|
151
|
+
*/
|
|
152
|
+
newValue?: VersionValue;
|
|
153
|
+
/**
|
|
154
|
+
* @description 附加消息.
|
|
155
|
+
*/
|
|
156
|
+
message?: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @description store 操作结果.
|
|
160
|
+
*/
|
|
161
|
+
type StoreResult = StoreGetResult | StoreSetResult;
|
|
162
|
+
/**
|
|
163
|
+
* @description store 接口. 负责从目标文件读取和写入版本.
|
|
164
|
+
*/
|
|
165
|
+
interface Store {
|
|
166
|
+
/**
|
|
167
|
+
* @description 绑定的目标配置.
|
|
168
|
+
*/
|
|
169
|
+
target: VersionTarget;
|
|
170
|
+
/**
|
|
171
|
+
* @description 读取当前版本值.
|
|
172
|
+
*/
|
|
173
|
+
get(): Promise<StoreGetResult> | StoreGetResult;
|
|
174
|
+
/**
|
|
175
|
+
* @description 直接写入版本值.
|
|
176
|
+
* @param value 版本值.
|
|
177
|
+
*/
|
|
178
|
+
set(value: VersionValue): Promise<StoreSetResult> | StoreSetResult;
|
|
179
|
+
/**
|
|
180
|
+
* @description 更新版本值. 语义上等同 set, 但保留给 CLI 和日志使用.
|
|
181
|
+
* @param value 版本值.
|
|
182
|
+
*/
|
|
183
|
+
update(value: VersionValue): Promise<StoreSetResult> | StoreSetResult;
|
|
6
184
|
}
|
|
7
185
|
|
|
8
|
-
export type { Store,
|
|
186
|
+
export type { BaseVersionTarget, CustomVersionTarget, Store, StoreGetResult, StoreResult, StoreSetResult, StructuredVersionTarget, StructuredVersionTargetType, TextStoreMode, TextVersionTarget, VersionTarget, VersionValue };
|
|
@@ -1,8 +1,186 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @description 版本值类型.
|
|
3
|
+
*/
|
|
4
|
+
type VersionValue = string | number;
|
|
5
|
+
/**
|
|
6
|
+
* @description 结构化文件类型.
|
|
7
|
+
*/
|
|
8
|
+
type StructuredVersionTargetType = 'json' | 'yaml' | 'toml' | 'xml' | 'env';
|
|
9
|
+
/**
|
|
10
|
+
* @description 文本文件更新模式.
|
|
11
|
+
*/
|
|
12
|
+
type TextStoreMode = 'raw' | 'replace';
|
|
13
|
+
/**
|
|
14
|
+
* @description 版本目标基础配置.
|
|
15
|
+
*/
|
|
16
|
+
interface BaseVersionTarget {
|
|
17
|
+
/**
|
|
18
|
+
* @description 目标文件路径.
|
|
19
|
+
*/
|
|
20
|
+
file: string;
|
|
21
|
+
/**
|
|
22
|
+
* @description 文件编码.
|
|
23
|
+
*/
|
|
24
|
+
encoding?: BufferEncoding;
|
|
25
|
+
/**
|
|
26
|
+
* @description 文件不存在时是否允许创建.
|
|
27
|
+
*/
|
|
28
|
+
createIfNotExists?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @description 结构化配置文件目标.
|
|
32
|
+
*/
|
|
33
|
+
interface StructuredVersionTarget extends BaseVersionTarget {
|
|
34
|
+
/**
|
|
35
|
+
* @description 目标文件类型.
|
|
36
|
+
*/
|
|
37
|
+
type: StructuredVersionTargetType;
|
|
38
|
+
/**
|
|
39
|
+
* @description 配置路径. 例如 version 或 build.app.version.
|
|
40
|
+
*/
|
|
41
|
+
path?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @description 文本文件目标.
|
|
45
|
+
*/
|
|
46
|
+
interface TextVersionTarget extends BaseVersionTarget {
|
|
47
|
+
/**
|
|
48
|
+
* @description 文本存储类型.
|
|
49
|
+
*/
|
|
50
|
+
type: 'text';
|
|
51
|
+
/**
|
|
52
|
+
* @description 文本更新模式.
|
|
53
|
+
*/
|
|
54
|
+
mode?: TextStoreMode;
|
|
55
|
+
/**
|
|
56
|
+
* @description 读取文本时是否需要 trim.
|
|
57
|
+
*/
|
|
58
|
+
trim?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* @description 替换模式的正则表达式.
|
|
61
|
+
*/
|
|
62
|
+
pattern?: string;
|
|
63
|
+
/**
|
|
64
|
+
* @description 替换模式的正则标记.
|
|
65
|
+
*/
|
|
66
|
+
flags?: string;
|
|
67
|
+
/**
|
|
68
|
+
* @description 替换模板. 支持 {{version}} 占位符.
|
|
69
|
+
*/
|
|
70
|
+
replacement?: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @description 自定义目标配置.
|
|
74
|
+
*/
|
|
75
|
+
interface CustomVersionTarget extends BaseVersionTarget {
|
|
76
|
+
/**
|
|
77
|
+
* @description 自定义类型.
|
|
78
|
+
*/
|
|
79
|
+
type: 'custom';
|
|
80
|
+
/**
|
|
81
|
+
* @description 自定义读取函数.
|
|
82
|
+
* @param file 文件路径.
|
|
83
|
+
*/
|
|
84
|
+
get?: (file: string) => Promise<VersionValue> | VersionValue;
|
|
85
|
+
/**
|
|
86
|
+
* @description 自定义写入函数.
|
|
87
|
+
* @param value 版本值.
|
|
88
|
+
* @param file 文件路径.
|
|
89
|
+
*/
|
|
90
|
+
set?: (value: VersionValue, file: string) => Promise<void> | void;
|
|
3
91
|
}
|
|
4
|
-
|
|
92
|
+
/**
|
|
93
|
+
* @description 支持的版本目标.
|
|
94
|
+
*/
|
|
95
|
+
type VersionTarget = StructuredVersionTarget | TextVersionTarget | CustomVersionTarget;
|
|
96
|
+
/**
|
|
97
|
+
* @description store 读取结果.
|
|
98
|
+
*/
|
|
99
|
+
interface StoreGetResult {
|
|
100
|
+
/**
|
|
101
|
+
* @description 操作名称.
|
|
102
|
+
*/
|
|
103
|
+
action: 'get';
|
|
104
|
+
/**
|
|
105
|
+
* @description 是否成功.
|
|
106
|
+
*/
|
|
107
|
+
success: boolean;
|
|
108
|
+
/**
|
|
109
|
+
* @description 目标文件路径.
|
|
110
|
+
*/
|
|
5
111
|
file: string;
|
|
112
|
+
/**
|
|
113
|
+
* @description 目标文件类型.
|
|
114
|
+
*/
|
|
115
|
+
type: VersionTarget['type'];
|
|
116
|
+
/**
|
|
117
|
+
* @description 当前读取到的值.
|
|
118
|
+
*/
|
|
119
|
+
value?: VersionValue;
|
|
120
|
+
/**
|
|
121
|
+
* @description 附加消息.
|
|
122
|
+
*/
|
|
123
|
+
message?: string;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @description store 写入结果.
|
|
127
|
+
*/
|
|
128
|
+
interface StoreSetResult {
|
|
129
|
+
/**
|
|
130
|
+
* @description 操作名称.
|
|
131
|
+
*/
|
|
132
|
+
action: 'set' | 'update';
|
|
133
|
+
/**
|
|
134
|
+
* @description 是否成功.
|
|
135
|
+
*/
|
|
136
|
+
success: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* @description 目标文件路径.
|
|
139
|
+
*/
|
|
140
|
+
file: string;
|
|
141
|
+
/**
|
|
142
|
+
* @description 目标文件类型.
|
|
143
|
+
*/
|
|
144
|
+
type: VersionTarget['type'];
|
|
145
|
+
/**
|
|
146
|
+
* @description 写入前的旧值.
|
|
147
|
+
*/
|
|
148
|
+
oldValue?: VersionValue;
|
|
149
|
+
/**
|
|
150
|
+
* @description 写入后的新值.
|
|
151
|
+
*/
|
|
152
|
+
newValue?: VersionValue;
|
|
153
|
+
/**
|
|
154
|
+
* @description 附加消息.
|
|
155
|
+
*/
|
|
156
|
+
message?: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @description store 操作结果.
|
|
160
|
+
*/
|
|
161
|
+
type StoreResult = StoreGetResult | StoreSetResult;
|
|
162
|
+
/**
|
|
163
|
+
* @description store 接口. 负责从目标文件读取和写入版本.
|
|
164
|
+
*/
|
|
165
|
+
interface Store {
|
|
166
|
+
/**
|
|
167
|
+
* @description 绑定的目标配置.
|
|
168
|
+
*/
|
|
169
|
+
target: VersionTarget;
|
|
170
|
+
/**
|
|
171
|
+
* @description 读取当前版本值.
|
|
172
|
+
*/
|
|
173
|
+
get(): Promise<StoreGetResult> | StoreGetResult;
|
|
174
|
+
/**
|
|
175
|
+
* @description 直接写入版本值.
|
|
176
|
+
* @param value 版本值.
|
|
177
|
+
*/
|
|
178
|
+
set(value: VersionValue): Promise<StoreSetResult> | StoreSetResult;
|
|
179
|
+
/**
|
|
180
|
+
* @description 更新版本值. 语义上等同 set, 但保留给 CLI 和日志使用.
|
|
181
|
+
* @param value 版本值.
|
|
182
|
+
*/
|
|
183
|
+
update(value: VersionValue): Promise<StoreSetResult> | StoreSetResult;
|
|
6
184
|
}
|
|
7
185
|
|
|
8
|
-
export type { Store,
|
|
186
|
+
export type { BaseVersionTarget, CustomVersionTarget, Store, StoreGetResult, StoreResult, StoreSetResult, StructuredVersionTarget, StructuredVersionTargetType, TextStoreMode, TextVersionTarget, VersionTarget, VersionValue };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { VersionTarget, Store } from './Store.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description store 工厂.
|
|
5
|
+
*/
|
|
6
|
+
declare class StoreFactory {
|
|
7
|
+
/**
|
|
8
|
+
* @description 根据目标配置创建 store.
|
|
9
|
+
* @param target 目标配置.
|
|
10
|
+
*/
|
|
11
|
+
static create(target: VersionTarget): Store;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { StoreFactory };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { VersionTarget, Store } from './Store.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description store 工厂.
|
|
5
|
+
*/
|
|
6
|
+
declare class StoreFactory {
|
|
7
|
+
/**
|
|
8
|
+
* @description 根据目标配置创建 store.
|
|
9
|
+
* @param target 目标配置.
|
|
10
|
+
*/
|
|
11
|
+
static create(target: VersionTarget): Store;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { StoreFactory };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
'use strict';var fsExtra=require('fs-extra'),fastXmlParser=require('fast-xml-parser'),E=require('js-yaml'),lodash=require('lodash'),smolToml=require('smol-toml');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var E__default=/*#__PURE__*/_interopDefault(E);/**
|
|
2
|
+
@tikkhun/version
|
|
3
|
+
2026.4.13
|
|
4
|
+
*/
|
|
5
|
+
var P=Object.defineProperty,G=Object.defineProperties;var C=Object.getOwnPropertyDescriptors;var w=Object.getOwnPropertySymbols;var O=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var S=(a,t,e)=>t in a?P(a,t,{enumerable:true,configurable:true,writable:true,value:e}):a[t]=e,u=(a,t)=>{for(var e in t||(t={}))O.call(t,e)&&S(a,e,t[e]);if(w)for(var e of w(t))v.call(t,e)&&S(a,e,t[e]);return a},l=(a,t)=>G(a,C(t));var n=(a,t,e)=>new Promise((r,s)=>{var i=o=>{try{g(e.next(o));}catch(d){s(d);}},c=o=>{try{g(e.throw(o));}catch(d){s(d);}},g=o=>o.done?r(o.value):Promise.resolve(o.value).then(i,c);g((e=e.apply(a,t)).next());});var h=class{constructor(t){this.target=t;}get(){return n(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 n(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 n(this,null,function*(){let e=yield this.set(t);return l(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 n(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 E__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 n(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,E__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(),o=i.slice(c+1).trim();lodash.set(e,g.replace(/__/g,"."),o);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[g,o]of Object.entries(s))r(o,[...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 L={file:"",mode:"raw",trim:true,replacement:"{{version}}"},p=class{constructor(t){this.target=u(u({type:"text"},L),t);}get(){return n(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 n(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 n(this,null,function*(){let e=yield this.set(t);return l(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 n(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 n(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 R=class{static create(t){return t.type==="text"?new p(t):t.type==="custom"?new y(t):new h(t)}},y=class{constructor(t){this.target=t;}get(){return n(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 n(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 n(this,null,function*(){let e=yield this.set(t);return l(u({},e),{action:"update"})})}};exports.StoreFactory=R;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import {pathExists,readFile,ensureFile,writeFile}from'fs-extra';import {XMLParser,XMLBuilder}from'fast-xml-parser';import E from'js-yaml';import {set,get}from'lodash';import {parse,stringify}from'smol-toml';/**
|
|
2
|
+
@tikkhun/version
|
|
3
|
+
2026.4.13
|
|
4
|
+
*/
|
|
5
|
+
var P=Object.defineProperty,G=Object.defineProperties;var C=Object.getOwnPropertyDescriptors;var w=Object.getOwnPropertySymbols;var O=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var S=(a,t,e)=>t in a?P(a,t,{enumerable:true,configurable:true,writable:true,value:e}):a[t]=e,u=(a,t)=>{for(var e in t||(t={}))O.call(t,e)&&S(a,e,t[e]);if(w)for(var e of w(t))v.call(t,e)&&S(a,e,t[e]);return a},l=(a,t)=>G(a,C(t));var n=(a,t,e)=>new Promise((r,s)=>{var i=o=>{try{g(e.next(o));}catch(d){s(d);}},c=o=>{try{g(e.throw(o));}catch(d){s(d);}},g=o=>o.done?r(o.value):Promise.resolve(o.value).then(i,c);g((e=e.apply(a,t)).next());});var h=class{constructor(t){this.target=t;}get(){return n(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 n(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 n(this,null,function*(){let e=yield this.set(t);return l(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 n(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 E.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 n(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,E.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(),o=i.slice(c+1).trim();set(e,g.replace(/__/g,"."),o);}return e}stringifyEnvContent(t){let e=[],r=(s,i)=>{if(s!==null&&typeof s=="object"&&!Array.isArray(s)){for(let[g,o]of Object.entries(s))r(o,[...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 L={file:"",mode:"raw",trim:true,replacement:"{{version}}"},p=class{constructor(t){this.target=u(u({type:"text"},L),t);}get(){return n(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 n(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 n(this,null,function*(){let e=yield this.set(t);return l(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 n(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 n(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 R=class{static create(t){return t.type==="text"?new p(t):t.type==="custom"?new y(t):new h(t)}},y=class{constructor(t){this.target=t;}get(){return n(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 n(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 n(this,null,function*(){let e=yield this.set(t);return l(u({},e),{action:"update"})})}};export{R as StoreFactory};
|
|
@@ -1,14 +1,70 @@
|
|
|
1
|
-
import { Store } from './Store.mjs';
|
|
1
|
+
import { TextVersionTarget, Store, StoreGetResult, VersionValue, StoreSetResult } from './Store.mjs';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @description 文本 store 配置.
|
|
5
|
+
*/
|
|
6
|
+
interface TextStoreOptions extends Omit<TextVersionTarget, 'type'> {
|
|
5
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* @description 文本 store 默认配置.
|
|
10
|
+
*/
|
|
6
11
|
declare const DEFAULT_TEXT_STORE_OPTIONS: TextStoreOptions;
|
|
12
|
+
/**
|
|
13
|
+
* @description 文本版本 store.
|
|
14
|
+
*/
|
|
7
15
|
declare class TextStore implements Store {
|
|
8
|
-
|
|
9
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @description 当前 store 绑定的目标.
|
|
18
|
+
*/
|
|
19
|
+
target: TextVersionTarget;
|
|
20
|
+
/**
|
|
21
|
+
* @description 初始化文本 store.
|
|
22
|
+
* @param options store 配置.
|
|
23
|
+
*/
|
|
10
24
|
constructor(options: TextStoreOptions);
|
|
11
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @description 读取文本文件中的版本值.
|
|
27
|
+
*/
|
|
28
|
+
get(): Promise<StoreGetResult>;
|
|
29
|
+
/**
|
|
30
|
+
* @description 写入文本文件中的版本值.
|
|
31
|
+
* @param value 版本值.
|
|
32
|
+
*/
|
|
33
|
+
set(value: VersionValue): Promise<StoreSetResult>;
|
|
34
|
+
/**
|
|
35
|
+
* @description 更新文本文件中的版本值.
|
|
36
|
+
* @param value 版本值.
|
|
37
|
+
*/
|
|
38
|
+
update(value: VersionValue): Promise<StoreSetResult>;
|
|
39
|
+
/**
|
|
40
|
+
* @description 从文本内容中提取版本值.
|
|
41
|
+
* @param text 文件内容.
|
|
42
|
+
*/
|
|
43
|
+
protected resolveValueFromText(text: string): VersionValue | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* @description 生成下一份文本内容.
|
|
46
|
+
* @param value 版本值.
|
|
47
|
+
*/
|
|
48
|
+
protected createNextText(value: VersionValue): Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* @description 写入文本内容.
|
|
51
|
+
* @param text 文件内容.
|
|
52
|
+
*/
|
|
53
|
+
protected writeText(text: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* @description 构建替换正则.
|
|
56
|
+
*/
|
|
57
|
+
protected createPattern(): RegExp;
|
|
58
|
+
/**
|
|
59
|
+
* @description 创建读取失败结果.
|
|
60
|
+
* @param message 错误消息.
|
|
61
|
+
*/
|
|
62
|
+
protected createGetError(message: string): StoreGetResult;
|
|
63
|
+
/**
|
|
64
|
+
* @description 提取异常消息.
|
|
65
|
+
* @param error 异常对象.
|
|
66
|
+
*/
|
|
67
|
+
protected getErrorMessage(error: unknown): string;
|
|
12
68
|
}
|
|
13
69
|
|
|
14
70
|
export { DEFAULT_TEXT_STORE_OPTIONS, TextStore, type TextStoreOptions };
|
|
@@ -1,14 +1,70 @@
|
|
|
1
|
-
import { Store } from './Store.js';
|
|
1
|
+
import { TextVersionTarget, Store, StoreGetResult, VersionValue, StoreSetResult } from './Store.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @description 文本 store 配置.
|
|
5
|
+
*/
|
|
6
|
+
interface TextStoreOptions extends Omit<TextVersionTarget, 'type'> {
|
|
5
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* @description 文本 store 默认配置.
|
|
10
|
+
*/
|
|
6
11
|
declare const DEFAULT_TEXT_STORE_OPTIONS: TextStoreOptions;
|
|
12
|
+
/**
|
|
13
|
+
* @description 文本版本 store.
|
|
14
|
+
*/
|
|
7
15
|
declare class TextStore implements Store {
|
|
8
|
-
|
|
9
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @description 当前 store 绑定的目标.
|
|
18
|
+
*/
|
|
19
|
+
target: TextVersionTarget;
|
|
20
|
+
/**
|
|
21
|
+
* @description 初始化文本 store.
|
|
22
|
+
* @param options store 配置.
|
|
23
|
+
*/
|
|
10
24
|
constructor(options: TextStoreOptions);
|
|
11
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @description 读取文本文件中的版本值.
|
|
27
|
+
*/
|
|
28
|
+
get(): Promise<StoreGetResult>;
|
|
29
|
+
/**
|
|
30
|
+
* @description 写入文本文件中的版本值.
|
|
31
|
+
* @param value 版本值.
|
|
32
|
+
*/
|
|
33
|
+
set(value: VersionValue): Promise<StoreSetResult>;
|
|
34
|
+
/**
|
|
35
|
+
* @description 更新文本文件中的版本值.
|
|
36
|
+
* @param value 版本值.
|
|
37
|
+
*/
|
|
38
|
+
update(value: VersionValue): Promise<StoreSetResult>;
|
|
39
|
+
/**
|
|
40
|
+
* @description 从文本内容中提取版本值.
|
|
41
|
+
* @param text 文件内容.
|
|
42
|
+
*/
|
|
43
|
+
protected resolveValueFromText(text: string): VersionValue | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* @description 生成下一份文本内容.
|
|
46
|
+
* @param value 版本值.
|
|
47
|
+
*/
|
|
48
|
+
protected createNextText(value: VersionValue): Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* @description 写入文本内容.
|
|
51
|
+
* @param text 文件内容.
|
|
52
|
+
*/
|
|
53
|
+
protected writeText(text: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* @description 构建替换正则.
|
|
56
|
+
*/
|
|
57
|
+
protected createPattern(): RegExp;
|
|
58
|
+
/**
|
|
59
|
+
* @description 创建读取失败结果.
|
|
60
|
+
* @param message 错误消息.
|
|
61
|
+
*/
|
|
62
|
+
protected createGetError(message: string): StoreGetResult;
|
|
63
|
+
/**
|
|
64
|
+
* @description 提取异常消息.
|
|
65
|
+
* @param error 异常对象.
|
|
66
|
+
*/
|
|
67
|
+
protected getErrorMessage(error: unknown): string;
|
|
12
68
|
}
|
|
13
69
|
|
|
14
70
|
export { DEFAULT_TEXT_STORE_OPTIONS, TextStore, type TextStoreOptions };
|