2017-07-28 21:40:39 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const BbPromise = require('bluebird');
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const archiver = require('archiver');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const glob = require('glob');
|
|
|
|
|
const semver = require('semver');
|
|
|
|
|
|
2017-08-30 00:19:23 +02:00
|
|
|
function setArtifactPath(funcName, func, artifactPath) {
|
2017-07-28 21:40:39 +02:00
|
|
|
const version = this.serverless.getVersion();
|
|
|
|
|
|
|
|
|
|
// Serverless changed the artifact path location in version 1.18
|
|
|
|
|
if (semver.lt(version, '1.18.0')) {
|
|
|
|
|
func.artifact = artifactPath;
|
2017-08-30 00:19:23 +02:00
|
|
|
func.package = _.assign({}, func.package, { disable: true });
|
|
|
|
|
this.serverless.cli.log(`${funcName} is packaged by the webpack plugin. Ignore messages from SLS.`);
|
2017-07-28 21:40:39 +02:00
|
|
|
} else {
|
|
|
|
|
func.package = {
|
|
|
|
|
artifact: artifactPath,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function zip(directory, name) {
|
|
|
|
|
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',
|
|
|
|
|
name
|
|
|
|
|
);
|
|
|
|
|
this.serverless.utils.writeFileDir(artifactFilePath);
|
|
|
|
|
|
|
|
|
|
const output = fs.createWriteStream(artifactFilePath);
|
|
|
|
|
|
|
|
|
|
const files = glob.sync('**', {
|
|
|
|
|
cwd: directory,
|
|
|
|
|
dot: true,
|
|
|
|
|
silent: true,
|
|
|
|
|
follow: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (_.isEmpty(files)) {
|
|
|
|
|
const error = new this.serverless
|
|
|
|
|
.classes.Error('Packaging: No files found');
|
|
|
|
|
return BbPromise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output.on('open', () => {
|
|
|
|
|
zip.pipe(output);
|
|
|
|
|
|
|
|
|
|
_.forEach(files, filePath => {
|
|
|
|
|
const fullPath = path.resolve(
|
|
|
|
|
directory,
|
|
|
|
|
filePath
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const stats = fs.statSync(fullPath);
|
|
|
|
|
|
|
|
|
|
if (!stats.isDirectory(fullPath)) {
|
|
|
|
|
zip.append(fs.readFileSync(fullPath), {
|
|
|
|
|
name: filePath,
|
|
|
|
|
mode: stats.mode,
|
|
|
|
|
date: new Date(0), // necessary to get the same hash when zipping the same content
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
zip.finalize();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new BbPromise((resolve, reject) => {
|
|
|
|
|
output.on('close', () => resolve(artifactFilePath));
|
|
|
|
|
zip.on('error', (err) => reject(err));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2017-10-23 07:29:17 -06:00
|
|
|
packageModules() {
|
|
|
|
|
const stats = this.compileStats;
|
|
|
|
|
|
2017-07-28 21:40:39 +02:00
|
|
|
return BbPromise.mapSeries(stats.stats, (compileStats, index) => {
|
|
|
|
|
const entryFunction = _.get(this.entryFunctions, index, {});
|
2017-07-31 17:19:49 +02:00
|
|
|
const filename = `${entryFunction.funcName || this.serverless.service.getServiceObject().name}.zip`;
|
2017-07-28 21:40:39 +02:00
|
|
|
const modulePath = compileStats.compilation.compiler.outputPath;
|
|
|
|
|
|
|
|
|
|
const startZip = _.now();
|
2017-07-31 17:19:49 +02:00
|
|
|
return zip.call(this, modulePath, filename)
|
2017-07-28 21:40:39 +02:00
|
|
|
.tap(() => this.options.verbose &&
|
|
|
|
|
this.serverless.cli.log(`Zip ${_.isEmpty(entryFunction) ? 'service' : 'function'}: ${modulePath} [${_.now() - startZip} ms]`))
|
|
|
|
|
.then(artifactPath => {
|
|
|
|
|
if (_.get(this.serverless, 'service.package.individually')) {
|
2017-08-30 00:19:23 +02:00
|
|
|
setArtifactPath.call(this, entryFunction.funcName, entryFunction.func, path.relative(this.serverless.config.servicePath, artifactPath));
|
2017-07-28 21:40:39 +02:00
|
|
|
}
|
|
|
|
|
return artifactPath;
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(artifacts => {
|
2017-08-01 14:53:44 +02:00
|
|
|
if (!_.get(this.serverless, 'service.package.individually') && !_.isEmpty(artifacts)) {
|
2017-07-28 21:40:39 +02:00
|
|
|
// Set the service artifact to all functions
|
|
|
|
|
const allFunctionNames = this.serverless.service.getAllFunctions();
|
|
|
|
|
_.forEach(allFunctionNames, funcName => {
|
2017-07-30 18:58:03 +02:00
|
|
|
const func = this.serverless.service.getFunction(funcName);
|
2017-08-30 00:19:23 +02:00
|
|
|
setArtifactPath.call(this, funcName, func, path.relative(this.serverless.config.servicePath, artifacts[0]));
|
2017-07-28 21:40:39 +02:00
|
|
|
});
|
2017-08-22 12:42:50 +02:00
|
|
|
// For Google set the service artifact path
|
|
|
|
|
if (_.get(this.serverless, 'service.provider.name') === 'google') {
|
|
|
|
|
_.set(this.serverless, 'service.package.artifact', path.relative(this.serverless.config.servicePath, artifacts[0]));
|
|
|
|
|
}
|
2017-07-28 21:40:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|