|
| 1 | + |
| 2 | +var fs = require('fs'); |
| 3 | +var path = require('path'); |
| 4 | +var archiver = require('archiver'); |
| 5 | + |
| 6 | +var pkg = require('./package.json'); |
| 7 | +var outputFileName = `wedatasphere-scriptis-${pkg.version}-dist.zip`; |
| 8 | + |
| 9 | +var outputFilePath = path.join(__dirname, outputFileName); |
| 10 | + |
| 11 | +// create a file to stream archive data to. |
| 12 | +var output = fs.createWriteStream(outputFilePath); |
| 13 | +var archive = archiver('zip', { |
| 14 | + zlib: { level: 9 } // Sets the compression level. |
| 15 | +}); |
| 16 | + |
| 17 | +// listen for all archive data to be written |
| 18 | +// 'close' event is fired only when a file descriptor is involved |
| 19 | +output.on('close', function() { |
| 20 | + console.log(`${outputFileName}: ${archive.pointer()} total bytes`); |
| 21 | + console.log('archiver has been finalized and the output file descriptor has closed.'); |
| 22 | +}); |
| 23 | + |
| 24 | +output.on('end', function() { |
| 25 | + console.log('Data has been drained'); |
| 26 | +}); |
| 27 | + |
| 28 | +// good practice to catch warnings (ie stat failures and other non-blocking errors) |
| 29 | +archive.on('warning', function(err) { |
| 30 | + if (err.code === 'ENOENT') { |
| 31 | + // log warning |
| 32 | + } else { |
| 33 | + // throw error |
| 34 | + throw err; |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +// good practice to catch this error explicitly |
| 39 | +archive.on('error', function(err) { |
| 40 | + throw err; |
| 41 | +}); |
| 42 | + |
| 43 | +// pipe archive data to the file |
| 44 | +archive.pipe(output); |
| 45 | + |
| 46 | +archive.directory('dist/'); |
| 47 | + |
| 48 | +var configSH = path.join(__dirname, 'config.sh'); |
| 49 | +archive.append(fs.createReadStream(configSH), { name: 'config.sh' }); |
| 50 | +var installSH = path.join(__dirname, 'install.sh'); |
| 51 | +archive.append(fs.createReadStream(installSH), { name: 'install.sh' }); |
| 52 | + |
| 53 | +// finalize the archive (ie we are done appending files but streams have to finish yet) |
| 54 | +// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand |
| 55 | +archive.finalize(); |
0 commit comments