Created
December 20, 2017 21:32
-
-
Save digitalkaoz/94933c246ba67032a1507083e2605a30 to your computer and use it in GitHub Desktop.
gatsby in aws lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require("aws-sdk"); | |
const {link} = require("linkfs"); | |
const mock = require('mock-require'); | |
const fs = require('fs'); | |
const tmpDir = require('os').tmpdir(); | |
exports.handler = (event, context) => { | |
rewriteFs(); | |
invokeGatsby(context); | |
} | |
const invokeGatsby = (context) => { | |
const gatsby = require("gatsby/dist/commands/build"); | |
gatsby({ | |
directory: __dirname, | |
}) | |
.then(uploadFilesToS3) | |
.then(invalidateCloudFront) | |
.then(context.succeed) | |
.catch(context.fail); | |
}; | |
//TODO replace with in-memory-fs ?! | |
const rewriteFs = () => { | |
// redirect paths | |
const linkedFs = link(fs, [ | |
[`${__dirname}/.cache`, tmpDir+"/.cache"], | |
[`${__dirname}/public`, tmpDir+"/public"] | |
]); | |
// those are missing in linkfs | |
linkedFs.ReadStream = fs.ReadStream; | |
linkedFs.WriteStream = fs.WriteStream; | |
// replace fs with linkfs globally | |
mock('fs', linkedFs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have this idea as part of a functional project, or is it just a concept?