Skip to content

Commit

Permalink
Improvements to support new serverless resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
czirker committed Apr 12, 2022
1 parent 7ee5ae0 commit 944a415
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
34 changes: 24 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const parse = require('parse-gitignore')
const findUp = require('find-up')
const ignore = require('ignore')
const merge = require('lodash/merge')
const Utils = require('serverless/lib/classes/Utils')

const getDirInfo = (folderPath) => {
const dirExists = fs.existsSync(folderPath)
Expand All @@ -23,7 +24,7 @@ const getDirInfo = (folderPath) => {
})
}

function getContentFactory (serverless) {
function getContentFactory(serverless) {
return (absolutePath) => {
let content

Expand Down Expand Up @@ -88,21 +89,34 @@ const folderResourceReducer = (findResourceInDirectoryInfos) => {
}
}

module.exports = async (serverless) => {
serverless.cli.log(`Start`)
module.exports = async function(serverless) {
let serverlessMod
let convention = {}
if (serverless.resolveVariable) {
serverlessMod = {
cli: console,
utils: new Utils({})
}
convention = await serverless.resolveVariable('self:custom.convention')
} else {
serverlessMod = serverless
convention = serverless.service.custom.convention
}

// serverlessMod.cli.log(`Start`)
const gitignorepath = await findUp('.gitignore')
const ignoreResourcePatterns = parse(fs.readFileSync(gitignorepath))
const getContent = getContentFactory(serverless)
const keys = Object.keys(serverless.service.custom.convention)
const getContent = getContentFactory(serverlessMod)
const keys = Object.keys(convention)
return keys.reduce((acc, key) => {
if (serverless.service.custom.convention[key].pattern && serverless.service.custom.convention[key].folders) {
const asFileArray = serverless.service.custom.convention[key].asFileArray
const findResourcesInDirInfos = findResourceInDirectoryInfosFactory(serverless,
serverless.service.custom.convention[key].pattern,
if (convention[key].pattern && convention[key].folders) {
const asFileArray = convention[key].asFileArray
const findResourcesInDirInfos = findResourceInDirectoryInfosFactory(serverlessMod,
convention[key].pattern,
ignoreResourcePatterns,
getContent,
asFileArray)
acc[key] = serverless.service.custom.convention[key].folders.reduce(folderResourceReducer(findResourcesInDirInfos), asFileArray ? [] : {})
acc[key] = convention[key].folders.reduce(folderResourceReducer(findResourcesInDirInfos), asFileArray ? [] : {})
} else {
throw new Error(`Include "${key}" is missing pattern or folders.`)
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-convention",
"version": "1.1.2",
"version": "1.2.2",
"description": "Serverless script for setting up file structure conventions",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit 944a415

Please sign in to comment.