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. * 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} content
* @param {string} hostname * @param {string} hostname
* @returns {string} * @returns {string}
@ -52,6 +46,13 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) {
return reject(err); return reject(err);
} }
if (!files.length) {
core.warning(
`No files matched for placeholder replacement with pattern: ${globPattern}`,
);
return resolve([]);
}
let processPromises = []; let processPromises = [];
files.forEach((file) => { files.forEach((file) => {
@ -68,6 +69,7 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) {
try { try {
await Promise.all(processPromises); await Promise.all(processPromises);
core.info(`Replaced placeholders in ${files.length} file(s).`);
resolve(files); resolve(files);
} catch (processError) { } catch (processError) {
reject(processError); reject(processError);
@ -78,6 +80,7 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) {
/** /**
* Reads files matching a glob pattern into a dictionary. * Reads files matching a glob pattern into a dictionary.
*
* @param {string} pattern * @param {string} pattern
* @param {string} prefix * @param {string} prefix
* @returns {Promise<Object>} * @returns {Promise<Object>}
@ -91,6 +94,12 @@ function readFilesIntoDict(pattern, prefix) {
return reject(err); return reject(err);
} }
if (!files.length) {
core.warning(
`No files matched for upload with pattern: ${globPattern}`,
);
}
let fileDict = {}; let fileDict = {};
let readPromises = []; let readPromises = [];
@ -116,6 +125,7 @@ function readFilesIntoDict(pattern, prefix) {
/** /**
* Validates the provided authentication credentials. * Validates the provided authentication credentials.
*
* @param {string} token * @param {string} token
* @param {string} username * @param {string} username
* @param {string} password * @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() { function postCode() {
core.info(`exec -> Done!`); core.info("🟢 Starting Screeps upload action...");
const protocol = core.getInput("protocol") || "https"; const protocol = core.getInput("protocol") || "https";
const hostname = core.getInput("hostname") || "screeps.com"; const hostname = core.getInput("hostname") || "screeps.com";
const port = core.getInput("port") || "443"; const port = core.getInput("port") || "443";
const path = core.getInput("path") || "/"; const path = core.getInput("path") || "/";
const token = core.getInput("token") || undefined; const token = core.getInput("token") || undefined;
const username = core.getInput("username") || undefined; const username = core.getInput("username") || undefined;
const password = core.getInput("password") || undefined; const password = core.getInput("password") || undefined;
const prefix = core.getInput("source-prefix"); const prefix = core.getInput("source-prefix");
const pattern = core.getInput("pattern") || "*.js"; const pattern = core.getInput("pattern") || "*.js";
const branch = core.getInput("branch") || "default"; const branch = core.getInput("branch") || "default";
const gitReplace = core.getInput("git-replace") || null; const gitReplace = core.getInput("git-replace") || null;
const login_arguments = { const login_arguments = {
@ -169,6 +178,12 @@ function postCode() {
path, 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); const errorMessage = validateAuthentication(token, username, password);
if (errorMessage) { if (errorMessage) {
core.setFailed(errorMessage); core.setFailed(errorMessage);
@ -180,18 +195,25 @@ function postCode() {
: Promise.resolve(); : Promise.resolve();
return replacePromise return replacePromise
.then(() => readFilesIntoDict(pattern, prefix)) .then(() => {
core.info("✅ Placeholder replacement complete.");
return readFilesIntoDict(pattern, prefix);
})
.then((files_to_push) => { .then((files_to_push) => {
core.info( const fileCount = Object.keys(files_to_push).length;
`Uploading ${ core.info(`📦 Files prepared for upload: ${fileCount}`);
Object.keys(files_to_push).length
} file(s) to branch '${branch}':`, 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) => { 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)); core.info(JSON.stringify(login_arguments, null, 2));
const api = new ScreepsAPI(login_arguments); const api = new ScreepsAPI(login_arguments);
@ -199,22 +221,21 @@ function postCode() {
if (token) { if (token) {
return api.code.set(branch, files_to_push).then((response) => { return api.code.set(branch, files_to_push).then((response) => {
core.info(JSON.stringify(response, null, 2)); core.info(JSON.stringify(response, null, 2));
console.log(`Code set successfully to ${branch}`); core.info(`✅ Code uploaded to branch '${branch}' using token.`);
}); });
} else { } else {
core.info(`Logging in as user ${username}`); core.info(`Logging in as user '${username}'...`);
return api return api
.auth(username, password, login_arguments) .auth(username, password, login_arguments)
.then(() => api.code.set(branch, files_to_push)) .then(() => api.code.set(branch, files_to_push))
.then(() => { .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) => { postCode().catch((err) => {
core.setFailed(err.message || err); core.setFailed(err.message || err);
}); });
core.info(`Done`);