1
Fork 0
userstyles/gulpfile.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

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