Lint examples + use prettier
This commit is contained in:
@ -3,51 +3,87 @@
|
|||||||
Run using `npm run linter`.
|
Run using `npm run linter`.
|
||||||
You might have to run `npm i -g semistandard` for this script to run properly.
|
You might have to run `npm i -g semistandard` for this script to run properly.
|
||||||
*/
|
*/
|
||||||
// Load modules
|
const fs = require('fs-extra');
|
||||||
const fs = require('fs-extra'), cp = require('child_process'), path = require('path'), chalk = require('chalk');
|
const cp = require('child_process');
|
||||||
// Set variables for paths
|
const path = require('path');
|
||||||
var snippetsPath = './snippets';
|
const chalk = require('chalk');
|
||||||
// Read files, lint each one individually and update
|
const prettier = require('prettier');
|
||||||
|
|
||||||
|
const SNIPPETS_PATH = './snippets';
|
||||||
|
const TEMP_PATH = './temp';
|
||||||
|
|
||||||
|
const codeRE = /```js([\s\S]*?)```/g;
|
||||||
|
|
||||||
|
console.time('lint');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let snippetFilenames = fs.readdirSync(snippetsPath);
|
const snippets = fs.readdirSync(SNIPPETS_PATH)
|
||||||
let jobCounter = 0;
|
.sort((a, b) => a.toLowerCase() - b.toLowerCase())
|
||||||
snippetFilenames.sort((a, b) => {
|
// turn it into an object so we can add data to it to be used in a different scope
|
||||||
a = a.toLowerCase();
|
.map(name => ({ name }));
|
||||||
b = b.toLowerCase();
|
|
||||||
if (a < b) return -1;
|
if (!fs.existsSync(TEMP_PATH)) {
|
||||||
if (a > b) return 1;
|
fs.mkdirSync(TEMP_PATH);
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
// Read each file, get its code, write it to a temporary file, pass it through
|
|
||||||
// semistandard, get the output from the file, update the original file.
|
|
||||||
for(let snippet of snippetFilenames){
|
|
||||||
// Start a timer for the file
|
|
||||||
console.time(`Linter (${snippet})`);
|
|
||||||
// Synchronously read data from the snippet, get the code, write it to a temporary file
|
|
||||||
let snippetData = fs.readFileSync(path.join(snippetsPath,snippet),'utf8');
|
|
||||||
let codeStart = snippetData.indexOf('```js'), codeEnd = snippetData.search(/```[\n\r\s]+```js/g);
|
|
||||||
let originalCode = snippetData.slice(codeStart+5,codeEnd);
|
|
||||||
while(jobCounter >= 20){
|
|
||||||
setTimeout(()=>{},1000);
|
|
||||||
}
|
}
|
||||||
const tempSnippet = snippet.replace('.md', '');
|
|
||||||
fs.writeFileSync(`${tempSnippet}.temp.js`,`${originalCode}`);
|
for (const snippet of snippets) {
|
||||||
// Run semistandard asynchronously (only way this manages to run), get linted code
|
snippet.data = fs.readFileSync(path.join(SNIPPETS_PATH, snippet.name), 'utf8');
|
||||||
// and write back to the original snippet file. Remove temporary file
|
snippet.tempNames = [];
|
||||||
cp.exec(`semistandard "${tempSnippet}.temp.js" --fix`,{},(error, stdOut, stdErr) => {
|
snippet.code = [];
|
||||||
jobCounter += 1;
|
|
||||||
let lintedCode = fs.readFileSync(`${tempSnippet}.temp.js`,'utf8');
|
let match = codeRE.exec(snippet.data);
|
||||||
fs.writeFile(path.join(snippetsPath,snippet), `${snippetData.slice(0, codeStart+5)+lintedCode+snippetData.slice(codeEnd)}`);
|
// make a counter so we write the definition + example code blocks to a different file
|
||||||
fs.unlink(`${tempSnippet}.temp.js`);
|
let counter = 0;
|
||||||
// Log a success message
|
while (match) {
|
||||||
console.log(`${chalk.green('SUCCESS!')} Linted snippet: ${snippet}`);
|
snippet.code.push(match[1]); // capture group
|
||||||
// Log the time taken for the file
|
snippet.tempNames.push(snippet.name.replace('.md', counter));
|
||||||
console.timeEnd(`Linter (${snippet})`);
|
match = codeRE.exec(snippet.data);
|
||||||
jobCounter -= 1;
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
snippet.code.forEach((str, i) => {
|
||||||
|
fs.writeFileSync(`${TEMP_PATH}/${snippet.tempNames[i]}.js`, `${snippet.code[i]}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cp.exec(`semistandard "${TEMP_PATH}" --fix`, {}, (err, stdout, stderr) => {
|
||||||
|
// Loop through each snippet now that semistandard has done its job,
|
||||||
|
// run prettier and write to the files
|
||||||
|
for (const snippet of snippets) {
|
||||||
|
// an array to store each linted code block (definition + example)
|
||||||
|
const lintedCode = [];
|
||||||
|
|
||||||
|
for (const tempName of snippet.tempNames) {
|
||||||
|
let data = fs.readFileSync(`${TEMP_PATH}/${tempName}.js`,'utf8');
|
||||||
|
|
||||||
|
// prettier sometimes throws an error..
|
||||||
|
try {
|
||||||
|
data = prettier.format(data, {
|
||||||
|
printWidth: 100,
|
||||||
|
singleQuote: true
|
||||||
|
});
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
lintedCode.push(data);
|
||||||
|
|
||||||
|
fs.unlink(`${TEMP_PATH}/${tempName}.js`);
|
||||||
}
|
}
|
||||||
catch (err){ // Handle errors (hopefully not!)
|
|
||||||
|
// We replace each ```js ``` code block with the newly linted code
|
||||||
|
let index = -1;
|
||||||
|
snippet.data = snippet.data.replace(codeRE, () => {
|
||||||
|
index++;
|
||||||
|
return '```js\n' + lintedCode[index] + '```';
|
||||||
|
});
|
||||||
|
|
||||||
|
fs.writeFileSync(path.join(SNIPPETS_PATH, snippet.name), snippet.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.removeSync(TEMP_PATH);
|
||||||
|
|
||||||
|
console.timeEnd('lint');
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
console.log(`${chalk.red('ERROR!')} During linting: ${err}`);
|
console.log(`${chalk.red('ERROR!')} During linting: ${err}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user