@diagramers/cli
Version:
Diagramers CLI - Command-line tools for managing Diagramers projects
132 lines • 5.98 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.ProjectUpdater = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const chalk_1 = __importDefault(require("chalk"));
class ProjectUpdater {
constructor() {
this.currentProjectPath = process.cwd();
}
async update(options) {
// Check if we're in a valid project directory
if (!await this.isValidProject()) {
throw new Error('Not a valid project directory. Please run this command from your project root.');
}
// Create backup if requested
if (options.backup) {
await this.createBackup();
}
// Get list of files that can be updated
const updateableFiles = await this.getUpdateableFiles();
// Check for conflicts
const conflicts = await this.checkConflicts(updateableFiles);
if (conflicts.length > 0 && !options.force) {
console.log(chalk_1.default.yellow('⚠️ Conflicts detected in the following files:'));
conflicts.forEach(file => console.log(chalk_1.default.yellow(` - ${file}`)));
console.log(chalk_1.default.yellow('Use --force to overwrite these files'));
throw new Error('Update aborted due to conflicts');
}
// Perform the update
await this.performUpdate(updateableFiles);
console.log(chalk_1.default.green('✅ Project updated successfully!'));
}
async isValidProject() {
const packageJsonPath = path.join(this.currentProjectPath, 'package.json');
const mainTsPath = path.join(this.currentProjectPath, 'src/main.ts');
const altMainTsPath = path.join(this.currentProjectPath, 'main.ts');
// Check for package.json and either src/main.ts or main.ts
const hasPackageJson = await fs.pathExists(packageJsonPath);
const hasMainTs = await fs.pathExists(mainTsPath) || await fs.pathExists(altMainTsPath);
return hasPackageJson && hasMainTs;
}
async createBackup() {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupPath = path.join(this.currentProjectPath, `backup-${timestamp}`);
await fs.copy(this.currentProjectPath, backupPath, {
filter: (src) => {
const relativePath = path.relative(this.currentProjectPath, src);
return !relativePath.startsWith('node_modules') &&
!relativePath.startsWith('.git') &&
!relativePath.startsWith('backup-');
}
});
console.log(chalk_1.default.blue(`📦 Backup created at: ${backupPath}`));
}
async getUpdateableFiles() {
// For now, we'll update specific files that are commonly modified in templates
const updateableFiles = [
'src/helpers/dbcontext.ts',
'src/helpers/result.ts',
'src/helpers/enums.ts',
'src/config/index.ts',
'src/config/development.ts',
'src/config/staging.ts',
'src/config/production.ts',
'src/server/index.ts',
'webpack.config.js',
'tsconfig.json'
];
return updateableFiles.filter(file => fs.existsSync(path.join(this.currentProjectPath, file)));
}
async checkConflicts(files) {
const conflicts = [];
for (const file of files) {
const projectPath = path.join(this.currentProjectPath, file);
if (await fs.pathExists(projectPath)) {
// For now, we'll consider all files as potentially conflicting
// In a real implementation, you'd compare with the template version
conflicts.push(file);
}
}
return conflicts;
}
async performUpdate(files) {
console.log(chalk_1.default.blue('📝 Updating project files...'));
// For now, we'll just report what would be updated
// In a real implementation, you'd copy from the latest template
for (const file of files) {
console.log(chalk_1.default.green(`✅ Would update: ${file}`));
}
console.log(chalk_1.default.yellow('⚠️ Update mechanism is being improved.'));
console.log(chalk_1.default.yellow(' For now, manual updates are recommended.'));
}
}
exports.ProjectUpdater = ProjectUpdater;
//# sourceMappingURL=project-updater.js.map