Skip to content

Commit

Permalink
Merge pull request #1573 from elizaOS/enhance/update_script
Browse files Browse the repository at this point in the history
feat: parse files through prettier
  • Loading branch information
shakkernerd authored Dec 30, 2024
2 parents ec741a5 + a3a0ea3 commit 6bcc2ed
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions scripts/update-versions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const { execSync } = require('child_process');

const packagesDir = path.join(__dirname, '../packages');
const externalDirs = ['../agent', '../client', '../docs'];
const lernaPath = path.join(__dirname, '../lerna.json');

// Simple Logger
function log(level, message) {
const timestamp = new Date().toISOString().split('T').join(' ').slice(0, 19);
console.log(`${timestamp} [${level.toUpperCase()}]: ${message}`);
}

// Helper to simplify file path for logs
function simplifyPath(filePath) {
const relativePath = path.relative(path.join(__dirname, '..'), filePath);
return `/${relativePath.replace(/\\/g, '/')}`;
}

// Prompt for version input
const rl = readline.createInterface({
input: process.stdin,
Expand All @@ -21,9 +34,21 @@ function askVersion() {
});
}

function runPrettier(filePaths) {
try {
execSync(`npx prettier --write ${filePaths.join(' ')}`, { stdio: 'ignore' });
log('info', `Formatted ${filePaths.length} files with Prettier.`);
} catch (error) {
log('error', `Failed to format files with Prettier: ${error.message}`);
}
}

// Update versions in all package.json files
async function updateVersions() {
const NEW_VERSION = await askVersion();
log('info', `Starting version update process to ${NEW_VERSION}.`);

const updatedFiles = [];

const updateDirectory = (dirPath) => {
const packagePath = path.join(dirPath, 'package.json');
Expand All @@ -35,12 +60,13 @@ async function updateVersions() {
if (oldVersion) {
packageJson.version = NEW_VERSION;
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2) + '\n');
console.log(`Updated ${dirPath}: ${oldVersion} -> ${packageJson.version}`);
log('info', `Updated ${simplifyPath(packagePath)}: ${oldVersion} -> ${packageJson.version}`);
updatedFiles.push(packagePath);
} else {
console.warn(`Version not found in ${dirPath}/package.json`);
log('warn', `Version not found in ${simplifyPath(packagePath)}`);
}
} else {
console.warn(`No package.json found in ${dirPath}`);
log('warn', `No package.json found in ${simplifyPath(packagePath)}`);
}
};

Expand All @@ -49,7 +75,7 @@ async function updateVersions() {
const packageDirs = fs.readdirSync(packagesDir);
packageDirs.forEach((dir) => updateDirectory(path.join(packagesDir, dir)));
} else {
console.warn(`Packages directory not found at ${packagesDir}`);
log('warn', `Packages directory not found at ${packagesDir}`);
}

// Update external folders
Expand All @@ -58,7 +84,7 @@ async function updateVersions() {
if (fs.existsSync(fullPath)) {
updateDirectory(fullPath);
} else {
console.warn(`External directory not found: ${fullPath}`);
log('warn', `External directory not found: ${simplifyPath(fullPath)}`);
}
});

Expand All @@ -70,13 +96,22 @@ async function updateVersions() {
if (oldVersion) {
lernaJson.version = NEW_VERSION;
fs.writeFileSync(lernaPath, JSON.stringify(lernaJson, null, 2) + '\n');
console.log(`Updated lerna.json: ${oldVersion} -> ${lernaJson.version}`);
log('info', `Updated ${simplifyPath(lernaPath)}: ${oldVersion} -> ${lernaJson.version}`);
updatedFiles.push(lernaPath);
} else {
console.warn(`Version not found in lerna.json`);
log('warn', `Version not found in ${simplifyPath(lernaPath)}`);
}
} else {
console.warn(`lerna.json not found at ${lernaPath}`);
log('warn', `lerna.json not found at ${lernaPath}`);
}

if (updatedFiles.length > 0) {
runPrettier(updatedFiles);
} else {
log('info', 'No files updated, skipping Prettier formatting.');
}

log('info', 'Version update process completed.');
}

updateVersions();

0 comments on commit 6bcc2ed

Please sign in to comment.