Keep local template/style for conflicted Vue pages; take main script/API/shared logic.
73 lines
2.1 KiB
JavaScript
73 lines
2.1 KiB
JavaScript
import { execFileSync } from 'node:child_process';
|
|
import { writeFileSync } from 'node:fs';
|
|
|
|
function gitShow(stage, file) {
|
|
return execFileSync('git', ['show', `${stage}:${file}`], {
|
|
encoding: 'utf8',
|
|
maxBuffer: 32 * 1024 * 1024,
|
|
});
|
|
}
|
|
|
|
/** Extract top-level SFC blocks; nested <template> inside root template is kept intact. */
|
|
function extractSfc(src) {
|
|
const scripts = [];
|
|
const styles = [];
|
|
let template = null;
|
|
|
|
const scriptRe = /<script\b[^>]*>[\s\S]*?<\/script>/gi;
|
|
let m;
|
|
while ((m = scriptRe.exec(src))) scripts.push(m[0]);
|
|
|
|
const styleRe = /<style\b[^>]*>[\s\S]*?<\/style>/gi;
|
|
while ((m = styleRe.exec(src))) styles.push(m[0]);
|
|
|
|
const start = src.search(/<template\b/i);
|
|
if (start >= 0) {
|
|
const end = src.toLowerCase().lastIndexOf('</template>');
|
|
if (end > start) {
|
|
template = src.slice(start, end + '</template>'.length);
|
|
}
|
|
}
|
|
|
|
return { scripts, template, styles };
|
|
}
|
|
|
|
/** Script/logic from main (theirs=:3), template+style from local/dev (ours=:2). */
|
|
function hybrid(oursSrc, theirsSrc) {
|
|
const ours = extractSfc(oursSrc);
|
|
const theirs = extractSfc(theirsSrc);
|
|
const scripts = theirs.scripts.length ? theirs.scripts : ours.scripts;
|
|
const template = ours.template || theirs.template;
|
|
const styles = ours.styles.length ? ours.styles : theirs.styles;
|
|
return [...scripts, template, ...styles].filter(Boolean).join('\n\n') + '\n';
|
|
}
|
|
|
|
const files = process.argv.slice(2);
|
|
if (!files.length) {
|
|
console.error('Usage: node _hybrid-vue-merge.mjs <file>...');
|
|
process.exit(1);
|
|
}
|
|
|
|
for (const file of files) {
|
|
const ours = gitShow(':2', file);
|
|
const theirs = gitShow(':3', file);
|
|
const merged = hybrid(ours, theirs);
|
|
const check = extractSfc(merged);
|
|
if (!check.scripts.length || !check.template) {
|
|
console.error('invalid hybrid result for', file, {
|
|
scripts: check.scripts.length,
|
|
hasTemplate: Boolean(check.template),
|
|
});
|
|
process.exit(2);
|
|
}
|
|
writeFileSync(file, merged, 'utf8');
|
|
execFileSync('git', ['add', file]);
|
|
console.log(
|
|
'hybrid-resolved',
|
|
file,
|
|
`scripts=${check.scripts.length}`,
|
|
`styles=${check.styles.length}`,
|
|
`tplLen=${check.template.length}`,
|
|
);
|
|
}
|