-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
988 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict'; | ||
|
||
const promptFunctionName = require('../../prompts/function-name'); | ||
const promptFunctionDefaultPath = require('../../prompts/function-default-path'); | ||
|
||
const functionWithPayloadSchemaTemplate = require('../../templates/function-with-payload-schema'); | ||
const functionWithoutPayloadSchemaTemplate = require('../../templates/function-without-payload-schema'); | ||
const functionSchemaFs = require('../../fs/function-schema'); | ||
|
||
const functionSourceTemplate = require('../../templates/function-source'); | ||
const functionSourceFs = require('../../fs/function-source'); | ||
|
||
const functionTestTemplate = require('../../templates/function-test'); | ||
const functionTestFs = require('../../fs/function-test'); | ||
|
||
const { ensureOptions, notEmpty, upperCamelCase } = require('../../utils'); | ||
const { Report } = require('../../report'); | ||
|
||
|
||
const getPrompts = () => [ | ||
|
||
promptFunctionName, | ||
promptFunctionDefaultPath, | ||
{ | ||
name: 'functionNewPath', | ||
type: prev => (prev === true ? 'text' : null), | ||
message: 'What\'s the functión name? (in-dash-case)', | ||
validate: notEmpty, | ||
format: functionName => upperCamelCase(functionName.trim()) | ||
}, | ||
{ | ||
name: 'hasPayload', | ||
type: 'toggle', | ||
message: 'Does the function have a payload?', | ||
initial: true, | ||
active: 'yes', | ||
inactive: 'no' | ||
}, | ||
{ | ||
name: 'hasClient', | ||
type: prev => (prev === true ? 'toggle' : null), | ||
message: 'Must your function be invoked with a client', | ||
initial: true, | ||
active: 'yes', | ||
inactive: 'no' | ||
} | ||
]; | ||
|
||
module.exports.command = 'create-function'; | ||
|
||
module.exports.describe = 'Create a new Lambda Function'; | ||
|
||
module.exports.builder = yargs => { | ||
|
||
return yargs | ||
.option('functionName', { | ||
alias: 'f', | ||
description: 'The function Name', | ||
type: 'string' | ||
}) | ||
.help(); | ||
}; | ||
|
||
module.exports.handler = async argv => { | ||
|
||
const options = await ensureOptions(getPrompts(), argv); | ||
|
||
const { | ||
functionName, functionDefaultPath, functionNewPath, hasClient, hasPayload | ||
} = options; | ||
|
||
const functionPath = functionDefaultPath && functionNewPath; | ||
|
||
const promises = []; | ||
|
||
// Function Schema | ||
const schemaContent = hasPayload ? functionWithPayloadSchemaTemplate({ functionName }) : functionWithoutPayloadSchemaTemplate({ functionName }); | ||
promises.push(functionSchemaFs.writeSchema(functionPath, functionName, schemaContent)); | ||
|
||
// Function Source | ||
const sourceContent = functionSourceTemplate({ functionName, hasClient, hasPayload }); | ||
promises.push(functionSourceFs.writeSource(functionPath, functionName, sourceContent)); | ||
|
||
// Function Test | ||
const testContent = functionTestTemplate({ functionPath, functionName }); | ||
promises.push(functionTestFs.writeTest(functionPath, functionName, testContent, true)); | ||
|
||
// Wait every file to be generated | ||
await Promise.all(promises); | ||
|
||
await Report.finish(); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
const { writeYml, openFile } = require('./base'); | ||
|
||
const getRelativePath = (functionPath, functionName) => path.join('schemas/src', functionPath || '', functionName); | ||
|
||
module.exports.writeSchema = (functionPath, functionName, content, needsChanges) => { | ||
return writeYml(getRelativePath(functionPath, functionName), content, needsChanges, false); | ||
}; | ||
|
||
module.exports.openSchema = (functionPath, functionName) => { | ||
return openFile(getRelativePath(functionPath, functionName), false); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
const { getFilePath: baseGetFilePath, writeRaw, openFile } = require('./base'); | ||
|
||
const getRelativePath = (functionPath, functionName) => path.join(process.env.MS_PATH || '', 'lambda', functionPath || '', `${functionName}.js`); | ||
|
||
module.exports.getFilePath = (functionPath, functionName) => baseGetFilePath(getRelativePath(functionPath, functionName), false); | ||
|
||
module.exports.writeSource = (functionPath, functionName, content, needsChanges) => { | ||
return writeRaw(getRelativePath(functionPath, functionName), content, needsChanges, false); | ||
}; | ||
|
||
module.exports.openSource = (functionPath, functionName) => { | ||
return openFile(getRelativePath(functionPath, functionName), false); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
const { getFilePath: baseGetFilePath, writeRaw, openFile } = require('./base'); | ||
|
||
const getRelativePath = (functionPath, functionName) => path.join('tests/lambda', functionPath || '', `${functionName}.js`); | ||
|
||
module.exports.getFilePath = (functionPath, functionName) => baseGetFilePath(getRelativePath(functionPath, functionName), false); | ||
|
||
module.exports.writeTest = (functionPath, functionName, content, needsChanges) => { | ||
return writeRaw(getRelativePath(functionPath, functionName), content, needsChanges, false); | ||
}; | ||
|
||
module.exports.openTest = (functionPath, functionName) => { | ||
return openFile(getRelativePath(functionPath, functionName), false); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
type: 'select', | ||
name: 'functionDefaultPath', | ||
message: 'Select a function path', | ||
choices: [ | ||
{ title: 'Default', description: '/src/lambda/{FunctionName}.js', value: false }, | ||
{ title: 'Other', description: 'Add a new path: /src/lambda/{path}/{FunctionName}.js', value: true } | ||
], | ||
initial: 0 | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const { notEmpty, areLettersAndNumbers, upperCamelCase } = require('../utils'); | ||
|
||
module.exports = { | ||
name: 'functionName', | ||
type: 'text', | ||
message: 'What\'s the functión name? (in-dash-case)', | ||
validate: notEmpty && areLettersAndNumbers, | ||
format: functionName => upperCamelCase(functionName.trim()) | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
module.exports = ({ | ||
functionName, | ||
hasClient, | ||
hasPayload | ||
}) => { | ||
|
||
const typeLambda = (payload, client) => (!payload && 'Lambda') || (!client && 'LambdaWithPayload') || 'LambdaWithClientAndPayload'; | ||
|
||
return `'use strict'; | ||
const { Handler, ${typeLambda(hasPayload, hasClient)} } = require('@janiscommerce/lambda'); | ||
class ${functionName} extends ${typeLambda(hasPayload, hasClient)} { | ||
async process() { | ||
// Process something | ||
} | ||
} | ||
module.exports.handler = (...args) => Handler.handle(${functionName}, ...args); | ||
`; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const { getFilePath } = require('../fs/function-source'); | ||
|
||
module.exports = ({ | ||
functionPath, | ||
functionName | ||
}) => { | ||
|
||
return `'use strict'; | ||
const sinon = require('sinon'); | ||
const { handler: ${functionName} } = require('${getFilePath(functionPath, functionName)}'); | ||
describe('Function ${functionName}', () => { | ||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
context('When event is invalid', () => { | ||
beforeEach(() => { | ||
// Some spies maybe? | ||
}); | ||
afterEach(() => { | ||
// Some notCalled maybe? | ||
}); | ||
}); | ||
context('When event is valid', () => { | ||
const validEvent = {}; | ||
}); | ||
}); | ||
`; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const apiSchemaTemplate = require('./api-schema'); | ||
|
||
module.exports = ({ | ||
functionName | ||
}) => apiSchemaTemplate({ | ||
path: `/${functionName}`, | ||
method: 'post', | ||
operationId: `${functionName}`, | ||
tags: ['Functions'], | ||
summary: `${functionName}`, | ||
requestBody: { | ||
description: `${functionName}`, | ||
required: true, | ||
content: { | ||
'application/json': { | ||
} | ||
} | ||
}, | ||
responses: { | ||
200: { | ||
$ref: '#/components/responses/GenericSuccess' | ||
} | ||
} | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const apiSchemaTemplate = require('./api-schema'); | ||
|
||
module.exports = ({ | ||
functionName | ||
}) => apiSchemaTemplate({ | ||
path: `/${functionName}`, | ||
method: 'post', | ||
operationId: `${functionName}`, | ||
tags: ['Functions'], | ||
summary: `${functionName}`, | ||
responses: { | ||
200: { | ||
$ref: '#/components/responses/GenericSuccess' | ||
} | ||
} | ||
}); |
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
Oops, something went wrong.