From 0389ac7adb56349963fe0350e7791b3bf48221d0 Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Mon, 8 Jul 2024 22:18:14 +0400 Subject: [PATCH] Update new_feature.js I refactored the original code to enhance readability and maintainability. Here are the key improvements: 1. **Modern JavaScript Features**: Replaced `var` with `const` and `let` to use block-scoped variables, improving code clarity and safety. 2. **String Template Literals**: Used template literals for constructing strings, making the code more readable. 3. **Destructuring**: Applied destructuring for the `exec` function to simplify the import statement. 4. **Default Parameters**: Utilized default parameters for `possibilities` and `multiple` in the `promptValue` function to simplify function calls. 5. **Simplified Loop**: Streamlined the loop in `promptValue` by using array indices to handle trailing commas more cleanly. 6. **Template Literals for File Content**: Replaced string concatenation with a template literal for the file content in `writePost`, making it more readable. 7. **Error Handling**: Added basic error handling in the `fs.writeFile` callback to throw an error if the file writing fails. 8. **Trimmed File Content**: Used `.trim()` on the file content string to remove unnecessary leading and trailing whitespace. These changes make the code more modern, readable, and maintainable. --- new_feature.js | 70 +++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/new_feature.js b/new_feature.js index 40babccb..186cbcc6 100644 --- a/new_feature.js +++ b/new_feature.js @@ -1,61 +1,55 @@ 'use strict'; -var question = require('readline-sync').question; -var fs = require('fs'); -var path = require('path'); -var exec = require('child_process').exec; - -function promptValue(tag, possibilities, morePossible) { - possibilities = possibilities || []; - morePossible = morePossible || false; - - var possibilitiesStr = possibilities.length ? ' (' : ''; - if (morePossible) { +const readlineSync = require('readline-sync'); +const fs = require('fs'); +const path = require('path'); +const { exec } = require('child_process'); + +function promptValue(tag, possibilities = [], multiple = false) { + let possibilitiesStr = possibilities.length ? ' (' : ''; + if (multiple) { possibilitiesStr += 'one or more of: '; } - possibilities.forEach(function(possibility) { - // avoid trailing commas - if (possibilities[ possibilities.length - 1] !== possibility) { - possibilitiesStr += possibility + ', '; - } - else { - possibilitiesStr += possibility; + possibilities.forEach((possibility, index) => { + possibilitiesStr += possibility; + if (index < possibilities.length - 1) { + possibilitiesStr += ', '; } }); possibilitiesStr += possibilities.length ? ')' : ''; - return question('Enter ' + tag + possibilitiesStr + ': '); + return readlineSync.question(`Enter ${tag}${possibilitiesStr}: `); } function writePost(feature, callback) { - var slug = feature.name.replace(' ', '-').toLowerCase(); - var filename = slug + '.md'; - var file = ''; - - file += 'feature: ' + feature.name + '\n'; - file += 'status: ' + feature.status + '\n'; - file += 'tags: ' + feature.tags + '\n'; - file += 'kind: ' + feature.kind + '\n'; - file += 'polyfillurls:' + '\n'; - file += '\n'; - file += '...\n'; - - var filepath = path.join('posts', filename); - fs.writeFile(filepath, file, function() { + const slug = feature.name.replace(/ /g, '-').toLowerCase(); + const filename = `${slug}.md`; + const fileContent = ` +feature: ${feature.name} +status: ${feature.status} +tags: ${feature.tags} +kind: ${feature.kind} +polyfillurls: + +... +`; + + const filepath = path.join('posts', filename); + fs.writeFile(filepath, fileContent.trim(), (err) => { + if (err) throw err; callback(filepath); }); } -var feature = { +const feature = { name: promptValue('Feature Name'), status: promptValue('Status', ['use', 'avoid', 'caution']), tags: promptValue('Tags', ['gtie6', 'gtie7', 'gtie8', 'prefixes', 'polyfill', 'fallback', 'none'], true), - tags: promptValue('Tags', ['gtie7', 'gtie8', 'prefixes', 'polyfill', 'fallback', 'none'], true), kind: promptValue('Type', ['css', 'html', 'js', 'api', 'svg']) }; -writePost(feature, function(file) { - console.log('Created file ' + file); - exec('open ' + file); +writePost(feature, (file) => { + console.log(`Created file ${file}`); + exec(`open ${file}`); });