@@ -5,7 +5,8 @@ const fs = require('fs')
5
5
const { promises : fsp } = fs
6
6
const { Octokit } = require ( '@octokit/rest' )
7
7
const path = require ( 'path' )
8
- const { Transform } = require ( 'stream' )
8
+ const { pipeline, Transform, Writable } = require ( 'stream' )
9
+ const forEach = ( write , final ) => new Writable ( { objectMode : true , write, final } )
9
10
const map = ( transform , flush = undefined ) => new Transform ( { objectMode : true , transform, flush } )
10
11
const vfs = require ( 'vinyl-fs' )
11
12
const zip = require ( 'gulp-vinyl-zip' )
@@ -37,15 +38,27 @@ function collectReleases ({ octokit, owner, repo, filter, page = 1, accum = [] }
37
38
}
38
39
39
40
function versionBundle ( bundleFile , tagName ) {
41
+ let uiDescriptorFound
40
42
return new Promise ( ( resolve , reject ) =>
41
43
vfs
42
44
. src ( bundleFile )
43
45
. pipe ( zip . src ( ) . on ( 'error' , reject ) )
44
46
. pipe (
45
47
map (
46
- ( file , enc , next ) => next ( null , file ) ,
48
+ ( file , _ , next ) => {
49
+ if ( file . path === 'ui.yml' && ( uiDescriptorFound = true ) && file . isStream ( ) ) {
50
+ const buffer = [ ]
51
+ pipeline (
52
+ file . contents ,
53
+ forEach ( ( chunk , _ , done ) => buffer . push ( chunk ) && done ( ) ) ,
54
+ ( err ) => ( err ? next ( err ) : next ( null , addVersionEntry ( file , tagName , Buffer . concat ( buffer ) ) ) )
55
+ )
56
+ } else {
57
+ next ( null , file )
58
+ }
59
+ } ,
47
60
function ( done ) {
48
- this . push ( new File ( { path : 'ui.yml' , contents : Buffer . from ( `version: ${ tagName } \n` ) } ) )
61
+ if ( ! uiDescriptorFound ) this . push ( addVersionEntry ( new File ( { path : 'ui.yml' } ) , tagName ) )
49
62
done ( )
50
63
}
51
64
)
@@ -55,6 +68,13 @@ function versionBundle (bundleFile, tagName) {
55
68
)
56
69
}
57
70
71
+ function addVersionEntry ( file , tagName , contents = Buffer . alloc ( 0 ) ) {
72
+ let versionEntry = `version: ${ tagName } \n`
73
+ if ( contents . length && contents [ contents . length - 1 ] !== 10 ) versionEntry = `\n${ versionEntry } `
74
+ file . contents = Buffer . concat ( [ contents , Buffer . from ( versionEntry ) ] )
75
+ return file
76
+ }
77
+
58
78
module . exports = ( dest , bundleName , owner , repo , ref , token , updateBranch ) => async ( ) => {
59
79
const octokit = new Octokit ( { auth : `token ${ token } ` } )
60
80
let variant = ref . replace ( / ^ r e f s \/ h e a d s \/ / , '' )
0 commit comments