forked from codephi/sam-magia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·68 lines (56 loc) · 1.82 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
const path = require('path')
const exec = require('child_process').execSync
const getTemplates = require('./libs/getTemplates')
const fs = require('fs')
const samCommands = []
let rootPath = process.cwd()
try {
if(process.argv[2]){
rootPath = path.resolve(process.argv[2])
if(process.argv[3]){
samCommands.push(fs.readFileSync(process.argv[3]).toString())
} else {
samCommands.push(fs.readFileSync(path.resolve(rootPath, 'deploy.sh')).toString())
}
} else {
samCommands.push(fs.readFileSync(path.resolve(rootPath, 'deploy.sh')).toString())
}
} catch(err) {
return console.error('Deploy file not found.')
}
console.log(samCommands)
const templates = getTemplates(rootPath)
console.log('\nHANDLER COMMANDS...')
function packageOutputPath(key){
return path.resolve(rootPath, '.packages', `${Date.now()}-${key}.yml` )
}
const commandsRun = templates.map(template => {
const commands = []
for(let key in samCommands){
const packageOutput = packageOutputPath(key)
const comm = samCommands[key].replace(/\$templateBasePath/g , template[0])
.replace(/\$templateFilename/g, template[2])
.replace(/\$packagePath/g , packageOutput)
commands.push(comm)
}
return commands
})
console.log('\n---=INIT DEPLOY=---')
try {
commandsRun.forEach(command => {
try {
for(let i = 0; i < command.length; i++){
console.log('\n-> Exec command', 1)
console.log(`\n${command[i]}`)
const stout = exec(command[i])
console.log(stout.toString())
}
} catch (err){
throw new Error(err.message.toString())
}
})
console.log('\nFINISH!!!')
} catch (err){
console.error(err.message)
}