From 751fd07d4541d47a9ae3dfb8fcf8a9e0eca31b3e Mon Sep 17 00:00:00 2001 From: Philipp Horstenkamp Date: Tue, 22 Apr 2025 00:53:28 +0200 Subject: [PATCH] Rework the index.js --- index.js | 78 +++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index 30cbb6f..8f1f904 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,7 @@ function replacePlaceholders(content, hostname) { * @param {string} hostname * @returns {Promise} */ -async function readReplaceAndWriteFiles(pattern, prefix, hostname) { +function readReplaceAndWriteFiles(pattern, prefix, hostname) { return new Promise((resolve, reject) => { const globPattern = prefix ? path.join(prefix, pattern) : pattern; @@ -143,7 +143,7 @@ function validateAuthentication(token, username, password) { /** * Posts code to Screeps server. */ -async function postCode() { +function postCode() { const protocol = core.getInput("protocol") || "https"; const hostname = core.getInput("hostname") || "screeps.com"; const port = core.getInput("port") || "443"; @@ -158,21 +158,6 @@ async function postCode() { const gitReplace = core.getInput("git-replace") || null; - if (gitReplace) { - await readReplaceAndWriteFiles(gitReplace, prefix, hostname); - } - - const files_to_push = await readFilesIntoDict(pattern, prefix); - - core.info( - `Uploading ${ - Object.keys(files_to_push).length - } file(s) to branch '${branch}':`, - ); - Object.keys(files_to_push).forEach((key) => { - core.info(` File: ${key}`); - }); - const login_arguments = { token, username, @@ -183,35 +168,48 @@ async function postCode() { path, }; - core.info("Authentication arguments:"); - core.info(JSON.stringify(login_arguments, null, 2)); - const errorMessage = validateAuthentication(token, username, password); if (errorMessage) { - core.error(errorMessage); - return; + core.setFailed(errorMessage); + return Promise.reject(new Error(errorMessage)); } - let api = new ScreepsAPI(login_arguments); + const replacePromise = gitReplace + ? readReplaceAndWriteFiles(gitReplace, prefix, hostname) + : Promise.resolve(); - if (token) { - const response = await api.code.set(branch, files_to_push); - core.info(JSON.stringify(response, null, 2)); - console.log(`Code set successfully to ${branch}`); - } else { - core.info(`Logging in as user ${username}`); - await Promise.resolve() - .then(() => api.auth(username, password, login_arguments)) - .then(() => { - return api.code.set(branch, files_to_push); - }) - .then(() => { - console.log(`Code set successfully to ${branch}`); - }) - .catch((err) => { - console.error("Error:", err); + return replacePromise + .then(() => readFilesIntoDict(pattern, prefix)) + .then((files_to_push) => { + core.info( + `Uploading ${ + Object.keys(files_to_push).length + } file(s) to branch '${branch}':`, + ); + Object.keys(files_to_push).forEach((key) => { + core.info(` File: ${key}`); }); - } + + core.info("Authentication arguments:"); + core.info(JSON.stringify(login_arguments, null, 2)); + + const api = new ScreepsAPI(login_arguments); + + 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}`); + }); + } else { + 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}`); + }); + } + }); } postCode().catch((err) => {