1
Fork 0

chore: adjust code style

This commit is contained in:
Bauke 2019-02-19 15:58:27 +01:00
parent e24c3824a2
commit 7e5977abe4
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
4 changed files with 82 additions and 78 deletions

View File

@ -7,18 +7,21 @@
}, },
"extends": "eslint:recommended", "extends": "eslint:recommended",
"rules": { "rules": {
"comma-dangle": [
"error",
"always-multiline"
],
"indent": [ "indent": [
"error", "error",
2 2
], ],
"no-console": "off",
"quotes": [ "quotes": [
"error", "error",
"single" "single"
], ],
"semi": [ "semi": [
"error", "error",
"never" "always"
] ]
} }
} }

1
.stylelintignore Normal file
View File

@ -0,0 +1 @@
css/**

44
bump.js
View File

@ -1,7 +1,7 @@
// Bumps a specified style's version // Bumps a specified style's version
const fs = require('fs') const fs = require('fs');
const path = require('path') const path = require('path');
/** /**
* @function bump * @function bump
@ -11,43 +11,43 @@ const path = require('path')
function bump(jsonPath, incrementType) { function bump(jsonPath, incrementType) {
if (!fs.existsSync(jsonPath)) if (!fs.existsSync(jsonPath))
throw Error(`File ${jsonPath} does not exist.`) throw Error(`File ${jsonPath} does not exist.`);
if (path.extname(jsonPath) !== '.json') if (path.extname(jsonPath) !== '.json')
throw Error(`File ${jsonPath} does not end in "json".`) throw Error(`File ${jsonPath} does not end in "json".`);
if (typeof incrementType === 'undefined') if (typeof incrementType === 'undefined')
throw Error('No valid increment type was included. Valid: "major", "minor" or "patch".') throw Error('No valid increment type was included. Valid: "major", "minor" or "patch".');
if (incrementType !== 'major' && incrementType !== 'minor' && incrementType !== 'patch') if (incrementType !== 'major' && incrementType !== 'minor' && incrementType !== 'patch')
throw Error(`Increment type ${incrementType} does not equal "major", "minor" or "patch".`) throw Error(`Increment type ${incrementType} does not equal "major", "minor" or "patch".`);
const jsonFile = require(jsonPath) const jsonFile = require(jsonPath);
if (typeof jsonFile.options.version === 'undefined') if (typeof jsonFile.options.version === 'undefined')
throw Error(`${jsonFile} does not include a "version" property.`) throw Error(`${jsonFile} does not include a "version" property.`);
const incrementTypes = { const incrementTypes = {
'major': 0, 'major': 0,
'minor': 1, 'minor': 1,
'patch': 2 'patch': 2,
} };
const oldVersion = jsonFile.options.version.split('.') const oldVersion = jsonFile.options.version.split('.');
let bumped = parseInt(oldVersion[incrementTypes[incrementType]]) + 1 let bumped = parseInt(oldVersion[incrementTypes[incrementType]]) + 1;
let newVersion let newVersion;
switch (incrementType) { switch (incrementType) {
case 'major': case 'major':
newVersion = `${bumped}.0.0` newVersion = `${bumped}.0.0`;
break break;
case 'minor': case 'minor':
newVersion = `${oldVersion[0]}.${bumped}.0` newVersion = `${oldVersion[0]}.${bumped}.0`;
break break;
case 'patch': case 'patch':
newVersion = `${oldVersion[0]}.${oldVersion[1]}.${bumped}` newVersion = `${oldVersion[0]}.${oldVersion[1]}.${bumped}`;
break break;
} }
jsonFile.options.version = newVersion jsonFile.options.version = newVersion;
fs.writeFileSync(jsonPath, JSON.stringify(jsonFile, null, 2)) fs.writeFileSync(jsonPath, JSON.stringify(jsonFile, null, 2));
} }
exports.bump = bump exports.bump = bump;

View File

@ -1,113 +1,113 @@
const beautify = require('gulp-cssbeautify') const beautify = require('gulp-cssbeautify');
const gulp = require('gulp') const gulp = require('gulp');
const eslint = require('gulp-eslint') const eslint = require('gulp-eslint');
const fs = require('fs') const fs = require('fs');
const stylelint = require('gulp-stylelint') const stylelint = require('gulp-stylelint');
const sass = require('gulp-sass') const sass = require('gulp-sass');
gulp.task('lint:js', () => { gulp.task('lint:js', () => {
return gulp.src(['**/*.js','!node_modules/**']) return gulp.src(['**/*.js','!node_modules/**'])
.pipe(eslint()) .pipe(eslint())
.pipe(eslint.format()) .pipe(eslint.format());
}) });
gulp.task('lint:sass', () => { gulp.task('lint:sass', () => {
const options = { const options = {
reporters: [{formatter: 'string', console: true}] reporters: [{formatter: 'string', console: true}],
} };
return gulp.src('sass/**/*.s*') return gulp.src('sass/**/*.s*')
.pipe(stylelint(options)) .pipe(stylelint(options));
}) });
gulp.task('build:dev', () => { gulp.task('build:dev', () => {
const options = { const options = {
outputStyle: 'compressed' outputStyle: 'compressed',
} };
const bOptions = { const bOptions = {
indent: ' ', indent: ' ',
autosemicolon: true autosemicolon: true,
} };
return gulp.src('sass/**/*.s*') return gulp.src('sass/**/*.s*')
.pipe(sass(options).on('error', sass.logError)) .pipe(sass(options).on('error', sass.logError))
.pipe(beautify(bOptions)) .pipe(beautify(bOptions))
.pipe(gulp.dest('temp')) .pipe(gulp.dest('temp'));
}) });
gulp.task('build:prod', () => { gulp.task('build:prod', () => {
const options = { const options = {
outputStyle: 'compressed' outputStyle: 'compressed',
} };
const bOptions = { const bOptions = {
indent: ' ', indent: ' ',
autosemicolon: true autosemicolon: true,
} };
return gulp.src('sass/**/*.s*') return gulp.src('sass/**/*.s*')
.pipe(sass(options).on('error', sass.logError)) .pipe(sass(options).on('error', sass.logError))
.pipe(beautify(bOptions)) .pipe(beautify(bOptions))
.pipe(gulp.dest('css')) .pipe(gulp.dest('css'));
}) });
gulp.task('generate', ['lint:js', 'lint:sass', 'build:dev', 'build:prod'], () => { gulp.task('generate', ['lint:js', 'lint:sass', 'build:dev', 'build:prod'], () => {
// Generates all *.user.css styles. // Generates all *.user.css styles.
const { bump } = require('./bump.js') const { bump } = require('./bump.js');
const klawSync = require('klaw-sync') const klawSync = require('klaw-sync');
const path = require('path') const path = require('path');
const userCss = require('usercss-creator') const userCss = require('usercss-creator');
// Define constants // Define constants
const stylesDirectory = path.join(__dirname, 'sass') const stylesDirectory = path.join(__dirname, 'sass');
const cssDirectory = path.join(__dirname, 'temp') const cssDirectory = path.join(__dirname, 'temp');
// Walk recursively through styles directory // Walk recursively through styles directory
let files = klawSync(stylesDirectory, {nodir: true}) let files = klawSync(stylesDirectory, {nodir: true});
// Filter out any files that aren't .json files // Filter out any files that aren't .json files
files = files.filter(file => path.extname(file.path) === '.json') files = files.filter(file => path.extname(file.path) === '.json');
let style let style;
let incrementType let incrementType;
for (const arg of process.argv) { for (const arg of process.argv) {
if (arg.includes('--bump')) { if (arg.includes('--bump')) {
style = arg.substring('--bump='.length, arg.length) style = arg.substring('--bump='.length, arg.length);
} }
switch (arg) { switch (arg) {
case '--major': case '--major':
incrementType = 'major' incrementType = 'major';
break break;
case '--minor': case '--minor':
incrementType = 'minor' incrementType = 'minor';
break break;
case '--patch': case '--patch':
incrementType = 'patch' incrementType = 'patch';
break break;
} }
} }
// Iterate through all .json files // Iterate through all .json files
for (const file of files) { for (const file of files) {
// Requiring the .json file as the descriptor of the style // Requiring the .json file as the descriptor of the style
const descriptor = require(file.path) const descriptor = require(file.path);
// If --bump=all then bump every style // If --bump=all then bump every style
if (style === 'all') { if (style === 'all') {
bump(file.path, incrementType) bump(file.path, incrementType);
} }
// Test if the style we want to bump is the one currently generating // Test if the style we want to bump is the one currently generating
else if (style === descriptor.folder) { else if (style === descriptor.folder) {
// Bump the style's .json with the increment type // Bump the style's .json with the increment type
bump(file.path, incrementType) bump(file.path, incrementType);
} }
// Create the css path and generate the .user.css file with the options // Create the css path and generate the .user.css file with the options
const cssPath = path.join(cssDirectory, descriptor.folder, descriptor.entry) const cssPath = path.join(cssDirectory, descriptor.folder, descriptor.entry);
const outPath = path.join(__dirname, 'css', descriptor.folder) const outPath = path.join(__dirname, 'css', descriptor.folder);
let styleFile = `@-moz-document domain('${descriptor.options.namespace}') {\n` let styleFile = `@-moz-document domain('${descriptor.options.namespace}') {\n`;
styleFile += fs.readFileSync(cssPath, {encoding: 'UTF-8'}) styleFile += fs.readFileSync(cssPath, {encoding: 'UTF-8'});
styleFile += '}' styleFile += '}';
fs.writeFileSync(cssPath, styleFile, {encoding: 'UTF-8'}) fs.writeFileSync(cssPath, styleFile, {encoding: 'UTF-8'});
userCss.create(cssPath, descriptor.options, outPath) userCss.create(cssPath, descriptor.options, outPath);
} }
}) });
gulp.task('watch', () => { gulp.task('watch', () => {
gulp.watch('sass/**/*.s*', ['lint:js', 'lint:sass', 'build:dev']) gulp.watch('sass/**/*.s*', ['lint:js', 'lint:sass', 'build:dev']);
}) });