mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const archiver = require('archiver');
|
|
const BbPromise = require('bluebird');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const globby = require('globby');
|
|
|
|
module.exports = {
|
|
zipDirectory(exclude, include, zipFileName) {
|
|
const patterns = ['**'];
|
|
|
|
exclude.forEach((pattern) => {
|
|
if (pattern.charAt(0) !== '!') {
|
|
patterns.push(`!${pattern}`);
|
|
} else {
|
|
patterns.push(pattern.substring(1));
|
|
}
|
|
});
|
|
|
|
// push the include globs to the end of the array
|
|
// (files and folders will be re-added again even if they were excluded beforehand)
|
|
include.forEach((pattern) => {
|
|
patterns.push(pattern);
|
|
});
|
|
|
|
const zip = archiver.create('zip');
|
|
// Create artifact in temp path and move it to the package path (if any) later
|
|
const artifactFilePath = path.join(this.serverless.config.servicePath,
|
|
'.serverless',
|
|
zipFileName
|
|
);
|
|
this.serverless.utils.writeFileDir(artifactFilePath);
|
|
|
|
const output = fs.createWriteStream(artifactFilePath);
|
|
|
|
output.on('open', () => {
|
|
zip.pipe(output);
|
|
|
|
const files = globby.sync(patterns, {
|
|
cwd: this.serverless.config.servicePath,
|
|
dot: true,
|
|
silent: true,
|
|
follow: true,
|
|
});
|
|
|
|
files.forEach((filePath) => {
|
|
const fullPath = path.resolve(
|
|
this.serverless.config.servicePath,
|
|
filePath
|
|
);
|
|
|
|
const stats = fs.statSync(fullPath);
|
|
|
|
if (!stats.isDirectory(fullPath)) {
|
|
zip.append(fs.readFileSync(fullPath), {
|
|
name: filePath,
|
|
mode: stats.mode,
|
|
});
|
|
}
|
|
});
|
|
|
|
zip.finalize();
|
|
});
|
|
|
|
return new BbPromise((resolve, reject) => {
|
|
output.on('close', () => resolve(artifactFilePath));
|
|
zip.on('error', (err) => reject(err));
|
|
});
|
|
},
|
|
};
|