diff --git a/lib/packExternalModules.js b/lib/packExternalModules.js index 7b88e7cb6e..2c5b330863 100644 --- a/lib/packExternalModules.js +++ b/lib/packExternalModules.js @@ -291,7 +291,12 @@ module.exports = { addModulesToPackageJson(prodModules, modulePackage); this.serverless.utils.writeFileSync(modulePackageJson, JSON.stringify(modulePackage, null, 2)); - // Copy modules + // GOOGLE: Copy modules only if not google-cloud-functions + // GCF Auto installs the package json + if (_.get(this.serverless, 'service.provider.name') === 'google') { + return BbPromise.resolve(); + } + const startCopy = _.now(); return BbPromise.fromCallback(callback => fse.copy(path.join(compositeModulePath, 'node_modules'), path.join(modulePath, 'node_modules'), callback)) .tap(() => this.options.verbose && this.serverless.cli.log(`Copy modules: ${modulePath} [${_.now() - startCopy} ms]`)) diff --git a/tests/packExternalModules.test.js b/tests/packExternalModules.test.js index 2188905f06..049772d0f2 100644 --- a/tests/packExternalModules.test.js +++ b/tests/packExternalModules.test.js @@ -212,6 +212,53 @@ describe('packExternalModules', () => { ])); }); + it('should skip module copy for Google provider', () => { + const expectedCompositePackageJSON = { + name: 'test-service', + version: '1.0.0', + description: 'Packaged externals for test-service', + private: true, + dependencies: { + '@scoped/vendor': '1.0.0', + uuid: '^5.4.1', + bluebird: '^3.4.0' + } + }; + const expectedPackageJSON = { + dependencies: { + '@scoped/vendor': '1.0.0', + uuid: '^5.4.1', + bluebird: '^3.4.0' + } + }; + + _.set(serverless, 'service.provider.name', 'google'); + module.webpackOutputPath = 'outputPath'; + fsExtraMock.pathExists.yields(null, false); + fsExtraMock.copy.yields(); + childProcessMock.exec.onFirstCall().yields(null, '{}', ''); + childProcessMock.exec.onSecondCall().yields(null, '', ''); + childProcessMock.exec.onThirdCall().yields(); + module.compileStats = stats; + return expect(module.packExternalModules()).to.be.fulfilled + .then(() => BbPromise.all([ + // The module package JSON and the composite one should have been stored + expect(writeFileSyncStub).to.have.been.calledTwice, + expect(writeFileSyncStub.firstCall.args[1]).to.equal(JSON.stringify(expectedCompositePackageJSON, null, 2)), + expect(writeFileSyncStub.secondCall.args[1]).to.equal(JSON.stringify(expectedPackageJSON, null, 2)), + // The modules should have been copied + expect(fsExtraMock.copy).to.have.not.been.called, + // npm ls and npm prune should have been called + expect(childProcessMock.exec).to.have.been.calledTwice, + expect(childProcessMock.exec.firstCall).to.have.been.calledWith( + 'npm ls -prod -json -depth=1' + ), + expect(childProcessMock.exec.secondCall).to.have.been.calledWith( + 'npm install' + ) + ])); + }); + it('should reject if npm install fails', () => { module.webpackOutputPath = 'outputPath'; fsExtraMock.pathExists.yields(null, false);