@@ -3,58 +3,69 @@ const filesize = require('filesize');
3
3
const Image = require ( '../../../logic/entities/Image' ) ;
4
4
const DEFAULTS = require ( '../../cli/defaults' ) ;
5
5
6
+ const NONE_TAG = '<none>' ;
6
7
7
- const extractFieldsForImageEntity = ( image , tag ) => {
8
- const newImage = {
9
- name : image . imageDisplayName ,
10
- size : filesize ( image . size ) ,
11
- _id : image . _id ,
12
- annotations : _ . get ( image , 'metadata' , { } ) ,
13
- tagId : tag . _id ,
14
- created : image . created ? new Date ( image . created ) : undefined ,
15
- } ;
16
- newImage . id = image . internalImageId ? image . internalImageId . substring ( 0 , 12 ) : '\b' ;
17
- if ( _ . isEqual ( tag , '<none>' ) ) {
18
- newImage . tag = tag ;
19
- newImage . pull = '' ;
20
- } else {
21
- newImage . tag = tag . tag ;
22
- newImage . pull = `${ tag . registry } /${ tag . repository } :${ tag . tag } ` ;
8
+ class ImagesHelper {
9
+ constructor ( ) {
10
+ this . extractFieldsForImageEntity = this . extractFieldsForImageEntity . bind ( this ) ;
11
+ this . extractImages = this . extractImages . bind ( this ) ;
23
12
}
24
- return newImage ;
25
- } ;
26
13
27
14
28
- const extractImages = ( images , filterRegistries ) => {
29
- if ( ! _ . isArray ( images ) ) {
30
- images = [ images ] ;
15
+ extractFieldsForImageEntity ( image , tag ) {
16
+ const newImage = {
17
+ name : image . imageDisplayName ,
18
+ size : filesize ( image . size ) ,
19
+ _id : image . _id ,
20
+ annotations : _ . get ( image , 'metadata' , { } ) ,
21
+ tagId : tag . _id ,
22
+ created : image . created ? new Date ( image . created ) : undefined ,
23
+ } ;
24
+ newImage . id = image . internalImageId ? image . internalImageId . substring ( 0 , 12 ) : '\b' ;
25
+ if ( _ . isEqual ( tag , NONE_TAG ) ) {
26
+ newImage . tag = tag ;
27
+ newImage . pull = '' ;
28
+ } else {
29
+ newImage . tag = tag . tag ;
30
+ newImage . pull = `${ tag . registry } /${ tag . repository } :${ tag . tag } ` ;
31
+ }
32
+ return newImage ;
31
33
}
32
- return _ . flatten ( images . map ( ( image ) => {
33
- const res = [ ] ;
34
- let addedCfCrTag = false ;
35
- _ . forEach ( image . tags , ( tag ) => {
36
- if ( _ . isEqual ( tag . tag , 'volume' ) ) {
37
- addedCfCrTag = true ;
38
- return ;
39
- }
40
- // in case we are filtering by registries, ignore the image if it is not from the registires list
41
- if ( filterRegistries && filterRegistries . indexOf ( tag . registry ) === - 1 ) {
42
- return ;
43
- }
44
- if ( DEFAULTS . CODEFRESH_REGISTRIES . indexOf ( tag . registry ) !== - 1 ) {
45
- addedCfCrTag = true ;
46
- }
47
- const data = extractFieldsForImageEntity ( image , tag ) ;
48
- res . push ( new Image ( data ) ) ;
49
- } ) ;
50
- if ( _ . isEmpty ( image . tags ) || ! addedCfCrTag ) {
51
- const data = extractFieldsForImageEntity ( image , '<none>' ) ;
52
- res . push ( new Image ( data ) ) ;
34
+
35
+
36
+ extractImages ( images , filterRegistries ) {
37
+ if ( ! _ . isArray ( images ) ) {
38
+ images = [ images ] ; // eslint-disable-line no-param-reassign
53
39
}
54
- return res ;
55
- } ) ) ;
56
- } ;
40
+ return _ . chain ( images )
41
+ . map ( ( image ) => {
42
+ const res = [ ] ;
43
+ let addedCfCrTag = false ;
44
+ _ . forEach ( image . tags , ( tag ) => {
45
+ if ( _ . isEqual ( tag . tag , 'volume' ) ) {
46
+ addedCfCrTag = true ;
47
+ return ;
48
+ }
49
+ // in case we are filtering by registries, ignore the image if it is not from the registires list
50
+ if ( filterRegistries && ! _ . includes ( filterRegistries , tag . registry ) ) {
51
+ return ;
52
+ }
53
+ if ( _ . includes ( DEFAULTS . CODEFRESH_REGISTRIES , tag . registry ) ) {
54
+ addedCfCrTag = true ;
55
+ }
56
+ const data = this . extractFieldsForImageEntity ( image , tag ) ;
57
+ res . push ( new Image ( data ) ) ;
58
+ } ) ;
59
+ if ( _ . isEmpty ( image . tags ) || ! addedCfCrTag ) {
60
+ const data = this . extractFieldsForImageEntity ( image , NONE_TAG ) ;
61
+ res . push ( new Image ( data ) ) ;
62
+ }
63
+ return res ;
64
+ } )
65
+ . flatten ( )
66
+ . orderBy ( [ 'info.created' ] , [ 'desc' ] )
67
+ . value ( ) ;
68
+ }
69
+ }
57
70
58
- module . exports = {
59
- extractImages,
60
- } ;
71
+ module . exports = new ImagesHelper ( ) ;
0 commit comments