UNPKG

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
#!/usr/bin/env node /** * 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 };