2018-06-20 13:40:10 +00:00
|
|
|
const beautify = require('gulp-cssbeautify')
|
2018-06-20 10:11:19 +00:00
|
|
|
const gulp = require('gulp')
|
|
|
|
const eslint = require('gulp-eslint')
|
2018-06-29 17:22:18 +00:00
|
|
|
const fs = require('fs')
|
2018-06-20 10:11:19 +00:00
|
|
|
const stylelint = require('gulp-stylelint')
|
|
|
|
const sass = require('gulp-sass')
|
|
|
|
|
|
|
|
gulp.task('lint:js', () => {
|
|
|
|
return gulp.src(['**/*.js','!node_modules/**'])
|
|
|
|
.pipe(eslint())
|
|
|
|
.pipe(eslint.format())
|
|
|
|
})
|
|
|
|
|
|
|
|
gulp.task('lint:sass', () => {
|
|
|
|
const options = {
|
|
|
|
reporters: [{formatter: 'string', console: true}]
|
|
|
|
}
|
|
|
|
return gulp.src('sass/**/*.sass')
|
|
|
|
.pipe(stylelint(options))
|
|
|
|
})
|
|
|
|
|
|
|
|
gulp.task('build:dev', () => {
|
|
|
|
const options = {
|
|
|
|
outputStyle: 'compressed'
|
|
|
|
}
|
2018-06-29 17:22:18 +00:00
|
|
|
const bOptions = {
|
|
|
|
indent: ' ',
|
|
|
|
autosemicolon: true
|
|
|
|
}
|
2018-06-20 10:11:19 +00:00
|
|
|
return gulp.src('sass/**/*.sass')
|
|
|
|
.pipe(sass(options).on('error', sass.logError))
|
2018-06-29 17:22:18 +00:00
|
|
|
.pipe(beautify(bOptions))
|
2018-06-20 10:11:19 +00:00
|
|
|
.pipe(gulp.dest('temp'))
|
|
|
|
})
|
|
|
|
|
|
|
|
gulp.task('build:prod', () => {
|
|
|
|
const options = {
|
|
|
|
outputStyle: 'compressed'
|
|
|
|
}
|
2018-06-20 13:40:10 +00:00
|
|
|
const bOptions = {
|
2018-06-28 22:15:47 +00:00
|
|
|
indent: ' ',
|
|
|
|
autosemicolon: true
|
2018-06-20 13:40:10 +00:00
|
|
|
}
|
2018-06-20 10:11:19 +00:00
|
|
|
return gulp.src('sass/**/*.sass')
|
|
|
|
.pipe(sass(options).on('error', sass.logError))
|
2018-06-20 13:40:10 +00:00
|
|
|
.pipe(beautify(bOptions))
|
2018-06-20 10:11:19 +00:00
|
|
|
.pipe(gulp.dest('css'))
|
|
|
|
})
|
|
|
|
|
2018-06-29 17:22:18 +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.
|
|
|
|
const { bump } = require('./bump.js')
|
|
|
|
const klawSync = require('klaw-sync')
|
|
|
|
const path = require('path')
|
|
|
|
const userCss = require('usercss-creator')
|
|
|
|
|
|
|
|
// Define constants
|
|
|
|
const stylesDirectory = path.join(__dirname, 'sass')
|
2018-06-29 17:22:18 +00:00
|
|
|
const cssDirectory = path.join(__dirname, 'temp')
|
2018-06-20 10:11:19 +00:00
|
|
|
|
|
|
|
// Walk recursively through styles directory
|
|
|
|
let files = klawSync(stylesDirectory, {nodir: true})
|
|
|
|
|
|
|
|
// Filter out any files that aren't .json files
|
|
|
|
files = files.filter(file => path.extname(file.path) === '.json')
|
|
|
|
|
|
|
|
let style
|
|
|
|
let incrementType
|
|
|
|
|
|
|
|
for (const arg of process.argv) {
|
|
|
|
if (arg.includes('--bump')) {
|
|
|
|
style = arg.substring('--bump='.length, arg.length)
|
|
|
|
}
|
|
|
|
switch (arg) {
|
|
|
|
case '--major':
|
|
|
|
incrementType = 'major'
|
|
|
|
break
|
|
|
|
case '--minor':
|
|
|
|
incrementType = 'minor'
|
|
|
|
break
|
|
|
|
case '--patch':
|
|
|
|
incrementType = 'patch'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through all .json files
|
|
|
|
for (const file of files) {
|
|
|
|
// Requiring the .json file as the descriptor of the style
|
|
|
|
const descriptor = require(file.path)
|
2018-06-28 22:15:47 +00:00
|
|
|
// If --bump=all then bump every style
|
|
|
|
if (style === 'all') {
|
|
|
|
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
|
2018-06-28 22:15:47 +00:00
|
|
|
else if (style === descriptor.folder) {
|
2018-06-20 10:11:19 +00:00
|
|
|
// Bump the style's .json with the increment type
|
|
|
|
bump(file.path, incrementType)
|
|
|
|
}
|
|
|
|
// Create the css path and generate the .user.css file with the options
|
|
|
|
const cssPath = path.join(cssDirectory, descriptor.folder, descriptor.entry)
|
2018-06-29 17:37:26 +00:00
|
|
|
const outPath = path.join(__dirname, 'css', descriptor.folder)
|
|
|
|
let styleFile = `@-moz-document domain('${descriptor.options.namespace}') {\n`
|
2018-06-29 17:22:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
gulp.task('watch', () => {
|
|
|
|
gulp.watch('sass/**/*.sass', ['lint:js', 'lint:sass', 'build:dev'])
|
|
|
|
})
|