Skip to content

Commit 3fcd060

Browse files
committed
codegen test3
recreate Signed-off-by: ruzell22 <[email protected]>
1 parent 6994e5b commit 3fcd060

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

.devcontainer/devcontainer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "Ubuntu",
55
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
66
"image": "mcr.microsoft.com/devcontainers/base:jammy",
7-
"runArgs": ["--network=host"],
7+
"runArgs": ["--network=host", "--init", "--privileged", "--memory=7g", "--memory-swap=17g", "--cpus=2"],
88
"features": {
99
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
1010
"moby": true,

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"tools:validate-bundle-names": "TS_NODE_PROJECT=./tools/tsconfig.json node --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/validate-bundle-names.js",
3737
"tools:bump-openapi-spec-dep-versions": "TS_NODE_PROJECT=./tools/tsconfig.json node --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/bump-openapi-spec-dep-versions.ts",
3838
"tools:create-production-only-archive": "TS_NODE_PROJECT=./tools/tsconfig.json node --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/create-production-only-archive.ts",
39+
"tools:download-file-to-disk": "TS_NODE_PROJECT=./tools/tsconfig.json node --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/download-file-to-disk.ts",
3940
"tools:get-latest-sem-ver-git-tag": "TS_NODE_PROJECT=./tools/tsconfig.json node --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node --no-warnings ./tools/get-latest-sem-ver-git-tag.ts",
4041
"tools:generate-sbom": "TS_NODE_PROJECT=tools/tsconfig.json node --experimental-json-modules --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/generate-sbom.ts",
4142
"tools:fix-pkg-npm-scope": "TS_NODE_PROJECT=tools/tsconfig.json node --experimental-json-modules --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/custom-checks/check-pkg-npm-scope.ts",
@@ -58,7 +59,7 @@
5859
"codegen:lerna": "lerna run codegen",
5960
"codegen:warmup-cleancodegendir": "node tools/clear-openapi-codegen-folders.js",
6061
"codegen:warmup-mkdir": "make-dir ./node_modules/@openapitools/openapi-generator-cli/versions/",
61-
"codegen:warmup-v6.6.0": "nwget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/6.6.0/openapi-generator-cli-6.6.0.jar -O ./node_modules/@openapitools/openapi-generator-cli/versions/6.6.0.jar",
62+
"codegen:warmup-v6.6.0": "yarn tools:download-file-to-disk --url=https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/6.6.0/openapi-generator-cli-6.6.0.jar --output-file-path=./node_modules/@openapitools/openapi-generator-cli/versions/6.6.0.jar",
6263
"watch-other": "lerna run --parallel watch",
6364
"watch-tsc": "tsc --build --watch",
6465
"watch": "run-p -r watch-*",

packages/cactus-cmd-api-server/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
],
4545
"scripts": {
4646
"benchmark": "tsx ./src/test/typescript/benchmark/run-cmd-api-server-benchmark.ts .tmp/benchmark-results/cmd-api-server/run-cmd-api-server-benchmark.ts.log",
47-
"codegen": "run-p 'codegen:*'",
47+
"codegen": "run-s 'codegen:*'",
4848
"codegen:openapi": "npm run generate-sdk",
4949
"codegen:proto": "run-s proto:openapi proto:protoc-gen-ts",
50-
"generate-sdk": "run-p 'generate-sdk:*'",
50+
"generate-sdk": "run-s 'generate-sdk:*'",
5151
"generate-sdk:kotlin": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g kotlin -o ./src/main/kotlin/generated/openapi/kotlin-client/ --reserved-words-mappings protected=protected --ignore-file-override ../../openapi-generator-ignore",
5252
"generate-sdk:typescript-axios": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g typescript-axios -o ./src/main/typescript/generated/openapi/typescript-axios --reserved-words-mappings protected=protected --ignore-file-override ../../openapi-generator-ignore",
5353
"proto:openapi": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g protobuf-schema --model-name-suffix=PB --additional-properties=packageName=org.hyperledger.cactus.cmd_api_server -o ./src/main/proto/generated/openapi/ -t=./src/main/openapi-generator/templates/protobuf-schema/",

tools/download-file-to-disk.ts

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import fs from "node:fs";
2+
import { Readable } from "stream";
3+
import { finished } from "stream/promises";
4+
import { ReadableStream } from "stream/web";
5+
import { fileURLToPath, parse } from "url";
6+
import path from "path";
7+
8+
import yargs from "yargs";
9+
import { hideBin } from "yargs/helpers";
10+
import { RuntimeError } from "run-time-error";
11+
12+
const TAG = "[tools/download-file-to-disk.ts]";
13+
14+
export interface IDownloadFileToDiskReq {
15+
readonly url: string;
16+
readonly outputFilePath: string;
17+
}
18+
19+
export interface IDownloadFileToDiskRes {
20+
readonly url: string;
21+
readonly outputFilePath: string;
22+
}
23+
24+
const nodePath = path.resolve(process.argv[1]);
25+
const modulePath = path.resolve(fileURLToPath(import.meta.url));
26+
const isRunningDirectlyViaCLI = nodePath === modulePath;
27+
28+
const main = async (argv: string[], env: NodeJS.ProcessEnv) => {
29+
const req = await createRequest(argv, env);
30+
await downloadFileToDisk(req);
31+
};
32+
33+
if (isRunningDirectlyViaCLI) {
34+
main(process.argv, process.env);
35+
}
36+
37+
async function createRequest(
38+
argv: string[],
39+
env: NodeJS.ProcessEnv,
40+
): Promise<IDownloadFileToDiskReq> {
41+
if (!argv) {
42+
throw new RuntimeError(`Process argv cannot be falsy.`);
43+
}
44+
if (!env) {
45+
throw new RuntimeError(`Process env cannot be falsy.`);
46+
}
47+
48+
const optOutputFilePath =
49+
"The absolute path on disk where the downloaded file will be streamed.";
50+
51+
const optUrl = "The URL to download from.";
52+
53+
const parsedCfg = await yargs(hideBin(argv))
54+
.env("CACTI_")
55+
.option("url", {
56+
alias: "u",
57+
type: "string",
58+
demandOption: false,
59+
description: optUrl,
60+
default: hideBin(argv)[0],
61+
})
62+
.option("output-file-path", {
63+
alias: "o",
64+
type: "string",
65+
description: optOutputFilePath,
66+
defaultDescription: "Defaults to the current working directory.",
67+
default: "./",
68+
}).argv;
69+
70+
const url = parsedCfg.url;
71+
72+
console.log("%s parsing URL '%s'", TAG, url);
73+
const { pathname } = parse(url);
74+
const pathnameOrDefault = pathname || "new_download_file";
75+
const filename = path.basename(pathnameOrDefault);
76+
77+
const endsWithDirSeparator = parsedCfg.outputFilePath.endsWith(path.sep);
78+
79+
const outputFilePath = endsWithDirSeparator
80+
? path.join(parsedCfg.outputFilePath, filename)
81+
: parsedCfg.outputFilePath;
82+
83+
const req: IDownloadFileToDiskReq = {
84+
url,
85+
outputFilePath,
86+
};
87+
88+
return req;
89+
}
90+
91+
export async function downloadFileToDisk(
92+
req: IDownloadFileToDiskReq,
93+
): Promise<unknown> {
94+
const { url, outputFilePath } = req;
95+
console.log("%s downloading %s into %s ...", TAG, url, outputFilePath);
96+
const stream = fs.createWriteStream(req.outputFilePath);
97+
const { body } = await fetch(req.url);
98+
if (!body) {
99+
throw new RuntimeError("fetching %s did not yield a response body.", url);
100+
}
101+
const bodyNodeJs = body as unknown as ReadableStream;
102+
await finished(Readable.fromWeb(bodyNodeJs).pipe(stream));
103+
console.log("%s downloaded %s into %s OK", TAG, url, outputFilePath);
104+
return { outputFilePath, url };
105+
}

0 commit comments

Comments
 (0)