diff --git a/index.js b/index.js index 1806af8..164cc3b 100644 --- a/index.js +++ b/index.js @@ -90,12 +90,11 @@ async function postCode() { const files_to_push = await readFilesIntoDict(pattern, prefix); + core.info(`Trying to upload the following files to ${branch}:`); Object.keys(files_to_push).forEach((key) => { core.info(`Key: ${key}`); }); - core.info(files_to_push); - const login_arguments = { token: token, username: username, @@ -106,20 +105,34 @@ async function postCode() { path: path, }; - core.info(`Trying to upload the following files to ${branch}:`); - Object.keys(login_arguments).forEach((key) => { - core.info(`Key: ${key}`); - }); + core.info("login_arguments:"); + core.info(JSON.stringify(login_arguments, null, 2)); const errorMessage = validateAuthentication(token, username, password); if (errorMessage) { core.error(errorMessage); return; } - - const api = new ScreepsAPI(login_arguments); - const response = await api.code.set(branch, files_to_push); - core.info(JSON.stringify(response, null, 2)); + let api = new ScreepsAPI(login_arguments); + 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 { + let response; + core.info(`Logging in as user ${username}`); + response = await Promise.resolve() + .then(() => api.auth(username, password, login_arguments)) + .then(() => { + api.code.set(branch, files_to_push); + }) + .then(() => { + console.log(`Code set successfully to ${branch}`); + }) + .catch((err) => { + console.error("Error:", err); + }); + } } postCode();