Skip to content

Commit b1c3a10

Browse files
author
Justin Poehnelt
authored
build: allow targetting a single response (#339)
1 parent 7170d39 commit b1c3a10

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ The repository makes use of [Bazel](https://bazel.build/) to generate outputs fr
5050
> **Note**: If a documentation item is not generated, be sure it is included
5151
in the appropriate index.yml file.
5252

53-
1. `npm run build:responses` (optional)
53+
1. `npm run responses` (optional)
5454

5555
> **Note**: This is an optional step requiring an API key. Set the `GOOGLE_MAPS_API_KEY` environmental variable before running.
5656
5757
> **Note**: This step only needs to run when the generation code or sample requests have been updated.
5858
5959
> **Note**: The geolocation request that only provides an IP is not deterministic and skipped in the binary executed.
6060
61+
> **Note**: A single response can be updated similar to `npm run responses -- --only maps_http_places_nearbysearch`.
62+
6163
1. `npm run test`

generator/responses/response.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const argv = options({
5353
demandOption: true,
5454
},
5555
skip: { type: "string" },
56+
only: { type: "string" },
5657
}).argv;
5758

5859
const extractRequests = async (
@@ -122,16 +123,16 @@ const executeJSONRequest = async (
122123
const xmlInsertRegionTags = (destination: string, regionTag: string) => {
123124
const contents = readFileSync(destination, "utf8");
124125

125-
const lines = contents.split("\n").filter(value => value !== "");
126+
const lines = contents.split("\n").filter((value) => value !== "");
126127
lines.splice(1, 0, `<!-- [START ${regionTag}] -->`);
127128
lines.push(`<!-- [END ${regionTag}] -->`);
128-
lines.push('');
129+
lines.push("");
129130
writeFileSync(destination, lines.join("\n"));
130131
};
131132

132133
const response = async (output, regionTag, request, xml = false) => {
133134
regionTag += "_response";
134-
console.log(`Generating response for: ${regionTag}`);
135+
console.log(`Generating response for: ${regionTag}, format: ${xml ? "xml" : "json"}`);
135136

136137
const captureError = /error/i.test(regionTag);
137138
const captureInvalid = /invalid/i.test(regionTag);
@@ -175,7 +176,7 @@ const response = async (output, regionTag, request, xml = false) => {
175176
try {
176177
unlinkSync(destination);
177178
} catch (e) {}
178-
179+
179180
const writeStream = createWriteStream(destination);
180181
response.data.pipe(writeStream);
181182

@@ -212,12 +213,29 @@ const response = async (output, regionTag, request, xml = false) => {
212213
)
213214
);
214215
};
216+
const toArray = (value: string | string[]): string[] => {
217+
return Array.isArray(value) ? value : [value];
218+
};
215219

216220
const main = async (argv: any) => {
217-
for (let [regionTag, request] of Object.entries(
218-
await extractRequests(argv.archive)
219-
)) {
220-
if (argv.skip.indexOf(regionTag) != -1) continue;
221+
const skip = toArray(argv.skip ?? []);
222+
const only = toArray(argv.only ?? []);
223+
224+
const requests = Object.entries(await extractRequests(argv.archive)).filter(
225+
([regionTag]) => {
226+
if (skip.length && skip.indexOf(regionTag) !== -1) {
227+
return false;
228+
}
229+
230+
if (only.length && only.indexOf(regionTag) === -1) {
231+
return false;
232+
}
233+
console.log(regionTag);
234+
return true;
235+
}
236+
);
237+
238+
for (let [regionTag, request] of requests) {
221239
await response(argv.output, regionTag, request, false);
222240

223241
if (request.match(/\/json\?/g)) {

0 commit comments

Comments
 (0)