@gadmin2n/cli 0.0.91 → 0.0.92
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/actions/index.d.ts +1 -1
- package/actions/index.js +1 -1
- package/actions/self-update.action.d.ts +5 -0
- package/actions/self-update.action.js +93 -0
- package/actions/update.action.d.ts +2 -0
- package/actions/update.action.js +945 -6
- package/commands/command.loader.js +2 -2
- package/commands/{template.command.d.ts → self-update.command.d.ts} +1 -1
- package/commands/self-update.command.js +35 -0
- package/commands/update.command.js +25 -8
- package/lib/install-manager.d.ts +48 -0
- package/lib/install-manager.js +125 -0
- package/lib/template-merge/config-loader.d.ts +15 -0
- package/lib/template-merge/config-loader.js +38 -0
- package/lib/template-merge/env-merger.d.ts +5 -0
- package/lib/template-merge/env-merger.js +57 -0
- package/lib/template-merge/glob.d.ts +5 -0
- package/lib/template-merge/glob.js +78 -0
- package/lib/template-merge/index.d.ts +8 -0
- package/lib/template-merge/index.js +24 -0
- package/lib/template-merge/json-merger.d.ts +5 -0
- package/lib/template-merge/json-merger.js +252 -0
- package/lib/template-merge/merger.d.ts +30 -0
- package/lib/template-merge/merger.js +2 -0
- package/lib/template-merge/prisma-merger.d.ts +5 -0
- package/lib/template-merge/prisma-merger.js +112 -0
- package/lib/template-merge/registry.d.ts +25 -0
- package/lib/template-merge/registry.js +42 -0
- package/lib/template-merge/ts-module-merger.d.ts +16 -0
- package/lib/template-merge/ts-module-merger.js +193 -0
- package/lib/version-check.d.ts +39 -0
- package/lib/version-check.js +157 -0
- package/package.json +2 -2
- package/actions/template.action.d.ts +0 -7
- package/actions/template.action.js +0 -570
- package/commands/template.command.js +0 -42
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonMerger = void 0;
|
|
4
|
+
const PRESERVE_INSTANCE_KEYS = new Set([
|
|
5
|
+
'name',
|
|
6
|
+
'version',
|
|
7
|
+
'description',
|
|
8
|
+
'private',
|
|
9
|
+
'author',
|
|
10
|
+
'license',
|
|
11
|
+
'homepage',
|
|
12
|
+
'repository',
|
|
13
|
+
'bugs',
|
|
14
|
+
]);
|
|
15
|
+
const DEEP_MERGE_KEYS = new Set([
|
|
16
|
+
'dependencies',
|
|
17
|
+
'devDependencies',
|
|
18
|
+
'peerDependencies',
|
|
19
|
+
'optionalDependencies',
|
|
20
|
+
'scripts',
|
|
21
|
+
'engines',
|
|
22
|
+
'volta',
|
|
23
|
+
]);
|
|
24
|
+
const POLICY_OBJECT_KEYS = new Set([
|
|
25
|
+
'dependencies',
|
|
26
|
+
'devDependencies',
|
|
27
|
+
'peerDependencies',
|
|
28
|
+
'optionalDependencies',
|
|
29
|
+
'scripts',
|
|
30
|
+
]);
|
|
31
|
+
function isPlainObject(v) {
|
|
32
|
+
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
33
|
+
}
|
|
34
|
+
function detectIndent(content) {
|
|
35
|
+
const lines = content.split('\n');
|
|
36
|
+
for (const line of lines) {
|
|
37
|
+
if (line.length === 0)
|
|
38
|
+
continue;
|
|
39
|
+
const m = line.match(/^([ \t]+)\S/);
|
|
40
|
+
if (!m)
|
|
41
|
+
continue;
|
|
42
|
+
const lead = m[1];
|
|
43
|
+
if (lead[0] === '\t')
|
|
44
|
+
return '\t';
|
|
45
|
+
if (lead.length >= 4)
|
|
46
|
+
return 4;
|
|
47
|
+
return 2;
|
|
48
|
+
}
|
|
49
|
+
return 2;
|
|
50
|
+
}
|
|
51
|
+
function unionArray(instArr, tmplArr) {
|
|
52
|
+
const seen = new Set();
|
|
53
|
+
const out = [];
|
|
54
|
+
for (const item of instArr) {
|
|
55
|
+
const key = JSON.stringify(item);
|
|
56
|
+
if (!seen.has(key)) {
|
|
57
|
+
seen.add(key);
|
|
58
|
+
out.push(item);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const item of tmplArr) {
|
|
62
|
+
const key = JSON.stringify(item);
|
|
63
|
+
if (!seen.has(key)) {
|
|
64
|
+
seen.add(key);
|
|
65
|
+
out.push(item);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
function deepMergeKeepInstance(inst, tmpl) {
|
|
71
|
+
if (isPlainObject(inst) && isPlainObject(tmpl)) {
|
|
72
|
+
const out = {};
|
|
73
|
+
for (const k of Object.keys(inst)) {
|
|
74
|
+
if (k in tmpl) {
|
|
75
|
+
out[k] = deepMergeKeepInstance(inst[k], tmpl[k]);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
out[k] = inst[k];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (const k of Object.keys(tmpl)) {
|
|
82
|
+
if (!(k in inst)) {
|
|
83
|
+
out[k] = tmpl[k];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
if (Array.isArray(inst) && Array.isArray(tmpl)) {
|
|
89
|
+
return unionArray(inst, tmpl);
|
|
90
|
+
}
|
|
91
|
+
return inst;
|
|
92
|
+
}
|
|
93
|
+
function formatVal(v) {
|
|
94
|
+
if (typeof v === 'string')
|
|
95
|
+
return v;
|
|
96
|
+
return JSON.stringify(v);
|
|
97
|
+
}
|
|
98
|
+
class JsonMerger {
|
|
99
|
+
constructor() {
|
|
100
|
+
this.name = 'json';
|
|
101
|
+
}
|
|
102
|
+
merge(ctx) {
|
|
103
|
+
let instData;
|
|
104
|
+
let tmplData;
|
|
105
|
+
try {
|
|
106
|
+
instData = JSON.parse(ctx.instanceContent);
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
110
|
+
return { ok: false, reason: `parse failure: instance JSON: ${msg}` };
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
tmplData = JSON.parse(ctx.templateContent);
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
117
|
+
return { ok: false, reason: `parse failure: template JSON: ${msg}` };
|
|
118
|
+
}
|
|
119
|
+
const indent = detectIndent(ctx.instanceContent);
|
|
120
|
+
const hasTrailingNewline = ctx.instanceContent.endsWith('\n');
|
|
121
|
+
const policies = ctx.policies || {};
|
|
122
|
+
const notes = [];
|
|
123
|
+
if (!isPlainObject(instData) || !isPlainObject(tmplData)) {
|
|
124
|
+
const merged = JSON.stringify(instData, null, indent) +
|
|
125
|
+
(hasTrailingNewline ? '\n' : '');
|
|
126
|
+
return { ok: true, merged, notes: ['no changes'] };
|
|
127
|
+
}
|
|
128
|
+
const result = {};
|
|
129
|
+
const orderedKeys = [];
|
|
130
|
+
for (const k of Object.keys(instData))
|
|
131
|
+
orderedKeys.push(k);
|
|
132
|
+
for (const k of Object.keys(tmplData)) {
|
|
133
|
+
if (!(k in instData))
|
|
134
|
+
orderedKeys.push(k);
|
|
135
|
+
}
|
|
136
|
+
for (const k of orderedKeys) {
|
|
137
|
+
const inInst = k in instData;
|
|
138
|
+
const inTmpl = k in tmplData;
|
|
139
|
+
const instVal = instData[k];
|
|
140
|
+
const tmplVal = tmplData[k];
|
|
141
|
+
const policyKey = `json.${k}`;
|
|
142
|
+
const policy = policies[policyKey];
|
|
143
|
+
if (POLICY_OBJECT_KEYS.has(k) && policy) {
|
|
144
|
+
if (policy === 'overwrite') {
|
|
145
|
+
if (inTmpl) {
|
|
146
|
+
result[k] = tmplVal;
|
|
147
|
+
notes.push(`${k}: overwritten with template (policy)`);
|
|
148
|
+
}
|
|
149
|
+
else if (inInst) {
|
|
150
|
+
result[k] = instVal;
|
|
151
|
+
}
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (policy === 'prefer-template' &&
|
|
155
|
+
inInst &&
|
|
156
|
+
inTmpl &&
|
|
157
|
+
isPlainObject(instVal) &&
|
|
158
|
+
isPlainObject(tmplVal)) {
|
|
159
|
+
const obj = {};
|
|
160
|
+
const adds = [];
|
|
161
|
+
const changes = [];
|
|
162
|
+
for (const ik of Object.keys(instVal)) {
|
|
163
|
+
if (ik in tmplVal) {
|
|
164
|
+
obj[ik] = tmplVal[ik];
|
|
165
|
+
if (JSON.stringify(instVal[ik]) !== JSON.stringify(tmplVal[ik])) {
|
|
166
|
+
changes.push(`${ik} (instance ${formatVal(instVal[ik])} -> template ${formatVal(tmplVal[ik])})`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
obj[ik] = instVal[ik];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
for (const tk of Object.keys(tmplVal)) {
|
|
174
|
+
if (!(tk in instVal)) {
|
|
175
|
+
obj[tk] = tmplVal[tk];
|
|
176
|
+
adds.push(tk);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
result[k] = obj;
|
|
180
|
+
if (adds.length > 0) {
|
|
181
|
+
notes.push(`${k}: added ${adds.length} from template (${adds.join(', ')})`);
|
|
182
|
+
}
|
|
183
|
+
for (const c of changes) {
|
|
184
|
+
notes.push(`${k}: preferred template version of ${c}`);
|
|
185
|
+
}
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
// 'preserve-instance' or unmatched policy → fall through
|
|
189
|
+
}
|
|
190
|
+
if (inInst && !inTmpl) {
|
|
191
|
+
result[k] = instVal;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (!inInst && inTmpl) {
|
|
195
|
+
result[k] = tmplVal;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (PRESERVE_INSTANCE_KEYS.has(k)) {
|
|
199
|
+
result[k] = instVal;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (DEEP_MERGE_KEYS.has(k) &&
|
|
203
|
+
isPlainObject(instVal) &&
|
|
204
|
+
isPlainObject(tmplVal)) {
|
|
205
|
+
const adds = [];
|
|
206
|
+
const kept = [];
|
|
207
|
+
const obj = {};
|
|
208
|
+
for (const ik of Object.keys(instVal)) {
|
|
209
|
+
if (ik in tmplVal) {
|
|
210
|
+
obj[ik] = instVal[ik];
|
|
211
|
+
if (JSON.stringify(instVal[ik]) !== JSON.stringify(tmplVal[ik])) {
|
|
212
|
+
kept.push(`${ik} (instance ${formatVal(instVal[ik])} vs template ${formatVal(tmplVal[ik])})`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
obj[ik] = instVal[ik];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
for (const tk of Object.keys(tmplVal)) {
|
|
220
|
+
if (!(tk in instVal)) {
|
|
221
|
+
obj[tk] = tmplVal[tk];
|
|
222
|
+
adds.push(tk);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
result[k] = obj;
|
|
226
|
+
if (adds.length > 0) {
|
|
227
|
+
notes.push(`${k}: added ${adds.length} from template (${adds.join(', ')})`);
|
|
228
|
+
}
|
|
229
|
+
for (const keep of kept) {
|
|
230
|
+
notes.push(`${k}: kept instance version of ${keep}`);
|
|
231
|
+
}
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (isPlainObject(instVal) && isPlainObject(tmplVal)) {
|
|
235
|
+
result[k] = deepMergeKeepInstance(instVal, tmplVal);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (Array.isArray(instVal) && Array.isArray(tmplVal)) {
|
|
239
|
+
result[k] = unionArray(instVal, tmplVal);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
result[k] = instVal;
|
|
243
|
+
}
|
|
244
|
+
if (notes.length === 0)
|
|
245
|
+
notes.push('no changes');
|
|
246
|
+
let serialized = JSON.stringify(result, null, indent);
|
|
247
|
+
if (hasTrailingNewline)
|
|
248
|
+
serialized += '\n';
|
|
249
|
+
return { ok: true, merged: serialized, notes };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
exports.JsonMerger = JsonMerger;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface MergeContext {
|
|
2
|
+
/** Relative path inside instance, e.g. 'web/package.json' */
|
|
3
|
+
filePath: string;
|
|
4
|
+
/** Absolute path to instance file */
|
|
5
|
+
instancePath: string;
|
|
6
|
+
/** Absolute path to template file */
|
|
7
|
+
templatePath: string;
|
|
8
|
+
instanceContent: string;
|
|
9
|
+
templateContent: string;
|
|
10
|
+
/** Optional per-merger policy values, dot-namespaced keys e.g. 'json.scripts': 'preserve-instance' */
|
|
11
|
+
policies?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
export declare type MergeOk = {
|
|
14
|
+
ok: true;
|
|
15
|
+
/** Final merged content to write to instance file */
|
|
16
|
+
merged: string;
|
|
17
|
+
/** Human-readable lines summarising what the merger did */
|
|
18
|
+
notes: string[];
|
|
19
|
+
};
|
|
20
|
+
export declare type MergeFail = {
|
|
21
|
+
ok: false;
|
|
22
|
+
/** Why this merger could not produce a result; caller should fall back */
|
|
23
|
+
reason: string;
|
|
24
|
+
};
|
|
25
|
+
export declare type MergeResult = MergeOk | MergeFail;
|
|
26
|
+
export interface Merger {
|
|
27
|
+
/** Unique id, e.g. 'json' | 'ts-module' | 'prisma' | 'env' | 'skip' */
|
|
28
|
+
readonly name: string;
|
|
29
|
+
merge(ctx: MergeContext): Promise<MergeResult> | MergeResult;
|
|
30
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrismaMerger = void 0;
|
|
4
|
+
const BLOCK_HEADER_RE = /^(model|enum|generator|datasource|type|view)\s+(\w+)\s*\{/;
|
|
5
|
+
function parseBlocks(content) {
|
|
6
|
+
const lines = content.split('\n');
|
|
7
|
+
const blocks = [];
|
|
8
|
+
let prefixLines = [];
|
|
9
|
+
let i = 0;
|
|
10
|
+
while (i < lines.length) {
|
|
11
|
+
const line = lines[i];
|
|
12
|
+
const m = line.match(BLOCK_HEADER_RE);
|
|
13
|
+
if (m) {
|
|
14
|
+
const kind = m[1];
|
|
15
|
+
const name = m[2];
|
|
16
|
+
let depth = 0;
|
|
17
|
+
const blockLines = [];
|
|
18
|
+
let endIdx = -1;
|
|
19
|
+
for (let j = i; j < lines.length; j++) {
|
|
20
|
+
const l = lines[j];
|
|
21
|
+
for (const ch of l) {
|
|
22
|
+
if (ch === '{')
|
|
23
|
+
depth++;
|
|
24
|
+
else if (ch === '}') {
|
|
25
|
+
depth--;
|
|
26
|
+
if (depth < 0) {
|
|
27
|
+
return { ok: false, error: 'unbalanced braces' };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
blockLines.push(l);
|
|
32
|
+
if (depth === 0) {
|
|
33
|
+
endIdx = j;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (endIdx === -1) {
|
|
38
|
+
return { ok: false, error: 'unbalanced braces' };
|
|
39
|
+
}
|
|
40
|
+
blocks.push({
|
|
41
|
+
kind,
|
|
42
|
+
name,
|
|
43
|
+
prefix: prefixLines.join('\n'),
|
|
44
|
+
content: blockLines.join('\n'),
|
|
45
|
+
});
|
|
46
|
+
prefixLines = [];
|
|
47
|
+
i = endIdx + 1;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
prefixLines.push(line);
|
|
51
|
+
i++;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return { ok: true, blocks, trailing: prefixLines.join('\n') };
|
|
55
|
+
}
|
|
56
|
+
class PrismaMerger {
|
|
57
|
+
constructor() {
|
|
58
|
+
this.name = 'prisma';
|
|
59
|
+
}
|
|
60
|
+
merge(ctx) {
|
|
61
|
+
const instParsed = parseBlocks(ctx.instanceContent);
|
|
62
|
+
if (!instParsed.ok) {
|
|
63
|
+
return { ok: false, reason: instParsed.error };
|
|
64
|
+
}
|
|
65
|
+
const tmplParsed = parseBlocks(ctx.templateContent);
|
|
66
|
+
if (!tmplParsed.ok) {
|
|
67
|
+
return { ok: false, reason: tmplParsed.error };
|
|
68
|
+
}
|
|
69
|
+
const instKeys = new Set();
|
|
70
|
+
const instKinds = new Set();
|
|
71
|
+
for (const b of instParsed.blocks) {
|
|
72
|
+
instKeys.add(`${b.kind}:${b.name}`);
|
|
73
|
+
instKinds.add(b.kind);
|
|
74
|
+
}
|
|
75
|
+
const notes = [];
|
|
76
|
+
const toAppend = [];
|
|
77
|
+
for (const tb of tmplParsed.blocks) {
|
|
78
|
+
if (tb.kind === 'generator' || tb.kind === 'datasource') {
|
|
79
|
+
if (instKinds.has(tb.kind)) {
|
|
80
|
+
notes.push(`kept instance ${tb.kind} ${tb.name}`);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
toAppend.push(tb);
|
|
84
|
+
notes.push(`added ${tb.kind} ${tb.name} from template`);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const key = `${tb.kind}:${tb.name}`;
|
|
88
|
+
if (instKeys.has(key)) {
|
|
89
|
+
notes.push(`kept instance version of ${tb.kind} ${tb.name}`);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
toAppend.push(tb);
|
|
93
|
+
notes.push(`added ${tb.kind} ${tb.name} from template`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let merged = ctx.instanceContent;
|
|
97
|
+
if (toAppend.length > 0) {
|
|
98
|
+
if (!merged.endsWith('\n'))
|
|
99
|
+
merged += '\n';
|
|
100
|
+
const parts = [];
|
|
101
|
+
for (const b of toAppend) {
|
|
102
|
+
const piece = (b.prefix ? b.prefix + '\n' : '') + b.content;
|
|
103
|
+
parts.push(piece);
|
|
104
|
+
}
|
|
105
|
+
merged += parts.join('\n') + '\n';
|
|
106
|
+
}
|
|
107
|
+
if (notes.length === 0)
|
|
108
|
+
notes.push('no changes');
|
|
109
|
+
return { ok: true, merged, notes };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.PrismaMerger = PrismaMerger;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Merger } from './merger';
|
|
2
|
+
export interface MergerRule {
|
|
3
|
+
/** Glob pattern, e.g. '**\/*.module.ts', 'package.json', 'config/prisma/*.prisma' */
|
|
4
|
+
pattern: string;
|
|
5
|
+
/** Refers to a Merger registered by this name in the registry */
|
|
6
|
+
mergerName: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class MergerRegistry {
|
|
9
|
+
private mergers;
|
|
10
|
+
private rules;
|
|
11
|
+
/** Register a merger keyed by merger.name. Replaces any existing entry. */
|
|
12
|
+
register(merger: Merger): void;
|
|
13
|
+
/** Append a rule. Order matters: first match wins in resolve(). */
|
|
14
|
+
addRule(rule: MergerRule): void;
|
|
15
|
+
/** Clear all rules (mergers stay registered). Used when user config replaces defaults. */
|
|
16
|
+
resetRules(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Resolve a Merger for the given file path by matching against rules in
|
|
19
|
+
* insertion order. Returns null if no rule matches OR if the matched rule
|
|
20
|
+
* names a merger that hasn't been registered.
|
|
21
|
+
*/
|
|
22
|
+
resolve(filePath: string): Merger | null;
|
|
23
|
+
/** Look up a merger by name. */
|
|
24
|
+
getMerger(name: string): Merger | null;
|
|
25
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MergerRegistry = void 0;
|
|
4
|
+
const glob_1 = require("./glob");
|
|
5
|
+
class MergerRegistry {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.mergers = new Map();
|
|
8
|
+
this.rules = [];
|
|
9
|
+
}
|
|
10
|
+
/** Register a merger keyed by merger.name. Replaces any existing entry. */
|
|
11
|
+
register(merger) {
|
|
12
|
+
this.mergers.set(merger.name, merger);
|
|
13
|
+
}
|
|
14
|
+
/** Append a rule. Order matters: first match wins in resolve(). */
|
|
15
|
+
addRule(rule) {
|
|
16
|
+
this.rules.push(rule);
|
|
17
|
+
}
|
|
18
|
+
/** Clear all rules (mergers stay registered). Used when user config replaces defaults. */
|
|
19
|
+
resetRules() {
|
|
20
|
+
this.rules = [];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolve a Merger for the given file path by matching against rules in
|
|
24
|
+
* insertion order. Returns null if no rule matches OR if the matched rule
|
|
25
|
+
* names a merger that hasn't been registered.
|
|
26
|
+
*/
|
|
27
|
+
resolve(filePath) {
|
|
28
|
+
var _a;
|
|
29
|
+
for (const rule of this.rules) {
|
|
30
|
+
if ((0, glob_1.globMatch)(filePath, rule.pattern)) {
|
|
31
|
+
return (_a = this.mergers.get(rule.mergerName)) !== null && _a !== void 0 ? _a : null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/** Look up a merger by name. */
|
|
37
|
+
getMerger(name) {
|
|
38
|
+
var _a;
|
|
39
|
+
return (_a = this.mergers.get(name)) !== null && _a !== void 0 ? _a : null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.MergerRegistry = MergerRegistry;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MergeContext, MergeResult, Merger } from './merger';
|
|
2
|
+
/**
|
|
3
|
+
* Smart merger for NestJS-style `*.module.ts` files.
|
|
4
|
+
*
|
|
5
|
+
* Strategy: extract symbols from template's `@Module(...)` metadata arrays
|
|
6
|
+
* (imports/providers/controllers/exports) and missing top-level `import { ... } from '...'`
|
|
7
|
+
* lines, then insert each missing item into the instance file. Existing items are
|
|
8
|
+
* preserved. Method bodies and other class members are not touched.
|
|
9
|
+
*
|
|
10
|
+
* Falls back (returns `{ok: false}`) when the instance has no `@Module` decorator
|
|
11
|
+
* or when AST parsing/insertion throws.
|
|
12
|
+
*/
|
|
13
|
+
export declare class TsModuleMerger implements Merger {
|
|
14
|
+
readonly name = "ts-module";
|
|
15
|
+
merge(ctx: MergeContext): MergeResult;
|
|
16
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TsModuleMerger = void 0;
|
|
4
|
+
const ts = require("typescript");
|
|
5
|
+
const schematics_1 = require("@gadmin2n/schematics");
|
|
6
|
+
const METADATA_KEYS = ['imports', 'providers', 'controllers', 'exports'];
|
|
7
|
+
/**
|
|
8
|
+
* Smart merger for NestJS-style `*.module.ts` files.
|
|
9
|
+
*
|
|
10
|
+
* Strategy: extract symbols from template's `@Module(...)` metadata arrays
|
|
11
|
+
* (imports/providers/controllers/exports) and missing top-level `import { ... } from '...'`
|
|
12
|
+
* lines, then insert each missing item into the instance file. Existing items are
|
|
13
|
+
* preserved. Method bodies and other class members are not touched.
|
|
14
|
+
*
|
|
15
|
+
* Falls back (returns `{ok: false}`) when the instance has no `@Module` decorator
|
|
16
|
+
* or when AST parsing/insertion throws.
|
|
17
|
+
*/
|
|
18
|
+
class TsModuleMerger {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.name = 'ts-module';
|
|
21
|
+
}
|
|
22
|
+
merge(ctx) {
|
|
23
|
+
var _a, _b;
|
|
24
|
+
const policies = (_a = ctx.policies) !== null && _a !== void 0 ? _a : {};
|
|
25
|
+
const notes = [];
|
|
26
|
+
try {
|
|
27
|
+
const templateSource = ts.createSourceFile('template.ts', ctx.templateContent, ts.ScriptTarget.ES2017, true);
|
|
28
|
+
const instanceSourceInitial = ts.createSourceFile('instance.ts', ctx.instanceContent, ts.ScriptTarget.ES2017, true);
|
|
29
|
+
if (!findModuleDecoratorArg(instanceSourceInitial)) {
|
|
30
|
+
return { ok: false, reason: 'no @Module decorator in instance' };
|
|
31
|
+
}
|
|
32
|
+
const templateModuleArg = findModuleDecoratorArg(templateSource);
|
|
33
|
+
let content = ctx.instanceContent;
|
|
34
|
+
// 1. Per-slot metadata merge (imports, providers, controllers, exports).
|
|
35
|
+
if (templateModuleArg) {
|
|
36
|
+
for (const key of METADATA_KEYS) {
|
|
37
|
+
const policy = (_b = policies[`ts-module.${key}`]) !== null && _b !== void 0 ? _b : 'union';
|
|
38
|
+
if (policy === 'skip') {
|
|
39
|
+
notes.push(`${key}: skip (policy)`);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const templateElements = extractMetadataArrayElements(templateModuleArg, key, templateSource);
|
|
43
|
+
if (templateElements.length === 0)
|
|
44
|
+
continue;
|
|
45
|
+
for (const element of templateElements) {
|
|
46
|
+
// Re-parse the current (possibly mutated) instance to get fresh
|
|
47
|
+
// existing-element lookup. This mirrors the pattern used in
|
|
48
|
+
// schematics' service.factory: each MetadataManager.insert call
|
|
49
|
+
// takes the latest content.
|
|
50
|
+
const reparsed = ts.createSourceFile('instance.ts', content, ts.ScriptTarget.ES2017, true);
|
|
51
|
+
const arg = findModuleDecoratorArg(reparsed);
|
|
52
|
+
if (!arg) {
|
|
53
|
+
return { ok: false, reason: 'lost @Module decorator during merge' };
|
|
54
|
+
}
|
|
55
|
+
const existing = extractMetadataArrayElements(arg, key, reparsed);
|
|
56
|
+
if (existing.includes(element)) {
|
|
57
|
+
notes.push(`${key}: kept ${element}`);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const next = new schematics_1.MetadataManager(content).insert(key, element);
|
|
61
|
+
if (next === content) {
|
|
62
|
+
// MetadataManager returned content unchanged (e.g. symbol already
|
|
63
|
+
// exists by its own equality check). Treat as kept.
|
|
64
|
+
notes.push(`${key}: kept ${element}`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
content = next;
|
|
68
|
+
notes.push(`${key}: added ${element}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// 2. Top-level import lines merge.
|
|
74
|
+
const templateImports = collectTopImports(templateSource);
|
|
75
|
+
const instanceForImports = ts.createSourceFile('instance.ts', content, ts.ScriptTarget.ES2017, true);
|
|
76
|
+
const instanceImports = collectTopImports(instanceForImports);
|
|
77
|
+
const existingSymbols = new Set();
|
|
78
|
+
for (const imp of instanceImports) {
|
|
79
|
+
for (const s of imp.symbols)
|
|
80
|
+
existingSymbols.add(s);
|
|
81
|
+
}
|
|
82
|
+
const linesToAdd = [];
|
|
83
|
+
for (const imp of templateImports) {
|
|
84
|
+
if (imp.symbols.length === 0)
|
|
85
|
+
continue;
|
|
86
|
+
const allExist = imp.symbols.every((s) => existingSymbols.has(s));
|
|
87
|
+
if (allExist)
|
|
88
|
+
continue;
|
|
89
|
+
linesToAdd.push(imp.line);
|
|
90
|
+
for (const s of imp.symbols)
|
|
91
|
+
existingSymbols.add(s);
|
|
92
|
+
notes.push(`import: added ${imp.symbols.join(', ')} from '${imp.from}'`);
|
|
93
|
+
}
|
|
94
|
+
if (linesToAdd.length > 0) {
|
|
95
|
+
let lastEnd = -1;
|
|
96
|
+
for (const stmt of instanceForImports.statements) {
|
|
97
|
+
if (ts.isImportDeclaration(stmt)) {
|
|
98
|
+
lastEnd = stmt.getEnd();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const insertion = '\n' + linesToAdd.join('\n');
|
|
102
|
+
if (lastEnd >= 0) {
|
|
103
|
+
content = content.slice(0, lastEnd) + insertion + content.slice(lastEnd);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// No existing imports — prepend.
|
|
107
|
+
content = linesToAdd.join('\n') + '\n' + content;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (content === ctx.instanceContent) {
|
|
111
|
+
return { ok: true, merged: content, notes: ['no changes'] };
|
|
112
|
+
}
|
|
113
|
+
return { ok: true, merged: content, notes };
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
117
|
+
return { ok: false, reason: `ts-module merge failed: ${message}` };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.TsModuleMerger = TsModuleMerger;
|
|
122
|
+
/**
|
|
123
|
+
* Walk the AST to find the first `@Module(...)` decorator's
|
|
124
|
+
* `ObjectLiteralExpression` argument. Returns `undefined` if not found.
|
|
125
|
+
*/
|
|
126
|
+
function findModuleDecoratorArg(source) {
|
|
127
|
+
let result;
|
|
128
|
+
const visit = (node) => {
|
|
129
|
+
if (result)
|
|
130
|
+
return;
|
|
131
|
+
if (ts.isDecorator(node) && ts.isCallExpression(node.expression)) {
|
|
132
|
+
const callee = node.expression.expression;
|
|
133
|
+
if (ts.isIdentifier(callee) && callee.text === 'Module') {
|
|
134
|
+
const arg = node.expression.arguments[0];
|
|
135
|
+
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
136
|
+
result = arg;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
ts.forEachChild(node, visit);
|
|
142
|
+
};
|
|
143
|
+
visit(source);
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Extract the source text of each element of a metadata array property
|
|
148
|
+
* (e.g. `imports: [A, B.forRoot({...})]`).
|
|
149
|
+
*/
|
|
150
|
+
function extractMetadataArrayElements(arg, key, source) {
|
|
151
|
+
for (const prop of arg.properties) {
|
|
152
|
+
if (!ts.isPropertyAssignment(prop))
|
|
153
|
+
continue;
|
|
154
|
+
let propName = '';
|
|
155
|
+
if (ts.isIdentifier(prop.name))
|
|
156
|
+
propName = prop.name.text;
|
|
157
|
+
else if (ts.isStringLiteral(prop.name))
|
|
158
|
+
propName = prop.name.text;
|
|
159
|
+
if (propName !== key)
|
|
160
|
+
continue;
|
|
161
|
+
if (!ts.isArrayLiteralExpression(prop.initializer))
|
|
162
|
+
continue;
|
|
163
|
+
return prop.initializer.elements.map((el) => el.getText(source));
|
|
164
|
+
}
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Collect top-level `import { Named, ... } from '...'` declarations from a
|
|
169
|
+
* source file. Default and namespace imports are intentionally ignored — the
|
|
170
|
+
* NestJS module template convention is named-imports only.
|
|
171
|
+
*/
|
|
172
|
+
function collectTopImports(source) {
|
|
173
|
+
const result = [];
|
|
174
|
+
for (const stmt of source.statements) {
|
|
175
|
+
if (!ts.isImportDeclaration(stmt))
|
|
176
|
+
continue;
|
|
177
|
+
if (!ts.isStringLiteral(stmt.moduleSpecifier))
|
|
178
|
+
continue;
|
|
179
|
+
const symbols = [];
|
|
180
|
+
const ic = stmt.importClause;
|
|
181
|
+
if (ic && ic.namedBindings && ts.isNamedImports(ic.namedBindings)) {
|
|
182
|
+
for (const el of ic.namedBindings.elements) {
|
|
183
|
+
symbols.push(el.name.text);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
result.push({
|
|
187
|
+
line: stmt.getText(source),
|
|
188
|
+
symbols,
|
|
189
|
+
from: stmt.moduleSpecifier.text,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|