mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
chore(sandbox): sandbox cleanup (#797)
Co-authored-by: Christian Pillsbury <cpillsbury@mux.com>
This commit is contained in:
co-authored by
Christian Pillsbury
parent
f168256848
commit
6d044e3c0f
@@ -1,76 +1,32 @@
|
||||
import path from 'node:path';
|
||||
import chalk from 'chalk';
|
||||
import { createTwoFilesPatch } from 'diff';
|
||||
import { compareSync } from 'dir-compare';
|
||||
import fs from 'fs-extra';
|
||||
import prompts from 'prompts';
|
||||
|
||||
const SOURCE = './src';
|
||||
const TEMPLATES = './templates';
|
||||
// Some file always live in src
|
||||
const IGNORE = new Set(['index.html', 'main.tsx']);
|
||||
import { confirm, filePath, getChanges, printDiff, SRC, srcPath, TEMPLATES, templatesPath } from './shared.js';
|
||||
|
||||
function colorizePatch(patch: string): string {
|
||||
return patch
|
||||
.split('\n')
|
||||
.map((line) => {
|
||||
if (line.startsWith('---') || line.startsWith('+++')) return chalk.dim(line);
|
||||
if (line.startsWith('-')) return chalk.red(line);
|
||||
if (line.startsWith('+')) return chalk.green(line);
|
||||
if (line.startsWith('@@')) return chalk.cyan(line);
|
||||
return chalk.gray(line);
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
console.log(chalk.bold(`\nComparing ${SRC} → ${TEMPLATES}\n`));
|
||||
|
||||
function printDiff(oldPath: string, newPath: string, label: string): void {
|
||||
const oldContent = fs.existsSync(oldPath) ? fs.readFileSync(oldPath, 'utf8') : '';
|
||||
const newContent = fs.existsSync(newPath) ? fs.readFileSync(newPath, 'utf8') : '';
|
||||
const patch = createTwoFilesPatch(label, label, oldContent, newContent, undefined, undefined, { context: 3 });
|
||||
|
||||
// Strip the first two header lines (Index + ===) that createTwoFilesPatch adds
|
||||
const lines = patch.split('\n');
|
||||
const body = lines.slice(2).join('\n');
|
||||
|
||||
console.log(colorizePatch(body));
|
||||
}
|
||||
|
||||
console.log(chalk.bold(`\nComparing ${SOURCE} → ${TEMPLATES}\n`));
|
||||
|
||||
const res = compareSync(SOURCE, TEMPLATES, { compareContent: true });
|
||||
|
||||
const changes = (res.diffSet ?? []).filter(
|
||||
(d) =>
|
||||
d.state !== 'equal' &&
|
||||
d.type1 !== 'directory' &&
|
||||
d.type2 !== 'directory' &&
|
||||
!IGNORE.has(d.name1 ?? '') &&
|
||||
!IGNORE.has(d.name2 ?? '')
|
||||
);
|
||||
const changes = getChanges();
|
||||
|
||||
if (changes.length === 0) {
|
||||
console.log(chalk.gray('No differences found. Directories are in sync.'));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
for (const diff of changes) {
|
||||
const name = diff.name1 ?? diff.name2;
|
||||
if (!name) continue;
|
||||
for (const change of changes) {
|
||||
const label = filePath(change);
|
||||
|
||||
const filePath = path.join(diff.relativePath, name);
|
||||
|
||||
if (diff.state === 'left') {
|
||||
console.log(chalk.green(` + ${filePath} (new in src)`));
|
||||
printDiff('', path.join(SOURCE, diff.relativePath, name), filePath);
|
||||
if (change.state === 'left') {
|
||||
console.log(chalk.green(` + ${label} (new in src)`));
|
||||
printDiff('', srcPath(change), label);
|
||||
}
|
||||
|
||||
if (diff.state === 'right') {
|
||||
console.log(chalk.red(` - ${filePath} (only in templates)`));
|
||||
if (change.state === 'right') {
|
||||
console.log(chalk.red(` - ${label} (only in templates)`));
|
||||
}
|
||||
|
||||
if (diff.state === 'distinct') {
|
||||
console.log(chalk.yellow(` ~ ${filePath} (modified)`));
|
||||
printDiff(path.join(TEMPLATES, diff.relativePath, name), path.join(SOURCE, diff.relativePath, name), filePath);
|
||||
if (change.state === 'distinct') {
|
||||
console.log(chalk.yellow(` ~ ${label} (modified)`));
|
||||
printDiff(templatesPath(change), srcPath(change), label);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,12 +34,7 @@ console.log();
|
||||
console.log(chalk.bold(`${changes.length} change(s) found.`));
|
||||
console.log();
|
||||
|
||||
const { ok } = await prompts({
|
||||
type: 'confirm',
|
||||
name: 'ok',
|
||||
message: `Copy all changes from ${SOURCE} to ${TEMPLATES}?`,
|
||||
initial: false,
|
||||
});
|
||||
const ok = await confirm(`Copy all changes from ${SRC} to ${TEMPLATES}?`);
|
||||
|
||||
if (!ok) {
|
||||
console.log(chalk.gray('\nAborted. No files were copied.'));
|
||||
@@ -91,17 +42,12 @@ if (!ok) {
|
||||
}
|
||||
|
||||
// Only copy changed/new files from src — skip 'right' entries (only in templates)
|
||||
for (const diff of changes) {
|
||||
if (diff.state === 'right') continue; // don't delete files that only exist in templates
|
||||
for (const change of changes) {
|
||||
if (change.state === 'right') continue;
|
||||
|
||||
if (!diff.name1) continue;
|
||||
|
||||
const relPath = path.join(diff.relativePath, diff.name1);
|
||||
const src = path.join(SOURCE, relPath);
|
||||
const dest = path.join(TEMPLATES, relPath);
|
||||
|
||||
await fs.copy(src, dest, { overwrite: true });
|
||||
console.log(chalk.green(` ✔ Copied ${relPath}`));
|
||||
const dest = templatesPath(change);
|
||||
await fs.copy(srcPath(change), dest, { overwrite: true });
|
||||
console.log(chalk.green(` ✔ Copied ${filePath(change)}`));
|
||||
}
|
||||
|
||||
console.log(chalk.bold.green('\nDone!\n'));
|
||||
|
||||
Reference in New Issue
Block a user