Created
October 27, 2017 15:43
-
-
Save amoretspero/8a0f3d607792cedd1d5a3a5fa0ef1c44 to your computer and use it in GitHub Desktop.
Multi-function serverless framework command issuer.
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
import {exec, spawn} from "child_process"; | |
import * as fs from "fs"; | |
import * as path from "path"; | |
const indivitualUsage = "ts-node function.ts <command> [--function <function>] [--args <args list...>]"; | |
const npmUsage = "npm run <command> -- [--function <function>] [--args <args list...>]"; | |
const commands = [ | |
"webpack", | |
"package", | |
"deploy", | |
"invoke", | |
"invoke-local", | |
]; | |
function isEmptyString(str: string) { | |
return (!str || /^\s*$/.test(str)); | |
} | |
const command = process.argv[2]; | |
if (isEmptyString(command) || commands.indexOf(command) === -1) { | |
global.console.log(npmUsage); | |
process.exit(-1); | |
} | |
let isSeparate = false; | |
let functionName = ""; | |
let commandArgs: string[] = []; | |
if (process.argv[3] === "--function") { | |
isSeparate = true; | |
functionName = process.argv[4]; | |
if (isEmptyString(functionName)) { | |
global.console.log(npmUsage); | |
process.exit(-1); | |
} | |
if (process.argv[5] === "--args") { | |
commandArgs = process.argv.slice(6); | |
} | |
} else if (process.argv[3] === "--args") { | |
commandArgs = process.argv.slice(4); | |
} | |
// Function-must-provided command check. | |
if ((["invoke", "invoke-local"].indexOf(command) !== -1) && !isSeparate) { | |
global.console.log(npmUsage); | |
process.exit(-1); | |
} | |
let targetPath = ""; | |
// Directory check. | |
if (isSeparate) { | |
const filePath = path.join(__dirname, "functions", functionName, "src", functionName + ".ts"); | |
if (!fs.existsSync(filePath)) { | |
global.console.error(`Specified function <${functionName}> does not exists.\nPaths searched: ${targetPath}`); | |
} else { | |
targetPath = path.join(__dirname, "functions", functionName, "src"); | |
} | |
} | |
const execServerlessCommand = command.replace(/-/g, " "); | |
const commandSeparator = process.platform.match(/^.*win.*$/) ? ";" : " && "; | |
const commandArgsString = commandArgs.join(" "); | |
const execCommand = `${isSeparate ? "cd \"" + targetPath + "\\\"" + commandSeparator : ""}serverless ${execServerlessCommand} ${isSeparate ? "--function " + functionName : ""}${commandArgsString}${isSeparate ? commandSeparator + "cd" + " " + "\"" + __dirname + "\\\"" : ""}`; | |
global.console.log(`Now executing & {${execCommand}}...`); | |
(async () => { | |
if (process.platform.match(/^.*win.*$/) !== null) { | |
const currentProc = spawn("powershell.exe", ["-Command", `& {${execCommand}}`], { | |
cwd: __dirname, | |
detached: false, | |
env: process.env, | |
stdio: [process.stdin, process.stdout, process.stderr], | |
}); | |
currentProc.on("close", (code) => { | |
global.console.log(`child process exited with code ${code}`); | |
}); | |
} else { | |
// TODO: test for linux. | |
const currentProc = spawn("echo", [execCommand]); | |
currentProc.stdout.on("data", (data) => { | |
global.console.log(`${data}`); | |
}); | |
currentProc.stderr.on("data", (data) => { | |
global.console.log(`${data}`); | |
}); | |
currentProc.on("close", (code) => { | |
global.console.log(`child process exited with code ${code}`); | |
}); | |
currentProc.stdin.end(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need more elegant argument parsing.