|
| 1 | +/** |
| 2 | + * Created by AlexanderC on 8/4/15. |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const NpmChain = require('../../../../lib.compiled/NodeJS/NpmChain').NpmChain; |
| 8 | +const Hash = require('deep-package-manager').Helpers_Hash; |
| 9 | +const helpers = require('./compile-prod'); |
| 10 | +const tmp = require('tmp'); |
| 11 | +const path = require('path'); |
| 12 | +const webpackConfig = require('./webpack.prod'); |
| 13 | +const fse = require('fs-extra'); |
| 14 | +const pify = require('pify'); |
| 15 | + |
| 16 | +module.exports = function (lambdaPath, debug, purge, libsToLink) { |
| 17 | + const dry = debug ? '[DRY] ' : ''; |
| 18 | + |
| 19 | + console.log(`Compiling lambda "${lambdaPath}" (purge=${purge ? 'true' : 'false'})`); |
| 20 | + console.debug(`${dry}Purge "node_modules" in "${lambdaPath}"`); |
| 21 | + |
| 22 | + const prepare = (purge && !debug) |
| 23 | + ? pify(fse.remove)(path.join(lambdaPath, 'node_modules')) |
| 24 | + : Promise.resolve(); |
| 25 | + |
| 26 | + return prepare |
| 27 | + .then(() => { |
| 28 | + if (libsToLink.length <= 0) { |
| 29 | + console.debug(`No global libraries available to link for "${lambdaPath}"`); |
| 30 | + |
| 31 | + return Promise.resolve(); |
| 32 | + } |
| 33 | + |
| 34 | + console.debug(`${dry}Linking global libraries in "${lambdaPath}" (${libsToLink.join(', ')})`); |
| 35 | + |
| 36 | + return Promise.all(libsToLink.map(lib => { |
| 37 | + return helpers.hasDependency(lambdaPath, lib) |
| 38 | + .then(hasLib => { |
| 39 | + if (!hasLib) { |
| 40 | + console.debug(`Skip linking extraneous library "${lib}" in "${lambdaPath}"`); |
| 41 | + } |
| 42 | + |
| 43 | + return Promise.resolve(hasLib); |
| 44 | + }); |
| 45 | + })).then(libsVector => { |
| 46 | + const libs = libsToLink.filter((lib, i) => { |
| 47 | + return libsVector[i]; |
| 48 | + }); |
| 49 | + |
| 50 | + if (libs.length <= 0) { |
| 51 | + return Promise.resolve(); |
| 52 | + } |
| 53 | + |
| 54 | + return helpers.npmLink(lambdaPath, libs, debug); |
| 55 | + }); |
| 56 | + }) |
| 57 | + .then(() => { |
| 58 | + console.debug(`${dry}Running "npm install" on "${lambdaPath}"`); |
| 59 | + |
| 60 | + return helpers.npmInstall(lambdaPath, debug); |
| 61 | + }) |
| 62 | + .then(() => { |
| 63 | + const buildPath = path.join( |
| 64 | + tmp.dirSync().name, |
| 65 | + `${Hash.md5(lambdaPath)}_${new Date().getTime()}` |
| 66 | + ); |
| 67 | + const configFile = path.join(buildPath, 'webpack.prod.js'); |
| 68 | + const bundlePath = path.join(buildPath, 'bundle'); |
| 69 | + |
| 70 | + console.debug(`Ensure working env in "${buildPath}"`); |
| 71 | + |
| 72 | + return pify(fse.ensureDir)(bundlePath) |
| 73 | + .then(() => { |
| 74 | + return webpackConfig(lambdaPath, bundlePath, libsToLink, debug) |
| 75 | + .then(webpackConfig => { |
| 76 | + const { rawConfig, tmpBootstrapJs } = webpackConfig; |
| 77 | + |
| 78 | + return pify(fse.outputFile)(configFile, rawConfig) |
| 79 | + .then(() => Promise.resolve(tmpBootstrapJs)); |
| 80 | + }); |
| 81 | + }) |
| 82 | + .then(tmpBootstrapJs => { |
| 83 | + console.debug(`Bundle "${lambdaPath}" using Webpack to "${bundlePath}"`); |
| 84 | + |
| 85 | + const removeTmpBootstrap = () => { |
| 86 | + console.debug(`Removing temporary bootstrap file "${tmpBootstrapJs}"`); |
| 87 | + |
| 88 | + return pify(fse.remove)(tmpBootstrapJs) |
| 89 | + .catch(() => Promise.resolve()); |
| 90 | + }; |
| 91 | + |
| 92 | + return helpers.bundle(configFile, debug) |
| 93 | + .then(() => removeTmpBootstrap()) |
| 94 | + .catch(error => { |
| 95 | + return removeTmpBootstrap() |
| 96 | + .then(() => Promise.reject(error)); |
| 97 | + }); |
| 98 | + }) |
| 99 | + .then(() => Promise.resolve({ bundlePath, buildPath })); |
| 100 | + }) |
| 101 | + .then(result => { |
| 102 | + const { bundlePath, buildPath } = result; |
| 103 | + const bundleDestination = path.join( |
| 104 | + lambdaPath, |
| 105 | + '..', |
| 106 | + `${path.basename(lambdaPath)}.zip` |
| 107 | + ); |
| 108 | + |
| 109 | + console.info(`Archive "${lambdaPath}" build to "${bundleDestination}"`); |
| 110 | + |
| 111 | + return pify(fse.remove)(bundleDestination) |
| 112 | + .catch(() => Promise.resolve()) |
| 113 | + .then(() => helpers.zip(bundlePath, bundleDestination)) |
| 114 | + .then(() => Promise.resolve(buildPath)); |
| 115 | + }) |
| 116 | + .then(buildPath => { |
| 117 | + console.debug(`Cleanup temporary data for "${lambdaPath}"`); |
| 118 | + |
| 119 | + return pify(fse.remove)(buildPath); |
| 120 | + }); |
| 121 | +}; |
0 commit comments