Skip to content

Commit

Permalink
feat: add setup script (#17)
Browse files Browse the repository at this point in the history
* Setup script

* Change to array flags

* Better approach to know the files affected

* Replaceflag method refactor

* Fix to change all occurrences not only the first one

* Setup script split into different files

* Custom cognito iam role name

* Refactor
  • Loading branch information
jordygarcias authored Jun 9, 2020
1 parent de58397 commit 094edd2
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 68 deletions.
144 changes: 79 additions & 65 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"deploy-prepack:prod": "NODE_ENV=production sls deploy --package .serverless --stage production",
"deploy:prod": "NODE_ENV=production sls deploy --stage production",
"remove-stack:dev": "sls remove --stage development",
"zip-release": "zip -r release.zip .serverless serverless.yaml package.json package-lock.json"
"zip-release": "zip -r release.zip .serverless serverless.yaml package.json package-lock.json",
"setup": "node scripts/setup.js"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -55,5 +56,7 @@
"serverless-webpack": "^5.3.2",
"webpack": "^4.43.0"
},
"dependencies": {}
"dependencies": {
"replace-in-file": "^6.0.0"
}
}
22 changes: 22 additions & 0 deletions scripts/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const flags = [/myapp/g, /MyApp/g]
const longNameFlags = [/My App/g]

/* DIR PATHS */
const UIPath = 'packages/ui/'
const APIPath = 'packages/api/'

const packageJson = 'package.json'
const yamlFile = 'serverless.yaml'
const manifest = 'public/manifest.json'
const index = 'public/index.html'

module.exports = {
flags,
longNameFlags,
UIPath,
APIPath,
packageJson,
yamlFile,
manifest,
index,
}
17 changes: 17 additions & 0 deletions scripts/installDependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const npm = require('npm')
const constants = require('./constants')

const installUIDependencies = () => {
npm.load(() => npm.commands.install(constants.UIPath, []))
}

const installAPIDependencies = () => {
npm.load(() => npm.commands.install(constants.APIPath, []))
}

const installDependencies = async () => {
installUIDependencies()
installAPIDependencies()
}

module.exports = installDependencies
47 changes: 47 additions & 0 deletions scripts/replaceFlags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const replace = require('replace-in-file')
const constants = require('./constants')

const getUndescoredString = (string) => string.split(' ').join('_')
const getLowerCaseString = (string) => string.toLowerCase()
const getLowerCasedUnderscoredString = (string) => getUndescoredString(getLowerCaseString(string))

const replaceShortFlags = (shortName) => replace.sync({
files: [
constants.packageJson,
`${constants.UIPath}${constants.packageJson}`,
`${constants.APIPath}${constants.packageJson}`,
`${constants.UIPath}${constants.manifest}`,
constants.yamlFile,
],
from: constants.flags,
to: getLowerCasedUnderscoredString(shortName),
})

const replaceLongFlags = (appName) => replace.sync({
files: [
`${constants.UIPath}${constants.manifest}`,
`${constants.UIPath}${constants.index}`,
],
from: constants.longNameFlags,
to: appName,
})

const replaceFlag = (appName, shortName) => {
const changeShortName = replaceShortFlags(shortName)
const changeAppName = replaceLongFlags(appName)

const filesChanged = [...changeShortName, ...changeAppName.filter((change) => {
const shortNameFile = changeShortName.find((ch) => ch.file === change.file)
/* conditional to get unique files */
if (shortNameFile) {
return change.hasChanged !== shortNameFile.hasChanged && change.hasChanged
}

return change
})]

const filesCount = filesChanged.filter((r) => r.hasChanged).length
console.info(`${filesCount} files changed!`)
}

module.exports = replaceFlag
12 changes: 12 additions & 0 deletions scripts/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const replaceFlags = require('./replaceFlags')
const installDependencies = require('./installDependencies')

const main = () => {
const appName = process.env.npm_config_appName
const shortName = process.env.npm_config_shortName || appName

replaceFlags(appName, shortName)
installDependencies()
}

main()
2 changes: 1 addition & 1 deletion serverless.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ functions:
- dynamodb:UpdateItem
Resource:
- !GetAtt UserTable.Arn

cognitoAutoConfirm:
handler: packages/api/functions/cognito/auto-confirm-user.handler

Expand Down

0 comments on commit 094edd2

Please sign in to comment.