|
1 | 1 | const fs = require('fs'); |
2 | 2 | const { resolve, join } = require('path'); |
3 | 3 | const { homedir, arch } = require('os'); |
| 4 | +let { pipeline } = require('stream'); |
| 5 | +const { createGunzip } = require('zlib'); |
| 6 | +const { promisify } = require('util'); |
4 | 7 |
|
5 | 8 | const _ = require('lodash'); |
6 | 9 | const rp = require('request-promise'); |
7 | 10 | const request = require('request'); |
8 | | -const decompress = require('decompress'); |
9 | | -const decompressTargz = require('decompress-targz'); |
10 | | -const decompressUnzip = require('decompress-unzip'); |
| 11 | +const tarStream = require('tar-stream'); |
| 12 | +const zip = require('zip'); |
11 | 13 | const compareVersions = require('compare-versions'); |
12 | 14 |
|
| 15 | +pipeline = promisify(pipeline); |
| 16 | + |
13 | 17 | const CODEFRESH_PATH = resolve(homedir(), '.Codefresh'); |
14 | 18 |
|
| 19 | +async function unzipFile(zipPath, outputPath) { |
| 20 | + const zipBuffer = await fs.promises.readFile(zipPath); |
| 21 | + const zr = zip.Reader(zipBuffer); |
| 22 | + |
| 23 | + const fileWrites = []; |
| 24 | + zr.forEach((entry) => { |
| 25 | + if (!entry.isFile()) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const outputFilePath = join(outputPath, entry.getName()); |
| 30 | + fileWrites.push(fs.promises.writeFile(outputFilePath, entry.getData(), { mode: entry.getMode() })); |
| 31 | + }); |
| 32 | + |
| 33 | + return Promise.all(fileWrites); |
| 34 | +} |
| 35 | + |
| 36 | +async function untarFile(tarPath, outputPath) { |
| 37 | + const zipFile = fs.createReadStream(tarPath); |
| 38 | + const unzipStream = createGunzip(); |
| 39 | + const extract = tarStream.extract(); |
| 40 | + |
| 41 | + extract.on('entry', async (headers, stream, next) => { |
| 42 | + if (headers.type !== 'file') { |
| 43 | + return next(); |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + const outputFilePath = join(outputPath, headers.name); |
| 48 | + const outputFile = fs.createWriteStream(outputFilePath, { mode: headers.mode }); |
| 49 | + await pipeline(stream, outputFile); |
| 50 | + return next(); |
| 51 | + } catch (error) { |
| 52 | + return next(error); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + await pipeline( |
| 57 | + zipFile, |
| 58 | + unzipStream, |
| 59 | + extract, |
| 60 | + ); |
| 61 | +} |
| 62 | + |
15 | 63 | const prepareSpwan = async ({ name, repoName, pathName, branch = 'master', excludeVersionPrefix = false, events }) => { |
16 | 64 | const dirPath = join(CODEFRESH_PATH, name); |
17 | 65 | const versionPath = join(CODEFRESH_PATH, name, 'version.txt'); |
| 66 | + const outputPath = join(CODEFRESH_PATH, name); |
18 | 67 | const filePath = join(CODEFRESH_PATH, name, repoName); |
19 | 68 | const fullPath = pathName ? join(repoName, branch, pathName) : join(repoName, branch); |
20 | 69 | const versionUrl = `https://raw.githubusercontent.com/codefresh-io/${fullPath}/VERSION`; |
@@ -70,25 +119,15 @@ const prepareSpwan = async ({ name, repoName, pathName, branch = 'master', exclu |
70 | 119 | }); |
71 | 120 | } |
72 | 121 |
|
73 | | - req.pipe(fs.createWriteStream(zipPath)); |
74 | | - const p = new Promise((resolveFn, rejectFn) => { |
75 | | - req.on('end', () => { |
76 | | - decompress(zipPath, join(homedir(), '.Codefresh', name), { |
77 | | - plugins: [ |
78 | | - decompressTargz(), |
79 | | - decompressUnzip(), |
80 | | - ], |
81 | | - }).then(() => { |
82 | | - fs.writeFile(versionPath, version, (err) => { |
83 | | - if (err) { |
84 | | - rejectFn(err); |
85 | | - } |
86 | | - }); |
87 | | - resolveFn(filePath); |
88 | | - }); |
89 | | - }); |
90 | | - }); |
91 | | - return p; |
| 122 | + await pipeline(req, fs.createWriteStream(zipPath)); |
| 123 | + |
| 124 | + if (zipPath.endsWith('.zip')) { |
| 125 | + await unzipFile(zipPath, outputPath); |
| 126 | + } else { |
| 127 | + await untarFile(zipPath, outputPath); |
| 128 | + } |
| 129 | + |
| 130 | + await fs.promises.writeFile(versionPath, version); |
92 | 131 | } |
93 | 132 | return filePath; |
94 | 133 | }; |
|
0 commit comments