feat(euler): update readme when generating
-
This commit is contained in:
parent
704ca26e2c
commit
816dad857f
3 changed files with 47 additions and 19 deletions
|
@ -3,5 +3,6 @@ const path = require('path');
|
||||||
module.exports = {
|
module.exports = {
|
||||||
resources: path.join(__dirname, 'resources'),
|
resources: path.join(__dirname, 'resources'),
|
||||||
src: path.join(__dirname, 'src'),
|
src: path.join(__dirname, 'src'),
|
||||||
thoughts: path.join(__dirname, 'thoughts')
|
thoughts: path.join(__dirname, 'thoughts'),
|
||||||
|
root: __dirname
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "gulp build",
|
"build": "gulp build",
|
||||||
"start": "node scripts/run",
|
"start": "node scripts/run",
|
||||||
"new": "node scripts/generate"
|
"new": "node scripts/generate",
|
||||||
|
"generate": "node scripts/generate"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@swc/core": "^1.2.125",
|
"@swc/core": "^1.2.125",
|
||||||
|
|
|
@ -1,18 +1,16 @@
|
||||||
const inquirer = require('inquirer');
|
const inquirer = require('inquirer');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { src, thoughts: thoughtsDir } = require('../constants');
|
const { root, src, thoughts: thoughtsDir } = require('../constants');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const cheerio = require('cheerio');
|
const cheerio = require('cheerio');
|
||||||
|
|
||||||
const problems = fs
|
const readmeContent = fs.readFileSync(path.join(root, 'readme.md')).toString();
|
||||||
.readFileSync(path.join(__dirname, '..', 'readme.md'))
|
|
||||||
.toString()
|
const problems = readmeContent.match(/^- \[.] (?:\[(.*)\]|(.*))/gm).map(res => {
|
||||||
.match(/^- \[.] (?:\[(.*)\]|(.*))/gm)
|
const sanitised = res.substring(8).replace('[', '').replace(']', '');
|
||||||
.map(res => {
|
return sanitised.match(/[0-9]* - (.*)/)[1];
|
||||||
const sanitised = res.substring(8).replace('[', '').replace(']', '');
|
});
|
||||||
return sanitised.match(/[0-9]* - (.*)/)[1];
|
|
||||||
});
|
|
||||||
|
|
||||||
inquirer
|
inquirer
|
||||||
.prompt([
|
.prompt([
|
||||||
|
@ -20,10 +18,21 @@ inquirer
|
||||||
name: 'problemNumber',
|
name: 'problemNumber',
|
||||||
message: 'Which problem would you like to solve?',
|
message: 'Which problem would you like to solve?',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
validate: input =>
|
validate: input => {
|
||||||
parseInt(input) > 100
|
input = parseInt(input);
|
||||||
? 'Please make sure you choose a number between 1 and 100!'
|
|
||||||
: true
|
if (input > 100) return 'Please make sure you choose a number between 1 and 100!';
|
||||||
|
else {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'thoughts',
|
name: 'thoughts',
|
||||||
|
@ -32,7 +41,7 @@ inquirer
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
.then(async ({ problemNumber, thoughts }) => {
|
.then(({ problemNumber, thoughts }) => {
|
||||||
const fileName = `${problemNumber} - ${problems[problemNumber - 1]}`;
|
const fileName = `${problemNumber} - ${problems[problemNumber - 1]}`;
|
||||||
|
|
||||||
// Fetch the problem data off of projecteuler.net
|
// Fetch the problem data off of projecteuler.net
|
||||||
|
@ -45,15 +54,32 @@ inquirer
|
||||||
.map(r => `// ${r}`)
|
.map(r => `// ${r}`)
|
||||||
.join('\n');
|
.join('\n');
|
||||||
|
|
||||||
|
// Generate the source file
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
path.join(src, `${fileName}.ts`),
|
path.join(src, `${fileName}.ts`),
|
||||||
`${problemContent}
|
`${problemContent}
|
||||||
export = {};
|
export = {};
|
||||||
|
|
||||||
// Output
|
// Output
|
||||||
console.log();`
|
console.log();`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Generate the thoughts file
|
||||||
if (thoughts) fs.writeFileSync(path.join(thoughtsDir, `${fileName}.md`), '');
|
if (thoughts) fs.writeFileSync(path.join(thoughtsDir, `${fileName}.md`), '');
|
||||||
|
|
||||||
|
// 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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue