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

Update new_feature.js #484

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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.
Imran-imtiaz48 authored Jul 8, 2024
commit 0389ac7adb56349963fe0350e7791b3bf48221d0
70 changes: 32 additions & 38 deletions new_feature.js
Original file line number Diff line number Diff line change
@@ -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}`);
});