no-migration
Version:
CLI tool to manage migration changes and user changes separately for React Native miniapp projects
177 lines ⢠8.22 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrationCommand = migrationCommand;
const chalk_1 = __importDefault(require("chalk"));
const gitHelper_1 = require("../utils/gitHelper");
const fileManager_1 = require("../utils/fileManager");
const child_process_1 = require("child_process");
const util_1 = require("util");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const execAsync = (0, util_1.promisify)(child_process_1.exec);
async function migrationCommand() {
try {
console.log(chalk_1.default.blue('š Starting migration process...'));
const git = new gitHelper_1.GitHelper();
// Step 1: Snapshot current user changes
console.log(chalk_1.default.cyan('šø Saving current user changes...'));
const currentChanges = await git.getAllChangedFiles();
const currentFiles = currentChanges.map(c => c.file);
if (currentFiles.length > 0) {
const userChangesSnapshot = {
timestamp: new Date().toISOString(),
files: await git.createChangesSnapshot(currentFiles),
};
fileManager_1.FileManager.writeUserChanges(userChangesSnapshot);
console.log(chalk_1.default.green(`ā
Saved ${currentFiles.length} user change(s) to user-changes.json`));
}
else {
console.log(chalk_1.default.yellow('ā ļø No user changes to save'));
}
// Step 2: Run momo-migration
console.log(chalk_1.default.cyan('\nš Running momo-migration...'));
try {
const { stdout, stderr } = await execAsync('momo-migration', {
cwd: process.cwd(),
env: process.env,
});
if (stdout)
console.log(stdout);
if (stderr)
console.error(stderr);
console.log(chalk_1.default.green('ā
momo-migration completed'));
}
catch (error) {
console.error(chalk_1.default.red('ā Error running momo-migration:'), error.message);
console.log(chalk_1.default.yellow('ā ļø Continuing with migration setup...'));
}
// Step 3: Get migration changes
console.log(chalk_1.default.cyan('\nš Detecting migration changes...'));
const afterMigrationChanges = await git.getAllChangedFiles();
const afterMigrationFiles = afterMigrationChanges.map(c => c.file);
// Filter out user changes to get only migration changes
const userChangesFiles = new Set(currentFiles);
const migrationFiles = afterMigrationFiles.filter(f => {
// If file was not in user changes, it's from migration
if (!userChangesFiles.has(f))
return true;
// If file was in user changes, check if content changed after migration
const userSnapshot = fileManager_1.FileManager.readUserChanges();
if (!userSnapshot)
return true;
const userFileChange = userSnapshot.files[f];
if (!userFileChange)
return true;
// Compare current content with user snapshot
const currentContent = git.getFileContent(f);
return currentContent !== userFileChange.content;
});
console.log(chalk_1.default.green(`ā
Detected ${migrationFiles.length} migration change(s)`));
// Step 4: Save migration changes and exclude list
if (migrationFiles.length > 0) {
const migrationChangesSnapshot = {
timestamp: new Date().toISOString(),
files: await git.createChangesSnapshot(migrationFiles),
};
fileManager_1.FileManager.writeMigrationChanges(migrationChangesSnapshot);
fileManager_1.FileManager.writeExcludeList(migrationFiles);
console.log(chalk_1.default.green('ā
Saved migration changes to migration-changes.json'));
console.log(chalk_1.default.green('ā
Saved exclude list to .git-exclude-list'));
}
// Step 5: Add scripts to package.json
console.log(chalk_1.default.cyan('\nš¦ Updating package.json...'));
await updatePackageJson();
// Step 6: Update .gitignore
console.log(chalk_1.default.cyan('\nš Updating .gitignore...'));
fileManager_1.FileManager.updateGitignore([
'migration-changes.json',
'user-changes.json',
'.git-exclude-list',
]);
console.log(chalk_1.default.green('ā
Updated .gitignore'));
console.log(chalk_1.default.green('\n⨠Migration setup completed successfully!'));
console.log(chalk_1.default.blue('ā¹ļø You can now use "nono commit -m <message>" to commit your changes'));
}
catch (error) {
console.error(chalk_1.default.red('ā Error during migration:'), error);
process.exit(1);
}
}
async function updatePackageJson() {
const packageJsonPath = path.join(process.cwd(), 'package.json');
if (!fs.existsSync(packageJsonPath)) {
console.log(chalk_1.default.yellow('ā ļø package.json not found'));
return;
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
// Add scripts
if (!packageJson.scripts) {
packageJson.scripts = {};
}
const scriptsToAdd = {
'nono:commit': 'nono commit',
'nono:migration': 'nono migration',
'nono:update': 'nono update',
};
let scriptsAdded = false;
for (const [key, value] of Object.entries(scriptsToAdd)) {
if (!packageJson.scripts[key]) {
packageJson.scripts[key] = value;
scriptsAdded = true;
}
}
// Check if we need to update port from 8181 to 8182
const needsPortChange = packageJson.name === 'vn.momo.profile' ||
packageJson.name === '@momo-mini-app/profile';
if (needsPortChange) {
console.log(chalk_1.default.cyan('š§ Updating ports from 8181 to 8182...'));
const packageJsonStr = JSON.stringify(packageJson, null, 2);
const updatedPackageJsonStr = packageJsonStr.replace(/8181/g, '8182');
fs.writeFileSync(packageJsonPath, updatedPackageJsonStr, 'utf-8');
console.log(chalk_1.default.green('ā
Updated ports to 8182'));
}
else if (scriptsAdded) {
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');
console.log(chalk_1.default.green('ā
Added nono scripts to package.json'));
}
else {
console.log(chalk_1.default.blue('ā¹ļø package.json already up to date'));
}
}
//# sourceMappingURL=migration.js.map