Last active
October 31, 2019 09:41
-
-
Save thomasmichaelwallace/265c654de3526c935c78ee8928a41bcd to your computer and use it in GitHub Desktop.
Serverless Commons Example
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
#!/usr/bin/env node | |
/* eslint-disable no-console */ | |
const path = require('path'); | |
const fs = require('fs'); | |
const templateFile = path.join(__dirname, '../../serverless.templates.yml'); | |
const projectFile = path.join(__dirname, '../../serverless.project.yml'); | |
const profileFile = path.join(__dirname, `../../serverless.project.${process.env.AWS_PROFILE}.yml`); | |
const slsFile = path.join(__dirname, '../../serverless.yml'); | |
if (!fs.existsSync(projectFile)) { | |
console.error('No serverless configuration found.'); | |
process.exit(0); | |
} | |
if (!fs.existsSync(templateFile)) { | |
console.error('No serverless templates found.'); | |
process.exit(0); | |
} | |
const banner = '# this file is auto-generated, and will be replaced on npm run configure'; | |
const templates = fs.readFileSync(templateFile); | |
const project = fs.readFileSync(projectFile); | |
let sls = `${banner}\n\n${templates}\n# project specific configuration: \n\n${project}`; | |
if (fs.existsSync(profileFile)) { | |
const profile = fs.readFileSync(profileFile); | |
sls = `${sls}\n# profile specific configuration: \n\n${profile}`; | |
} | |
fs.writeFileSync(slsFile, sls); | |
console.log('Compiled serverless.yml.'); |
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
{ | |
"name": "our-commons", | |
"version": "2.0.0", | |
"description": "Install common configuration for common files of serverless projects.", | |
"scripts": { | |
"postinstall": "node postinstall.js" | |
}, | |
"bin": { | |
"build-dp-sls-template": "./build-dp-sls-template.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
// common deps! | |
} | |
} |
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
/* eslint-disable no-console */ | |
const fs = require('fs'); | |
const path = require('path'); | |
const tasks = require('./scripts.js'); // <- outputs a { scripts: { } } object that defines package.json scripts. | |
const moduleDir = path.resolve(); | |
const projectDir = path.resolve(moduleDir, '../../'); | |
const postInstall = () => { | |
console.info('Updating commons...'); | |
console.log('Loading config...'); | |
let config = { | |
files: true, | |
exclude: [], | |
scripts: true, | |
build: true, | |
}; | |
const configFile = path.join(projectDir, 'commons.json'); | |
if (fs.existsSync(configFile)) { | |
const fragment = JSON.parse(fs.readFileSync(configFile)); | |
config = Object.assign({}, config, fragment); | |
} | |
console.log(`Using config: ${JSON.stringify(config)}`); | |
const { | |
files, exclude, scripts, build, | |
} = config; | |
// files begining with a '.' are ignored by npm, so need to be saved with the prefix "dot" and are then renamed by script on copy to '.' | |
// e.g. dotgitignore becomes .gitignore | |
const dotFiles = ['.gitignore', '.npmrc']; | |
// all files to keep in sync with common, this is relative to the commons package root: | |
const allFiles = ['.editorconfig', '.eslintrc.yml', '.gitignore', '.npmrc', 'serverless.templates.yml', | |
'webpack.config.js', 'webpack.base.config.js', 'webpack.prod.config.js', 'webpack.test.config.js']; | |
const copy = allFiles | |
.filter(f => exclude.indexOf(f) < 0) | |
.map(f => (dotFiles.indexOf(f) > -1 ? f.replace(/^\./, 'dot') : f)); | |
if (files) { | |
copy.forEach((f) => { | |
console.info(`Installing common ${f.replace(/^dot/, '.')}...`); | |
fs | |
.createReadStream(path.join(moduleDir, f)) | |
.pipe(fs.createWriteStream(path.join(projectDir, f.replace(/^dot/, '.')))); | |
}); | |
} | |
if (scripts) { | |
console.info('Installing common scripts...'); | |
const packageJson = path.join(projectDir, 'package.json'); | |
fs.readFile(packageJson, (err, data) => { | |
const original = JSON.parse(data); | |
const updated = Object.assign({}, original, tasks); | |
fs.writeFile(packageJson, JSON.stringify(updated, null, 2)); | |
}); | |
} | |
if (!build) { | |
console.info('Deleting build...'); | |
fs.unlinkSync(path.join(projectDir, '.gitlab-ci.yml')); | |
} | |
}; | |
if (process.env.DP_SLS_SKIP_INSTALL) { | |
console.log('Skipping sls-common post install.'); | |
} else { | |
postInstall(); | |
} | |
/* eslint-enable no-console */ |
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
# for example | |
plugins: | |
# it doesn't normally hurt to load a plugin you won't use, so make this all common | |
- serverless-webpack | |
- serverless-aws-documentation | |
- serverless-plugin-bind-deployment-id | |
- serverless-plugin-tracing | |
# common options | |
package: | |
individually: true | |
# templates | |
.node-provider: &provider # you can now use this as '<<: *provider' to mix-in common options | |
name: aws | |
runtime: nodejs8.10 | |
stage: local | |
region: eu-west-1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment