11const PackageManager = {
2- Yarn1 : `Yarn Classic` ,
3- Yarn2 : `Yarn` ,
4- Npm : `npm` ,
5- Pnpm : `pnpm` ,
2+ Yarn1 : `Yarn Classic` ,
3+ Yarn2 : `Yarn` ,
4+ Npm : `npm` ,
5+ Pnpm : `pnpm` ,
66}
77
88module . exports = {
99 name : `flatpak-builder` ,
1010 factory : require => {
1111 const { BaseCommand } = require ( `@yarnpkg/cli` ) ;
12+ const { parseSyml } = require ( '@yarnpkg/parsers' ) ;
1213 const { Configuration, Manifest, scriptUtils, structUtils, tgzUtils, execUtils, miscUtils, hashUtils } = require ( '@yarnpkg/core' )
1314 const { Filename, ZipFS, npath, ppath, PortablePath, xfs } = require ( '@yarnpkg/fslib' ) ;
1415 const { getLibzipPromise } = require ( '@yarnpkg/libzip' ) ;
@@ -20,157 +21,158 @@ module.exports = {
2021
2122 // from https://github.com/yarnpkg/berry/blob/%40yarnpkg/shell/3.2.3/packages/plugin-essentials/sources/commands/set/version.ts#L194
2223 async function setPackageManager ( projectCwd ) {
23- const bundleVersion = YarnVersion ;
24+ const bundleVersion = YarnVersion ;
2425
25- const manifest = ( await Manifest . tryFind ( projectCwd ) ) || new Manifest ( ) ;
26-
27- if ( bundleVersion && miscUtils . isTaggedYarnVersion ( bundleVersion ) ) {
28- manifest . packageManager = `yarn@${ bundleVersion } ` ;
29- const data = { } ;
30- manifest . exportTo ( data ) ;
26+ const manifest = ( await Manifest . tryFind ( projectCwd ) ) || new Manifest ( ) ;
3127
32- const path = ppath . join ( projectCwd , Manifest . fileName ) ;
33- const content = `${ JSON . stringify ( data , null , manifest . indent ) } \n` ;
28+ if ( bundleVersion && miscUtils . isTaggedYarnVersion ( bundleVersion ) ) {
29+ manifest . packageManager = `yarn@${ bundleVersion } ` ;
30+ const data = { } ;
31+ manifest . exportTo ( data ) ;
3432
35- await xfs . changeFilePromise ( path , content , {
36- automaticNewlines : true ,
37- } ) ;
38- }
33+ const path = ppath . join ( projectCwd , Manifest . fileName ) ;
34+ const content = `${ JSON . stringify ( data , null , manifest . indent ) } \n` ;
35+
36+ await xfs . changeFilePromise ( path , content , {
37+ automaticNewlines : true ,
38+ } ) ;
39+ }
3940 }
4041
4142 // func from https://github.com/yarnpkg/berry/blob/%40yarnpkg/shell/3.2.3/packages/yarnpkg-core/sources/scriptUtils.ts#L215
42- async function prepareExternalProject ( cwd , outputPath , { configuration, locator, stdout, yarn_v1, workspace= null } ) {
43- const devirtualizedLocator = locator && structUtils . isVirtualLocator ( locator )
44- ? structUtils . devirtualizeLocator ( locator )
45- : locator ;
46-
47- const name = devirtualizedLocator
48- ? structUtils . stringifyLocator ( devirtualizedLocator )
49- : `an external project` ;
50-
51- const stderr = stdout ;
52-
53- stdout . write ( `Packing ${ name } from sources\n` ) ;
54-
55- const packageManagerSelection = await scriptUtils . detectPackageManager ( cwd ) ;
56- let effectivePackageManager ;
57- if ( packageManagerSelection !== null ) {
58- stdout . write ( `Using ${ packageManagerSelection . packageManager } for bootstrap. Reason: ${ packageManagerSelection . reason } \n\n` ) ;
59- effectivePackageManager = packageManagerSelection . packageManager ;
60- } else {
61- stdout . write ( `No package manager configuration detected; defaulting to Yarn\n\n` ) ;
62- effectivePackageManager = PackageManager . Yarn2 ;
63- }
64- if ( effectivePackageManager === PackageManager . Pnpm ) {
65- effectivePackageManager = PackageManager . Npm ;
66- }
43+ async function prepareExternalProject ( cwd , outputPath , { configuration, locator, stdout, yarn_v1, workspace = null } ) {
44+ const devirtualizedLocator = locator && structUtils . isVirtualLocator ( locator )
45+ ? structUtils . devirtualizeLocator ( locator )
46+ : locator ;
47+
48+ const name = devirtualizedLocator
49+ ? structUtils . stringifyLocator ( devirtualizedLocator )
50+ : `an external project` ;
51+
52+ const stderr = stdout ;
53+
54+ stdout . write ( `Packing ${ name } from sources\n` ) ;
55+
56+ const packageManagerSelection = await scriptUtils . detectPackageManager ( cwd ) ;
57+ let effectivePackageManager ;
58+ if ( packageManagerSelection !== null ) {
59+ stdout . write ( `Using ${ packageManagerSelection . packageManager } for bootstrap. Reason: ${ packageManagerSelection . reason } \n\n` ) ;
60+ effectivePackageManager = packageManagerSelection . packageManager ;
61+ } else {
62+ stdout . write ( `No package manager configuration detected; defaulting to Yarn\n\n` ) ;
63+ effectivePackageManager = PackageManager . Yarn2 ;
64+ }
65+ if ( effectivePackageManager === PackageManager . Pnpm ) {
66+ effectivePackageManager = PackageManager . Npm ;
67+ }
6768
68- const workflows = new Map ( [
69- [ PackageManager . Yarn1 , async ( ) => {
70- const workspaceCli = workspace !== null
71- ? [ `workspace` , workspace ]
72- : [ ] ;
69+ const workflows = new Map ( [
70+ [ PackageManager . Yarn1 , async ( ) => {
71+ const workspaceCli = workspace !== null
72+ ? [ `workspace` , workspace ]
73+ : [ ] ;
7374
74- await setPackageManager ( cwd ) ;
75+ await setPackageManager ( cwd ) ;
7576
76- await Configuration . updateConfiguration ( cwd , {
77- yarnPath : yarn_v1 ,
78- } ) ;
77+ await Configuration . updateConfiguration ( cwd , {
78+ yarnPath : yarn_v1 ,
79+ } ) ;
7980
80- await xfs . appendFilePromise ( ppath . join ( cwd , `.npmignore` ) , `/.yarn\n` ) ;
81-
82- const pack = await execUtils . pipevp ( `yarn` , [ ...workspaceCli , `pack` , `--filename` , npath . fromPortablePath ( outputPath ) ] , { cwd, stdout, stderr} ) ;
83- if ( pack . code !== 0 )
84- return pack . code ;
85-
86- return 0 ;
87- } ] ,
88- [ PackageManager . Yarn2 , async ( ) => {
89- const workspaceCli = workspace !== null
90- ? [ `workspace` , workspace ]
91- : [ ] ;
92- const lockfilePath = ppath . join ( cwd , Filename . lockfile ) ;
93- if ( ! ( await xfs . existsPromise ( lockfilePath ) ) )
94- await xfs . writeFilePromise ( lockfilePath , `` ) ;
95-
96- const pack = await execUtils . pipevp ( `yarn` , [ ...workspaceCli , `pack` , `--filename` , npath . fromPortablePath ( outputPath ) ] , { cwd, stdout, stderr} ) ;
97- if ( pack . code !== 0 )
98- return pack . code ;
99- return 0 ;
100- } ] ,
101- [ PackageManager . Npm , async ( ) => {
102- const workspaceCli = workspace !== null
103- ? [ `--workspace` , workspace ]
104- : [ ] ;
105- const packStream = new PassThrough ( ) ;
106- const packPromise = miscUtils . bufferStream ( packStream ) ;
107- const pack = await execUtils . pipevp ( `npm` , [ `pack` , `--silent` , ...workspaceCli ] , { cwd, stdout : packStream , stderr} ) ;
108- if ( pack . code !== 0 )
109- return pack . code ;
110-
111- const packOutput = ( await packPromise ) . toString ( ) . trim ( ) . replace ( / ^ .* \n / s, `` ) ;
112- const packTarget = ppath . resolve ( cwd , npath . toPortablePath ( packOutput ) ) ;
113- await xfs . renamePromise ( packTarget , outputPath ) ;
114- return 0 ;
115- } ] ,
116- ] ) ;
117- const workflow = workflows . get ( effectivePackageManager ) ;
118- const code = await workflow ( ) ;
119- if ( code === 0 || typeof code === `undefined` )
120- return ;
121- else
122- throw `Packing the package failed (exit code ${ code } )` ;
81+ await xfs . appendFilePromise ( ppath . join ( cwd , `.npmignore` ) , `/.yarn\n` ) ;
82+
83+ const pack = await execUtils . pipevp ( `yarn` , [ ...workspaceCli , `pack` , `--filename` , npath . fromPortablePath ( outputPath ) ] , { cwd, stdout, stderr } ) ;
84+ if ( pack . code !== 0 )
85+ return pack . code ;
86+
87+ return 0 ;
88+ } ] ,
89+ [ PackageManager . Yarn2 , async ( ) => {
90+ const workspaceCli = workspace !== null
91+ ? [ `workspace` , workspace ]
92+ : [ ] ;
93+ const lockfilePath = ppath . join ( cwd , Filename . lockfile ) ;
94+ if ( ! ( await xfs . existsPromise ( lockfilePath ) ) )
95+ await xfs . writeFilePromise ( lockfilePath , `` ) ;
96+
97+ const pack = await execUtils . pipevp ( `yarn` , [ ...workspaceCli , `pack` , `--filename` , npath . fromPortablePath ( outputPath ) ] , { cwd, stdout, stderr } ) ;
98+ if ( pack . code !== 0 )
99+ return pack . code ;
100+ return 0 ;
101+ } ] ,
102+ [ PackageManager . Npm , async ( ) => {
103+ const workspaceCli = workspace !== null
104+ ? [ `--workspace` , workspace ]
105+ : [ ] ;
106+ const packStream = new PassThrough ( ) ;
107+ const packPromise = miscUtils . bufferStream ( packStream ) ;
108+ const pack = await execUtils . pipevp ( `npm` , [ `pack` , `--silent` , ...workspaceCli ] , { cwd, stdout : packStream , stderr } ) ;
109+ if ( pack . code !== 0 )
110+ return pack . code ;
111+
112+ const packOutput = ( await packPromise ) . toString ( ) . trim ( ) . replace ( / ^ .* \n / s, `` ) ;
113+ const packTarget = ppath . resolve ( cwd , npath . toPortablePath ( packOutput ) ) ;
114+ await xfs . renamePromise ( packTarget , outputPath ) ;
115+ return 0 ;
116+ } ] ,
117+ ] ) ;
118+ const workflow = workflows . get ( effectivePackageManager ) ;
119+ const code = await workflow ( ) ;
120+ if ( code === 0 || typeof code === `undefined` )
121+ return ;
122+ else
123+ throw `Packing the package failed (exit code ${ code } )` ;
123124 }
124125
125126 class convertToZipCommand extends BaseCommand {
126127 static paths = [ [ `convertToZip` ] ] ;
127- yarn_v1 = Option . String ( { required : true } ) ;
128+ yarn_v1 = Option . String ( { required : true } ) ;
128129
129130 async execute ( ) {
130131 const configuration = await Configuration . find ( this . context . cwd ,
131132 this . context . plugins ) ;
132- // const lockfile = configuration.get(' lockfileFilename' );
133- const cacheFolder = configuration . get ( 'cacheFolder' ) ;
133+ const lockfilePath = ppath . join ( this . context . cwd , configuration . get ( ` lockfileFilename` ) ) ;
134+ const cacheFolder = ` ${ configuration . get ( 'globalFolder' ) } /cache` ;
134135 const locatorFolder = `${ cacheFolder } /locator` ;
135136
136137 const compressionLevel = configuration . get ( `compressionLevel` ) ;
137138 const stdout = this . context . stdout ;
138139 const gitChecksumPatches = [ ] ; // {name:, oriHash:, newHash:}
139140
140- async function patchLockfileChecksum ( cwd , configuration , patches ) {
141- const lockfilePath = ppath . join ( cwd , configuration . get ( `lockfileFilename` ) ) ;
141+ async function patchLockfileChecksum ( lockfilePath , patches ) {
142+ let currentContent = `` ;
143+ try {
144+ currentContent = await xfs . readFilePromise ( lockfilePath , `utf8` ) ;
145+ } catch ( error ) {
146+ }
147+ const newContent = patches . reduce ( ( acc , item , i ) => {
148+ stdout . write ( `patch '${ item . name } ' checksum:\n-${ item . oriHash } \n+${ item . newHash } \n\n\n` ) ;
149+ const regex = new RegExp ( item . oriHash , "g" ) ;
150+ return acc . replace ( regex , item . newHash ) ;
151+ } , currentContent ) ;
142152
143- let currentContent = `` ;
144- try {
145- currentContent = await xfs . readFilePromise ( lockfilePath , `utf8` ) ;
146- } catch ( error ) {
147- }
148- const newContent = patches . reduce ( ( acc , item , i ) => {
149- stdout . write ( `patch '${ item . name } ' checksum:\n-${ item . oriHash } \n+${ item . newHash } \n\n\n` ) ;
150- const regex = new RegExp ( item . oriHash , "g" ) ;
151- return acc . replace ( regex , item . newHash ) ;
152- } , currentContent ) ;
153-
154- await xfs . writeFilePromise ( lockfilePath , newContent ) ;
153+ await xfs . writeFilePromise ( lockfilePath , newContent ) ;
155154 }
156155
156+ async function getLockFileMeta ( lockfilePath ) {
157+ const content = await xfs . readFilePromise ( lockfilePath , `utf8` ) ;
158+ const parsed = parseSyml ( content ) ;
159+ return parsed . __metadata ;
160+ }
157161
158- stdout . write ( `yarn cacheFolder: ${ cacheFolder } \n` ) ;
162+ const lockMeta = await getLockFileMeta ( lockfilePath ) ;
163+ stdout . write ( `yarn lock: ${ lockfilePath } \n` ) ;
164+ stdout . write ( `yarn lock version: ${ lockMeta . version } \n` ) ;
165+ stdout . write ( `yarn lock cacheKey: ${ lockMeta . cacheKey } \n` ) ;
159166
160167 const convertToZip = async ( tgz , target , opts ) => {
161- const { compressionLevel, ...bufferOpts } = opts ;
162- const zipFs = new ZipFS ( target , {
163- create : true ,
164- libzip : await getLibzipPromise ( ) ,
165- level : compressionLevel
166- } ) ;
167- const tgzBuffer = fs . readFileSync ( tgz ) ;
168- await tgzUtils . extractArchiveTo ( tgzBuffer , zipFs ,
169- bufferOpts ) ;
170- zipFs . saveAndClose ( ) ;
168+ const tgzBuf = await xfs . readFilePromise ( tgz ) ;
169+ const fs = await tgzUtils . convertToZip ( tgzBuf , opts ) ;
170+ fs . discardAndClose ( ) ;
171+ await xfs . copyFilePromise ( fs . path , target ) ;
172+ await xfs . unlinkPromise ( fs . path ) ;
171173 }
172174
173- stdout . write ( `converting cache to zip\n` ) ;
175+ stdout . write ( `converting tgz to zip: ${ cacheFolder } \n` ) ;
174176
175177 const files = fs . readdirSync ( locatorFolder ) ;
176178 const tasks = [ ]
@@ -187,11 +189,11 @@ module.exports = {
187189 const sha = match [ 2 ] ;
188190 let checksum ;
189191
190- if ( entry_type === 'tgz' ) {
192+ if ( entry_type === 'tgz' ) {
191193 resolution = Buffer . from ( match [ 1 ] , 'base64' ) . toString ( ) ;
192194 locator = structUtils . parseLocator ( resolution , true ) ;
193195 }
194- else if ( entry_type === 'git' ) {
196+ else if ( entry_type === 'git' ) {
195197 const gitJson = JSON . parse ( fs . readFileSync ( tgzFile , 'utf8' ) ) ;
196198
197199 resolution = gitJson . resolution ;
@@ -201,24 +203,25 @@ module.exports = {
201203 const repoPathRel = gitJson . repo_dir_rel ;
202204
203205 const cloneTarget = `${ cacheFolder } /${ repoPathRel } ` ;
204-
206+
205207 const repoUrlParts = gitUtils . splitRepoUrl ( locator . reference ) ;
206208 const packagePath = ppath . join ( cloneTarget , `package.tgz` ) ;
207209
208210 await prepareExternalProject ( cloneTarget , packagePath , {
209- configuration : configuration ,
210- stdout,
211- workspace : repoUrlParts . extra . workspace ,
212- locator,
213- yarn_v1 : this . yarn_v1 ,
211+ configuration : configuration ,
212+ stdout,
213+ workspace : repoUrlParts . extra . workspace ,
214+ locator,
215+ yarn_v1 : this . yarn_v1 ,
214216 } ) ;
215217
216218 tgzFile = packagePath ;
217219
218220 }
219221 const filename =
220- `${ structUtils . slugifyLocator ( locator ) } -${ sha } .zip` ;
222+ `${ structUtils . slugifyLocator ( locator ) } -${ lockMeta . cacheKey } .zip` ;
221223 const targetFile = `${ cacheFolder } /${ filename } `
224+
222225 tasks . push ( async ( ) => {
223226 await convertToZip ( tgzFile , targetFile , {
224227 compressionLevel : compressionLevel ,
@@ -227,28 +230,26 @@ module.exports = {
227230 } ) ;
228231
229232 if ( entry_type === 'git' ) {
230- const file_checksum = await hashUtils . checksumFile ( targetFile ) ;
231-
232- if ( file_checksum !== checksum ) {
233- const newSha = file_checksum . slice ( 0 , 10 ) ;
234- const newTarget = `${ cacheFolder } /${ structUtils . slugifyLocator ( locator ) } -${ newSha } .zip` ;
235- fs . renameSync ( targetFile , newTarget ) ;
236-
237- gitChecksumPatches . push ( {
238- name : locator . name ,
239- oriHash : checksum ,
240- newHash : file_checksum ,
241- } ) ;
242- }
233+ const file_checksum = await hashUtils . checksumFile ( targetFile ) ;
234+
235+ if ( file_checksum !== checksum ) {
236+ const newSha = file_checksum . slice ( 0 , 10 ) ;
237+ const newTarget = `${ cacheFolder } /${ structUtils . slugifyLocator ( locator ) } -${ lockMeta . cacheKey } .zip` ;
238+ fs . renameSync ( targetFile , newTarget ) ;
239+
240+ gitChecksumPatches . push ( {
241+ name : locator . name ,
242+ oriHash : checksum ,
243+ newHash : file_checksum ,
244+ } ) ;
245+ }
243246 }
244247 } ) ;
245248 }
246-
247- while ( tasks . length ) {
248- await Promise . all ( tasks . splice ( 0 , 128 ) . map ( t => t ( ) ) ) ;
249- }
250249
251- patchLockfileChecksum ( this . context . cwd , configuration , gitChecksumPatches ) ;
250+ await Promise . all ( tasks . map ( t => t ( ) ) ) ;
251+
252+ patchLockfileChecksum ( lockfilePath , gitChecksumPatches ) ;
252253 stdout . write ( `converting finished\n` ) ;
253254 }
254255 }
@@ -259,4 +260,3 @@ module.exports = {
259260 } ;
260261 }
261262} ;
262-
0 commit comments