/
codestyle.js
107 lines (93 loc) · 2.93 KB
/
codestyle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { codestyle as config } from '../config'
import fs from 'fs'
import ignore from 'ignore'
import Observable from 'zen-observable'
import path from 'path'
import prettier from 'prettier'
import pkg from '../package.json'
import { sync as glob } from 'glob'
function globVinyl(pattern) {
return glob(pattern).map(file => ({ path: path.resolve(file) }))
}
function readVinyl(vinyl) {
vinyl.contents = fs.readFileSync(vinyl.path, 'utf8')
return vinyl
}
function writeVinyl(vinyl) {
fs.writeFileSync(vinyl.path, vinyl.contents, 'utf8')
return vinyl
}
function prettierFormat(vinyl) {
const prettierOpt = Object.assign({ filepath: vinyl.path }, pkg.prettier)
const newContents = prettier.format(vinyl.contents, prettierOpt)
vinyl.prettierWasFormatted = vinyl.contents !== newContents
vinyl.contents = newContents
return vinyl
}
function prettierCheck(vinyl) {
const prettierOpt = Object.assign({ filepath: vinyl.path }, pkg.prettier)
vinyl.prettierIsInvalid = !prettier.check(vinyl.contents, prettierOpt)
return vinyl
}
function makeFilter(ignoreFile) {
const ignoreList = fs.readFileSync(ignoreFile, 'utf8').split(/\r?\n/)
const filter = ignore().add(ignoreList)
return vinyl => !filter.ignores(vinyl.path)
}
function reduceResult(prev, curr) {
const isInvalid = prev.isInvalid || 0
const wasFormatted = prev.hasFormatted || 0
return {
isInvalid: curr.prettierIsInvalid ? isInvalid + 1 : isInvalid,
hasFormatted: curr.prettierWasFormatted ? wasFormatted + 1 : wasFormatted
}
}
function printResult(result, cb) {
if (result.hasFormatted) {
console.warn(
`\nCode style: Prettier formatted ${result.hasFormatted} files.\n`
)
}
if (result.isInvalid) {
console.error(
`\nCode style: Prettier found ${result.isInvalid} incorrectly formatted files.\n`
)
process.exit(1)
return cb()
}
return cb()
}
/**
* Task: Codestyle
* Formats all code according to style throughout the app
* Corresponds to:
* $ prettier --write --ignore-path .lintignore "**\/*.{md,css,scss,js,json}"
* @param {Object} cb - Gulp callback function
* @returns {Object}
*/
export function codestyle(cb) {
return Observable.from(globVinyl(config.glob))
.filter(makeFilter(config.ignorefile))
.map(readVinyl)
.map(prettierFormat)
.map(writeVinyl)
.reduce(reduceResult)
.subscribe(result => printResult(result, cb))
}
/**
* Task: Is Valid Codestyle?
* Checks for valid codestyle usage throughout the app,
* exits with non-zero if the codestyle does not pass.
* Corresponds to:
* $ prettier --list-different --ignore-path .lintignore "**\/*.{md,css,scss,js,json}"
* @param {Object} cb - Gulp callback function
* @returns {Object}
*/
export function codestyleIsValid(cb) {
return Observable.from(globVinyl(config.glob))
.filter(makeFilter(config.ignorefile))
.map(readVinyl)
.map(prettierCheck)
.reduce(reduceResult)
.subscribe(result => printResult(result, cb))
}