Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse files through prettier #1573

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Loading