@field123/hacksprint-2022-js-sdk-test-response-generation 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +43 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +145 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +145 -0
- package/dist/cli.mjs.map +1 -0
- package/package.json +39 -0
package/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# @field123/hacksprint-2022-js-sdk-test-response-generation
|
2
|
+
|
3
|
+
# Test Data Retrival Script
|
4
|
+
|
5
|
+
The script populates a store with test data, runs CURL calls to get data then tears down the data to create a clean slate.
|
6
|
+
|
7
|
+
Utilizes the [EPCC Terraform Provider](https://registry.terraform.io/providers/elasticpath/epcc/latest) to populate and tear down down data in a store.
|
8
|
+
|
9
|
+
## Setup
|
10
|
+
|
11
|
+
Create a `.env` file based on the `.env.example` file.
|
12
|
+
|
13
|
+
## Install
|
14
|
+
|
15
|
+
```sh
|
16
|
+
yarn add -D @field123/hacksprint-2022-js-sdk-test-response-generation
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```sh
|
22
|
+
npx hacksprint-2022-js-sdk-test-response-generation endpoints -s <source> -o <dest> -e <environment-file>
|
23
|
+
```
|
24
|
+
|
25
|
+
```sh
|
26
|
+
npx hacksprint-2022-js-sdk-test-response-generation endpoints -s ./postman-export/postman_collection.json -o ./output -e ./postman-export/postman_environment.json
|
27
|
+
```
|
28
|
+
|
29
|
+
```sh
|
30
|
+
npx hacksprint-2022-js-sdk-test-response-generation endpoints -s ./postman-export/postman_collection.json -o ./output -e ./postman-export/postman_environment.json -v
|
31
|
+
```
|
32
|
+
|
33
|
+
| Key | CLI opt | Default | Description |
|
34
|
+
| ---- | ----------------- | -------- | ------------------------------------------------------ |
|
35
|
+
| s | --source \<file\> | required | Source postman collection json file. |
|
36
|
+
| e | --env \<file\> | required | Source postman environment json file. |
|
37
|
+
| o | --output \<dir\> | \<dir\> | Output location for the generated endpoint json files. |
|
38
|
+
| v | --verbose | false | Logs details of terraform commands to console. |
|
39
|
+
| help | -h, --help | n/a | display help for command |
|
40
|
+
|
41
|
+
## Sources
|
42
|
+
|
43
|
+
`main.tf` comes from this repo: https://gitlab.elasticpath.com/commerce-cloud/playground/sample-catalogs-accelerator/-/blob/MT-11900/store-scope/variants/get-started-with-pcm-catalog/main.tf
|
package/dist/cli.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
package/dist/cli.js
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
6
|
+
var __spreadValues = (a, b) => {
|
7
|
+
for (var prop in b || (b = {}))
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
10
|
+
if (__getOwnPropSymbols)
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
12
|
+
if (__propIsEnum.call(b, prop))
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
14
|
+
}
|
15
|
+
return a;
|
16
|
+
};
|
17
|
+
|
18
|
+
// src/cli.ts
|
19
|
+
var import_commander = require("commander");
|
20
|
+
|
21
|
+
// src/load-env.ts
|
22
|
+
var import_dotenv = require("dotenv");
|
23
|
+
function loadEnv() {
|
24
|
+
const result = (0, import_dotenv.config)();
|
25
|
+
if (result.error) {
|
26
|
+
throw new Error("Failed to load environment variables. Check .env exists and is setup to match example.");
|
27
|
+
}
|
28
|
+
return result.parsed;
|
29
|
+
}
|
30
|
+
|
31
|
+
// src/newman-exec.ts
|
32
|
+
var import_newman = require("newman");
|
33
|
+
var import_fs = require("fs");
|
34
|
+
async function executeCollection(options, env) {
|
35
|
+
const postmanCollection = await getPostmanCollection(options.source);
|
36
|
+
const postmanEnvironment = await getPostmanEnvironment(options.env);
|
37
|
+
return new Promise((resolve, reject) => {
|
38
|
+
(0, import_newman.run)(__spreadValues({
|
39
|
+
collection: postmanCollection,
|
40
|
+
reporters: ["cli", "@field123/ep-resp"],
|
41
|
+
reporter: {
|
42
|
+
"@field123/ep-resp": __spreadValues({}, options)
|
43
|
+
},
|
44
|
+
environment: postmanEnvironment
|
45
|
+
}, env ? { envVar: createEnvironment(env) } : {}), (err) => {
|
46
|
+
if (err) {
|
47
|
+
reject(err);
|
48
|
+
}
|
49
|
+
console.log("collection run complete!");
|
50
|
+
resolve();
|
51
|
+
});
|
52
|
+
});
|
53
|
+
}
|
54
|
+
function createEnvironment(env) {
|
55
|
+
return [
|
56
|
+
{
|
57
|
+
key: "clientID",
|
58
|
+
value: env.EPCC_CLIENT_ID
|
59
|
+
},
|
60
|
+
{
|
61
|
+
key: "clientSecret",
|
62
|
+
value: env.EPCC_CLIENT_SECRET
|
63
|
+
}
|
64
|
+
];
|
65
|
+
}
|
66
|
+
function getPostmanEnvironment(source) {
|
67
|
+
console.log(`Attempting to load postman environment file from ${source}`);
|
68
|
+
return getFile(source);
|
69
|
+
}
|
70
|
+
function getPostmanCollection(source) {
|
71
|
+
console.log(`Attempting to load postman collection file from ${source}`);
|
72
|
+
return getFile(source);
|
73
|
+
}
|
74
|
+
function getFile(name) {
|
75
|
+
return new Promise((resolve, reject) => {
|
76
|
+
(0, import_fs.readFile)(name, "utf-8", (err, data) => {
|
77
|
+
if (err) {
|
78
|
+
reject(err);
|
79
|
+
}
|
80
|
+
resolve(JSON.parse(data));
|
81
|
+
});
|
82
|
+
});
|
83
|
+
}
|
84
|
+
|
85
|
+
// src/terraform.ts
|
86
|
+
var import_child_process = require("child_process");
|
87
|
+
function applyTerraform(options = { verbose: false }) {
|
88
|
+
console.log("Attempting to terraform apply...");
|
89
|
+
return handleTerraformCommand(options, "apply", "-auto-approve").then(() => console.log("Applied terraform successfully!"));
|
90
|
+
}
|
91
|
+
function destroyTerraform(options = { verbose: false }) {
|
92
|
+
console.log("Attempting to terraform destroy...");
|
93
|
+
return handleTerraformCommand(options, "destroy", "-auto-approve").then(() => console.log("Destroyed terraform successfully!"));
|
94
|
+
}
|
95
|
+
function handleTerraformCommand(options, ...args) {
|
96
|
+
return new Promise((resolve, reject) => {
|
97
|
+
try {
|
98
|
+
const child = (0, import_child_process.spawn)("terraform", args);
|
99
|
+
if (options.verbose) {
|
100
|
+
child.stdout.setEncoding("utf8");
|
101
|
+
child.stdout.on("data", (data) => {
|
102
|
+
console.log("stdout: " + data);
|
103
|
+
});
|
104
|
+
child.stderr.on("data", (data) => {
|
105
|
+
console.log("stderr: " + data);
|
106
|
+
});
|
107
|
+
}
|
108
|
+
child.on("close", (code) => {
|
109
|
+
if (code !== 0) {
|
110
|
+
return reject(`terraform running failed with ${code}`);
|
111
|
+
}
|
112
|
+
resolve();
|
113
|
+
});
|
114
|
+
} catch (error) {
|
115
|
+
console.log(`error inside terraform with args ${args}: `, error);
|
116
|
+
reject(error);
|
117
|
+
}
|
118
|
+
});
|
119
|
+
}
|
120
|
+
|
121
|
+
// src/cli.ts
|
122
|
+
function initiateCli() {
|
123
|
+
import_commander.program.name("ep-resource-json");
|
124
|
+
import_commander.program.command("endpoints").description("Used to fetch json response data from Elastic Path endpoints and output them into json files.").requiredOption("-s, --source <source>", "Source postman collection json file.").requiredOption("-e, --env <environment-file>", "Source postman environment json file.").option("-o, --output <output>", "Output location for the generated endpoint json files.", process.cwd()).option("-v, --verbose", "Logs details of terraform commands to console.", false).action(outputJsonResponesAction);
|
125
|
+
import_commander.program.command("ter-destroy").description("Run terraform destroy in isolation.").action(isolatedTerraformDestroyAction);
|
126
|
+
import_commander.program.parse(process.argv);
|
127
|
+
console.log("program: ", import_commander.program.opts());
|
128
|
+
}
|
129
|
+
function outputJsonResponesAction(args) {
|
130
|
+
const env = loadEnv();
|
131
|
+
return applyTerraform({ verbose: args.verbose }).then(() => {
|
132
|
+
return executeCollection(args, env);
|
133
|
+
}).then(() => {
|
134
|
+
return destroyTerraform({ verbose: args.verbose });
|
135
|
+
}).catch((err) => {
|
136
|
+
destroyTerraform({ verbose: args.verbose });
|
137
|
+
console.error("error apply terraform: ", err);
|
138
|
+
});
|
139
|
+
}
|
140
|
+
function isolatedTerraformDestroyAction() {
|
141
|
+
loadEnv();
|
142
|
+
return destroyTerraform();
|
143
|
+
}
|
144
|
+
initiateCli();
|
145
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts","../src/load-env.ts","../src/newman-exec.ts","../src/terraform.ts"],"sourcesContent":["import { program } from \"commander\";\nimport { loadEnv } from \"./load-env\";\nimport { executeCollection } from \"./newman-exec\";\nimport { applyTerraform, destroyTerraform } from \"./terraform\";\nimport { EndpointCliOptions } from \"./types\";\n\n/**\n * Checks if the script has been provided the required arguments.\n */\nfunction initiateCli(): void {\n program.name(\"ep-resource-json\");\n\n program\n .command(\"endpoints\")\n .description(\n \"Used to fetch json response data from Elastic Path endpoints and output them into json files.\"\n )\n .requiredOption(\n \"-s, --source <source>\",\n \"Source postman collection json file.\"\n )\n .requiredOption(\n \"-e, --env <environment-file>\",\n \"Source postman environment json file.\"\n )\n .option(\n \"-o, --output <output>\",\n \"Output location for the generated endpoint json files.\",\n process.cwd()\n )\n .option(\n \"-v, --verbose\",\n \"Logs details of terraform commands to console.\",\n false\n )\n .action(outputJsonResponesAction);\n\n program\n .command(\"ter-destroy\")\n .description(\"Run terraform destroy in isolation.\")\n .action(isolatedTerraformDestroyAction);\n\n program.parse(process.argv);\n\n // const { source } = program.opts<{ source: string }>();\n\n console.log(\"program: \", program.opts());\n}\nfunction outputJsonResponesAction(args: EndpointCliOptions): Promise<void> {\n const env = loadEnv();\n return applyTerraform({ verbose: args.verbose })\n .then(() => {\n return executeCollection(args, env);\n })\n .then(() => {\n return destroyTerraform({ verbose: args.verbose });\n })\n .catch((err) => {\n destroyTerraform({ verbose: args.verbose });\n console.error(\"error apply terraform: \", err);\n });\n}\n\nfunction isolatedTerraformDestroyAction(): Promise<void> {\n loadEnv();\n return destroyTerraform();\n}\n\ninitiateCli();\n","import { config as dotEnvConfig } from \"dotenv\";\nimport { EPEnv } from \"./types\";\n\nexport function loadEnv(): EPEnv {\n const result = dotEnvConfig();\n if (result.error) {\n throw new Error(\n \"Failed to load environment variables. Check .env exists and is setup to match example.\"\n );\n }\n // Casting parsed response.\n return result!.parsed as EPEnv;\n}\n","import { run } from \"newman\";\nimport { readFile } from \"fs\";\nimport type {\n CollectionDefinition,\n VariableScopeDefinition,\n} from \"postman-collection\";\nimport { EndpointCliOptions, EPEnv } from \"./types\";\n\nexport async function executeCollection(\n options: EndpointCliOptions,\n env?: EPEnv\n): Promise<void> {\n const postmanCollection = await getPostmanCollection(options.source);\n const postmanEnvironment = await getPostmanEnvironment(options.env);\n return new Promise((resolve, reject) => {\n run(\n {\n collection: postmanCollection,\n reporters: [\"cli\", \"@field123/ep-resp\"],\n reporter: {\n \"@field123/ep-resp\": {\n ...options,\n },\n },\n environment: postmanEnvironment,\n ...(env ? { envVar: createEnvironment(env) } : {}),\n },\n (err) => {\n if (err) {\n reject(err);\n }\n console.log(\"collection run complete!\");\n resolve();\n }\n );\n });\n}\n\nfunction createEnvironment(env: EPEnv): { key: string; value: string }[] {\n return [\n {\n key: \"clientID\",\n value: env.EPCC_CLIENT_ID,\n },\n {\n key: \"clientSecret\",\n value: env.EPCC_CLIENT_SECRET,\n },\n ];\n}\n\nfunction getPostmanEnvironment(\n source: string\n): Promise<VariableScopeDefinition> {\n console.log(`Attempting to load postman environment file from ${source}`);\n return getFile(source);\n}\n\nfunction getPostmanCollection(source: string): Promise<CollectionDefinition> {\n console.log(`Attempting to load postman collection file from ${source}`);\n return getFile(source);\n}\n\nfunction getFile(name: string): Promise<any> {\n return new Promise((resolve, reject) => {\n readFile(name, \"utf-8\", (err, data) => {\n if (err) {\n reject(err);\n }\n resolve(JSON.parse(data));\n });\n });\n}\n","import { spawn } from \"child_process\";\n\ninterface TerraformOptions {\n verbose: boolean;\n}\n\nexport function applyTerraform(\n options: TerraformOptions = { verbose: false }\n): Promise<void> {\n console.log(\"Attempting to terraform apply...\");\n return handleTerraformCommand(options, \"apply\", \"-auto-approve\").then(() =>\n console.log(\"Applied terraform successfully!\")\n );\n}\n\nexport function destroyTerraform(\n options: TerraformOptions = { verbose: false }\n): Promise<void> {\n console.log(\"Attempting to terraform destroy...\");\n return handleTerraformCommand(options, \"destroy\", \"-auto-approve\").then(() =>\n console.log(\"Destroyed terraform successfully!\")\n );\n}\n\nfunction handleTerraformCommand(\n options: TerraformOptions,\n ...args: string[]\n): Promise<void> {\n return new Promise((resolve, reject) => {\n try {\n const child = spawn(\"terraform\", args);\n\n if (options.verbose) {\n child.stdout.setEncoding(\"utf8\");\n child.stdout.on(\"data\", (data) => {\n console.log(\"stdout: \" + data);\n });\n child.stderr.on(\"data\", (data) => {\n console.log(\"stderr: \" + data);\n });\n }\n\n child.on(\"close\", (code) => {\n if (code !== 0) {\n return reject(`terraform running failed with ${code}`);\n }\n resolve();\n });\n } catch (error) {\n console.log(`error inside terraform with args ${args}: `, error);\n reject(error);\n }\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,uBAAwB;;;ACAxB,oBAAuC;AAGhC,mBAA0B;AAC/B,QAAM,SAAS,0BAAa;AAC5B,MAAI,OAAO,OAAO;AAChB,UAAM,IAAI,MACR,wFACF;AAAA,EACF;AAEA,SAAO,OAAQ;AACjB;;;ACZA,oBAAoB;AACpB,gBAAyB;AAOzB,iCACE,SACA,KACe;AACf,QAAM,oBAAoB,MAAM,qBAAqB,QAAQ,MAAM;AACnE,QAAM,qBAAqB,MAAM,sBAAsB,QAAQ,GAAG;AAClE,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,2BACE;AAAA,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AAC;AAAA,EACnD,CAAC,EACA,MAAM,CAAC,QAAQ;AACd,qBAAiB,EAAE,SAAS,KAAK,QAAQ,CAAC;AAC1C,YAAQ,MAAM,2BAA2B,GAAG;AAAA,EAC9C,CAAC;AACL;AAEA,0CAAyD;AACvD,UAAQ;AACR,SAAO,iBAAiB;AAC1B;AAEA,YAAY;","names":[]}
|
package/dist/cli.mjs
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
6
|
+
var __spreadValues = (a, b) => {
|
7
|
+
for (var prop in b || (b = {}))
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
10
|
+
if (__getOwnPropSymbols)
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
12
|
+
if (__propIsEnum.call(b, prop))
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
14
|
+
}
|
15
|
+
return a;
|
16
|
+
};
|
17
|
+
|
18
|
+
// src/cli.ts
|
19
|
+
import { program } from "commander";
|
20
|
+
|
21
|
+
// src/load-env.ts
|
22
|
+
import { config as dotEnvConfig } from "dotenv";
|
23
|
+
function loadEnv() {
|
24
|
+
const result = dotEnvConfig();
|
25
|
+
if (result.error) {
|
26
|
+
throw new Error("Failed to load environment variables. Check .env exists and is setup to match example.");
|
27
|
+
}
|
28
|
+
return result.parsed;
|
29
|
+
}
|
30
|
+
|
31
|
+
// src/newman-exec.ts
|
32
|
+
import { run } from "newman";
|
33
|
+
import { readFile } from "fs";
|
34
|
+
async function executeCollection(options, env) {
|
35
|
+
const postmanCollection = await getPostmanCollection(options.source);
|
36
|
+
const postmanEnvironment = await getPostmanEnvironment(options.env);
|
37
|
+
return new Promise((resolve, reject) => {
|
38
|
+
run(__spreadValues({
|
39
|
+
collection: postmanCollection,
|
40
|
+
reporters: ["cli", "@field123/ep-resp"],
|
41
|
+
reporter: {
|
42
|
+
"@field123/ep-resp": __spreadValues({}, options)
|
43
|
+
},
|
44
|
+
environment: postmanEnvironment
|
45
|
+
}, env ? { envVar: createEnvironment(env) } : {}), (err) => {
|
46
|
+
if (err) {
|
47
|
+
reject(err);
|
48
|
+
}
|
49
|
+
console.log("collection run complete!");
|
50
|
+
resolve();
|
51
|
+
});
|
52
|
+
});
|
53
|
+
}
|
54
|
+
function createEnvironment(env) {
|
55
|
+
return [
|
56
|
+
{
|
57
|
+
key: "clientID",
|
58
|
+
value: env.EPCC_CLIENT_ID
|
59
|
+
},
|
60
|
+
{
|
61
|
+
key: "clientSecret",
|
62
|
+
value: env.EPCC_CLIENT_SECRET
|
63
|
+
}
|
64
|
+
];
|
65
|
+
}
|
66
|
+
function getPostmanEnvironment(source) {
|
67
|
+
console.log(`Attempting to load postman environment file from ${source}`);
|
68
|
+
return getFile(source);
|
69
|
+
}
|
70
|
+
function getPostmanCollection(source) {
|
71
|
+
console.log(`Attempting to load postman collection file from ${source}`);
|
72
|
+
return getFile(source);
|
73
|
+
}
|
74
|
+
function getFile(name) {
|
75
|
+
return new Promise((resolve, reject) => {
|
76
|
+
readFile(name, "utf-8", (err, data) => {
|
77
|
+
if (err) {
|
78
|
+
reject(err);
|
79
|
+
}
|
80
|
+
resolve(JSON.parse(data));
|
81
|
+
});
|
82
|
+
});
|
83
|
+
}
|
84
|
+
|
85
|
+
// src/terraform.ts
|
86
|
+
import { spawn } from "child_process";
|
87
|
+
function applyTerraform(options = { verbose: false }) {
|
88
|
+
console.log("Attempting to terraform apply...");
|
89
|
+
return handleTerraformCommand(options, "apply", "-auto-approve").then(() => console.log("Applied terraform successfully!"));
|
90
|
+
}
|
91
|
+
function destroyTerraform(options = { verbose: false }) {
|
92
|
+
console.log("Attempting to terraform destroy...");
|
93
|
+
return handleTerraformCommand(options, "destroy", "-auto-approve").then(() => console.log("Destroyed terraform successfully!"));
|
94
|
+
}
|
95
|
+
function handleTerraformCommand(options, ...args) {
|
96
|
+
return new Promise((resolve, reject) => {
|
97
|
+
try {
|
98
|
+
const child = spawn("terraform", args);
|
99
|
+
if (options.verbose) {
|
100
|
+
child.stdout.setEncoding("utf8");
|
101
|
+
child.stdout.on("data", (data) => {
|
102
|
+
console.log("stdout: " + data);
|
103
|
+
});
|
104
|
+
child.stderr.on("data", (data) => {
|
105
|
+
console.log("stderr: " + data);
|
106
|
+
});
|
107
|
+
}
|
108
|
+
child.on("close", (code) => {
|
109
|
+
if (code !== 0) {
|
110
|
+
return reject(`terraform running failed with ${code}`);
|
111
|
+
}
|
112
|
+
resolve();
|
113
|
+
});
|
114
|
+
} catch (error) {
|
115
|
+
console.log(`error inside terraform with args ${args}: `, error);
|
116
|
+
reject(error);
|
117
|
+
}
|
118
|
+
});
|
119
|
+
}
|
120
|
+
|
121
|
+
// src/cli.ts
|
122
|
+
function initiateCli() {
|
123
|
+
program.name("ep-resource-json");
|
124
|
+
program.command("endpoints").description("Used to fetch json response data from Elastic Path endpoints and output them into json files.").requiredOption("-s, --source <source>", "Source postman collection json file.").requiredOption("-e, --env <environment-file>", "Source postman environment json file.").option("-o, --output <output>", "Output location for the generated endpoint json files.", process.cwd()).option("-v, --verbose", "Logs details of terraform commands to console.", false).action(outputJsonResponesAction);
|
125
|
+
program.command("ter-destroy").description("Run terraform destroy in isolation.").action(isolatedTerraformDestroyAction);
|
126
|
+
program.parse(process.argv);
|
127
|
+
console.log("program: ", program.opts());
|
128
|
+
}
|
129
|
+
function outputJsonResponesAction(args) {
|
130
|
+
const env = loadEnv();
|
131
|
+
return applyTerraform({ verbose: args.verbose }).then(() => {
|
132
|
+
return executeCollection(args, env);
|
133
|
+
}).then(() => {
|
134
|
+
return destroyTerraform({ verbose: args.verbose });
|
135
|
+
}).catch((err) => {
|
136
|
+
destroyTerraform({ verbose: args.verbose });
|
137
|
+
console.error("error apply terraform: ", err);
|
138
|
+
});
|
139
|
+
}
|
140
|
+
function isolatedTerraformDestroyAction() {
|
141
|
+
loadEnv();
|
142
|
+
return destroyTerraform();
|
143
|
+
}
|
144
|
+
initiateCli();
|
145
|
+
//# sourceMappingURL=cli.mjs.map
|
package/dist/cli.mjs.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts","../src/load-env.ts","../src/newman-exec.ts","../src/terraform.ts"],"sourcesContent":["import { program } from \"commander\";\nimport { loadEnv } from \"./load-env\";\nimport { executeCollection } from \"./newman-exec\";\nimport { applyTerraform, destroyTerraform } from \"./terraform\";\nimport { EndpointCliOptions } from \"./types\";\n\n/**\n * Checks if the script has been provided the required arguments.\n */\nfunction initiateCli(): void {\n program.name(\"ep-resource-json\");\n\n program\n .command(\"endpoints\")\n .description(\n \"Used to fetch json response data from Elastic Path endpoints and output them into json files.\"\n )\n .requiredOption(\n \"-s, --source <source>\",\n \"Source postman collection json file.\"\n )\n .requiredOption(\n \"-e, --env <environment-file>\",\n \"Source postman environment json file.\"\n )\n .option(\n \"-o, --output <output>\",\n \"Output location for the generated endpoint json files.\",\n process.cwd()\n )\n .option(\n \"-v, --verbose\",\n \"Logs details of terraform commands to console.\",\n false\n )\n .action(outputJsonResponesAction);\n\n program\n .command(\"ter-destroy\")\n .description(\"Run terraform destroy in isolation.\")\n .action(isolatedTerraformDestroyAction);\n\n program.parse(process.argv);\n\n // const { source } = program.opts<{ source: string }>();\n\n console.log(\"program: \", program.opts());\n}\nfunction outputJsonResponesAction(args: EndpointCliOptions): Promise<void> {\n const env = loadEnv();\n return applyTerraform({ verbose: args.verbose })\n .then(() => {\n return executeCollection(args, env);\n })\n .then(() => {\n return destroyTerraform({ verbose: args.verbose });\n })\n .catch((err) => {\n destroyTerraform({ verbose: args.verbose });\n console.error(\"error apply terraform: \", err);\n });\n}\n\nfunction isolatedTerraformDestroyAction(): Promise<void> {\n loadEnv();\n return destroyTerraform();\n}\n\ninitiateCli();\n","import { config as dotEnvConfig } from \"dotenv\";\nimport { EPEnv } from \"./types\";\n\nexport function loadEnv(): EPEnv {\n const result = dotEnvConfig();\n if (result.error) {\n throw new Error(\n \"Failed to load environment variables. Check .env exists and is setup to match example.\"\n );\n }\n // Casting parsed response.\n return result!.parsed as EPEnv;\n}\n","import { run } from \"newman\";\nimport { readFile } from \"fs\";\nimport type {\n CollectionDefinition,\n VariableScopeDefinition,\n} from \"postman-collection\";\nimport { EndpointCliOptions, EPEnv } from \"./types\";\n\nexport async function executeCollection(\n options: EndpointCliOptions,\n env?: EPEnv\n): Promise<void> {\n const postmanCollection = await getPostmanCollection(options.source);\n const postmanEnvironment = await getPostmanEnvironment(options.env);\n return new Promise((resolve, reject) => {\n run(\n {\n collection: postmanCollection,\n reporters: [\"cli\", \"@field123/ep-resp\"],\n reporter: {\n \"@field123/ep-resp\": {\n ...options,\n },\n },\n environment: postmanEnvironment,\n ...(env ? { envVar: createEnvironment(env) } : {}),\n },\n (err) => {\n if (err) {\n reject(err);\n }\n console.log(\"collection run complete!\");\n resolve();\n }\n );\n });\n}\n\nfunction createEnvironment(env: EPEnv): { key: string; value: string }[] {\n return [\n {\n key: \"clientID\",\n value: env.EPCC_CLIENT_ID,\n },\n {\n key: \"clientSecret\",\n value: env.EPCC_CLIENT_SECRET,\n },\n ];\n}\n\nfunction getPostmanEnvironment(\n source: string\n): Promise<VariableScopeDefinition> {\n console.log(`Attempting to load postman environment file from ${source}`);\n return getFile(source);\n}\n\nfunction getPostmanCollection(source: string): Promise<CollectionDefinition> {\n console.log(`Attempting to load postman collection file from ${source}`);\n return getFile(source);\n}\n\nfunction getFile(name: string): Promise<any> {\n return new Promise((resolve, reject) => {\n readFile(name, \"utf-8\", (err, data) => {\n if (err) {\n reject(err);\n }\n resolve(JSON.parse(data));\n });\n });\n}\n","import { spawn } from \"child_process\";\n\ninterface TerraformOptions {\n verbose: boolean;\n}\n\nexport function applyTerraform(\n options: TerraformOptions = { verbose: false }\n): Promise<void> {\n console.log(\"Attempting to terraform apply...\");\n return handleTerraformCommand(options, \"apply\", \"-auto-approve\").then(() =>\n console.log(\"Applied terraform successfully!\")\n );\n}\n\nexport function destroyTerraform(\n options: TerraformOptions = { verbose: false }\n): Promise<void> {\n console.log(\"Attempting to terraform destroy...\");\n return handleTerraformCommand(options, \"destroy\", \"-auto-approve\").then(() =>\n console.log(\"Destroyed terraform successfully!\")\n );\n}\n\nfunction handleTerraformCommand(\n options: TerraformOptions,\n ...args: string[]\n): Promise<void> {\n return new Promise((resolve, reject) => {\n try {\n const child = spawn(\"terraform\", args);\n\n if (options.verbose) {\n child.stdout.setEncoding(\"utf8\");\n child.stdout.on(\"data\", (data) => {\n console.log(\"stdout: \" + data);\n });\n child.stderr.on(\"data\", (data) => {\n console.log(\"stderr: \" + data);\n });\n }\n\n child.on(\"close\", (code) => {\n if (code !== 0) {\n return reject(`terraform running failed with ${code}`);\n }\n resolve();\n });\n } catch (error) {\n console.log(`error inside terraform with args ${args}: `, error);\n reject(error);\n }\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;ACAA;AAGO,mBAA0B;AAC/B,QAAM,SAAS,aAAa;AAC5B,MAAI,OAAO,OAAO;AAChB,UAAM,IAAI,MACR,wFACF;AAAA,EACF;AAEA,SAAO,OAAQ;AACjB;;;ACZA;AACA;AAOA,iCACE,SACA,KACe;AACf,QAAM,oBAAoB,MAAM,qBAAqB,QAAQ,MAAM;AACnE,QAAM,qBAAqB,MAAM,sBAAsB,QAAQ,GAAG;AAClE,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,QACE;AAAA,MACE,YAAY;AAAA,MACZ,WAAW,CAAC,OAAO,mBAAmB;AAAA,MACtC,UAAU;AAAA,QACR,qBAAqB,mBAChB;AAAA,MAEP;AAAA,MACA,aAAa;AAAA,OACT,MAAM,EAAE,QAAQ,kBAA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Q;AACR,SAAO,iBAAiB;AAC1B;AAEA,YAAY;","names":[]}
|
package/package.json
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
{
|
2
|
+
"name": "@field123/hacksprint-2022-js-sdk-test-response-generation",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"main": "dist/cli.js",
|
5
|
+
"repository": "https://gitlab.elasticpath.com/Michelle.Wan/hacksprint-2022-js-sdk-test-response-generation.git",
|
6
|
+
"author": "Elastic Path",
|
7
|
+
"scripts": {
|
8
|
+
"postinstall": "terraform init && terraform get",
|
9
|
+
"build": "tsup",
|
10
|
+
"semantic-release": "semantic-release"
|
11
|
+
},
|
12
|
+
"files": [
|
13
|
+
"dist/"
|
14
|
+
],
|
15
|
+
"devDependencies": {
|
16
|
+
"@semantic-release/git": "^10.0.1",
|
17
|
+
"@semantic-release/gitlab": "^9.1.0",
|
18
|
+
"@semantic-release/npm": "^9.0.1",
|
19
|
+
"@types/node": "^17.0.23",
|
20
|
+
"semantic-release": "^19.0.2",
|
21
|
+
"tsup": "^5.12.4",
|
22
|
+
"typescript": "^4.6.3"
|
23
|
+
},
|
24
|
+
"dependencies": {
|
25
|
+
"@field123/newman-reporter-ep-resp": "^0.0.5",
|
26
|
+
"@jahed/terraform": "1.1.7",
|
27
|
+
"@types/newman": "^5.3.0",
|
28
|
+
"axios": "^0.26.1",
|
29
|
+
"commander": "^9.1.0",
|
30
|
+
"dotenv": "^16.0.0",
|
31
|
+
"newman": "^5.3.2"
|
32
|
+
},
|
33
|
+
"publishConfig": {
|
34
|
+
"access": "public"
|
35
|
+
},
|
36
|
+
"bin": {
|
37
|
+
"hacksprint-2022-js-sdk-test-response-generation": "dist/cli.js"
|
38
|
+
}
|
39
|
+
}
|