cloud-red
Version:
Harnessing Serverless for your cloud integration needs
103 lines (90 loc) • 2.32 kB
JavaScript
const fs = require('fs-extra');
const error = require('./util/error');
const path = require('path');
function genLambdaJs(flowFileName) {
return `'use strict';
const { FlowBuilder } = require('cloud-red');
const flow = new FlowBuilder('./${flowFileName}');
exports.handler = (event, context) => {
return flow.handleEvent(event, context);
};`;
}
function genFlowJson() {
return ``;
}
function genPackageJson(lambdaName, lambdaFile, flowFile) {
return `{
"name": "${lambdaName}",
"version": "1.0.0",
"description": "",
"main": "${lambdaFile}",
"files": [
"${lambdaFile}",
"${flowFile}",
"node_modules/**/*"
],
"scripts": {
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cloud-red": "^0.7.23"
},
"devDependencies": {
"aws-sdk": "^2.513.0"
}
}
`;
}
function createFile(baseDir, fileName, content) {
fs.outputFileSync(path.join(baseDir, fileName), content, 'utf-8');
}
function initCmd(args) {
// args._[0] = init
const projectName = args._[1];
if (!projectName) {
error(
'Project name not specified. \n\nType "cloud-red help init" for more information',
true
);
}
const defaultLambdaFilename = 'lambda.js';
const defaultFlowFilename = `${projectName}-flow.json`;
const projectDir = path.join(process.cwd(), projectName);
fs.ensureDir(projectDir)
.then(() => {
// create `package.json` file
createFile(
projectDir,
'package.json',
genPackageJson(projectName, defaultLambdaFilename, defaultFlowFilename)
);
// create `lambda.js` file
createFile(
projectDir,
defaultLambdaFilename,
genLambdaJs(defaultFlowFilename)
);
// create `{project}-flow.json` file
createFile(
projectDir,
defaultFlowFilename,
genFlowJson(defaultFlowFilename)
);
console.log(
`Successfully generated boilerplate for template: ${projectName}`
);
console.log(
`NOTE: You are ready to edit the flow cloud-red editor. Happy flowing!!`
);
})
.catch(err => {
error(`Directory cannot be created due to: ${err.toString()}`, true);
});
}
module.exports = {
createFile,
genFlowJson,
initCmd
};