@@ -40,19 +40,19 @@ const initNightmare = () => Nightmare({ // eslint-disable-line
40
40
const formatFromSizeToSize = ( from , to ) => `(${ filesize ( from ) } => ${ filesize ( to ) } )` ;
41
41
42
42
module . exports = _options => new Promise ( ( resolve , reject ) => {
43
- const options = _ . assign ( { } , defaults , _options ) ;
43
+ const options = _ . merge ( { } , defaults , _options ) ;
44
44
const startTime = process . hrtime ( ) ;
45
45
console . log ( chalk . magenta ( 'Working on components screenshots...' ) ) ;
46
- if ( options . componentsForNavPath === undefined || ! fs . existsSync ( options . componentsForNavPath ) ) {
46
+ if ( options . components . forNavPath === undefined || ! fs . existsSync ( options . components . forNavPath ) ) {
47
47
reject ( 'Can not find components nav JSON file' ) ;
48
48
return ;
49
49
}
50
- if ( ! options . componentsBuildScreenshots ) {
50
+ if ( ! options . components . buildScreenshots ) {
51
51
console . log ( chalk . dim ( 'Skipped by request.' ) ) ;
52
52
resolve ( ) ;
53
53
return ;
54
54
}
55
- const componentsForNav = JSON . parse ( fs . readFileSync ( options . componentsForNavPath , 'utf8' ) ) ;
55
+ const componentsForNav = JSON . parse ( fs . readFileSync ( options . components . forNavPath , 'utf8' ) ) ;
56
56
57
57
const nightmare = initNightmare ( ) ;
58
58
@@ -70,18 +70,15 @@ module.exports = _options => new Promise((resolve, reject) => {
70
70
71
71
for ( let comp_idx = 0 ; comp_idx < componentsForNav [ category ] . length ; comp_idx += 1 ) {
72
72
const component = componentsForNav [ category ] [ comp_idx ] ;
73
- let selector = options . screenshotSelector ;
74
- if ( component . frontMatter . screenshot && component . frontMatter . screenshot . selector ) {
75
- selector = `${ selector } ${ component . frontMatter . screenshot . selector } ` ;
76
- }
73
+ const frontMatter = _ . merge ( { } , options . components . frontMatter , component . frontMatter ) ;
77
74
78
75
// Grab the size of the component enclosing rectangle
79
76
// https://github.com/segmentio/nightmare/issues/498#issuecomment-189156529
80
77
const componentRect = yield nightmare
81
- . viewport ( options . screenshotViewportWidth , options . screenshotViewportHeight )
78
+ . viewport ( options . screenshot . viewportWidth , options . screenshot . viewportHeight )
82
79
// .wait(1000)
83
80
. goto ( `http://localhost:${ options . serverPort } ${ component . href } ` )
84
- . wait ( selector )
81
+ . wait ( frontMatter . screenshot . selector )
85
82
. evaluate ( ( _selector ) => {
86
83
// Hide scrollbar that could pop up due to .scrollTo
87
84
const sheet = document . styleSheets [ 0 ] ;
@@ -97,7 +94,7 @@ module.exports = _options => new Promise((resolve, reject) => {
97
94
} ;
98
95
}
99
96
return false ;
100
- } , selector ) ;
97
+ } , frontMatter . screenshot . selector ) ;
101
98
102
99
// Capture the component
103
100
if ( componentRect === false ) {
@@ -107,12 +104,12 @@ module.exports = _options => new Promise((resolve, reject) => {
107
104
108
105
const screenshotDir = path . dirname ( component . screenshot . path ) ;
109
106
const tmpPngObj = tmp . fileSync ( { dir : screenshotDir } ) ;
110
- componentRect . height = Math . min ( componentRect . height , options . screenshotViewportHeight ) ;
107
+ componentRect . height = Math . min ( componentRect . height , options . screenshot . viewportHeight ) ;
111
108
yield nightmare
112
109
// we can not use .screenshot() with componentRect, so constrain the viewport instead
113
110
. viewport (
114
- componentRect . width || options . screenshotViewportWidth ,
115
- componentRect . height || options . screenshotViewportHeight
111
+ componentRect . width || options . screenshot . viewportWidth ,
112
+ componentRect . height || options . screenshot . viewportHeight
116
113
) . scrollTo ( componentRect . y , componentRect . x )
117
114
// .wait(1000)
118
115
. screenshot ( tmpPngObj . name ) ; // do *not* use componentRect here or risk distortions
@@ -122,15 +119,14 @@ module.exports = _options => new Promise((resolve, reject) => {
122
119
const tmpJpegPath = path . join ( tmpJpegDirObj . name , path . basename ( component . screenshot . path ) ) ;
123
120
const screenshot = yield Jimp . read ( tmpPngObj . name ) ;
124
121
yield new Promise ( ( write_resolve , write_reject ) => {
125
- if ( component . frontMatter . screenshot === undefined ||
126
- component . frontMatter . screenshot . autocrop !== false ) {
122
+ if ( frontMatter . screenshot . autocrop ) {
127
123
screenshot . autocrop ( false ) ;
128
124
}
129
125
// Allow shrinking, up to a point
130
- const scaleHeight = screenshot . bitmap . height <= options . screenshotTargetMinHeight
131
- ? 0.0 : options . screenshotTargetMinHeight / screenshot . bitmap . height ;
132
- const scaleWidth = screenshot . bitmap . width <= options . screenshotTargetMinWidth
133
- ? 0.0 : options . screenshotTargetMinWidth / screenshot . bitmap . width ;
126
+ const scaleHeight = screenshot . bitmap . height <= options . screenshot . targetMinHeight
127
+ ? 0.0 : options . screenshot . targetMinHeight / screenshot . bitmap . height ;
128
+ const scaleWidth = screenshot . bitmap . width <= options . screenshot . targetMinWidth
129
+ ? 0.0 : options . screenshot . targetMinWidth / screenshot . bitmap . width ;
134
130
const scale = Math . max ( scaleHeight , scaleWidth ) ;
135
131
screenshot
136
132
. scale ( scale > 0 ? scale : 1.0 )
@@ -142,7 +138,7 @@ module.exports = _options => new Promise((resolve, reject) => {
142
138
// Optimize
143
139
imagemin ( [ tmpJpegPath ] , screenshotDir , {
144
140
plugins : [
145
- imageminMozjpeg ( { quality : options . mozjpegQuality } ) ,
141
+ imageminMozjpeg ( { quality : options . screenshot . mozjpegQuality } ) ,
146
142
// imageminJpegRecompress(), // this guy is useless
147
143
// imageminJpegtran(), // this guy is useless
148
144
] ,
0 commit comments