Updated tools, proper linting, build README

This commit is contained in:
Angelos Chalaris
2017-12-14 19:38:11 +02:00
parent 3e79c9071d
commit 94c1923ee7
13 changed files with 74 additions and 61 deletions

View File

@ -1,31 +1,44 @@
var fs = require('fs-extra');
var cp = require('child_process');
var path = require('path');
/*
This is the linter script that lints snippets.
Run using `npm run linter`.
*/
// Load modules
const fs = require('fs-extra'), cp = require('child_process'), path = require('path');
// Set variables for paths
var snippetsPath = './snippets';
var snippetFilename = '';
console.time('Linter');
if(process.argv.length < 3){
console.log('Please specify the filename of a snippet to be linted.');
console.log('Example usage: npm run lint "snippet-file.md"');
process.exit(0);
}
else {
snippetFilename = process.argv[2];
let snippetData = fs.readFileSync(path.join(snippetsPath,snippetFilename),'utf8');
try {
// Read files, lint each one individually and update
try {
let snippetFilenames = fs.readdirSync(snippetsPath);
snippetFilenames.sort((a, b) => {
a = a.toLowerCase();
b = b.toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
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 originalCode = snippetData.slice(snippetData.indexOf('```js')+5,snippetData.lastIndexOf('```'));
fs.writeFileSync('currentSnippet.js',`${originalCode}`);
cp.exec('semistandard "currentSnippet.js" --fix',{},(error, stdOut, stdErr) => {
let lintedCode = fs.readFileSync('currentSnippet.js','utf8');
fs.writeFile(path.join(snippetsPath,snippetFilename), `${snippetData.slice(0, snippetData.indexOf('```js')+5)+lintedCode+'```\n'}`);
console.timeEnd('Linter');
fs.writeFileSync(`${snippet}.temp.js`,`${originalCode}`);
// Run semistandard asynchronously (only way this manages to run), get linted code
// and write back to the original snippet file. Remove temporary file
cp.exec(`semistandard "${snippet}.temp.js" --fix`,{},(error, stdOut, stdErr) => {
let lintedCode = fs.readFileSync(`${snippet}.temp.js`,'utf8');
fs.writeFile(path.join(snippetsPath,snippet), `${snippetData.slice(0, snippetData.indexOf('```js')+5)+lintedCode+'```\n'}`);
fs.unlink(`${snippet}.temp.js`);
// Log a success message
console.log(`${chalk.red('SUCCESS!')} Linted snippet: ${snippet}`);
// Log the time taken for the file
console.timeEnd(`Linter (${snippet})`);
});
}
catch (err){
console.log('Error during snippet loading: '+err);
process.exit(1);
}
}
catch (err){ // Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During linting: ${err}`);
process.exit(1);
}