UNPKG

1.44 kBJavaScriptView Raw
1const fs = require( "fs" );
2
3function init ( lambdaName ) {
4
5 lambdaName = lambdaName || process.env.COLLY__LAMBDA_NAME;
6
7 const defaultLambdaTemplate = `
8exports.handler = function ( event, context, callback ) {
9 // Start here
10}
11 `;
12
13 const defaultFunctionTemplate = `
14{
15 "memorySize": 128,
16 "name": "${lambdaName}",
17 "description": "",
18 "handler": "${lambdaName}/index.handler",
19 "runtime": "nodejs6.10",
20 "timeout": 3,
21 "userDefinedCodeToDeploy": [],
22 "deployedAssets": {
23 "live": {}
24 }
25}
26 `;
27
28 function createLambdaDirectory ( lambdaName ) {
29 return new Promise( ( resolve, reject ) => {
30 const path = `${process.cwd()}/${lambdaName}`;
31 if ( !fs.existsSync( path ) ){
32 fs.mkdirSync( path );
33 resolve();
34 } else {
35 reject( `Lambda "${lambdaName}" already exists` );
36 }
37 });
38 }
39
40 function createFile ( fileName, template, lambdaName) {
41 return new Promise( ( resolve ) => {
42 const path = `${process.cwd()}/${lambdaName}/${fileName}`;
43 if ( !fs.existsSync( path ) ) {
44 fs.writeFileSync( path, template );
45 resolve();
46 }
47 });
48 }
49
50 return new Promise( ( resolve, reject ) => {
51
52 createLambdaDirectory( lambdaName )
53 .then( createFile( "index.js", defaultLambdaTemplate, lambdaName ) )
54 .then( createFile( "function.json", defaultFunctionTemplate, lambdaName ) )
55 .then( () => {
56 resolve( `Lambda "${lambdaName}" created!` );
57 })
58 .catch( reject );
59
60 });
61
62}
63
64module.exports = init;