Refactor index.js
All checks were successful
Lint / pre-commit Linting (push) Successful in 45s

This commit is contained in:
Philipp Horstenkamp 2025-04-22 01:07:34 +02:00
parent ffa09c46c0
commit 616584e43a
Signed by: Philipp
GPG Key ID: DD53EAC36AFB61B4

View File

@ -7,12 +7,6 @@ const path = require("path");
/**
* Replaces specific placeholder strings within the provided content with corresponding dynamic values.
*
* This function targets:
* - {{gitHash}} -> GITHUB_SHA
* - {{gitRef}} -> GITHUB_REF
* - {{deployTime}} -> ISO timestamp
* - {{hostname}} -> hostname
*
* @param {string} content
* @param {string} hostname
* @returns {string}
@ -52,6 +46,13 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) {
return reject(err);
}
if (!files.length) {
core.warning(
`No files matched for placeholder replacement with pattern: ${globPattern}`,
);
return resolve([]);
}
let processPromises = [];
files.forEach((file) => {
@ -68,6 +69,7 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) {
try {
await Promise.all(processPromises);
core.info(`Replaced placeholders in ${files.length} file(s).`);
resolve(files);
} catch (processError) {
reject(processError);
@ -78,6 +80,7 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) {
/**
* Reads files matching a glob pattern into a dictionary.
*
* @param {string} pattern
* @param {string} prefix
* @returns {Promise<Object>}
@ -91,6 +94,12 @@ function readFilesIntoDict(pattern, prefix) {
return reject(err);
}
if (!files.length) {
core.warning(
`No files matched for upload with pattern: ${globPattern}`,
);
}
let fileDict = {};
let readPromises = [];
@ -116,6 +125,7 @@ function readFilesIntoDict(pattern, prefix) {
/**
* Validates the provided authentication credentials.
*
* @param {string} token
* @param {string} username
* @param {string} password
@ -141,22 +151,21 @@ function validateAuthentication(token, username, password) {
}
/**
* Posts code to Screeps server.
* Main execution logic for the Screeps upload action.
*/
function postCode() {
core.info(`exec -> Done!`);
core.info("🟢 Starting Screeps upload action...");
const protocol = core.getInput("protocol") || "https";
const hostname = core.getInput("hostname") || "screeps.com";
const port = core.getInput("port") || "443";
const path = core.getInput("path") || "/";
const token = core.getInput("token") || undefined;
const username = core.getInput("username") || undefined;
const password = core.getInput("password") || undefined;
const prefix = core.getInput("source-prefix");
const pattern = core.getInput("pattern") || "*.js";
const branch = core.getInput("branch") || "default";
const gitReplace = core.getInput("git-replace") || null;
const login_arguments = {
@ -169,6 +178,12 @@ function postCode() {
path,
};
core.info("🔧 Inputs:");
core.info(` prefix: ${prefix}`);
core.info(` pattern: ${pattern}`);
core.info(` branch: ${branch}`);
core.info(` gitReplace: ${gitReplace}`);
const errorMessage = validateAuthentication(token, username, password);
if (errorMessage) {
core.setFailed(errorMessage);
@ -180,18 +195,25 @@ function postCode() {
: Promise.resolve();
return replacePromise
.then(() => readFilesIntoDict(pattern, prefix))
.then(() => {
core.info("✅ Placeholder replacement complete.");
return readFilesIntoDict(pattern, prefix);
})
.then((files_to_push) => {
core.info(
`Uploading ${
Object.keys(files_to_push).length
} file(s) to branch '${branch}':`,
);
const fileCount = Object.keys(files_to_push).length;
core.info(`📦 Files prepared for upload: ${fileCount}`);
if (fileCount === 0) {
core.warning("No files were found to upload. Exiting.");
return;
}
core.info(`⬆️ Uploading to branch '${branch}':`);
Object.keys(files_to_push).forEach((key) => {
core.info(` File: ${key}`);
core.info(` - ${key}`);
});
core.info("Authentication arguments:");
core.info("🔐 Authentication:");
core.info(JSON.stringify(login_arguments, null, 2));
const api = new ScreepsAPI(login_arguments);
@ -199,22 +221,21 @@ function postCode() {
if (token) {
return api.code.set(branch, files_to_push).then((response) => {
core.info(JSON.stringify(response, null, 2));
console.log(`Code set successfully to ${branch}`);
core.info(`✅ Code uploaded to branch '${branch}' using token.`);
});
} else {
core.info(`Logging in as user ${username}`);
core.info(`Logging in as user '${username}'...`);
return api
.auth(username, password, login_arguments)
.then(() => api.code.set(branch, files_to_push))
.then(() => {
console.log(`Code set successfully to ${branch}`);
core.info(`✅ Code uploaded to branch '${branch}' via basic auth.`);
});
}
});
}
core.info(`Starting`);
// Run the action and catch any errors
postCode().catch((err) => {
core.setFailed(err.message || err);
});
core.info(`Done`);