Created
June 1, 2020 17:27
-
-
Save bdukes/bdb5c087e8e2e64348f1d5f277cd8dc1 to your computer and use it in GitHub Desktop.
.prettierignore for gulp-plugin-prettier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-env node*/ | |
'use strict'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const _ = require('lodash'); | |
const ignore = require('ignore'); | |
const gulpFilter = require('gulp-filter'); | |
const filters = new Map(); | |
function getFilter(filterFilePath) { | |
if (filters.has(filterFilePath)) { | |
return filters.get(filterFilePath); | |
} | |
const filterBuilder = ignore(); | |
if (fs.existsSync(filterFilePath)) { | |
filterBuilder.add(fs.readFileSync(filterFilePath).toString()); | |
} | |
filters.set(filterFilePath, filterBuilder.createFilter()); | |
return filters.get(filterFilePath); | |
} | |
function getRelativePath(file) { | |
return path.relative(process.cwd(), file.path); | |
} | |
module.exports = { | |
getFilter(filterFilePath = '.gitignore') { | |
return getFilter(filterFilePath); | |
}, | |
filterStream(filterFilePath = '.gitignore', options = undefined) { | |
const filterFile = _.flow([getRelativePath, getFilter(filterFilePath)]); | |
return gulpFilter(filterFile, options); | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gulp = require('gulp'); | |
const gulpif = require('gulp-if'); | |
const through = require('through2'); | |
const gitignoreFilter = require('./gitignore-filter'); | |
const prettier = require('gulp-plugin-prettier'); | |
function prettier() { | |
const prettierFilter = gitignoreFilter.filterStream('.prettierignore', { | |
restore: args.fix, | |
}); | |
const prettierOptions = { | |
reporter: args.fix ? 'none' : 'error' | |
}; | |
return gulp.src('src/**/*.js') | |
.pipe(prettierFilter) | |
.pipe(prettier.format(undefined, prettierOptions)) | |
.pipe(prettierFilter.restore || through.obj()) | |
.pipe(gulpif(args.fix, gulp.dest('src/'))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment