Skip to content

Commit

Permalink
create-rule task also updates README (fixed microsoft#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
IllusionMH committed Feb 9, 2019
1 parent f80af6a commit 310b54e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
30 changes: 29 additions & 1 deletion build-tasks/common/readme-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ function getAllRules() {
return rules;
}

function addNewRule(name, ruleMarkup) {
let match;
let insertPosition;

while ((match = praseRe.exec(readmeContent))) {
const existingName = match[1];
// Looks for first rule that should follow new rule.
// Match position will be used to insert new rule row
if (name < existingName) {
insertPosition = match.index;
break;
}
}

// In case when new rule should be added to the end of the table - look for insret position before </tbody>
if (insertPosition === undefined) {
insertPosition = readmeContent.match(/\s+<\/tbody>\s+<\/table>/).index;
}
const contentBeforeNewRule = readmeContent.slice(0, insertPosition);
const contentAfterNewRule = readmeContent.slice(insertPosition);
const newContent = contentBeforeNewRule + ruleMarkup + contentAfterNewRule;
writeFile(README_NAME, newContent);

// To position cursor on the same line with new rule name should add 3 lines to match lines added with template
return `./${README_NAME}:${contentBeforeNewRule.split('\n').length + 3}`;
}

module.exports = {
getAllRules
getAllRules,
addNewRule
};
13 changes: 11 additions & 2 deletions build-tasks/create-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const fs = require('fs');
const inquirer = require('inquirer');
const path = require('path');
const { writeFile } = require('./common/files');
const readmeTemplate = require('./templates/readme-rule-entry.template');
const { addNewRule } = require('./common/readme-rules');

const questions = [
{
Expand Down Expand Up @@ -88,13 +90,15 @@ const questions = [
inquirer.prompt(questions).then(answers => {
const sourceFileName = createImplementationFile(answers);
const testFileNames = createTestFiles(answers);
const readmePosition = createReadmeEntry(answers);

console.log(`Rule '${answers.name}' created.`);
console.log(`Source file: ${sourceFileName}`);
console.log(`Test files: ${testFileNames.join(', ')}`);
console.log(`Test files: ${testFileNames.join(' ')}`);
console.log(`README.md entry: ${readmePosition}`);

// Attempt to open the files in the current editor.
tryOpenFiles([...testFileNames, sourceFileName]);
tryOpenFiles([...testFileNames, sourceFileName, readmePosition]);
});

function createImplementationFile(answers) {
Expand Down Expand Up @@ -139,6 +143,11 @@ function createTestFiles(answers) {
return testFiles;
}

function createReadmeEntry(answers) {
const content = readmeTemplate(answers);
return addNewRule(answers.name, content);
}

function camelCase(input) {
return input.toLowerCase().replace(/-(.)/g, (match, group1) => group1.toUpperCase());
}
Expand Down
8 changes: 8 additions & 0 deletions build-tasks/templates/readme-rule-entry.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = ({ name, description }) => `
<tr>
<td>
<code>${name}</code>
</td>
<td>${description}</td>
<td>@next</td>
</tr>`;

0 comments on commit 310b54e

Please sign in to comment.