Skip to content

Commit

Permalink
feat: offer in progess rules on doc generation fix #197
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Aug 28, 2024
1 parent d63ebd4 commit 7635d7c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ docs/.vitepress/cache
docs/.vitepress/dist
notes.md
vue-mess-detector.json
.inProgress
85 changes: 57 additions & 28 deletions src/generator/createDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,46 @@ import inquirer from 'inquirer' // it cause some error when this file is ts
// For some reason If I try to import a constant and use it in `questions` it doesnt work...
const RULESETS = ['vue-essential', 'vue-strong', 'vue-recommended', 'vue-caution', 'rrd']

/**
* Represents a question for the Inquirer.js prompt.
* @typedef {object} Question
* @property {string} type - The type of the question (e.g., 'list', 'input').
* @property {string} name - The name of the question.
* @property {string} message - The message to be displayed to the user.
* @property {string[]} [choices] - The available choices for the user to select from (for list-type questions).
*/

/**
* An array of questions to be asked using the Inquirer.js prompt.
* @type {Question[]}
*/
const questions = [
{
type: 'list',
name: 'ruleset',
message: 'What ruleset this markdown file belongs to?',
choices: RULESETS,
},
{
type: 'input',
name: 'name',
message: 'What is the name of the markdown file? (use kebab-case name)',
},
]
const inProgressPath = './.inProgress'

const getNewDocQuestions = () => {
return [
{
type: 'list',
name: 'ruleset',
message: 'What ruleset this markdown doc file belongs to?',
choices: RULESETS,
},
{
type: 'input',
name: 'name',
message: 'What is the name of the markdown file? (use kebab-case name)',
},
]
}

const getQuestions = async () => {
try {
const content = await fs.readFile(inProgressPath, 'utf8')
const inProgressRules = content.split('\n').filter(Boolean)

if (inProgressRules.length > 0) {
return [{
type: 'list',
name: 'choice',
message: 'Select the rule for doc generation:',
choices: [
...inProgressRules,
{ name: 'Other rule', value: 'new' }
],
}]
}
} catch (error) {
if (error.code !== 'ENOENT') throw error
}

return getNewDocQuestions()
}

async function createFile({ ruleset, name }) {
const rulename = name.split('-').map(str => str.charAt(0).toUpperCase() + str.slice(1)).join(' ')
Expand All @@ -42,8 +56,23 @@ async function createFile({ ruleset, name }) {
console.log(`1️⃣ File ${filePath} generated!`)
}

inquirer.prompt(questions)
.then(createFile)
inquirer.prompt(await getQuestions())
.then(async (answers) => {
if (answers.choice === 'new') {
const newAnswers = await inquirer.prompt(getNewDocQuestions())
Object.assign(answers, newAnswers)
} else if (answers.choice) {
const [ruleset, name] = answers.choice.split('/')
answers.ruleset = ruleset
answers.name = name

// Remove the selected rule from inProgress file
const content = await fs.readFile(inProgressPath, 'utf8')
const updatedContent = content.split('\n').filter(line => line !== answers.choice).join('\n')
await fs.writeFile(inProgressPath, updatedContent, 'utf8')
}
await createFile(answers)
})
.catch((error) => {
console.error(error)
})
14 changes: 14 additions & 0 deletions src/generator/createRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ const createFiles = async (answers) => {

// TODO Add your new reportRuleName import and function call to src/rulesReport.ts
// console.log(`5️⃣ report${pascalCaseRulename} function call to ./src/rulesReport.ts`)

const inProgressPath = './.inProgress'
let inProgressContent = ''
try {
inProgressContent = await fs.readFile(inProgressPath, 'utf8')
}
catch (error) {
if (error.code !== 'ENOENT')
throw error
}
if (!inProgressContent.includes(answers.name)) {
await fs.appendFile(inProgressPath, `${answers.ruleset}/${answers.name}\n`, 'utf8')
console.log(`5️⃣ ${answers.name} added to .inProgress`)
}
}

inquirer.prompt(questions)
Expand Down

0 comments on commit 7635d7c

Please sign in to comment.