bin/build-readups.js
#!/usr/bin/env node
/*eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const rulesOutput = {};
const parser = (rule, baseDir, url) => {
const filePath = `${baseDir}${rule}/README.md`;
let excerpt = '';
if (!fs.existsSync(filePath)) {
return excerpt;
}
const readme = fs.readFileSync(filePath, 'utf-8');
if (!readme) {
return excerpt;
}
if (/## Options/.test(readme)) {
excerpt = readme.replace(/## Options[\s\S]+/, ''); //eslint-disable-line no-useless-escape
} else {
excerpt = readme.replace(/```\n[\s\S]+/gi, '```');
}
if (url) {
// excerpt += `More [info](${url${d}).`;
}
return excerpt;
};
rimraf('.tmp_rules', error => {
if (error) {
console.error(error);
process.exit(1);
}
fs.mkdirSync('.tmp_rules');
const git = require('simple-git')('.tmp_rules');
console.log('Cloning repos...');
const stylelint = new Promise((resolve, reject) => {
git.clone('https://github.com/stylelint/stylelint', 'stylelint', gitError => {
if (gitError) {
reject(gitError);
return;
}
const rulesPath = '.tmp_rules/stylelint/lib/rules/';
const rules = fs
.readdirSync(rulesPath)
.filter(file => fs.statSync(path.join(rulesPath, file)).isDirectory());
rules.forEach(d => {
rulesOutput[d] = parser(
d,
rulesPath,
'https://github.com/stylelint/stylelint/blob/master/lib/rules/'
);
});
resolve();
});
});
const stylelintA11y = new Promise((resolve, reject) => {
git.clone('https://github.com/YozhikM/stylelint-a11y', 'stylelint-a11y', gitError => {
if (gitError) {
reject(gitError);
return;
}
const rulesPath = '.tmp_rules/stylelint-a11y/src/rules/';
const rules = fs
.readdirSync(rulesPath)
.filter(file => fs.statSync(path.join(rulesPath, file)).isDirectory());
rules.forEach(d => {
rulesOutput[`a11y/${d}`] = parser(
d,
rulesPath,
'https://github.com/YozhikM/stylelint-a11y/blob/master/src/rules/'
);
});
resolve();
});
});
const stylelintSCSS = new Promise((resolve, reject) => {
git.clone('https://github.com/kristerkari/stylelint-scss', 'stylelint-scss', gitError => {
if (gitError) {
reject(gitError);
return;
}
const rulesPath = '.tmp_rules/stylelint-scss/src/rules/';
const rules = fs
.readdirSync(rulesPath)
.filter(file => fs.statSync(path.join(rulesPath, file)).isDirectory());
rules.forEach(d => {
rulesOutput[`scss/${d}`] = parser(
d,
rulesPath,
'https://github.com/kristerkari/stylelint-scss/blob/master/src/rules/'
);
});
resolve();
});
});
const stylelintOrder = new Promise((resolve, reject) => {
git.clone('https://github.com/hudochenkov/stylelint-order', 'stylelint-order', gitError => {
if (gitError) {
reject(gitError);
return;
}
const rulesPath = '.tmp_rules/stylelint-order/rules/';
const rules = fs
.readdirSync(rulesPath)
.filter(
file => fs.statSync(path.join(rulesPath, file)).isDirectory() && file !== 'deprecated'
);
rules.forEach(d => {
rulesOutput[`order/${d}`] = parser(
d,
rulesPath,
'https://github.com/stylelint/stylelint/blob/master/rules/'
);
});
resolve();
});
});
Promise.all([stylelint, stylelintA11y, stylelintSCSS, stylelintOrder]).then(() => {
fs.writeFile('config/contents/rules.json', JSON.stringify(rulesOutput, null, 2), err => {
if (err) {
console.log(err);
}
console.log('rules.json saved!');
});
});
});