2024-10-09 17:02:40 +00:00
|
|
|
const inquirer = require('inquirer');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2024-10-09 17:02:41 +00:00
|
|
|
const { root, src, thoughts: thoughtsDir } = require('../constants');
|
2024-10-09 17:02:40 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const cheerio = require('cheerio');
|
2024-10-09 17:02:42 +00:00
|
|
|
const chalk = require('chalk');
|
2024-10-09 17:02:40 +00:00
|
|
|
|
2024-10-09 17:02:42 +00:00
|
|
|
process.argv.shift();
|
|
|
|
process.argv.shift();
|
|
|
|
|
2024-10-09 17:02:41 +00:00
|
|
|
const readmeContent = fs.readFileSync(path.join(root, 'readme.md')).toString();
|
|
|
|
|
|
|
|
const problems = readmeContent.match(/^- \[.] (?:\[(.*)\]|(.*))/gm).map(res => {
|
|
|
|
const sanitised = res.substring(8).replace('[', '').replace(']', '');
|
|
|
|
return sanitised.match(/[0-9]* - (.*)/)[1];
|
|
|
|
});
|
2024-10-09 17:02:40 +00:00
|
|
|
|
2024-10-09 17:02:42 +00:00
|
|
|
const questions = [];
|
|
|
|
|
|
|
|
if (isNaN(process.argv[0]))
|
|
|
|
questions.push({
|
|
|
|
name: 'problemNumber',
|
|
|
|
message: 'Which problem would you like to solve?',
|
|
|
|
type: 'number',
|
|
|
|
validate: input => {
|
|
|
|
input = parseInt(input);
|
|
|
|
|
2024-10-09 17:02:42 +00:00
|
|
|
let alreadyGenerated = false;
|
|
|
|
fs.readdirSync(src).forEach(file => {
|
|
|
|
if (file.startsWith(input)) alreadyGenerated = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (alreadyGenerated) return 'Please choose a problem you have not already completed!';
|
|
|
|
else return true;
|
2024-10-09 17:02:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-09 17:02:40 +00:00
|
|
|
inquirer
|
|
|
|
.prompt([
|
2024-10-09 17:02:42 +00:00
|
|
|
...questions,
|
2024-10-09 17:02:41 +00:00
|
|
|
{
|
|
|
|
name: 'thoughts',
|
|
|
|
message: 'Should I generate a thoughts document for you?',
|
|
|
|
type: 'confirm',
|
|
|
|
default: false
|
2024-10-09 17:02:40 +00:00
|
|
|
}
|
|
|
|
])
|
2024-10-09 17:02:41 +00:00
|
|
|
.then(({ problemNumber, thoughts }) => {
|
2024-10-09 17:02:42 +00:00
|
|
|
if (!problemNumber) problemNumber = parseInt(process.argv[0]);
|
2024-10-09 17:02:41 +00:00
|
|
|
const fileName = `${problemNumber} - ${problems[problemNumber - 1]}`;
|
2024-10-09 17:02:40 +00:00
|
|
|
|
|
|
|
// Fetch the problem data off of projecteuler.net
|
2024-10-09 17:02:42 +00:00
|
|
|
axios
|
|
|
|
.get(`https://projecteuler.net/problem=${problemNumber}`)
|
|
|
|
.then(({ data }) => {
|
|
|
|
const $ = cheerio.load(data);
|
|
|
|
const problemContent = $('.problem_content')
|
|
|
|
.text()
|
|
|
|
.trim()
|
|
|
|
.split('\n')
|
|
|
|
.map(r => `// ${r}`)
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
// Generate the source file
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(src, `${fileName}.ts`),
|
|
|
|
`${problemContent}
|
2024-10-09 17:02:41 +00:00
|
|
|
export = {};
|
2024-10-09 17:02:40 +00:00
|
|
|
|
2024-10-09 17:02:41 +00:00
|
|
|
// Output
|
|
|
|
console.log();`
|
2024-10-09 17:02:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Generate the thoughts file
|
|
|
|
if (thoughts)
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(thoughtsDir, `${fileName}.md`),
|
|
|
|
`<div align="center">
|
|
|
|
|
|
|
|
### ${problems[problemNumber - 1]}
|
|
|
|
</div>`
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check it off in the readme
|
|
|
|
const regex = new RegExp(` \\[.\\] ${problemNumber} - .*`);
|
|
|
|
const match = readmeContent.match(regex);
|
|
|
|
|
|
|
|
let newLine = ` [x] [${match[0]
|
|
|
|
.replace('[ ]', '')
|
|
|
|
.trim()}](src/${encodeURIComponent(fileName)}.ts)`;
|
|
|
|
|
|
|
|
if (thoughts)
|
|
|
|
newLine += `\n - [Thoughts](thoughts/${encodeURIComponent(fileName)}.md)`;
|
|
|
|
|
|
|
|
const newContent = readmeContent.replace(regex, newLine);
|
|
|
|
|
|
|
|
fs.writeFileSync(path.join(root, 'readme.md'), newContent);
|
|
|
|
|
|
|
|
// If the problem's ID is greater than 100, add it to the gitignore
|
|
|
|
if (problemNumber > 100) {
|
|
|
|
const gitignorePath = path.join(root, '.gitignore');
|
|
|
|
const gitignoreContent = fs.readFileSync(gitignorePath).toString();
|
|
|
|
let newContent = `${gitignoreContent}\nsrc/${fileName}.ts`;
|
|
|
|
if (thoughts) newContent += `\nthoughts/${fileName}.md`;
|
|
|
|
|
|
|
|
fs.writeFileSync(gitignorePath, newContent);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() =>
|
|
|
|
console.error(
|
|
|
|
chalk.red(
|
|
|
|
chalk.bold(
|
|
|
|
'There was an error generating files for that challenge - please ensure that it exists on Project Euler!'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2024-10-09 17:02:40 +00:00
|
|
|
);
|
|
|
|
});
|