apigee-templater-cli
Advanced tools
Comparing version 0.4.17 to 0.4.18
426
dist/cli.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -33,15 +24,26 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
constructor() { | ||
this.apigeeService = new apigee_x_module_1.ApigeeService(); | ||
this.apigeeGenerator = new apigee_templater_module_1.ApigeeGenerator([ | ||
new apigee_templater_module_1.SpikeArrestPlugin(), | ||
new apigee_templater_module_1.AuthApiKeyPlugin(), | ||
new apigee_templater_module_1.AuthSfPlugin(), | ||
new apigee_templater_module_1.QuotaPlugin(), | ||
new apigee_templater_module_1.TargetsPlugin(), | ||
new apigee_templater_module_1.ProxiesPlugin(), | ||
], [ | ||
new apigee_templater_module_1.Json1Converter(), | ||
new apigee_templater_module_1.Json2Converter(), | ||
new apigee_templater_module_1.OpenApiV3Converter() | ||
]); | ||
/** | ||
* The ApigeeService object, using default application credentials | ||
* @date 3/16/2022 - 11:20:23 AM | ||
* | ||
* @type {ApiManagementInterface} | ||
*/ | ||
Object.defineProperty(this, "apigeeService", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: new apigee_x_module_1.ApigeeService() | ||
}); | ||
/** | ||
* The ApigeeGenerator object using the default profile of plugins | ||
* @date 3/16/2022 - 11:20:50 AM | ||
* | ||
* @type {ApigeeTemplateService} | ||
*/ | ||
Object.defineProperty(this, "apigeeGenerator", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: new apigee_templater_module_1.ApigeeGenerator() | ||
}); | ||
} | ||
@@ -52,4 +54,4 @@ /** | ||
* | ||
* @param {*} rawArgs | ||
* @returns {{ file: any; input: any; deploy: any; environment: any; filter: any; name: any; basePath: any; targetUrl: any; verbose: any; keyPath: any; help: any; }} | ||
* @param {cliArgs} rawArgs The command line arguments | ||
* @return {cliArgs}} Processed arguments | ||
*/ | ||
@@ -100,87 +102,91 @@ parseArgumentsIntoOptions(rawArgs) { | ||
* @param {cliArgs} options The options collection of user inputs | ||
* @returns {cliArgs} Updated cliArgs options collection | ||
* @return {cliArgs} Updated cliArgs options collection | ||
*/ | ||
promptForMissingOptions(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const questions = []; | ||
if (!options.name) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'name', | ||
message: 'What should the proxy be called?', | ||
default: 'MyProxy', | ||
transformer: (input, answer) => { | ||
return input.replace(/ /g, "-"); | ||
} | ||
}); | ||
} | ||
if (!options.basePath) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'basePath', | ||
message: 'Which base path should be used?', | ||
transformer: (input, answer) => { | ||
return `/${input}`; | ||
} | ||
}); | ||
} | ||
if (!options.targetUrl) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'targetUrl', | ||
message: 'Which backend target should be called?', | ||
transformer: (input, answer) => { | ||
return `https://${input}`; | ||
} | ||
}); | ||
} | ||
if (!options.deploy) { | ||
questions.push({ | ||
type: 'confirm', | ||
name: 'deploy', | ||
message: 'Do you want to deploy the proxy to an Apigee X environment?' | ||
}); | ||
} | ||
if (!options.keyPath) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'keyPath', | ||
message: 'No GOOGLE_APPLICATION_CREDENTIALS found, please enter a path to a GCP project JSON key:', | ||
when: (answers) => { | ||
return answers.deploy; | ||
} | ||
}); | ||
} | ||
if (!options.environment) { | ||
questions.push({ | ||
type: 'list', | ||
name: 'environment', | ||
message: 'Which Apigee X environment to you want to deploy to?', | ||
when: (answers) => { | ||
return answers.deploy; | ||
}, | ||
choices: (answers) => { | ||
if (answers.keyPath) | ||
process.env.GOOGLE_APPLICATION_CREDENTIALS = answers.keyPath; | ||
let envs = []; | ||
this.apigeeService.getEnvironments().then((result) => { | ||
return result; | ||
}).catch((error) => { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Invalid GCP service account key file passed, please pass a service account with Apigee deployment roles attached.`); | ||
return []; | ||
}); | ||
} | ||
}); | ||
} | ||
const answers = yield inquirer_1.default.prompt(questions); | ||
if (answers.basePath && !answers.basePath.startsWith("/")) | ||
answers.basePath = "/" + answers.basePath; | ||
if (answers.targetUrl && !answers.targetUrl.startsWith("https://")) | ||
answers.targetUrl = "https://" + answers.targetUrl; | ||
return Object.assign(Object.assign({}, options), { name: options.name || answers.name, basePath: options.basePath || answers.basePath, targetUrl: options.targetUrl || answers.targetUrl, deploy: options.deploy || answers.deploy, environment: options.environment || answers.environment, keyPath: options.keyPath || answers.keyPath }); | ||
}); | ||
async promptForMissingOptions(options) { | ||
const questions = []; | ||
if (!options.name) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'name', | ||
message: 'What should the proxy be called?', | ||
default: 'MyProxy', | ||
transformer: (input) => { | ||
return input.replace(/ /g, "-"); | ||
} | ||
}); | ||
} | ||
if (!options.basePath) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'basePath', | ||
message: 'Which base path should be used?', | ||
transformer: (input) => { | ||
return `/${input}`; | ||
} | ||
}); | ||
} | ||
if (!options.targetUrl) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'targetUrl', | ||
message: 'Which backend target should be called?', | ||
transformer: (input) => { | ||
return `https://${input}`; | ||
} | ||
}); | ||
} | ||
if (!options.deploy) { | ||
questions.push({ | ||
type: 'confirm', | ||
name: 'deploy', | ||
message: 'Do you want to deploy the proxy to an Apigee X environment?' | ||
}); | ||
} | ||
if (!options.keyPath) { | ||
questions.push({ | ||
type: 'input', | ||
name: 'keyPath', | ||
message: 'No GOOGLE_APPLICATION_CREDENTIALS found, please enter a path to a GCP project JSON key:', | ||
when: (answers) => { | ||
return answers.deploy; | ||
} | ||
}); | ||
} | ||
if (!options.environment) { | ||
questions.push({ | ||
type: 'list', | ||
name: 'environment', | ||
message: 'Which Apigee X environment to you want to deploy to?', | ||
when: (answers) => { | ||
return answers.deploy; | ||
}, | ||
choices: (answers) => { | ||
if (answers.keyPath) | ||
process.env.GOOGLE_APPLICATION_CREDENTIALS = answers.keyPath; | ||
this.apigeeService.getEnvironments().then((result) => { | ||
return result; | ||
}).catch(() => { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Invalid GCP service account key file passed, please pass a service account with Apigee deployment roles attached.`); | ||
return []; | ||
}); | ||
} | ||
}); | ||
} | ||
const answers = await inquirer_1.default.prompt(questions); | ||
if (answers.basePath && !answers.basePath.startsWith("/")) | ||
answers.basePath = "/" + answers.basePath; | ||
if (answers.targetUrl && !answers.targetUrl.startsWith("https://")) | ||
answers.targetUrl = "https://" + answers.targetUrl; | ||
return { | ||
...options, | ||
name: options.name || answers.name, | ||
basePath: options.basePath || answers.basePath, | ||
targetUrl: options.targetUrl || answers.targetUrl, | ||
deploy: options.deploy || answers.deploy, | ||
environment: options.environment || answers.environment, | ||
keyPath: options.keyPath || answers.keyPath, | ||
}; | ||
} | ||
/** | ||
* Prints example and full commands | ||
* @returns none | ||
**/ | ||
@@ -196,6 +202,5 @@ printHelp() { | ||
console.log(`${chalk_1.default.bold(chalk_1.default.blueBright("All parameters:"))}`); | ||
for (let line of helpCommands) { | ||
for (const line of helpCommands) { | ||
console.log(`${chalk_1.default.bold(chalk_1.default.green(line.name))}: ${chalk_1.default.grey(line.description)} `); | ||
} | ||
//console.log(""); | ||
} | ||
@@ -208,68 +213,69 @@ /** | ||
* @param {cliArgs} args The user input args to the process | ||
* @returns {*} none | ||
*/ | ||
process(args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let options = this.parseArgumentsIntoOptions(args); | ||
if (options.keyPath) | ||
process.env.GOOGLE_APPLICATION_CREDENTIALS = options.keyPath; | ||
if (options.verbose) | ||
this.logVerbose(JSON.stringify(options), "options:"); | ||
console.log(""); | ||
console.log(`${chalk_1.default.green(">")} ${chalk_1.default.bold(chalk_1.default.greenBright("Welcome to apigee-template"))}, use -h for more command line options. `); | ||
if (options.help) { | ||
this.printHelp(); | ||
return; | ||
async process(args) { | ||
let options = this.parseArgumentsIntoOptions(args); | ||
if (options.keyPath) | ||
process.env.GOOGLE_APPLICATION_CREDENTIALS = options.keyPath; | ||
if (options.verbose) | ||
this.logVerbose(JSON.stringify(options), "options:"); | ||
console.log(`${chalk_1.default.green(">")} ${chalk_1.default.bold(chalk_1.default.greenBright("Welcome to apigee-template"))}, use -h for more command line options. `); | ||
if (options.help) { | ||
this.printHelp(); | ||
return; | ||
} | ||
if (fs_1.default.existsSync(options.file)) { | ||
options.input = fs_1.default.readFileSync(options.file, "utf-8"); | ||
} | ||
if (!options.input && !options.file) { | ||
if (process.env.GOOGLE_APPLICATION_CREDENTIALS) | ||
options.keyPath = process.env.GOOGLE_APPLICATION_CREDENTIALS; | ||
try { | ||
options = await this.promptForMissingOptions(options); | ||
} | ||
if (fs_1.default.existsSync(options.file)) { | ||
options.input = fs_1.default.readFileSync(options.file, "utf-8"); | ||
catch (error) { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Invalid GCP service account key file passed, please pass a service account with Apigee deployment roles attached.`); | ||
} | ||
if (!options.input && !options.file) { | ||
if (process.env.GOOGLE_APPLICATION_CREDENTIALS) | ||
options.keyPath = process.env.GOOGLE_APPLICATION_CREDENTIALS; | ||
try { | ||
options = yield this.promptForMissingOptions(options); | ||
} | ||
catch (error) { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Invalid GCP service account key file passed, please pass a service account with Apigee deployment roles attached.`); | ||
} | ||
let newInput = { | ||
name: options.name, | ||
proxyEndpoints: [ | ||
{ | ||
name: "default", | ||
basePath: options.basePath, | ||
targetName: "default", | ||
targetUrl: options.targetUrl | ||
} | ||
] | ||
}; | ||
options.input = JSON.stringify(newInput); | ||
const newInput = new apigee_templater_module_1.ApigeeTemplateInput({ | ||
name: options.name, | ||
proxyEndpoints: [ | ||
{ | ||
name: "default", | ||
basePath: options.basePath, | ||
targetName: "default", | ||
targetUrl: options.targetUrl | ||
} | ||
] | ||
}); | ||
options.input = JSON.stringify(newInput); | ||
} | ||
const _proxyDir = "."; | ||
if (options.filter) { | ||
// users can add their own preprocessing filter scripts here | ||
eval(fs_1.default.readFileSync(options.filter, "utf-8")); | ||
} | ||
if (options.verbose) | ||
this.logVerbose(options.input, "template:"); | ||
this.apigeeGenerator.generateProxyFromString(options.input, _proxyDir).then((result) => { | ||
if (result && result.template) | ||
console.log(`${chalk_1.default.green(">")} Proxy ${chalk_1.default.bold(chalk_1.default.blue(result.template.name))} generated to ${chalk_1.default.magentaBright(chalk_1.default.bold(result.localPath))} in ${chalk_1.default.bold(chalk_1.default.green(Math.round(result.duration) + " milliseconds"))}.`); | ||
if (options.deploy && !options.environment) { | ||
console.error(`${chalk_1.default.redBright("! Error:")} No environment found to deploy to, please pass the -e parameter with an Apigee X environment.`); | ||
} | ||
var _proxyDir = "."; | ||
if (options.filter) { | ||
// users can add their own preprocessing filter scripts here | ||
eval(fs_1.default.readFileSync(options.filter, "utf-8")); | ||
else if (options.deploy && !process.env.GOOGLE_APPLICATION_CREDENTIALS) { | ||
console.error(`${chalk_1.default.redBright("! Error:")} No GCP credentials found, please set the GOOGLE_APPLICATION_CREDENTIALS environment variable or use the -k parameter, see https://cloud.google.com/docs/authentication/getting-started for more information.`); | ||
} | ||
if (options.verbose) | ||
this.logVerbose(options.input, "template:"); | ||
this.apigeeGenerator.generateProxyFromString(options.input, _proxyDir).then((result) => { | ||
console.log(`${chalk_1.default.green(">")} Proxy ${chalk_1.default.bold(chalk_1.default.blue(result.template.name))} generated to ${chalk_1.default.magentaBright(chalk_1.default.bold(result.localPath))} in ${chalk_1.default.bold(chalk_1.default.green(Math.round(result.duration) + " milliseconds"))}.`); | ||
if (options.deploy && !options.environment) { | ||
console.error(`${chalk_1.default.redBright("! Error:")} No environment found to deploy to, please pass the -e parameter with an Apigee X environment.`); | ||
} | ||
else if (options.deploy && !process.env.GOOGLE_APPLICATION_CREDENTIALS) { | ||
console.error(`${chalk_1.default.redBright("! Error:")} No GCP credentials found, please set the GOOGLE_APPLICATION_CREDENTIALS environment variable or use the -k parameter, see https://cloud.google.com/docs/authentication/getting-started for more information.`); | ||
} | ||
else if (options.deploy) { | ||
var startTime = perf_hooks_1.performance.now(); | ||
else if (options.deploy) { | ||
const startTime = perf_hooks_1.performance.now(); | ||
if (result && result.template) { | ||
this.apigeeService.updateProxy(result.template.name, _proxyDir + "/" + result.template.name + ".zip").then((updateResult) => { | ||
if (updateResult && updateResult.revision) { | ||
this.apigeeService.deployProxyRevision(options.environment, result.template.name, updateResult.revision).then((deploymentResult) => { | ||
var endTime = perf_hooks_1.performance.now(); | ||
var duration = endTime - startTime; | ||
console.log(`${chalk_1.default.green(">")} Proxy ${chalk_1.default.bold(chalk_1.default.blue(result.template.name + " version " + updateResult.revision))} deployed to environment ${chalk_1.default.bold(chalk_1.default.magentaBright(options.environment))} in ${chalk_1.default.bold(chalk_1.default.green(Math.round(duration) + " milliseconds"))}.`); | ||
}).catch((error) => { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Error deploying proxy revision.`); | ||
}); | ||
if (result && result.template) | ||
this.apigeeService.deployProxyRevision(options.environment, result.template.name, updateResult.revision).then(() => { | ||
const endTime = perf_hooks_1.performance.now(); | ||
const duration = endTime - startTime; | ||
if (result && result.template) | ||
console.log(`${chalk_1.default.green(">")} Proxy ${chalk_1.default.bold(chalk_1.default.blue(result.template.name + " version " + updateResult.revision))} deployed to environment ${chalk_1.default.bold(chalk_1.default.magentaBright(options.environment))} in ${chalk_1.default.bold(chalk_1.default.green(Math.round(duration) + " milliseconds"))}.`); | ||
}).catch(() => { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Error deploying proxy revision.`); | ||
}); | ||
} | ||
@@ -283,6 +289,6 @@ }).catch((error) => { | ||
} | ||
}).catch((error) => { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Error templating proxy, invalid inputs given.`); | ||
process.exit(); | ||
}); | ||
} | ||
}).catch(() => { | ||
console.error(`${chalk_1.default.redBright("! Error:")} Error templating proxy, invalid inputs given.`); | ||
process.exit(); | ||
}); | ||
@@ -298,7 +304,5 @@ } | ||
logVerbose(input, label) { | ||
//console.log(""); | ||
if (label) | ||
console.log(`${chalk_1.default.grey("> " + label)}`); | ||
console.log(`${chalk_1.default.grey("> " + input)}`); | ||
//console.log(""); | ||
} | ||
@@ -316,13 +320,68 @@ } | ||
constructor() { | ||
this.file = ""; | ||
this.input = ""; | ||
this.deploy = false; | ||
this.environment = ""; | ||
this.filter = ""; | ||
this.name = ""; | ||
this.basePath = ""; | ||
this.targetUrl = ""; | ||
this.verbose = false; | ||
this.keyPath = ""; | ||
this.help = false; | ||
Object.defineProperty(this, "file", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "input", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "deploy", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: false | ||
}); | ||
Object.defineProperty(this, "environment", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "filter", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "name", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "basePath", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "targetUrl", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "verbose", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: false | ||
}); | ||
Object.defineProperty(this, "keyPath", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "help", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: false | ||
}); | ||
} | ||
@@ -378,2 +437,1 @@ } | ||
]; | ||
//# sourceMappingURL=cli.js.map |
{ | ||
"name": "apigee-templater-cli", | ||
"version": "0.4.17", | ||
"version": "0.4.18", | ||
"description": "A CLI to easily template and deploy Apigee proxies.", | ||
@@ -28,4 +28,4 @@ "homepage": "https://github.com/tyayers/apigee-templater", | ||
"dependencies": { | ||
"apigee-templater-module": "^0.4.19", | ||
"apigee-x-module": "^0.5.15", | ||
"apigee-templater-module": "^0.5.3", | ||
"apigee-x-module": "^0.7.0", | ||
"arg": "^5.0.1", | ||
@@ -38,6 +38,7 @@ "chalk": "^4.1.2", | ||
"devDependencies": { | ||
"ts-node": "^10.4.0", | ||
"@types/inquirer": "^8.2.0", | ||
"ts-node": "^10.5.0", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.5.5" | ||
"typescript": "^4.6.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
512
21265
4
+ Addedapigee-templater-module@0.5.4(transitive)
+ Addedapigee-x-module@0.7.4(transitive)
- Removedapigee-templater-module@0.4.20(transitive)
- Removedapigee-x-module@0.5.15(transitive)
Updatedapigee-x-module@^0.7.0