uppaal-to-tchecker
Version:
JavaScript implementation of Uppaal to TChecker translator - convert Uppaal timed automata models to TChecker format
190 lines (157 loc) โข 6.43 kB
JavaScript
/**
* NPM Publishing Script
* Automates the npm publishing process with pre-checks
*/
const { exec } = require('child_process');
const fs = require('fs').promises;
const path = require('path');
const util = require('util');
const execAsync = util.promisify(exec);
async function runCommand(command, description) {
console.log(`๐ ${description}...`);
try {
const { stdout, stderr } = await execAsync(command);
if (stdout) console.log(stdout.trim());
if (stderr) console.warn(stderr.trim());
return { success: true, stdout, stderr };
} catch (error) {
console.error(`โ Failed: ${error.message}`);
return { success: false, error };
}
}
async function checkPrerequisites() {
console.log('๐ Checking prerequisites...\n');
// Check if npm is logged in
const whoami = await runCommand('npm whoami', 'Checking npm authentication');
if (!whoami.success) {
console.error('โ You are not logged in to npm. Run: npm login');
return false;
}
console.log(`โ
Logged in as: ${whoami.stdout.trim()}\n`);
// Check if package.json exists and is valid
try {
const packageJson = JSON.parse(await fs.readFile('package.json', 'utf8'));
console.log(`๐ฆ Package: ${packageJson.name}@${packageJson.version}`);
console.log(`๐ Description: ${packageJson.description}\n`);
} catch (error) {
console.error('โ Invalid package.json:', error.message);
return false;
}
// Check if working directory is clean
const gitStatus = await runCommand('git status --porcelain', 'Checking git status');
if (gitStatus.success && gitStatus.stdout.trim()) {
console.warn('โ ๏ธ Working directory is not clean:');
console.log(gitStatus.stdout);
console.log('Consider committing changes before publishing.\n');
} else {
console.log('โ
Working directory is clean\n');
}
return true;
}
async function runTests() {
console.log('๐งช Running tests...\n');
const test = await runCommand('npm test', 'Running test suite');
return test.success;
}
async function checkPackageSize() {
console.log('๐ Checking package size...\n');
const packDryRun = await runCommand('npm pack --dry-run', 'Analyzing package contents');
if (packDryRun.success) {
// Extract file size info
const lines = packDryRun.stdout.split('\n');
const sizeInfo = lines.find(line => line.includes('package size'));
if (sizeInfo) {
console.log(`๐ฆ ${sizeInfo}`);
}
// Show included files
console.log('\n๐ Files to be published:');
const fileLines = lines.filter(line => line.trim() && !line.includes('npm notice'));
fileLines.slice(0, 10).forEach(line => {
if (line.includes('B ')) {
console.log(` ${line.trim()}`);
}
});
if (fileLines.length > 10) {
console.log(` ... and ${fileLines.length - 10} more files`);
}
}
return packDryRun.success;
}
async function publishPackage(dryRun = false) {
const command = dryRun ? 'npm publish --dry-run' : 'npm publish';
const description = dryRun ? 'Dry run publish' : 'Publishing to npm';
console.log(`\n๐ค ${description}...\n`);
const result = await runCommand(command, description);
if (result.success && !dryRun) {
console.log('\n๐ Package published successfully!');
// Get package info
try {
const packageJson = JSON.parse(await fs.readFile('package.json', 'utf8'));
console.log(`\n๐ฆ Package info:`);
console.log(` Name: ${packageJson.name}`);
console.log(` Version: ${packageJson.version}`);
console.log(` NPM URL: https://www.npmjs.com/package/${packageJson.name}`);
console.log(`\n๐ง Install command:`);
console.log(` npm install -g ${packageJson.name}`);
console.log(` # or`);
console.log(` npm install ${packageJson.name}`);
} catch (error) {
console.warn('Could not read package info:', error.message);
}
}
return result.success;
}
async function main() {
console.log('๐ NPM Publishing Script\n');
console.log('='.repeat(50) + '\n');
try {
// Step 1: Check prerequisites
if (!(await checkPrerequisites())) {
process.exit(1);
}
// Step 2: Run tests
if (!(await runTests())) {
console.error('โ Tests failed. Fix tests before publishing.');
process.exit(1);
}
// Step 3: Check package size
if (!(await checkPackageSize())) {
console.warn('โ ๏ธ Could not analyze package size, continuing...');
}
// Step 4: Dry run
console.log('\n' + '='.repeat(50));
console.log('๐ Performing dry run...\n');
if (!(await publishPackage(true))) {
console.error('โ Dry run failed. Please fix issues before publishing.');
process.exit(1);
}
// Step 5: Confirm publish
console.log('\n' + '='.repeat(50));
console.log('โ
Dry run successful!\n');
// In a real scenario, you might want to add interactive confirmation
console.log('๐จ READY TO PUBLISH TO NPM ๐จ');
console.log('This will make your package publicly available.');
console.log('\nTo continue with actual publishing, run:');
console.log('npm publish\n');
// For automation, uncomment the next lines:
// if (!(await publishPackage(false))) {
// console.error('โ Publishing failed.');
// process.exit(1);
// }
} catch (error) {
console.error('โ Publishing script failed:', error.message);
process.exit(1);
}
}
// Handle command line arguments
const args = process.argv.slice(2);
if (args.includes('--publish')) {
// Enable actual publishing
publishPackage(false).then(success => {
process.exit(success ? 0 : 1);
});
} else {
main();
}
module.exports = { checkPrerequisites, runTests, checkPackageSize, publishPackage };