create-flow-os 0.0.47-dev.1772047685 → 0.0.47-dev.1772051359
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/package.json +1 -1
- package/src/init/merge.ts +38 -18
package/package.json
CHANGED
package/src/init/merge.ts
CHANGED
|
@@ -136,16 +136,36 @@ export function mergeCodeTemplates(a: string, b: string): string {
|
|
|
136
136
|
return replaceObjectArg(base, extB, merged);
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
139
|
+
/** Pattern universali: aggiungi blocco solo se manca. Vale per tutti i file di tutti i package. */
|
|
140
|
+
type AddIfMissingPattern = {
|
|
141
|
+
/** Controlla se il contenuto ha già questo (anche su righe diverse, formattazione diversa) */
|
|
142
|
+
hasSignature: (content: string) => boolean;
|
|
143
|
+
/** Estrae il blocco completo dal template da aggiungere */
|
|
144
|
+
extractBlock: (content: string) => string | null;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const ADD_IF_MISSING_PATTERNS: AddIfMissingPattern[] = [
|
|
148
|
+
{
|
|
149
|
+
hasSignature: (c) => /\bexport\s+default\b/.test(c.replace(/\s+/g, " ")),
|
|
150
|
+
extractBlock: (c) => {
|
|
151
|
+
const m = c.match(/export\s+default\s+[\s\S]+?;\s*$/m) ?? c.match(/export\s+default\s+[\s\S]+?(?=\s*\n\s*\n|$)/s);
|
|
152
|
+
return m?.[0]?.trim() ?? null;
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
];
|
|
156
|
+
|
|
157
|
+
/** Applica i pattern add-if-missing: aggiunge blocchi dal template solo se mancano. Univoco per tutti i file. */
|
|
158
|
+
function applyAddIfMissing(userOrMerged: string, templateContent: string): string {
|
|
159
|
+
let result = userOrMerged;
|
|
160
|
+
for (const p of ADD_IF_MISSING_PATTERNS) {
|
|
161
|
+
const block = p.extractBlock(templateContent);
|
|
162
|
+
if (block && !p.hasSignature(result)) {
|
|
163
|
+
const trimmed = result.trimEnd();
|
|
164
|
+
const sep = trimmed.endsWith("\n") || !trimmed ? "" : "\n\n";
|
|
165
|
+
result = trimmed + sep + block;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return result;
|
|
149
169
|
}
|
|
150
170
|
|
|
151
171
|
/** Merge per file con pattern fn({...}) - config(), defineConfig(), ecc. Additivo: non sovrascrive. */
|
|
@@ -153,19 +173,17 @@ export function mergeCodeObject(userContent: string, templateContent: string, _p
|
|
|
153
173
|
const conflicts: Conflict[] = [];
|
|
154
174
|
const userExt = extractObjectArg(userContent);
|
|
155
175
|
const templateExt = extractObjectArg(templateContent);
|
|
156
|
-
if (!templateExt)
|
|
176
|
+
if (!templateExt) {
|
|
177
|
+
const base = userContent || templateContent;
|
|
178
|
+
return { merged: applyAddIfMissing(base, templateContent), conflicts: [], ext: null };
|
|
179
|
+
}
|
|
157
180
|
const userObj = userExt?.obj ?? {};
|
|
158
181
|
const templateObj = templateExt.obj;
|
|
159
182
|
const mergedObj = deepMergeAdditive(userObj, templateObj, "", conflicts);
|
|
160
183
|
const base = userContent || templateContent;
|
|
161
184
|
const extToReplace = userExt ?? templateExt;
|
|
162
185
|
let merged = replaceObjectArg(base, extToReplace, mergedObj);
|
|
163
|
-
|
|
164
|
-
if (templateExport && !userHasExportDefault(userContent) && !userHasExportDefault(merged)) {
|
|
165
|
-
const userTrimmed = merged.trimEnd();
|
|
166
|
-
const sep = userTrimmed.endsWith("\n") || !userTrimmed ? "" : "\n\n";
|
|
167
|
-
merged = userTrimmed + sep + templateExport;
|
|
168
|
-
}
|
|
186
|
+
merged = applyAddIfMissing(merged, templateContent);
|
|
169
187
|
return { merged, conflicts, ext: templateExt };
|
|
170
188
|
}
|
|
171
189
|
|
|
@@ -219,5 +237,7 @@ export async function mergeFile(
|
|
|
219
237
|
return merged;
|
|
220
238
|
}
|
|
221
239
|
|
|
222
|
-
|
|
240
|
+
// Altri file (html, css, md, ecc.): stesso sistema add-if-missing univoco
|
|
241
|
+
const base = userContent ?? templateContent;
|
|
242
|
+
return applyAddIfMissing(base, templateContent);
|
|
223
243
|
}
|