Skip to content

Commit

Permalink
feat(scripts): convert all service initialization script to Typescript
Browse files Browse the repository at this point in the history
Signed-off-by: hainenber <[email protected]>
  • Loading branch information
hainenber committed Nov 29, 2024
1 parent 0696a23 commit c4ab72f
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 167 deletions.
109 changes: 109 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@
"type": "module",
"license": "MIT",
"scripts": {
"start-nexus": "node scripts/start-nexus.js",
"start-jenkins": "node scripts/start-jenkins.js",
"start-nexus": "deno --allow-env --allow-read --allow-sys --allow-write --allow-run --allow-net scripts/start-nexus.ts",
"start-jenkins": "deno --allow-env --allow-read --allow-run --allow-write scripts/start-jenkins.ts",
"lint-fix": "deno fmt",
"sync_forked_repos": "deno run --allow-net --allow-env scripts/sync_forked_repos.ts"
},
"dependencies": {
"@logtape/logtape": "^0.8.0",
"@octokit/types": "^13.6.2",
"axios": "^1.7.8",
"dotenv": "^16.4.5",
"es-toolkit": "^1.27.0",
"execa": "^9.5.1",
"glob": "^11.0.0",
"mustache": "^4.2.0",
"octokit": "^4.0.2",
"rimraf": "^6.0.1"
},
"devDependencies": {
"@types/mustache": "^4.2.5",
"@types/node": "^22.10.0"
}
}
59 changes: 36 additions & 23 deletions scripts/start-jenkins.js → scripts/start-jenkins.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { execa } from "execa";
import { join as pathJoin } from "path";
import { writeFile } from "fs/promises";
import { cpSync, globSync, mkdirSync, readdirSync } from "fs";
import { join as pathJoin } from "node:path";
import {
cpSync,
mkdirSync,
readdirSync,
writeFileSync,
} from "node:fs";
import { globSync } from "glob";
import { drop, head, isNil, isNotNil } from "es-toolkit";
import { cwd } from "process";
import { cwd } from "node:process";
import {
configureLogger,
fileExists,
Expand All @@ -12,9 +17,9 @@ import {
getArtifactVersionData,
PROJECT_NAME,
VERSION_LIMIT,
} from "./utils/index.js";
import { Readable } from "stream";
} from "./utils/index.ts";
import { rimrafSync } from "rimraf";
import axios from "axios";

// Constants
const SERVICE = "jenkins";
Expand Down Expand Up @@ -43,15 +48,16 @@ const jenkinsProjectPath = pathJoin(ROOT_DIR, SERVICE);
logger.info(
`Not found ${SERVICE}-${version}. Downloading ${artifact_name} from ${download_url}`,
);
const response = await fetch(download_url);
if (!response.ok) {
const response = await axios.get(download_url, {
responseType: "arraybuffer",
});
if (response.status >= 200 && response.status < 300) {
logger.fatal(
`Failed to fetch ${artifact_name} from ${download_url}: ${response.statusText}`,
);
process.exit(1);
}
const stream = Readable.fromWeb(response.body);
await writeFile(artifactPath, stream);
writeFileSync(artifactPath, response.data);
} else {
logger.info(`${artifact_name} exists. Skip download`);
}
Expand Down Expand Up @@ -91,12 +97,12 @@ const jenkinsProjectPath = pathJoin(ROOT_DIR, SERVICE);
);
process.exit(1);
} else {
const javaVersion = head(javaVersionData).split(/\s+/).at(1);
const javaVersionComponents = javaVersion.split(".");
const javaMajorVersion = head(javaVersionComponents) === "1"
? javaVersionComponents.at(1)
: head(javaVersionComponents);
if (parseInt(javaMajorVersion, 10) < JENKINS_MINIMAL_JAVA_VERSION) {
const javaVersion = head(javaVersionData)?.split(/\s+/).at(1);
const javaVersionComponents = javaVersion?.split(".");
const javaMajorVersion = head(javaVersionComponents ?? []) === "1"
? javaVersionComponents?.at(1)
: head(javaVersionComponents ?? []);
if (parseInt(javaMajorVersion ?? '8', 10) < JENKINS_MINIMAL_JAVA_VERSION) {
logger.fatal(
`Current machine has Java version that is less than required 17 (version: ${javaMajorVersion})`,
);
Expand Down Expand Up @@ -133,13 +139,20 @@ const jenkinsProjectPath = pathJoin(ROOT_DIR, SERVICE);
const jenkinsBinary = head(
globSync(pathJoin(jenkinsProjectPath, "jenkins-*.war")),
);
await execa({
timeout: 60000,
stdout: ["inherit"],
stderr: ["inherit"],
})`java -jar ${jenkinsPluginManager} --war ${jenkinsBinary} --verbose --plugin-download-directory ${
pathJoin(jenkinsProjectPath, "data", "plugins")
} --plugin-file ${jenkinsPluginConfigPath}`;
if (jenkinsPluginManager && jenkinsBinary) {
await execa({
timeout: 60000,
stdout: ["inherit"],
stderr: ["inherit"],
})`java -jar ${jenkinsPluginManager} --war ${jenkinsBinary} --verbose --plugin-download-directory ${
pathJoin(jenkinsProjectPath, "data", "plugins")
} --plugin-file ${jenkinsPluginConfigPath}`;
} else {
logger.fatal(
`Not finding neither Jenkins Plugin Manager JAR nor Jenkins WAR`,
);
process.exit(1);
}
} else {
logger.warn(
"Configuration file for Jenkins plugins are not found. Handle plugins manually.",
Expand Down
92 changes: 0 additions & 92 deletions scripts/start-nexus.js

This file was deleted.

Loading

0 comments on commit c4ab72f

Please sign in to comment.