Skip to content

Commit 28697b3

Browse files
authored
Support to deploy from current . location
1 parent d364e37 commit 28697b3

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

cli.js

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ const COLORS = function (name) {
3535
const colorCodes = ["\x1b[31m", "\x1b[32m", "\x1b[33m", "\x1b[34m", "\x1b[35m", "\x1b[36m", "\x1b[31m", "\x1b[32m", "\x1b[33m", "\x1b[34m", "\x1b[35m", "\x1b[36m", "\x1b[31m", "\x1b[32m", "\x1b[33m", "\x1b[34m", "\x1b[35m", "\x1b[36m", "\x1b[31m", "\x1b[32m", "\x1b[33m", "\x1b[34m", "\x1b[35m", "\x1b[36m", "\x1b[31m", "\x1b[32m", "\x1b[33m", "\x1b[34m", "\x1b[35m", "\x1b[36m"]
3636
return colorCodes[(name.toUpperCase().charCodeAt(0) - 65) % colorCodes.length]
3737
}
38-
const envFilePath = path.resolve('.env')
39-
if (fs.existsSync(envFilePath)) {
40-
require('dotenv').config({ path: envFilePath })
41-
}
4238

4339
const showBoxBanner = function () {
4440
console.log("╓───────────────────────────────────────────────────────────────╖")
@@ -61,21 +57,21 @@ const getErrorMessage = function (error) {
6157
}
6258

6359
const deployStack = function (options) {
64-
const { configFile, envFile, dataFile, envName, configStackFolder, configStackName, regionName, headless } = options
65-
const envFilePath = path.resolve(envFile || '.env')
60+
const { configFile, envFile, dataFile, envName, configFolder, configStackName, regionName, headless } = options
61+
const envFilePath = path.resolve(configFolder || configStackName, envFile || `.${envName ? envName + '.' : ''}env`)
6662
if (fs.existsSync(envFilePath)) {
6763
require('dotenv').config({ path: envFilePath })
6864
}
6965
const envOptions = { DEPLOYMENT_ENV: envName, DEPLOYMENT_REGION: regionName }
7066
var config = simplify.getInputConfig(path.resolve(configFile || 'config.json'), envOptions)
7167
const stackConfigFile = path.resolve(config.OutputFolder, envName || process.env.DEPLOYMENT_ENV, 'StackConfig.json')
72-
const stackYamlFile = path.resolve(configStackFolder, `${configStackName}`, `template.yaml`)
68+
const stackYamlFile = path.resolve(configFolder || configStackName, `template.yaml`)
7369
if (!fs.existsSync(stackYamlFile)) {
7470
simplify.finishWithErrors(`${opName}-CheckTemplate`, `${stackYamlFile} not found.`)
7571
}
7672
config.FunctionName = `${process.env.FUNCTION_NAME}-${process.env.DEPLOYMENT_ENV}`
7773
const stackFullName = `${process.env.PROJECT_NAME || config.FunctionName}-${configStackName}-${process.env.DEPLOYMENT_ENV}`
78-
const stackExtension = path.resolve(configStackFolder, configStackName, `extension`)
74+
const stackExtension = path.resolve(configFolder || configStackName, `extension`)
7975
return new Promise(function (resolve) {
8076
provider.setConfig(config).then(function () {
8177
simplify.uploadLocalFile({
@@ -129,8 +125,9 @@ const deployStack = function (options) {
129125
let resultErrors = null
130126
let stackOutputData = {}
131127
let stackParamteres = {}
132-
if (fs.existsSync(stackConfigFile)) {
133-
stackOutputData = JSON.parse(fs.readFileSync(stackConfigFile))
128+
if (fs.existsSync(configFolder, stackConfigFile)) {
129+
const stackFileContent = fs.readFileSync(path.resolve(configFolder, stackConfigFile))
130+
stackOutputData = JSON.parse(stackFileContent.toString())
134131
Object.keys(stackOutputData).map(stackName => {
135132
Object.keys(stackOutputData[stackName]).map(param => {
136133
if (["LastUpdate", "Type"].indexOf(param) == -1) {
@@ -182,7 +179,7 @@ const deployStack = function (options) {
182179
}
183180

184181
function saveParameters(resultParameters) {
185-
fs.writeFileSync(path.resolve(configStackFolder, configStackName, dataFile), JSON.stringify(resultParameters, null, 4))
182+
fs.writeFileSync(path.resolve(configFolder || configStackName, dataFile), JSON.stringify(resultParameters, null, 4))
186183
}
187184

188185
function processParameters(resultErrors, resultParameters, stackParamteres, docYaml) {
@@ -217,8 +214,9 @@ const deployStack = function (options) {
217214
var parameters = {
218215
Environment: process.env.DEPLOYMENT_ENV
219216
}
220-
if (fs.existsSync(path.resolve(configStackFolder, configStackName, dataFile))) {
221-
parameters = { ...parameters, ...JSON.parse(fs.readFileSync(path.resolve(configStackFolder, configStackName, dataFile))) }
217+
if (fs.existsSync(path.resolve(configFolder || configStackName, dataFile))) {
218+
const fileParameters = JSON.parse(fs.readFileSync(path.resolve(configFolder || configStackName, dataFile)))
219+
parameters = { ...parameters, ...fileParameters }
222220
}
223221
var stackPluginModule = {}
224222
if (fs.existsSync(stackExtension + '.js')) {
@@ -240,13 +238,13 @@ const deployStack = function (options) {
240238
simplify.finishWithErrors(`${opName}-LoadYAMLResource:`, getErrorMessage(error)) && resolve()
241239
}
242240
})
243-
}).catch(error => simplify.finishWithErrors(`${opName}-LoadYAMLResource:`, getErrorMessage(error)) && resolve())
241+
}).catch(error => simplify.finishWithErrors(`${opName}-LoadConfig:`, getErrorMessage(error)) && resolve())
244242
})
245243
}
246244

247245
const destroyStack = function (options) {
248-
const { configFile, envFile, envName, configStackFolder, configStackName, regionName } = options
249-
const envFilePath = path.resolve(envFile || '.env')
246+
const { configFile, envFile, envName, configFolder, configStackName, regionName } = options
247+
const envFilePath = path.resolve(configFolder || configStackName, envFile || `.${envName ? envName + '.' : ''}env`)
250248
if (fs.existsSync(envFilePath)) {
251249
require('dotenv').config({ path: envFilePath })
252250
}
@@ -257,7 +255,7 @@ const destroyStack = function (options) {
257255
return new Promise(function (resolve) {
258256
provider.setConfig(config).then(function () {
259257
function deleteStack(stackName, stackPluginModule) {
260-
const stackExtension = path.resolve(configStackFolder, stackName, `extension`)
258+
const stackExtension = path.resolve(configFolder || stackName, `extension`)
261259
const stackFullName = `${process.env.PROJECT_NAME || config.FunctionName}-${stackName}-${process.env.DEPLOYMENT_ENV}`
262260
simplify.consoleWithMessage(`${opName}-CleanupResource`, `StackName - (${stackFullName})`)
263261
return new Promise(function (resolve) {
@@ -291,7 +289,7 @@ const destroyStack = function (options) {
291289
}
292290
function deleteByStackName(stackName) {
293291
var stackPluginModule = {}
294-
const stackExtension = path.resolve(configStackFolder, stackName, `extension`)
292+
const stackExtension = path.resolve(configFolder || stackName, `extension`)
295293
if (fs.existsSync(stackExtension + '.js')) {
296294
stackPluginModule = require(stackExtension)
297295
}
@@ -317,16 +315,17 @@ const destroyStack = function (options) {
317315
}
318316

319317
const deployFunction = function (options) {
320-
const { regionName, functionName, envName, configFile, envFile, roleFile, policyFile, sourceDir, forceUpdate, asFunctionLayer, publishNewVersion } = options
321-
const envFilePath = path.resolve(functionName ? functionName : '', envFile || '.env')
318+
const { regionName, functionName, envName, configFile, configFolder, envFile, roleFile, policyFile, sourceDir, forceUpdate, asFunctionLayer, publishNewVersion } = options
319+
const configFolderOrFunctionName = configFolder || (functionName ? functionName : '')
320+
const envFilePath = path.resolve(configFolderOrFunctionName, envFile || `.${envName ? envName + '.' : ''}env`)
322321
if (fs.existsSync(envFilePath)) {
323322
require('dotenv').config({ path: envFilePath })
324323
}
325324
const envOptions = { FUNCTION_NAME: functionName, DEPLOYMENT_ENV: envName, DEPLOYMENT_REGION: regionName }
326-
var config = simplify.getInputConfig(path.resolve(functionName ? functionName : '', configFile || 'config.json'), envOptions)
325+
var config = simplify.getInputConfig(path.resolve(configFolderOrFunctionName, configFile || 'config.json'), envOptions)
327326
const stackConfigFile = path.resolve(config.OutputFolder, envName || process.env.DEPLOYMENT_ENV, 'StackConfig.json')
328-
var policyDocument = simplify.getContentFile(path.resolve(functionName ? functionName : '', policyFile || 'policy.json'), envOptions)
329-
var assumeRoleDocument = simplify.getContentFile(path.resolve(functionName ? functionName : '', roleFile || 'role.json'), envOptions)
327+
var policyDocument = simplify.getContentFile(path.resolve(configFolderOrFunctionName, policyFile || 'policy.json'), envOptions)
328+
var assumeRoleDocument = simplify.getContentFile(path.resolve(configFolderOrFunctionName, roleFile || 'role.json'), envOptions)
330329
if (!fs.existsSync(path.resolve(config.OutputFolder))) {
331330
fs.mkdirSync(path.resolve(config.OutputFolder), { recursive: true })
332331
}
@@ -350,8 +349,8 @@ const deployFunction = function (options) {
350349
adaptor: provider.getStorage(),
351350
...{
352351
bucketKey: config.Bucket.Key,
353-
inputDirectory: path.resolve(functionName ? functionName : '', sourceDir || 'src'),
354-
outputFilePath: path.resolve(functionName ? functionName : '', 'dist'),
352+
inputDirectory: path.resolve(configFolderOrFunctionName, sourceDir || 'src'),
353+
outputFilePath: path.resolve(configFolderOrFunctionName, 'dist'),
355354
hashInfo: { FileSha256: forceUpdate ? 'INVALID' : functionMeta.lashHash256 }
356355
}
357356
})
@@ -403,9 +402,9 @@ const deployFunction = function (options) {
403402
}
404403
if (asFunctionLayer) {
405404
try {
406-
let configInput = JSON.parse(fs.readFileSync(path.resolve(functionName ? functionName : '', configFile || 'config.json')))
405+
let configInput = JSON.parse(fs.readFileSync(path.resolve(configFolderOrFunctionName, configFile || 'config.json')))
407406
configInput.Function.Layers = data.Layers
408-
fs.writeFileSync(path.resolve(functionName ? functionName : '', configFile || 'config.json'), JSON.stringify(configInput, null, 4))
407+
fs.writeFileSync(path.resolve(configFolderOrFunctionName, configFile || 'config.json'), JSON.stringify(configInput, null, 4))
409408
resolve()
410409
} catch (error) {
411410
simplify.finishWithErrors(`${opName}-DeployLayer`, getErrorMessage(error)) && resolve()
@@ -446,13 +445,14 @@ const deployFunction = function (options) {
446445
}
447446

448447
const destroyFunction = function (options) {
449-
const { regionName, functionName, envName, configFile, envFile, withFunctionLayer } = options
450-
const envFilePath = path.resolve(functionName ? functionName : '', envFile || '.env')
448+
const { regionName, functionName, envName, configFile, configFolder, envFile, withFunctionLayer } = options
449+
const configFolderOrFunctionName = configFolder || (functionName ? functionName : '')
450+
const envFilePath = path.resolve(configFolderOrFunctionName, envFile || `.${envName ? envName + '.' : ''}env`)
451451
if (fs.existsSync(envFilePath)) {
452452
require('dotenv').config({ path: envFilePath })
453453
}
454454
const envOptions = { FUNCTION_NAME: functionName, DEPLOYMENT_ENV: envName, DEPLOYMENT_REGION: regionName }
455-
var config = simplify.getInputConfig(path.resolve(functionName ? functionName : '', configFile || 'config.json'), envOptions)
455+
var config = simplify.getInputConfig(path.resolve(configFolderOrFunctionName, configFile || 'config.json'), envOptions)
456456
const stackConfigFile = path.resolve(config.OutputFolder, envName || process.env.DEPLOYMENT_ENV, 'StackConfig.json')
457457
const outputFunctionFilePath = path.resolve(config.OutputFolder, `${envName || process.env.DEPLOYMENT_ENV}`, `${functionName || process.env.FUNCTION_NAME}.json`)
458458
const hashFunctionFilePath = path.resolve(config.OutputFolder, `${envName || process.env.DEPLOYMENT_ENV}`, `${functionName || process.env.FUNCTION_NAME}.hash`)
@@ -472,9 +472,9 @@ const destroyFunction = function (options) {
472472
}).then(data => {
473473
delete stackList[functionName || process.env.FUNCTION_NAME]
474474
fs.writeFileSync(stackConfigFile, JSON.stringify(stackList, null, 4))
475-
let configInput = JSON.parse(fs.readFileSync(path.resolve(functionName ? functionName : '', configFile || 'config.json')))
475+
let configInput = JSON.parse(fs.readFileSync(path.resolve(configFolderOrFunctionName, configFile || 'config.json')))
476476
configInput.Function.Layers = []
477-
fs.writeFileSync(path.resolve(functionName ? functionName : '', configFile || 'config.json'), JSON.stringify(configInput, null, 4))
477+
fs.writeFileSync(path.resolve(configFolderOrFunctionName, configFile || 'config.json'), JSON.stringify(configInput, null, 4))
478478
fs.unlinkSync(hashFunctionFilePath)
479479
fs.unlinkSync(outputFunctionFilePath)
480480
simplify.consoleWithMessage(`${opName}-DestroyFunction`, `Done. ${data.FunctionName}`)
@@ -647,6 +647,7 @@ const showAvailableStacks = (options, promptDescription) => {
647647
}
648648
})
649649
utilities.printTableWithJSON(tableStackData)
650+
return Promise.resolve()
650651
}
651652

652653
showBoxBanner()
@@ -665,7 +666,7 @@ var argv = require('yargs').usage('simplify-cli command [options]')
665666
.string('source').describe('source', 'function source to deploy').default('source', 'src')
666667
.string('env').describe('env', 'environment name')
667668
.string('region').describe('region', getOptionDesc('deploy', 'region'))
668-
.string('dotenv').describe('dotenv', getOptionDesc('deploy', 'dotenv')).default('dotenv', '.env')
669+
.string('dotenv').describe('dotenv', getOptionDesc('deploy', 'dotenv'))
669670
.boolean('update').describe('update', getOptionDesc('deploy', 'update')).default('update', false)
670671
.boolean('publish').describe('publish', getOptionDesc('deploy', 'publish')).default('publish', false)
671672
.boolean('layer').describe('layer', getOptionDesc('deploy', 'layer')).default('layer', false)
@@ -678,6 +679,14 @@ var cmdArg = argv['stack'] || argv['function'] || optCMD
678679
var cmdType = cmdArg ? fs.existsSync(path.resolve(argv.location, cmdArg, "template.yaml")) ? "CF-Stack" : "Function" : undefined
679680
cmdType = argv['function'] ? "Function" : argv['stack'] ? "CF-Stack" : cmdType
680681

682+
const envFilePath = path.resolve(argv['dotenv'] || `.${argv.env ? argv.env + '.' : ''}env`)
683+
684+
console.log(`ENV: ${envFilePath}`)
685+
686+
if (fs.existsSync(envFilePath)) {
687+
require('dotenv').config({ path: envFilePath })
688+
}
689+
681690
if (argv._.length == 0) {
682691
yargs.showHelp()
683692
console.log(`\n`, ` * ${CBRIGHT}Supported command list${CRESET}:`, '\n')
@@ -716,7 +725,7 @@ const processCLI = function (cmdRun, session) {
716725
functionName: cmdArg,
717726
configFile: argv.config,
718727
configStackName: cmdArg,
719-
configStackFolder: argv.location,
728+
configFolder: argv.location,
720729
envName: argv.env,
721730
envFile: argv['dotenv'],
722731
dataFile: argv.parameters,
@@ -742,6 +751,7 @@ const processCLI = function (cmdRun, session) {
742751
return (cmdType === "Function" ? destroyFunction({
743752
regionName: argv.region,
744753
configFile: argv.config,
754+
configFolder: argv.location,
745755
envName: argv.env,
746756
envFile: argv['dotenv'],
747757
functionName: cmdArg,
@@ -751,7 +761,7 @@ const processCLI = function (cmdRun, session) {
751761
configFile: argv.config,
752762
envName: argv.env,
753763
envFile: argv['dotenv'],
754-
configStackFolder: argv.location,
764+
configFolder: argv.location,
755765
configStackName: cmdArg
756766
}))
757767
} else {

0 commit comments

Comments
 (0)